VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/getopt.cpp@ 8148

Last change on this file since 8148 was 8148, checked in by vboxsync, 17 years ago

iShort must be > 0, so assert on that.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: getopt.cpp 8148 2008-04-18 13:52:58Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/getopt.h>
31#include <iprt/err.h>
32#include <iprt/string.h>
33#include <iprt/assert.h>
34#include <iprt/ctype.h>
35
36
37
38RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
39{
40 pValueUnion->u64 = 0;
41 pValueUnion->pDef = NULL;
42
43 if ( !piThis
44 || *piThis >= argc
45 )
46 return 0;
47
48 int iThis = (*piThis)++;
49 const char *pszArgThis = argv[iThis];
50
51 if (*pszArgThis == '-')
52 {
53 for (size_t i = 0; i < cOptions; i++)
54 {
55 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
56 Assert(paOptions[i].iShort > 0);
57
58 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
59 {
60 /*
61 * A value is required with the argument. We're trying to very
62 * understanding here and will permit any of the following:
63 * -svalue, -s:value, -s=value,
64 * -s value, -s: value, -s= value
65 * (Ditto for long options.)
66 */
67 bool fShort = false;
68 size_t cchLong = 2;
69 if ( ( paOptions[i].pszLong
70 && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong)))
71 && ( pszArgThis[cchLong] == '\0'
72 || pszArgThis[cchLong] == ':'
73 || pszArgThis[cchLong] == '=')
74 )
75 || (fShort = (pszArgThis[1] == paOptions[i].iShort))
76 )
77 {
78 pValueUnion->pDef = &paOptions[i]; /* in case of error. */
79
80 /*
81 * Find the argument value
82 */
83 const char *pszValue;
84 if ( fShort
85 ? pszArgThis[2] == '\0'
86 || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0')
87 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0')
88 {
89 if (iThis + 1 >= argc)
90 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
91 pszValue = argv[iThis + 1];
92 (*piThis)++;
93 }
94 else /* same argument. */
95 pszValue = fShort
96 ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')]
97 : &pszArgThis[cchLong + 1];
98
99 /*
100 * Transform into a option value as requested.
101 * If decimal conversion fails, we'll check for "0x<xdigit>" and
102 * try a 16 based conversion. We will not interpret any of the
103 * generic ints as octals.
104 */
105 switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
106 {
107 case RTGETOPT_REQ_STRING:
108 pValueUnion->psz = pszValue;
109 break;
110
111#define MY_INT_CASE(req,type,memb,convfn) \
112 case req: \
113 { \
114 type Value; \
115 if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
116 && ( pszValue[0] != '0' \
117 || (pszValue[1] != 'x' && pszValue[1] != 'X') \
118 || !RT_C_IS_XDIGIT(pszValue[2]) \
119 || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
120 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
121 pValueUnion->memb = Value; \
122 break; \
123 }
124#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
125 case req: \
126 { \
127 type Value; \
128 if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
129 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
130 pValueUnion->memb = Value; \
131 break; \
132 }
133
134 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
135 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
136 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
137 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
138 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
139 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
140 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
141 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
142
143 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
144 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
145 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
146 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
147 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
148 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
149 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
150 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
151
152 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
153 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
154 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
155 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
156 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
157 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
158 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
159 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
160
161 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
162 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
163 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
164 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
165 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
166 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
167 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
168 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
169#undef MY_INT_CASE
170#undef MY_BASE_INT_CASE
171
172 default:
173 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
174 return VERR_INTERNAL_ERROR;
175 }
176 return paOptions[i].iShort;
177 }
178 }
179 else if ( ( paOptions[i].pszLong
180 && !strcmp(pszArgThis, paOptions[i].pszLong))
181 || ( pszArgThis[1] == paOptions[i].iShort
182 && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */
183 )
184 {
185 pValueUnion->pDef = &paOptions[i];
186 return paOptions[i].iShort;
187 }
188 }
189 }
190
191 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
192 * encountering the first argument. */
193
194 return VERR_GETOPT_UNKNOWN_OPTION;
195}
196
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette