getopt.cpp revision 81587231c9c584851518872e197f6f02dffe68ca
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*******************************************************************************/
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/getopt.h>
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/err.h>
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/string.h>
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync#include <iprt/assert.h>
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#include <iprt/ctype.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;
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
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync return VINF_SUCCESS;
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync}
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
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:
e0b01f0907f9ade6df65e65e86196fa09a11a245vboxsync * --long:value, --long=value, --long value,
e0b01f0907f9ade6df65e65e86196fa09a11a245vboxsync * --long: value, --long= value
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync size_t cchLong = strlen(pOpt->pszLong);
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ( !strncmp(pszOption, pOpt->pszLong, cchLong)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync && ( pszOption[cchLong] == '\0'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || pszOption[cchLong] == ':'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || pszOption[cchLong] == '='))
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt;
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
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync{
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Make sure the union is completely cleared out, whatever happens below.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync pValueUnion->u64 = 0;
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync pValueUnion->pDef = NULL;
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
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 /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Do a long option search first and the 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
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /** @todo Handle '--' and possibly implement an RTGetOptInit that lets us
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * optionally sort the stuff and set other policeis sorts the result. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /** @todo Implement short form short option like in:
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * ls -latr .
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * tar -xzvf foobar.tar.gz
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
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);
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ( pszArgThis[cchLong] == '\0'
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || pszArgThis[cchLong + 1] == '\0')
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (iThis + 1 >= pState->argc)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pszValue = pState->argv[iThis + 1];
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pState->iNext++;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync else /* same argument. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pszValue = &pszArgThis[cchLong + 1];
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /*
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Transform into a option value as requested.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * If decimal conversion fails, we'll check for "0x<xdigit>" and
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * try a 16 based conversion. We will not interpret any of the
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * generic ints as octals.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync switch (pOpt->fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync case RTGETOPT_REQ_STRING:
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pValueUnion->psz = pszValue;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync break;
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#define MY_INT_CASE(req,type,memb,convfn) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync case req: \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync { \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync type Value; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync && ( pszValue[0] != '0' \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || (pszValue[1] != 'x' && pszValue[1] != 'X') \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || !RT_C_IS_XDIGIT(pszValue[2]) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pValueUnion->memb = Value; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync break; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync case req: \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync { \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync type Value; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pValueUnion->memb = Value; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync break; \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#undef MY_INT_CASE
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#undef MY_BASE_INT_CASE
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync default:
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync AssertMsgFailed(("i=%d f=%#x\n", pOpt - &pState->paOptions[0], pOpt->fFlags));
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return VERR_INTERNAL_ERROR;
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync }
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 }
81587231c9c584851518872e197f6f02dffe68cavboxsync
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}
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync