getopt.cpp revision 4fa08e9052161e65bfe8d2e3bb38fa227af628c0
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync/* $Id$ */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Command Line Parsing
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/*
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * available from http://www.virtualbox.org. This file is free software;
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * you can redistribute it and/or modify it under the terms of the GNU
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * General Public License (GPL) as published by the Free Software
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * The contents of this file may alternatively be used under the terms
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * of the Common Development and Distribution License Version 1.0
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * VirtualBox OSE distribution, in which case the provisions of the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * CDDL are applicable instead of those of the GPL.
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync *
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * You may elect to license modified versions of this file under the
bd8e360cd1db83dcb2694ea9122ce3bc5bae678avboxsync * terms and conditions of either the GPL or the CDDL or both.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync *
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * additional information or have any questions.
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/*******************************************************************************
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync* Header Files *
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync*******************************************************************************/
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include <iprt/net.h> /* must come before getopt.h */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/getopt.h>
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include "internal/iprt.h"
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/err.h>
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/string.h>
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync#include <iprt/assert.h>
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#include <iprt/ctype.h>
5ef92c0006229b3fc7bc7eb4ad80ed65fb0dd98bvboxsync#include <iprt/uuid.h>
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync PCRTGETOPTDEF paOptions, size_t cOptions,
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync int iFirst, uint32_t fFlags)
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync{
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->argv = argv;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->argc = argc;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->paOptions = paOptions;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->cOptions = cOptions;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->iNext = iFirst;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->pszNextShort = NULL;
7be2140da7230ed5736528554a4dc34b2ac482acvboxsync pState->pDef = NULL;
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /* validate the options. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync for (size_t i = 0; i < cOptions; i++)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(paOptions[i].iShort > 0);
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(paOptions[i].iShort != '-');
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync /** @todo Add an flag for sorting the arguments so that all the options comes
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync * first. */
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync return VINF_SUCCESS;
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync}
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsyncRT_EXPORT_SYMBOL(RTGetOptInit);
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync/**
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync *
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * @todo This should be move to some generic part of the runtime.
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync *
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * failure.
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync *
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * @param pszValue The value to convert.
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * @param pAddr Where to store the result.
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync */
c882ddf98a60a4aab1218108b334083f98b7d66avboxsyncstatic int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync{
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync char *pszNext;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (*pszNext++ != '.')
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (*pszNext++ != '.')
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (*pszNext++ != '.')
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync pszNext = RTStrStripL(pszNext);
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync if (*pszNext)
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VINF_SUCCESS;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync}
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync/**
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * Converts an stringified Ethernet MAC address into the RTMAC representation.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync *
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * @todo This should be move to some generic part of the runtime.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync *
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * failure.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync *
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * @param pszValue The value to convert.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * @param pAddr Where to store the result.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync */
b6097213cd07c5383c49168bfb61f915dbd4587avboxsyncstatic int rtgetoptConvertMacAddr(const char *pszValue, PRTMAC pAddr)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync{
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync /*
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * Not quite sure if I should accept stuff like "08::27:::1" here...
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * The code is accepting "::" patterns now, except for for the first
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * and last parts.
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync */
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync /* first */
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync char *pszNext;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (*pszNext++ != ':')
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync /* middle */
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync for (unsigned i = 1; i < 5; i++)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync {
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (*pszNext == ':')
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync pAddr->au8[i] = 0;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync else
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync {
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (*pszNext != ':')
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync }
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync pszNext++;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync }
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync /* last */
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync pszNext = RTStrStripL(pszNext);
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync if (*pszNext)
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync return VINF_SUCCESS;
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync}
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync/**
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Searches for a long option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync *
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @returns Pointer to a matching option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param pszOption The alleged long option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param paOptions Option array.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param cOptions Number of items in the array.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync{
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync PCRTGETOPTDEF pOpt = paOptions;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync while (cOptions-- > 0)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (pOpt->pszLong)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /*
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * A value is required with the argument. We're trying to be very
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * understanding here and will permit any of the following:
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * --long12:value, --long12=value, --long12 value,
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * --long:value, --long=value, --long value,
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * --long: value, --long= value
fd546afe09073de92e5422c1334f1c27b4108687vboxsync *
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * If the option is index, then all trailing chars must be
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * digits. For error reporting reasons we also match where
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * there is no index.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync size_t cchLong = strlen(pOpt->pszLong);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (!strncmp(pszOption, pOpt->pszLong, cchLong))
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync while (RT_C_IS_DIGIT(pszOption[cchLong]))
fd546afe09073de92e5422c1334f1c27b4108687vboxsync cchLong++;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if ( pszOption[cchLong] == '\0'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || pszOption[cchLong] == ':'
fd546afe09073de92e5422c1334f1c27b4108687vboxsync || pszOption[cchLong] == '=')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return pOpt;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync /*
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * The option takes an index but no value.
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * As above, we also match where there is no index.
fd546afe09073de92e5422c1334f1c27b4108687vboxsync */
fd546afe09073de92e5422c1334f1c27b4108687vboxsync size_t cchLong = strlen(pOpt->pszLong);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (!strncmp(pszOption, pOpt->pszLong, cchLong))
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync while (RT_C_IS_DIGIT(pszOption[cchLong]))
fd546afe09073de92e5422c1334f1c27b4108687vboxsync cchLong++;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (pszOption[cchLong] == '\0')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return pOpt;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync else if (!strcmp(pszOption, pOpt->pszLong))
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pOpt++;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return NULL;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync}
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync/**
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Searches for a matching short option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync *
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @returns Pointer to a matching option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param chOption The option char.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param paOptions Option array.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * @param cOptions Number of items in the array.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync{
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync PCRTGETOPTDEF pOpt = paOptions;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync while (cOptions-- > 0)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (pOpt->iShort == chOption)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pOpt++;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return NULL;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync}
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync/**
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Value string -> Value union.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync *
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * @returns IPRT status code.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * @param fFlags The value flags.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * @param pszValue The value string.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * @param pValueUnion Where to return the processed value.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsyncstatic int rtGetOptProcessValue(uint32_t fFlags, const char *pszValue, PRTGETOPTUNION pValueUnion)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync{
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync /*
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Transform into a option value as requested.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * If decimal conversion fails, we'll check for "0x<xdigit>" and
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * try a 16 based conversion. We will not interpret any of the
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * generic ints as octals.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync switch (fFlags & ( RTGETOPT_REQ_MASK
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync | RTGETOPT_FLAG_HEX
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync | RTGETOPT_FLAG_DEC
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync | RTGETOPT_FLAG_OCT))
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync {
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case RTGETOPT_REQ_STRING:
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->psz = pszValue;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync case RTGETOPT_REQ_BOOL_ONOFF:
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync if (!RTStrICmp(pszValue, "on"))
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync pValueUnion->f = true;
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync else if (!RTStrICmp(pszValue, "off"))
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync pValueUnion->f = false;
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync else
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync {
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync pValueUnion->psz = pszValue;
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync return VERR_GETOPT_UNKNOWN_OPTION;
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync }
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync break;
4fa08e9052161e65bfe8d2e3bb38fa227af628c0vboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#define MY_INT_CASE(req,type,memb,convfn) \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case req: \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync { \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync type Value; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync && ( pszValue[0] != '0' \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync || (pszValue[1] != 'x' && pszValue[1] != 'X') \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync || !RT_C_IS_XDIGIT(pszValue[2]) \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->memb = Value; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case req: \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync { \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync type Value; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->memb = Value; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break; \
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#undef MY_INT_CASE
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#undef MY_BASE_INT_CASE
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case RTGETOPT_REQ_IPV4ADDR:
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync {
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync RTNETADDRIPV4 Addr;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (rtgetoptConvertIPv4Addr(pszValue, &Addr) != VINF_SUCCESS)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->IPv4Addr = Addr;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#if 0 /** @todo CIDR */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync#endif
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case RTGETOPT_REQ_MACADDR:
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync {
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync RTMAC Addr;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (rtgetoptConvertMacAddr(pszValue, &Addr) != VINF_SUCCESS)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->MacAddr = Addr;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync case RTGETOPT_REQ_UUID:
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync {
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync RTUUID Uuid;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (RTUuidFromStr(&Uuid, pszValue) != VINF_SUCCESS)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->Uuid = Uuid;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync break;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync default:
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync AssertMsgFailed(("f=%#x\n", fFlags));
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_INTERNAL_ERROR;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync }
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VINF_SUCCESS;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync}
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync{
fd546afe09073de92e5422c1334f1c27b4108687vboxsync /*
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * Reset the variables kept in state.
fd546afe09073de92e5422c1334f1c27b4108687vboxsync */
7be2140da7230ed5736528554a4dc34b2ac482acvboxsync pState->pDef = NULL;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->uIndex = UINT64_MAX;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Make sure the union is completely cleared out, whatever happens below.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync pValueUnion->u64 = 0;
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync pValueUnion->pDef = NULL;
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync /** @todo Handle '--' (end of options).*/
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync /** @todo Add a flag to RTGetOptInit for handling the various help options in
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync * a common way. (-?,-h,-help,--help,++) */
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync /** @todo Add a flag to RTGetOptInit for handling the standard version options
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync * in a common way. (-V,--version) */
9735c8af814f4e79e0c2e4b63b2a61a30faa67bfvboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * The next option.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
81587231c9c584851518872e197f6f02dffe68cavboxsync bool fShort;
81587231c9c584851518872e197f6f02dffe68cavboxsync int iThis;
81587231c9c584851518872e197f6f02dffe68cavboxsync const char *pszArgThis;
81587231c9c584851518872e197f6f02dffe68cavboxsync PCRTGETOPTDEF pOpt;
81587231c9c584851518872e197f6f02dffe68cavboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync if (pState->pszNextShort)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * We've got short options left over from the previous call.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
81587231c9c584851518872e197f6f02dffe68cavboxsync pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions);
81587231c9c584851518872e197f6f02dffe68cavboxsync if (!pOpt)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync pValueUnion->psz = pState->pszNextShort;
81587231c9c584851518872e197f6f02dffe68cavboxsync return VERR_GETOPT_UNKNOWN_OPTION;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->pszNextShort++;
81587231c9c584851518872e197f6f02dffe68cavboxsync pszArgThis = pState->pszNextShort - 2;
81587231c9c584851518872e197f6f02dffe68cavboxsync iThis = pState->iNext;
81587231c9c584851518872e197f6f02dffe68cavboxsync fShort = true;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync else
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Pop off the next argument.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
81587231c9c584851518872e197f6f02dffe68cavboxsync if (pState->iNext >= pState->argc)
81587231c9c584851518872e197f6f02dffe68cavboxsync return 0;
81587231c9c584851518872e197f6f02dffe68cavboxsync iThis = pState->iNext++;
81587231c9c584851518872e197f6f02dffe68cavboxsync pszArgThis = pState->argv[iThis];
81587231c9c584851518872e197f6f02dffe68cavboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
43c126a098acd369888f9302a322366ccb2bd4d7vboxsync * Do a long option search first and then a short option one.
81587231c9c584851518872e197f6f02dffe68cavboxsync * This way we can make sure single dash long options doesn't
81587231c9c584851518872e197f6f02dffe68cavboxsync * get mixed up with short ones.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
81587231c9c584851518872e197f6f02dffe68cavboxsync pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions);
81587231c9c584851518872e197f6f02dffe68cavboxsync if ( !pOpt
81587231c9c584851518872e197f6f02dffe68cavboxsync && pszArgThis[0] == '-'
81587231c9c584851518872e197f6f02dffe68cavboxsync && pszArgThis[1] != '-'
81587231c9c584851518872e197f6f02dffe68cavboxsync && pszArgThis[1] != '\0')
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions);
81587231c9c584851518872e197f6f02dffe68cavboxsync fShort = pOpt != NULL;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync else
81587231c9c584851518872e197f6f02dffe68cavboxsync fShort = false;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (pOpt)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pValueUnion->pDef = pOpt; /* in case of no value or error. */
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync {
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync /*
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Find the argument value.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync *
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * A value is required with the argument. We're trying to be very
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * understanding here and will permit any of the following:
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * -svalue, -s:value, -s=value,
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * -s value, -s: value, -s= value
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * (Ditto for long options.)
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync const char *pszValue;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (fShort)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ( pszArgThis[2] == '\0'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || ( pszArgThis[3] == '\0'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync && ( pszArgThis[2] == ':'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || pszArgThis[2] == '=')) )
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync {
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync if (iThis + 1 >= pState->argc)
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync pszValue = pState->argv[iThis + 1];
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync pState->iNext++;
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync }
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync else /* same argument. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pszValue = &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')];
81587231c9c584851518872e197f6f02dffe68cavboxsync if (pState->pszNextShort)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->pszNextShort = NULL;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->iNext++;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync else
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync size_t cchLong = strlen(pOpt->pszLong);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (pszArgThis[cchLong] == '\0')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_INDEX_MISSING;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync
fd546afe09073de92e5422c1334f1c27b4108687vboxsync uint64_t uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync char *pszRet = NULL;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync int rc = RTStrToUInt64Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (rc == VWRN_TRAILING_CHARS)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if ( pszRet[0] != ':'
fd546afe09073de92e5422c1334f1c27b4108687vboxsync && pszRet[0] != '=')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->uIndex = uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pszValue = pszRet + 1;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else if (rc == VINF_SUCCESS)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (iThis + 1 >= pState->argc)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->uIndex = uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pszValue = pState->argv[iThis + 1];
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->iNext++;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else
fd546afe09073de92e5422c1334f1c27b4108687vboxsync AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if ( pszArgThis[cchLong] == '\0'
fd546afe09073de92e5422c1334f1c27b4108687vboxsync || pszArgThis[cchLong + 1] == '\0')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (iThis + 1 >= pState->argc)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pszValue = pState->argv[iThis + 1];
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->iNext++;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else /* same argument. */
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pszValue = &pszArgThis[cchLong + 1];
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /*
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Set up the ValueUnion.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync int rc = rtGetOptProcessValue(pOpt->fFlags, pszValue, pValueUnion);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (RT_FAILURE(rc))
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return rc;
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync else if (fShort)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Deal with "compressed" short option lists, correcting the next
81587231c9c584851518872e197f6f02dffe68cavboxsync * state variables for the start and end cases.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
81587231c9c584851518872e197f6f02dffe68cavboxsync if (pszArgThis[2])
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync if (!pState->pszNextShort)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync /* start */
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->pszNextShort = &pszArgThis[2];
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->iNext--;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync else if (pState->pszNextShort)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync /* end */
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->pszNextShort = NULL;
81587231c9c584851518872e197f6f02dffe68cavboxsync pState->iNext++;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync size_t cchLong = strlen(pOpt->pszLong);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (pszArgThis[cchLong] == '\0')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_INDEX_MISSING;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync
fd546afe09073de92e5422c1334f1c27b4108687vboxsync uint64_t uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync char *pszRet = NULL;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (RTStrToUInt64Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->uIndex = uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else
fd546afe09073de92e5422c1334f1c27b4108687vboxsync AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
81587231c9c584851518872e197f6f02dffe68cavboxsync
7be2140da7230ed5736528554a4dc34b2ac482acvboxsync pState->pDef = pOpt;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt->iShort;
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync }
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /*
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Not a known option argument. If it starts with a switch char (-) we'll
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * fail with unkown option, and if it doesn't we'll return it as a non-option.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync if (*pszArgThis == '-')
81587231c9c584851518872e197f6f02dffe68cavboxsync {
81587231c9c584851518872e197f6f02dffe68cavboxsync pValueUnion->psz = pszArgThis;
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync return VERR_GETOPT_UNKNOWN_OPTION;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync pValueUnion->psz = pszArgThis;
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync return VINF_GETOPT_NOT_OPTION;
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync}
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsyncRT_EXPORT_SYMBOL(RTGetOpt);
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsyncRTDECL(int) RTGetOptFetchValue(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion, uint32_t fFlags)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync{
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync /*
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Validate input.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync PCRTGETOPTDEF pOpt = pState->pDef;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync AssertReturn(pOpt, VERR_GETOPT_UNKNOWN_OPTION);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync AssertReturn(!(fFlags & ~RTGETOPT_VALID_MASK), VERR_INVALID_PARAMETER);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync AssertReturn((fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING, VERR_INVALID_PARAMETER);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync /*
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Make sure the union is completely cleared out, whatever happens below.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->u64 = 0;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->pDef = NULL;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync /*
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync * Pop off the next argument and convert it into a value union.
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync if (pState->iNext >= pState->argc)
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync int iThis = pState->iNext++;
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync const char *pszValue = pState->argv[iThis];
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->pDef = pOpt; /* in case of no value or error. */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return rtGetOptProcessValue(fFlags, pszValue, pValueUnion);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync}
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsyncRT_EXPORT_SYMBOL(RTGetOptFetchValue);