getopt.cpp revision 81587231c9c584851518872e197f6f02dffe68ca
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Command Line Parsing
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2007 Sun Microsystems, Inc.
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 * 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 * 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 * 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* Header Files *
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync*******************************************************************************/
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync /* validate the options. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Searches for a long option.
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.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync while (cOptions-- > 0)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
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 * Searches for a matching short option.
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.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync while (cOptions-- > 0)
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
81587231c9c584851518872e197f6f02dffe68cavboxsync * Make sure the union is completely cleared out, whatever happens below.
81587231c9c584851518872e197f6f02dffe68cavboxsync * The next option.
81587231c9c584851518872e197f6f02dffe68cavboxsync * We've got short options left over from the previous call.
81587231c9c584851518872e197f6f02dffe68cavboxsync pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions);
81587231c9c584851518872e197f6f02dffe68cavboxsync * Pop off the next argument.
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 pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions);
81587231c9c584851518872e197f6f02dffe68cavboxsync pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions);
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 pValueUnion->pDef = pOpt; /* in case of no value or error. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync * Find the argument value.
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.)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync const char *pszValue;
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync else /* same argument. */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pszValue = &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')];
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync else /* same argument. */
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 switch (pOpt->fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
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 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 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 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)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync AssertMsgFailed(("i=%d f=%#x\n", pOpt - &pState->paOptions[0], pOpt->fFlags));
81587231c9c584851518872e197f6f02dffe68cavboxsync * Deal with "compressed" short option lists, correcting the next
81587231c9c584851518872e197f6f02dffe68cavboxsync * state variables for the start and end cases.
81587231c9c584851518872e197f6f02dffe68cavboxsync /* start */
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.