VirtualBox

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

Last change on this file since 20364 was 18744, checked in by vboxsync, 16 years ago

Runtime/getopt: provide way to figure out which option definition matched

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.3 KB
Line 
1/* $Id: getopt.cpp 18744 2009-04-06 11:08:34Z vboxsync $ */
2/** @file
3 * IPRT - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/net.h>
35#include <iprt/getopt.h>
36#include <iprt/err.h>
37#include <iprt/string.h>
38#include <iprt/assert.h>
39#include <iprt/ctype.h>
40
41
42
43RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
44 PCRTGETOPTDEF paOptions, size_t cOptions,
45 int iFirst, uint32_t fFlags)
46{
47 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
48
49 pState->argv = argv;
50 pState->argc = argc;
51 pState->paOptions = paOptions;
52 pState->cOptions = cOptions;
53 pState->iNext = iFirst;
54 pState->pszNextShort = NULL;
55 pState->pDef = NULL;
56
57 /* validate the options. */
58 for (size_t i = 0; i < cOptions; i++)
59 {
60 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
61 Assert(paOptions[i].iShort > 0);
62 Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
63 Assert(paOptions[i].iShort != '-');
64 }
65
66 /** @todo Add an flag for sorting the arguments so that all the options comes
67 * first. */
68 return VINF_SUCCESS;
69}
70
71
72/**
73 * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
74 *
75 * @todo This should be move to some generic part of the runtime.
76 *
77 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
78 * failure.
79 *
80 * @param pszValue The value to convert.
81 * @param pAddr Where to store the result.
82 */
83static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
84{
85 char *pszNext;
86 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
87 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
88 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
89 if (*pszNext++ != '.')
90 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
91
92 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
93 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
94 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
95 if (*pszNext++ != '.')
96 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
97
98 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
99 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
100 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
101 if (*pszNext++ != '.')
102 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
103
104 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
105 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
106 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
107 pszNext = RTStrStripL(pszNext);
108 if (*pszNext)
109 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
110
111 return VINF_SUCCESS;
112}
113
114
115/**
116 * Converts an stringified Ethernet MAC address into the RTMAC representation.
117 *
118 * @todo This should be move to some generic part of the runtime.
119 *
120 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
121 * failure.
122 *
123 * @param pszValue The value to convert.
124 * @param pAddr Where to store the result.
125 */
126static int rtgetoptConvertMacAddr(const char *pszValue, PRTMAC pAddr)
127{
128 /*
129 * Not quite sure if I should accept stuff like "08::27:::1" here...
130 * The code is accepting "::" patterns now, except for for the first
131 * and last parts.
132 */
133
134 /* first */
135 char *pszNext;
136 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
137 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
138 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
139 if (*pszNext++ != ':')
140 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
141
142 /* middle */
143 for (unsigned i = 1; i < 5; i++)
144 {
145 if (*pszNext == ':')
146 pAddr->au8[i] = 0;
147 else
148 {
149 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
150 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
151 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
152 if (*pszNext != ':')
153 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
154 }
155 pszNext++;
156 }
157
158 /* last */
159 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
160 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
161 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
162 pszNext = RTStrStripL(pszNext);
163 if (*pszNext)
164 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
165
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Searches for a long option.
172 *
173 * @returns Pointer to a matching option.
174 * @param pszOption The alleged long option.
175 * @param paOptions Option array.
176 * @param cOptions Number of items in the array.
177 */
178static PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions)
179{
180 PCRTGETOPTDEF pOpt = paOptions;
181 while (cOptions-- > 0)
182 {
183 if (pOpt->pszLong)
184 {
185 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
186 {
187 /*
188 * A value is required with the argument. We're trying to be very
189 * understanding here and will permit any of the following:
190 * --long:value, --long=value, --long value,
191 * --long: value, --long= value
192 */
193 size_t cchLong = strlen(pOpt->pszLong);
194 if ( !strncmp(pszOption, pOpt->pszLong, cchLong)
195 && ( pszOption[cchLong] == '\0'
196 || pszOption[cchLong] == ':'
197 || pszOption[cchLong] == '='))
198 return pOpt;
199 }
200 else if (!strcmp(pszOption, pOpt->pszLong))
201 return pOpt;
202 }
203 pOpt++;
204 }
205 return NULL;
206}
207
208
209/**
210 * Searches for a matching short option.
211 *
212 * @returns Pointer to a matching option.
213 * @param chOption The option char.
214 * @param paOptions Option array.
215 * @param cOptions Number of items in the array.
216 */
217static PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions)
218{
219 PCRTGETOPTDEF pOpt = paOptions;
220 while (cOptions-- > 0)
221 {
222 if (pOpt->iShort == chOption)
223 return pOpt;
224 pOpt++;
225 }
226 return NULL;
227}
228
229
230RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
231{
232 pState->pDef = NULL;
233 /*
234 * Make sure the union is completely cleared out, whatever happens below.
235 */
236 pValueUnion->u64 = 0;
237 pValueUnion->pDef = NULL;
238
239 /** @todo Handle '--' (end of options).*/
240 /** @todo Add a flag to RTGetOptInit for handling the various help options in
241 * a common way. (-?,-h,-help,--help,++) */
242 /** @todo Add a flag to RTGetOptInit for handling the standard version options
243 * in a common way. (-V,--version) */
244
245 /*
246 * The next option.
247 */
248 bool fShort;
249 int iThis;
250 const char *pszArgThis;
251 PCRTGETOPTDEF pOpt;
252
253 if (pState->pszNextShort)
254 {
255 /*
256 * We've got short options left over from the previous call.
257 */
258 pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions);
259 if (!pOpt)
260 {
261 pValueUnion->psz = pState->pszNextShort;
262 return VERR_GETOPT_UNKNOWN_OPTION;
263 }
264 pState->pszNextShort++;
265 pszArgThis = pState->pszNextShort - 2;
266 iThis = pState->iNext;
267 fShort = true;
268 }
269 else
270 {
271 /*
272 * Pop off the next argument.
273 */
274 if (pState->iNext >= pState->argc)
275 return 0;
276 iThis = pState->iNext++;
277 pszArgThis = pState->argv[iThis];
278
279 /*
280 * Do a long option search first and then a short option one.
281 * This way we can make sure single dash long options doesn't
282 * get mixed up with short ones.
283 */
284 pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions);
285 if ( !pOpt
286 && pszArgThis[0] == '-'
287 && pszArgThis[1] != '-'
288 && pszArgThis[1] != '\0')
289 {
290 pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions);
291 fShort = pOpt != NULL;
292 }
293 else
294 fShort = false;
295 }
296
297 if (pOpt)
298 {
299 pValueUnion->pDef = pOpt; /* in case of no value or error. */
300
301 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
302 {
303 /*
304 * Find the argument value.
305 *
306 * A value is required with the argument. We're trying to be very
307 * understanding here and will permit any of the following:
308 * -svalue, -s:value, -s=value,
309 * -s value, -s: value, -s= value
310 * (Ditto for long options.)
311 */
312 const char *pszValue;
313 if (fShort)
314 {
315 if ( pszArgThis[2] == '\0'
316 || ( pszArgThis[3] == '\0'
317 && ( pszArgThis[2] == ':'
318 || pszArgThis[2] == '=')) )
319 {
320 if (iThis + 1 >= pState->argc)
321 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
322 pszValue = pState->argv[iThis + 1];
323 pState->iNext++;
324 }
325 else /* same argument. */
326 pszValue = &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')];
327 if (pState->pszNextShort)
328 {
329 pState->pszNextShort = NULL;
330 pState->iNext++;
331 }
332 }
333 else
334 {
335 size_t cchLong = strlen(pOpt->pszLong);
336 if ( pszArgThis[cchLong] == '\0'
337 || pszArgThis[cchLong + 1] == '\0')
338 {
339 if (iThis + 1 >= pState->argc)
340 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
341 pszValue = pState->argv[iThis + 1];
342 pState->iNext++;
343 }
344 else /* same argument. */
345 pszValue = &pszArgThis[cchLong + 1];
346 }
347
348 /*
349 * Transform into a option value as requested.
350 * If decimal conversion fails, we'll check for "0x<xdigit>" and
351 * try a 16 based conversion. We will not interpret any of the
352 * generic ints as octals.
353 */
354 switch (pOpt->fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
355 {
356 case RTGETOPT_REQ_STRING:
357 pValueUnion->psz = pszValue;
358 break;
359
360#define MY_INT_CASE(req,type,memb,convfn) \
361 case req: \
362 { \
363 type Value; \
364 if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
365 && ( pszValue[0] != '0' \
366 || (pszValue[1] != 'x' && pszValue[1] != 'X') \
367 || !RT_C_IS_XDIGIT(pszValue[2]) \
368 || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
369 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
370 pValueUnion->memb = Value; \
371 break; \
372 }
373#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
374 case req: \
375 { \
376 type Value; \
377 if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
378 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
379 pValueUnion->memb = Value; \
380 break; \
381 }
382
383 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
384 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
385 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
386 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
387 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
388 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
389 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
390 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
391
392 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
393 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
394 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
395 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
396 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
397 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
398 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
399 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
400
401 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
402 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
403 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
404 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
405 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
406 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
407 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
408 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
409
410 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
411 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
412 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
413 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
414 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
415 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
416 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
417 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
418
419#undef MY_INT_CASE
420#undef MY_BASE_INT_CASE
421
422 case RTGETOPT_REQ_IPV4ADDR:
423 {
424 RTNETADDRIPV4 Addr;
425 if (rtgetoptConvertIPv4Addr(pszValue, &Addr) != VINF_SUCCESS)
426 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
427 pValueUnion->IPv4Addr = Addr;
428 break;
429 }
430#if 0 /** @todo CIDR */
431#endif
432
433 case RTGETOPT_REQ_MACADDR:
434 {
435 RTMAC Addr;
436 if (rtgetoptConvertMacAddr(pszValue, &Addr) != VINF_SUCCESS)
437 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
438 pValueUnion->MacAddr = Addr;
439 break;
440 }
441
442 default:
443 AssertMsgFailed(("i=%d f=%#x\n", pOpt - &pState->paOptions[0], pOpt->fFlags));
444 return VERR_INTERNAL_ERROR;
445 }
446 }
447 else if (fShort)
448 {
449 /*
450 * Deal with "compressed" short option lists, correcting the next
451 * state variables for the start and end cases.
452 */
453 if (pszArgThis[2])
454 {
455 if (!pState->pszNextShort)
456 {
457 /* start */
458 pState->pszNextShort = &pszArgThis[2];
459 pState->iNext--;
460 }
461 }
462 else if (pState->pszNextShort)
463 {
464 /* end */
465 pState->pszNextShort = NULL;
466 pState->iNext++;
467 }
468 }
469
470 pState->pDef = pOpt;
471 return pOpt->iShort;
472 }
473
474 /*
475 * Not a known option argument. If it starts with a switch char (-) we'll
476 * fail with unkown option, and if it doesn't we'll return it as a non-option.
477 */
478
479 if (*pszArgThis == '-')
480 {
481 pValueUnion->psz = pszArgThis;
482 return VERR_GETOPT_UNKNOWN_OPTION;
483 }
484
485 pValueUnion->psz = pszArgThis;
486 return VINF_GETOPT_NOT_OPTION;
487}
488
Note: See TracBrowser for help on using the repository browser.

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