getopt.cpp revision edc1243d361fe369a09b921e4cd3a41b1da5d828
/* $Id$ */
/** @file
* IPRT - Command Line Parsing
*/
/*
* Copyright (C) 2007-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Standard options that gets included unless RTGETOPTINIT_FLAGS_NO_STD_OPTS is
* set.
*/
static RTGETOPTDEF const g_aStdOptions[] =
{
};
/** The index of --help in g_aStdOptions. Used for some trickery. */
#define RTGETOPT_STD_OPTIONS_HELP_IDX 0
{
AssertReturn(!(fFlags & ~(RTGETOPTINIT_FLAGS_OPTS_FIRST | RTGETOPTINIT_FLAGS_NO_STD_OPTS)), VERR_INVALID_PARAMETER);
pState->cNonOptions = 0;
/* validate the options. */
{
}
return VINF_SUCCESS;
}
/**
* Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
*
* @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
* failure.
*
* @param pszValue The value to convert.
* @param pAddr Where to store the result.
*/
{
return VINF_SUCCESS;
}
/**
* Converts an stringified Ethernet MAC address into the RTMAC representation.
*
* @todo This should be move to some generic part of the runtime.
*
* @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
* failure.
*
* @param pszValue The value to convert.
* @param pAddr Where to store the result.
*/
{
/*
* Not quite sure if I should accept stuff like "08::27:::1" here...
* The code is accepting "::" patterns now, except for for the first
* and last parts.
*/
/* first */
char *pszNext;
if (*pszNext++ != ':')
/* middle */
for (unsigned i = 1; i < 5; i++)
{
if (*pszNext == ':')
else
{
if (*pszNext != ':')
}
pszNext++;
}
/* last */
if (*pszNext)
return VINF_SUCCESS;
}
/**
* Searches for a long option.
*
* @returns Pointer to a matching option.
* @param pszOption The alleged long option.
* @param paOptions Option array.
* @param cOptions Number of items in the array.
* @param fFlags Init flags.
*/
static PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
{
while (cOptions-- > 0)
{
{
{
/*
* A value is required with the argument. We're trying to be
* understanding here and will permit any of the following:
* --long12:value, --long12=value, --long12 value,
* --long:value, --long=value, --long value,
*
* If the option is index, then all trailing chars must be
* digits. For error reporting reasons we also match where
* there is no index.
*/
{
cchLong++;
return pOpt;
}
}
{
/*
* The option takes an index but no value.
* As above, we also match where there is no index.
*/
{
cchLong++;
return pOpt;
}
}
return pOpt;
}
pOpt++;
}
if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
return &g_aStdOptions[i];
return NULL;
}
/**
* Searches for a matching short option.
*
* @returns Pointer to a matching option.
* @param chOption The option char.
* @param paOptions Option array.
* @param cOptions Number of items in the array.
* @param fFlags Init flags.
*/
static PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
{
while (cOptions-- > 0)
{
return pOpt;
pOpt++;
}
if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
{
return &g_aStdOptions[i];
if (chOption == '?')
return &g_aStdOptions[RTGETOPT_STD_OPTIONS_HELP_IDX];
}
return NULL;
}
/**
* Value string -> Value union.
*
* @returns IPRT status code.
* @param fFlags The value flags.
* @param pszValue The value string.
* @param pValueUnion Where to return the processed value.
*/
{
/*
* Transform into a option value as requested.
* If decimal conversion fails, we'll check for "0x<xdigit>" and
* try a 16 based conversion. We will not interpret any of the
* generic ints as octals.
*/
switch (fFlags & ( RTGETOPT_REQ_MASK
{
case RTGETOPT_REQ_STRING:
break;
case RTGETOPT_REQ_BOOL:
)
pValueUnion->f = true;
)
pValueUnion->f = false;
else
{
return VERR_GETOPT_UNKNOWN_OPTION;
}
break;
case RTGETOPT_REQ_BOOL_ONOFF:
pValueUnion->f = true;
pValueUnion->f = false;
else
{
return VERR_GETOPT_UNKNOWN_OPTION;
}
break;
case req: \
{ \
&& ( pszValue[0] != '0' \
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
break; \
}
case req: \
{ \
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
break; \
}
case RTGETOPT_REQ_IPV4ADDR:
{
break;
}
#if 0 /** @todo CIDR */
#endif
case RTGETOPT_REQ_MACADDR:
{
break;
}
case RTGETOPT_REQ_UUID:
{
break;
}
default:
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
/**
* Moves one argv option entries.
*
* @param papszTo Destination.
* @param papszFrom Source.
*/
{
{
}
}
{
/*
* Reset the variables kept in state.
*/
/*
* Make sure the union is completely cleared out, whatever happens below.
*/
pValueUnion->u64 = 0;
/*
* The next option.
*/
bool fShort;
int iThis;
const char *pszArgThis;
if (pState->pszNextShort)
{
/*
* We've got short options left over from the previous call.
*/
pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions, pState->fFlags);
if (!pOpt)
{
return VERR_GETOPT_UNKNOWN_OPTION;
}
pState->pszNextShort++;
fShort = true;
}
else
{
/*
* Pop off the next argument. Sorting options and dealing with the
* dash-dash makes this a little extra complicated.
*/
for (;;)
{
return 0;
if (pState->cNonOptions)
{
{
return VINF_GETOPT_NOT_OPTION;
}
{
continue;
}
}
/*
* Do a long option search first and then a short option one.
* This way we can make sure single dash long options doesn't
* get mixed up with short ones.
*/
if ( !pOpt
&& pszArgThis[0] == '-'
{
}
else
fShort = false;
/* Look for dash-dash. */
{
continue;
}
/* Options first hacks. */
{
if (pOpt)
else if (*pszArgThis == '-')
{
return VERR_GETOPT_UNKNOWN_OPTION;
}
else
{
/* not an option, add it to the non-options and try again. */
pState->cNonOptions++;
/* Switch to returning non-options if we've reached the end. */
continue;
}
}
/* done */
break;
}
}
if (pOpt)
{
{
/*
* Find the argument value.
*
* A value is required with the argument. We're trying to be
* understanding here and will permit any of the following:
* -svalue, -s value, -s:value and -s=value
* (Ditto for long options.)
*/
const char *pszValue;
if (fShort)
{
{
}
else /* same argument. */
if (pState->pszNextShort)
{
}
}
else
{
{
return VERR_GETOPT_INDEX_MISSING;
if (rc == VWRN_TRAILING_CHARS)
{
if ( pszRet[0] != ':'
&& pszRet[0] != '=')
}
else if (rc == VINF_SUCCESS)
{
}
else
}
else
{
{
}
else /* same argument. */
}
}
/*
* Set up the ValueUnion.
*/
if (RT_FAILURE(rc))
return rc;
}
else if (fShort)
{
/*
* Deal with "compressed" short option lists, correcting the next
* state variables for the start and end cases.
*/
if (pszArgThis[2])
{
if (!pState->pszNextShort)
{
/* start */
}
}
else if (pState->pszNextShort)
{
/* end */
}
}
{
return VERR_GETOPT_INDEX_MISSING;
else
}
}
/*
* Not a known option argument. If it starts with a switch char (-) we'll
* fail with unknown option, and if it doesn't we'll return it as a non-option.
*/
if (*pszArgThis == '-')
{
return VERR_GETOPT_UNKNOWN_OPTION;
}
return VINF_GETOPT_NOT_OPTION;
}
{
/*
* Validate input.
*/
/*
* Make sure the union is completely cleared out, whatever happens below.
*/
pValueUnion->u64 = 0;
/*
* Pop off the next argument and convert it into a value union.
*/
const char *pszValue = pState->argv[iThis + (pState->cNonOptions != INT32_MAX ? pState->cNonOptions : 0)];
}
{
if (ch == VINF_GETOPT_NOT_OPTION)
else if (ch > 0)
{
if (RT_C_IS_GRAPH(ch))
else
}
else if (ch == VERR_GETOPT_UNKNOWN_OPTION)
else if (ch == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
/** @todo r=klaus not really ideal, as the value isn't available */
else if (pValueUnion->pDef)
else
return RTEXITCODE_SYNTAX;
}