summaryrefslogtreecommitdiff
path: root/src/VBox/Runtime/common/misc/getopt.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/VBox/Runtime/common/misc/getopt.cpp')
-rw-r--r--src/VBox/Runtime/common/misc/getopt.cpp89
1 files changed, 19 insertions, 70 deletions
diff --git a/src/VBox/Runtime/common/misc/getopt.cpp b/src/VBox/Runtime/common/misc/getopt.cpp
index f4321404..cf538f20 100644
--- a/src/VBox/Runtime/common/misc/getopt.cpp
+++ b/src/VBox/Runtime/common/misc/getopt.cpp
@@ -4,7 +4,7 @@
*/
/*
- * Copyright (C) 2007-2011 Oracle Corporation
+ * 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;
@@ -27,6 +27,7 @@
/*******************************************************************************
* Header Files *
*******************************************************************************/
+#include <iprt/cidr.h>
#include <iprt/net.h> /* must come before getopt.h */
#include <iprt/getopt.h>
#include "internal/iprt.h"
@@ -92,8 +93,6 @@ RT_EXPORT_SYMBOL(RTGetOptInit);
/**
* Converts an stringified IPv4 address into the RTNETADDRIPV4 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.
*
@@ -102,32 +101,8 @@ RT_EXPORT_SYMBOL(RTGetOptInit);
*/
static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
{
- char *pszNext;
- int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- if (*pszNext++ != '.')
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
-
- rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- if (*pszNext++ != '.')
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
-
- rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- if (*pszNext++ != '.')
+ if (RT_FAILURE(RTNetStrToIPv4Addr(pszValue, pAddr)))
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
-
- rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- pszNext = RTStrStripL(pszNext);
- if (*pszNext)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
-
return VINF_SUCCESS;
}
@@ -135,8 +110,6 @@ static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
/**
* 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.
*
@@ -145,42 +118,9 @@ static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
*/
static int rtgetoptConvertMacAddr(const char *pszValue, PRTMAC pAddr)
{
- /*
- * 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;
- int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- if (*pszNext++ != ':')
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
-
- /* middle */
- for (unsigned i = 1; i < 5; i++)
- {
- if (*pszNext == ':')
- pAddr->au8[i] = 0;
- else
- {
- rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- if (*pszNext != ':')
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- }
- pszNext++;
- }
-
- /* last */
- rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
- if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
- return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
- pszNext = RTStrStripL(pszNext);
- if (*pszNext)
+ int rc = RTNetStrToMacAddr(pszValue, pAddr);
+ if (RT_FAILURE(rc))
return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
return VINF_SUCCESS;
@@ -257,7 +197,7 @@ static PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paO
if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
if ( !strcmp(pszOption, g_aStdOptions[i].pszLong)
- || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE
+ || ( g_aStdOptions[i].fFlags & RTGETOPT_FLAG_ICASE
&& !RTStrICmp(pszOption, g_aStdOptions[i].pszLong)))
return &g_aStdOptions[i];
@@ -435,8 +375,17 @@ static int rtGetOptProcessValue(uint32_t fFlags, const char *pszValue, PRTGETOPT
pValueUnion->IPv4Addr = Addr;
break;
}
-#if 0 /** @todo CIDR */
-#endif
+
+ case RTGETOPT_REQ_IPV4CIDR:
+ {
+ RTNETADDRIPV4 network;
+ RTNETADDRIPV4 netmask;
+ if (RT_FAILURE(RTCidrStrToIPv4(pszValue, &network, &netmask)))
+ return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
+ pValueUnion->CidrIPv4.IPv4Network.u = network.u;
+ pValueUnion->CidrIPv4.IPv4Netmask.u = netmask.u;
+ break;
+ }
case RTGETOPT_REQ_MACADDR:
{
@@ -791,8 +740,8 @@ RTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion)
else if (ch == VERR_GETOPT_UNKNOWN_OPTION)
RTMsgError("Unknown option: '%s'", pValueUnion->psz);
else if (ch == VERR_GETOPT_INVALID_ARGUMENT_FORMAT)
- /** @todo r=klaus not really ideal, as the option isn't available */
- RTMsgError("Invalid argument format: '%s'", pValueUnion->psz);
+ /** @todo r=klaus not really ideal, as the value isn't available */
+ RTMsgError("The value given '%s' has an invalid format.", pValueUnion->pDef->pszLong);
else if (pValueUnion->pDef)
RTMsgError("%s: %Rrs\n", pValueUnion->pDef->pszLong, ch);
else