dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync/* $Id$ */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - Command Line Parsing
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2007-2013 Oracle Corporation
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.
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync/*******************************************************************************
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync* Header Files *
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync*******************************************************************************/
3a491eb2162ca9e2024712f737c6317882307e13vboxsync#include <iprt/cidr.h>
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include <iprt/net.h> /* must come before getopt.h */
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync#include <iprt/getopt.h>
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync#include "internal/iprt.h"
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsync
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync#include <iprt/assert.h>
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync#include <iprt/ctype.h>
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync#include <iprt/err.h>
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync#include <iprt/message.h>
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync#include <iprt/string.h>
5ef92c0006229b3fc7bc7eb4ad80ed65fb0dd98bvboxsync#include <iprt/uuid.h>
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync/*******************************************************************************
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync* Global Variables *
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync*******************************************************************************/
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync/**
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * Standard options that gets included unless RTGETOPTINIT_FLAGS_NO_STD_OPTS is
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * set.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsyncstatic RTGETOPTDEF const g_aStdOptions[] =
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync{
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync { "--help", 'h', RTGETOPT_REQ_NOTHING },
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync { "-help", 'h', RTGETOPT_REQ_NOTHING },
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync { "--version", 'V', RTGETOPT_REQ_NOTHING },
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync { "-version", 'V', RTGETOPT_REQ_NOTHING },
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync};
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync/** The index of --help in g_aStdOptions. Used for some trickery. */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync#define RTGETOPT_STD_OPTIONS_HELP_IDX 0
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
dccbafa70b5a9a6f933c5566e4542caf9f379b97vboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync PCRTGETOPTDEF paOptions, size_t cOptions,
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync int iFirst, uint32_t fFlags)
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync{
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync AssertReturn(!(fFlags & ~(RTGETOPTINIT_FLAGS_OPTS_FIRST | RTGETOPTINIT_FLAGS_NO_STD_OPTS)), 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;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->uIndex = UINT32_MAX;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->fFlags = fFlags;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->cNonOptions = 0;
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}
aa4bcf0a4b2db3ac352b56a291d49cb8d4b66d32vboxsyncRT_EXPORT_SYMBOL(RTGetOptInit);
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync/**
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
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{
edc1243d361fe369a09b921e4cd3a41b1da5d828vboxsync if (RT_FAILURE(RTNetStrToIPv4Addr(pszValue, pAddr)))
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync return VINF_SUCCESS;
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync}
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
c882ddf98a60a4aab1218108b334083f98b7d66avboxsync
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync/**
b6097213cd07c5383c49168bfb61f915dbd4587avboxsync * Converts an stringified Ethernet MAC address into the RTMAC representation.
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
10019aae08864f54606add8cef1a241932a0c8fcvboxsync int rc = RTNetStrToMacAddr(pszValue, pAddr);
10019aae08864f54606add8cef1a241932a0c8fcvboxsync if (RT_FAILURE(rc))
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.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * @param fFlags Init flags.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
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 /*
7868f7238f92c22fcfede63182607629da7a8eadvboxsync * A value is required with the argument. We're trying to be
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 *
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);
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync if ( !strncmp(pszOption, pOpt->pszLong, cchLong)
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync && !RTStrNICmp(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);
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync if ( !strncmp(pszOption, pOpt->pszLong, cchLong)
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync && !RTStrNICmp(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 }
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync else if ( !strcmp(pszOption, pOpt->pszLong)
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync && !RTStrICmp(pszOption, pOpt->pszLong)))
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pOpt++;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync if ( !strcmp(pszOption, g_aStdOptions[i].pszLong)
aed76135bb01dd67d28b24137b66a747491521a2vboxsync || ( g_aStdOptions[i].fFlags & RTGETOPT_FLAG_ICASE
8e3f7f941e446e203bd8f3d0782d761aeb5af6c9vboxsync && !RTStrICmp(pszOption, g_aStdOptions[i].pszLong)))
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return &g_aStdOptions[i];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
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.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * @param fFlags Init flags.
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsyncstatic PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync{
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync PCRTGETOPTDEF pOpt = paOptions;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync while (cOptions-- > 0)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync {
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (pOpt->iShort == chOption)
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync return pOpt;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync pOpt++;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (g_aStdOptions[i].iShort == chOption)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return &g_aStdOptions[i];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (chOption == '?')
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return &g_aStdOptions[RTGETOPT_STD_OPTIONS_HELP_IDX];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
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
8f78050183f7368c70cfc93edd1180326edc1208vboxsync case RTGETOPT_REQ_BOOL:
8f78050183f7368c70cfc93edd1180326edc1208vboxsync if ( !RTStrICmp(pszValue, "true")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "t")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "yes")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "y")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "enabled")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "enable")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "en")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "e")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "on")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrCmp(pszValue, "1")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync )
8f78050183f7368c70cfc93edd1180326edc1208vboxsync pValueUnion->f = true;
8f78050183f7368c70cfc93edd1180326edc1208vboxsync else if ( !RTStrICmp(pszValue, "false")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "f")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "no")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "n")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "disabled")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "disable")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "dis")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "d")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrICmp(pszValue, "off")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync || !RTStrCmp(pszValue, "0")
8f78050183f7368c70cfc93edd1180326edc1208vboxsync )
8f78050183f7368c70cfc93edd1180326edc1208vboxsync pValueUnion->f = false;
8f78050183f7368c70cfc93edd1180326edc1208vboxsync else
8f78050183f7368c70cfc93edd1180326edc1208vboxsync {
8f78050183f7368c70cfc93edd1180326edc1208vboxsync pValueUnion->psz = pszValue;
8f78050183f7368c70cfc93edd1180326edc1208vboxsync return VERR_GETOPT_UNKNOWN_OPTION;
8f78050183f7368c70cfc93edd1180326edc1208vboxsync }
8f78050183f7368c70cfc93edd1180326edc1208vboxsync break;
8f78050183f7368c70cfc93edd1180326edc1208vboxsync
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
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync#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 }
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync#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
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i8, RTStrToInt8Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i16, RTStrToInt16Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i32, RTStrToInt32Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i64, RTStrToInt64Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u8, RTStrToUInt8Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u16, RTStrToUInt16Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u32, RTStrToUInt32Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u64, RTStrToUInt64Full)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i8, RTStrToInt8Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i16, RTStrToInt16Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i32, RTStrToInt32Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i64, RTStrToInt64Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u8, RTStrToUInt8Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u16, RTStrToUInt16Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u32, RTStrToUInt32Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u64, RTStrToUInt64Full, 16)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i8, RTStrToInt8Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i16, RTStrToInt16Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i32, RTStrToInt32Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i64, RTStrToInt64Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u8, RTStrToUInt8Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u16, RTStrToUInt16Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u32, RTStrToUInt32Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u64, RTStrToUInt64Full, 10)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i8, RTStrToInt8Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i16, RTStrToInt16Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i32, RTStrToInt32Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i64, RTStrToInt64Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u8, RTStrToUInt8Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u16, RTStrToUInt16Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u32, RTStrToUInt32Full, 8)
1a05eb58618c8f646bd1304d10c73792e0034f20vboxsync MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u64, 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 }
3a491eb2162ca9e2024712f737c6317882307e13vboxsync
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync case RTGETOPT_REQ_IPV4CIDR:
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync {
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync RTNETADDRIPV4 network;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync RTNETADDRIPV4 netmask;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync if (RT_FAILURE(RTCidrStrToIPv4(pszValue, &network, &netmask)))
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync pValueUnion->CidrIPv4.IPv4Network.u = network.u;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync pValueUnion->CidrIPv4.IPv4Netmask.u = netmask.u;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync break;
a0a3a26a4065b9401681a8c99a11bd83e08f94ccvboxsync }
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
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync/**
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * Moves one argv option entries.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync *
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * @param papszTo Destination.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * @param papszFrom Source.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsyncstatic void rtGetOptMoveArgvEntries(char **papszTo, char **papszFrom)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync{
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (papszTo != papszFrom)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync Assert((uintptr_t)papszTo < (uintptr_t)papszFrom);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync char * const pszMoved = papszFrom[0];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync memmove(&papszTo[1], &papszTo[0], (uintptr_t)papszFrom - (uintptr_t)papszTo);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync papszTo[0] = pszMoved;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync}
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsyncRTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
4d6dcfe00aab559241d9ed05b89f803ab5ddf611vboxsync{
fd546afe09073de92e5422c1334f1c27b4108687vboxsync /*
fd546afe09073de92e5422c1334f1c27b4108687vboxsync * Reset the variables kept in state.
fd546afe09073de92e5422c1334f1c27b4108687vboxsync */
7be2140da7230ed5736528554a4dc34b2ac482acvboxsync pState->pDef = NULL;
7e0c3d180b978b8f4b5b33f8e924520248ee3ab3vboxsync pState->uIndex = UINT32_MAX;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync
81587231c9c584851518872e197f6f02dffe68cavboxsync /*
81587231c9c584851518872e197f6f02dffe68cavboxsync * Make sure the union is completely cleared out, whatever happens below.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
dc88ff18a0306635c385b14211765e618351b253vboxsync 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 */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions, pState->fFlags);
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 /*
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * Pop off the next argument. Sorting options and dealing with the
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * dash-dash makes this a little extra complicated.
81587231c9c584851518872e197f6f02dffe68cavboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync for (;;)
81587231c9c584851518872e197f6f02dffe68cavboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->iNext >= pState->argc)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return 0;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->cNonOptions)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->cNonOptions == INT32_MAX)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pValueUnion->psz = pState->argv[pState->iNext++];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return VINF_GETOPT_NOT_OPTION;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->iNext + pState->cNonOptions >= pState->argc)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->cNonOptions = INT32_MAX;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync continue;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync iThis = pState->iNext++;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pszArgThis = pState->argv[iThis + pState->cNonOptions];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /*
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * Do a long option search first and then a short option one.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * This way we can make sure single dash long options doesn't
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync * get mixed up with short ones.
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions, pState->fFlags);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if ( !pOpt
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync && pszArgThis[0] == '-'
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync && pszArgThis[1] != '-'
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync && pszArgThis[1] != '\0')
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions, pState->fFlags);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync fShort = pOpt != NULL;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync else
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync fShort = false;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /* Look for dash-dash. */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (!pOpt && !strcmp(pszArgThis, "--"))
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->cNonOptions = INT32_MAX;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync continue;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /* Options first hacks. */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->fFlags & RTGETOPTINIT_FLAGS_OPTS_FIRST)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pOpt)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync else if (*pszArgThis == '-')
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pValueUnion->psz = pszArgThis;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync return VERR_GETOPT_UNKNOWN_OPTION;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync else
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync {
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /* not an option, add it to the non-options and try again. */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->iNext--;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->cNonOptions++;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /* Switch to returning non-options if we've reached the end. */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->iNext + pState->cNonOptions >= pState->argc)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pState->cNonOptions = INT32_MAX;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync continue;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync }
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync /* done */
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync break;
81587231c9c584851518872e197f6f02dffe68cavboxsync }
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 *
7868f7238f92c22fcfede63182607629da7a8eadvboxsync * A value is required with the argument. We're trying to be
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * understanding here and will permit any of the following:
7868f7238f92c22fcfede63182607629da7a8eadvboxsync * -svalue, -s value, -s:value and -s=value
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync * (Ditto for long options.)
14e0667f834cd0e2a8c365084cd2ad0a893109e8vboxsync */
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync const char *pszValue;
45e9c1c72518aeba6673332bdd4d70b59e1c11a4vboxsync if (fShort)
d618bed3882cd227cf8f5d0d2824f8db42fad415vboxsync {
7868f7238f92c22fcfede63182607629da7a8eadvboxsync if (pszArgThis[2] == '\0')
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync {
ea6c70405e39fa563a55780ef25e0933d8c73a1avboxsync if (iThis + 1 >= pState->argc)
da97b7ac000d0f02e86c31a4f2767a00d83c6167vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 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
7e0c3d180b978b8f4b5b33f8e924520248ee3ab3vboxsync uint32_t uIndex;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync char *pszRet = NULL;
7e0c3d180b978b8f4b5b33f8e924520248ee3ab3vboxsync int rc = RTStrToUInt32Ex(&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;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
fd546afe09073de92e5422c1334f1c27b4108687vboxsync pState->iNext++;
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else
fd546afe09073de92e5422c1334f1c27b4108687vboxsync AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
fd546afe09073de92e5422c1334f1c27b4108687vboxsync }
fd546afe09073de92e5422c1334f1c27b4108687vboxsync else
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
7868f7238f92c22fcfede63182607629da7a8eadvboxsync if (pszArgThis[cchLong] == '\0')
fd546afe09073de92e5422c1334f1c27b4108687vboxsync {
fd546afe09073de92e5422c1334f1c27b4108687vboxsync if (iThis + 1 >= pState->argc)
fd546afe09073de92e5422c1334f1c27b4108687vboxsync return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync pszValue = pState->argv[iThis + pState->cNonOptions + 1];
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 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
7e0c3d180b978b8f4b5b33f8e924520248ee3ab3vboxsync uint32_t uIndex;
7e0c3d180b978b8f4b5b33f8e924520248ee3ab3vboxsync if (RTStrToUInt32Full(&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
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * fail with unknown option, and if it doesn't we'll return it as a non-option.
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(!(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 */
dc88ff18a0306635c385b14211765e618351b253vboxsync 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++;
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync const char *pszValue = pState->argv[iThis + (pState->cNonOptions != INT32_MAX ? pState->cNonOptions : 0)];
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync pValueUnion->pDef = pOpt; /* in case of no value or error. */
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync if (pState->cNonOptions && pState->cNonOptions != INT32_MAX)
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
b1cf57acefb5d1ce6ae2466f43e225a81083ff34vboxsync
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync return rtGetOptProcessValue(fFlags, pszValue, pValueUnion);
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsync}
f6f8bfbbbc6a59ba94b01886ed5a8d6e5813073bvboxsyncRT_EXPORT_SYMBOL(RTGetOptFetchValue);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync
f181d21bd7f14d7ee3709246500c9353bb33c922vboxsyncRTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion)
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync{
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync if (ch == VINF_GETOPT_NOT_OPTION)
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("Invalid parameter: %s", pValueUnion->psz);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync else if (ch > 0)
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync {
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync if (RT_C_IS_GRAPH(ch))
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("Unhandled option: -%c", ch);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync else
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("Unhandled option: %i (%#x)", ch, ch);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync }
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync else if (ch == VERR_GETOPT_UNKNOWN_OPTION)
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("Unknown option: '%s'", pValueUnion->psz);
01b2e367e947049139a5d5813ccc1fa162b11f76vboxsync else if (ch == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
7bc0784812dcf63751c5eebfdd1c5d24ac26b389vboxsync /** @todo r=klaus not really ideal, as the value isn't available */
7bc0784812dcf63751c5eebfdd1c5d24ac26b389vboxsync RTMsgError("The value given '%s' has an invalid format.", pValueUnion->pDef->pszLong);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync else if (pValueUnion->pDef)
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("%s: %Rrs\n", pValueUnion->pDef->pszLong, ch);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync else
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync RTMsgError("%Rrs\n", ch);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync
f181d21bd7f14d7ee3709246500c9353bb33c922vboxsync return RTEXITCODE_SYNTAX;
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync}
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsyncRT_EXPORT_SYMBOL(RTGetOptPrintError);
03c2683d4e59cc500217b1d5d76fdf9b8cdfefa5vboxsync