VirtualBox

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

Last change on this file since 34789 was 34542, checked in by vboxsync, 14 years ago

RTGetOpt: Made it a bit less understanding about spaces after the option/value separator (':' or '='). See defect #5320.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.3 KB
Line 
1/* $Id: getopt.cpp 34542 2010-11-30 22:57:21Z vboxsync $ */
2/** @file
3 * IPRT - Command Line Parsing
4 */
5
6/*
7 * Copyright (C) 2007-2010 Oracle Corporation
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
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/net.h> /* must come before getopt.h */
31#include <iprt/getopt.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/err.h>
37#include <iprt/message.h>
38#include <iprt/string.h>
39#include <iprt/uuid.h>
40
41
42/*******************************************************************************
43* Global Variables *
44*******************************************************************************/
45/**
46 * Standard options that gets included unless RTGETOPTINIT_FLAGS_NO_STD_OPTS is
47 * set.
48 */
49static RTGETOPTDEF const g_aStdOptions[] =
50{
51 { "--help", 'h', RTGETOPT_REQ_NOTHING },
52 { "-help", 'h', RTGETOPT_REQ_NOTHING },
53 { "--version", 'V', RTGETOPT_REQ_NOTHING },
54 { "-version", 'V', RTGETOPT_REQ_NOTHING },
55};
56/** The index of --help in g_aStdOptions. Used for some trickery. */
57#define RTGETOPT_STD_OPTIONS_HELP_IDX 0
58
59
60
61RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
62 PCRTGETOPTDEF paOptions, size_t cOptions,
63 int iFirst, uint32_t fFlags)
64{
65 AssertReturn(!(fFlags & ~(RTGETOPTINIT_FLAGS_OPTS_FIRST | RTGETOPTINIT_FLAGS_NO_STD_OPTS)), VERR_INVALID_PARAMETER);
66
67 pState->argv = argv;
68 pState->argc = argc;
69 pState->paOptions = paOptions;
70 pState->cOptions = cOptions;
71 pState->iNext = iFirst;
72 pState->pszNextShort = NULL;
73 pState->pDef = NULL;
74 pState->uIndex = UINT32_MAX;
75 pState->fFlags = fFlags;
76 pState->cNonOptions = 0;
77
78 /* validate the options. */
79 for (size_t i = 0; i < cOptions; i++)
80 {
81 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK));
82 Assert(paOptions[i].iShort > 0);
83 Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION);
84 Assert(paOptions[i].iShort != '-');
85 }
86
87 return VINF_SUCCESS;
88}
89RT_EXPORT_SYMBOL(RTGetOptInit);
90
91
92/**
93 * Converts an stringified IPv4 address into the RTNETADDRIPV4 representation.
94 *
95 * @todo This should be move to some generic part of the runtime.
96 *
97 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
98 * failure.
99 *
100 * @param pszValue The value to convert.
101 * @param pAddr Where to store the result.
102 */
103static int rtgetoptConvertIPv4Addr(const char *pszValue, PRTNETADDRIPV4 pAddr)
104{
105 char *pszNext;
106 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 10, &pAddr->au8[0]);
107 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
108 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
109 if (*pszNext++ != '.')
110 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
111
112 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[1]);
113 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
114 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
115 if (*pszNext++ != '.')
116 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
117
118 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[2]);
119 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
120 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
121 if (*pszNext++ != '.')
122 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
123
124 rc = RTStrToUInt8Ex(pszNext, &pszNext, 10, &pAddr->au8[3]);
125 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
126 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
127 pszNext = RTStrStripL(pszNext);
128 if (*pszNext)
129 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
130
131 return VINF_SUCCESS;
132}
133
134
135/**
136 * Converts an stringified Ethernet MAC address into the RTMAC representation.
137 *
138 * @todo This should be move to some generic part of the runtime.
139 *
140 * @returns VINF_SUCCESS on success, VERR_GETOPT_INVALID_ARGUMENT_FORMAT on
141 * failure.
142 *
143 * @param pszValue The value to convert.
144 * @param pAddr Where to store the result.
145 */
146static int rtgetoptConvertMacAddr(const char *pszValue, PRTMAC pAddr)
147{
148 /*
149 * Not quite sure if I should accept stuff like "08::27:::1" here...
150 * The code is accepting "::" patterns now, except for for the first
151 * and last parts.
152 */
153
154 /* first */
155 char *pszNext;
156 int rc = RTStrToUInt8Ex(RTStrStripL(pszValue), &pszNext, 16, &pAddr->au8[0]);
157 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
158 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
159 if (*pszNext++ != ':')
160 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
161
162 /* middle */
163 for (unsigned i = 1; i < 5; i++)
164 {
165 if (*pszNext == ':')
166 pAddr->au8[i] = 0;
167 else
168 {
169 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[i]);
170 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
171 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
172 if (*pszNext != ':')
173 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
174 }
175 pszNext++;
176 }
177
178 /* last */
179 rc = RTStrToUInt8Ex(pszNext, &pszNext, 16, &pAddr->au8[5]);
180 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
181 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
182 pszNext = RTStrStripL(pszNext);
183 if (*pszNext)
184 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
185
186 return VINF_SUCCESS;
187}
188
189
190/**
191 * Searches for a long option.
192 *
193 * @returns Pointer to a matching option.
194 * @param pszOption The alleged long option.
195 * @param paOptions Option array.
196 * @param cOptions Number of items in the array.
197 * @param fFlags Init flags.
198 */
199static PCRTGETOPTDEF rtGetOptSearchLong(const char *pszOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
200{
201 PCRTGETOPTDEF pOpt = paOptions;
202 while (cOptions-- > 0)
203 {
204 if (pOpt->pszLong)
205 {
206 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
207 {
208 /*
209 * A value is required with the argument. We're trying to be
210 * understanding here and will permit any of the following:
211 * --long12:value, --long12=value, --long12 value,
212 * --long:value, --long=value, --long value,
213 *
214 * If the option is index, then all trailing chars must be
215 * digits. For error reporting reasons we also match where
216 * there is no index.
217 */
218 size_t cchLong = strlen(pOpt->pszLong);
219 if (!strncmp(pszOption, pOpt->pszLong, cchLong))
220 {
221 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
222 while (RT_C_IS_DIGIT(pszOption[cchLong]))
223 cchLong++;
224 if ( pszOption[cchLong] == '\0'
225 || pszOption[cchLong] == ':'
226 || pszOption[cchLong] == '=')
227 return pOpt;
228 }
229 }
230 else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
231 {
232 /*
233 * The option takes an index but no value.
234 * As above, we also match where there is no index.
235 */
236 size_t cchLong = strlen(pOpt->pszLong);
237 if (!strncmp(pszOption, pOpt->pszLong, cchLong))
238 {
239 while (RT_C_IS_DIGIT(pszOption[cchLong]))
240 cchLong++;
241 if (pszOption[cchLong] == '\0')
242 return pOpt;
243 }
244 }
245 else if (!strcmp(pszOption, pOpt->pszLong))
246 return pOpt;
247 }
248 pOpt++;
249 }
250
251 if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
252 for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
253 if (!strcmp(pszOption, g_aStdOptions[i].pszLong))
254 return &g_aStdOptions[i];
255
256 return NULL;
257}
258
259
260/**
261 * Searches for a matching short option.
262 *
263 * @returns Pointer to a matching option.
264 * @param chOption The option char.
265 * @param paOptions Option array.
266 * @param cOptions Number of items in the array.
267 * @param fFlags Init flags.
268 */
269static PCRTGETOPTDEF rtGetOptSearchShort(int chOption, PCRTGETOPTDEF paOptions, size_t cOptions, uint32_t fFlags)
270{
271 PCRTGETOPTDEF pOpt = paOptions;
272 while (cOptions-- > 0)
273 {
274 if (pOpt->iShort == chOption)
275 return pOpt;
276 pOpt++;
277 }
278
279 if (!(fFlags & RTGETOPTINIT_FLAGS_NO_STD_OPTS))
280 {
281 for (uint32_t i = 0; i < RT_ELEMENTS(g_aStdOptions); i++)
282 if (g_aStdOptions[i].iShort == chOption)
283 return &g_aStdOptions[i];
284 if (chOption == '?')
285 return &g_aStdOptions[RTGETOPT_STD_OPTIONS_HELP_IDX];
286 }
287 return NULL;
288}
289
290
291/**
292 * Value string -> Value union.
293 *
294 * @returns IPRT status code.
295 * @param fFlags The value flags.
296 * @param pszValue The value string.
297 * @param pValueUnion Where to return the processed value.
298 */
299static int rtGetOptProcessValue(uint32_t fFlags, const char *pszValue, PRTGETOPTUNION pValueUnion)
300{
301 /*
302 * Transform into a option value as requested.
303 * If decimal conversion fails, we'll check for "0x<xdigit>" and
304 * try a 16 based conversion. We will not interpret any of the
305 * generic ints as octals.
306 */
307 switch (fFlags & ( RTGETOPT_REQ_MASK
308 | RTGETOPT_FLAG_HEX
309 | RTGETOPT_FLAG_DEC
310 | RTGETOPT_FLAG_OCT))
311 {
312 case RTGETOPT_REQ_STRING:
313 pValueUnion->psz = pszValue;
314 break;
315
316 case RTGETOPT_REQ_BOOL_ONOFF:
317 if (!RTStrICmp(pszValue, "on"))
318 pValueUnion->f = true;
319 else if (!RTStrICmp(pszValue, "off"))
320 pValueUnion->f = false;
321 else
322 {
323 pValueUnion->psz = pszValue;
324 return VERR_GETOPT_UNKNOWN_OPTION;
325 }
326 break;
327
328#define MY_INT_CASE(req, type, memb, convfn) \
329 case req: \
330 { \
331 type Value; \
332 if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
333 && ( pszValue[0] != '0' \
334 || (pszValue[1] != 'x' && pszValue[1] != 'X') \
335 || !RT_C_IS_XDIGIT(pszValue[2]) \
336 || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
337 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
338 pValueUnion->memb = Value; \
339 break; \
340 }
341#define MY_BASE_INT_CASE(req, type, memb, convfn, base) \
342 case req: \
343 { \
344 type Value; \
345 if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
346 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
347 pValueUnion->memb = Value; \
348 break; \
349 }
350
351 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i8, RTStrToInt8Full)
352 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i16, RTStrToInt16Full)
353 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i32, RTStrToInt32Full)
354 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i64, RTStrToInt64Full)
355 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u8, RTStrToUInt8Full)
356 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u16, RTStrToUInt16Full)
357 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u32, RTStrToUInt32Full)
358 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u64, RTStrToUInt64Full)
359
360 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i8, RTStrToInt8Full, 16)
361 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i16, RTStrToInt16Full, 16)
362 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i32, RTStrToInt32Full, 16)
363 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i64, RTStrToInt64Full, 16)
364 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u8, RTStrToUInt8Full, 16)
365 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u16, RTStrToUInt16Full, 16)
366 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u32, RTStrToUInt32Full, 16)
367 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u64, RTStrToUInt64Full, 16)
368
369 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i8, RTStrToInt8Full, 10)
370 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i16, RTStrToInt16Full, 10)
371 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i32, RTStrToInt32Full, 10)
372 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i64, RTStrToInt64Full, 10)
373 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u8, RTStrToUInt8Full, 10)
374 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u16, RTStrToUInt16Full, 10)
375 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u32, RTStrToUInt32Full, 10)
376 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u64, RTStrToUInt64Full, 10)
377
378 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i8, RTStrToInt8Full, 8)
379 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i16, RTStrToInt16Full, 8)
380 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i32, RTStrToInt32Full, 8)
381 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i64, RTStrToInt64Full, 8)
382 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u8, RTStrToUInt8Full, 8)
383 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u16, RTStrToUInt16Full, 8)
384 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u32, RTStrToUInt32Full, 8)
385 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u64, RTStrToUInt64Full, 8)
386
387#undef MY_INT_CASE
388#undef MY_BASE_INT_CASE
389
390 case RTGETOPT_REQ_IPV4ADDR:
391 {
392 RTNETADDRIPV4 Addr;
393 if (rtgetoptConvertIPv4Addr(pszValue, &Addr) != VINF_SUCCESS)
394 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
395 pValueUnion->IPv4Addr = Addr;
396 break;
397 }
398#if 0 /** @todo CIDR */
399#endif
400
401 case RTGETOPT_REQ_MACADDR:
402 {
403 RTMAC Addr;
404 if (rtgetoptConvertMacAddr(pszValue, &Addr) != VINF_SUCCESS)
405 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
406 pValueUnion->MacAddr = Addr;
407 break;
408 }
409
410 case RTGETOPT_REQ_UUID:
411 {
412 RTUUID Uuid;
413 if (RTUuidFromStr(&Uuid, pszValue) != VINF_SUCCESS)
414 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
415 pValueUnion->Uuid = Uuid;
416 break;
417 }
418
419 default:
420 AssertMsgFailed(("f=%#x\n", fFlags));
421 return VERR_INTERNAL_ERROR;
422 }
423
424 return VINF_SUCCESS;
425}
426
427
428/**
429 * Moves one argv option entries.
430 *
431 * @param papszTo Destination.
432 * @param papszFrom Source.
433 */
434static void rtGetOptMoveArgvEntries(char **papszTo, char **papszFrom)
435{
436 if (papszTo != papszFrom)
437 {
438 Assert((uintptr_t)papszTo < (uintptr_t)papszFrom);
439 char * const pszMoved = papszFrom[0];
440 memmove(&papszTo[1], &papszTo[0], (uintptr_t)papszFrom - (uintptr_t)papszTo);
441 papszTo[0] = pszMoved;
442 }
443}
444
445
446RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion)
447{
448 /*
449 * Reset the variables kept in state.
450 */
451 pState->pDef = NULL;
452 pState->uIndex = UINT32_MAX;
453
454 /*
455 * Make sure the union is completely cleared out, whatever happens below.
456 */
457 pValueUnion->u64 = 0;
458 pValueUnion->pDef = NULL;
459
460 /*
461 * The next option.
462 */
463 bool fShort;
464 int iThis;
465 const char *pszArgThis;
466 PCRTGETOPTDEF pOpt;
467
468 if (pState->pszNextShort)
469 {
470 /*
471 * We've got short options left over from the previous call.
472 */
473 pOpt = rtGetOptSearchShort(*pState->pszNextShort, pState->paOptions, pState->cOptions, pState->fFlags);
474 if (!pOpt)
475 {
476 pValueUnion->psz = pState->pszNextShort;
477 return VERR_GETOPT_UNKNOWN_OPTION;
478 }
479 pState->pszNextShort++;
480 pszArgThis = pState->pszNextShort - 2;
481 iThis = pState->iNext;
482 fShort = true;
483 }
484 else
485 {
486 /*
487 * Pop off the next argument. Sorting options and dealing with the
488 * dash-dash makes this a little extra complicated.
489 */
490 for (;;)
491 {
492 if (pState->iNext >= pState->argc)
493 return 0;
494
495 if (pState->cNonOptions)
496 {
497 if (pState->cNonOptions == INT32_MAX)
498 {
499 pValueUnion->psz = pState->argv[pState->iNext++];
500 return VINF_GETOPT_NOT_OPTION;
501 }
502
503 if (pState->iNext + pState->cNonOptions >= pState->argc)
504 {
505 pState->cNonOptions = INT32_MAX;
506 continue;
507 }
508 }
509
510 iThis = pState->iNext++;
511 pszArgThis = pState->argv[iThis + pState->cNonOptions];
512
513 /*
514 * Do a long option search first and then a short option one.
515 * This way we can make sure single dash long options doesn't
516 * get mixed up with short ones.
517 */
518 pOpt = rtGetOptSearchLong(pszArgThis, pState->paOptions, pState->cOptions, pState->fFlags);
519 if ( !pOpt
520 && pszArgThis[0] == '-'
521 && pszArgThis[1] != '-'
522 && pszArgThis[1] != '\0')
523 {
524 pOpt = rtGetOptSearchShort(pszArgThis[1], pState->paOptions, pState->cOptions, pState->fFlags);
525 fShort = pOpt != NULL;
526 }
527 else
528 fShort = false;
529
530 /* Look for dash-dash. */
531 if (!pOpt && !strcmp(pszArgThis, "--"))
532 {
533 rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
534 pState->cNonOptions = INT32_MAX;
535 continue;
536 }
537
538 /* Options first hacks. */
539 if (pState->fFlags & RTGETOPTINIT_FLAGS_OPTS_FIRST)
540 {
541 if (pOpt)
542 rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
543 else if (*pszArgThis == '-')
544 {
545 pValueUnion->psz = pszArgThis;
546 return VERR_GETOPT_UNKNOWN_OPTION;
547 }
548 else
549 {
550 /* not an option, add it to the non-options and try again. */
551 pState->iNext--;
552 pState->cNonOptions++;
553
554 /* Switch to returning non-options if we've reached the end. */
555 if (pState->iNext + pState->cNonOptions >= pState->argc)
556 pState->cNonOptions = INT32_MAX;
557 continue;
558 }
559 }
560
561 /* done */
562 break;
563 }
564 }
565
566 if (pOpt)
567 {
568 pValueUnion->pDef = pOpt; /* in case of no value or error. */
569
570 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
571 {
572 /*
573 * Find the argument value.
574 *
575 * A value is required with the argument. We're trying to be
576 * understanding here and will permit any of the following:
577 * -svalue, -s value, -s:value and -s=value
578 * (Ditto for long options.)
579 */
580 const char *pszValue;
581 if (fShort)
582 {
583 if (pszArgThis[2] == '\0')
584 {
585 if (iThis + 1 >= pState->argc)
586 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
587 pszValue = pState->argv[iThis + pState->cNonOptions + 1];
588 rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
589 pState->iNext++;
590 }
591 else /* same argument. */
592 pszValue = &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')];
593 if (pState->pszNextShort)
594 {
595 pState->pszNextShort = NULL;
596 pState->iNext++;
597 }
598 }
599 else
600 {
601 size_t cchLong = strlen(pOpt->pszLong);
602 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
603 {
604
605 if (pszArgThis[cchLong] == '\0')
606 return VERR_GETOPT_INDEX_MISSING;
607
608 uint32_t uIndex;
609 char *pszRet = NULL;
610 int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex);
611 if (rc == VWRN_TRAILING_CHARS)
612 {
613 if ( pszRet[0] != ':'
614 && pszRet[0] != '=')
615 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT;
616 pState->uIndex = uIndex;
617 pszValue = pszRet + 1;
618 }
619 else if (rc == VINF_SUCCESS)
620 {
621 if (iThis + 1 >= pState->argc)
622 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
623 pState->uIndex = uIndex;
624 pszValue = pState->argv[iThis + pState->cNonOptions + 1];
625 rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
626 pState->iNext++;
627 }
628 else
629 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
630 }
631 else
632 {
633 if (pszArgThis[cchLong] == '\0')
634 {
635 if (iThis + 1 >= pState->argc)
636 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
637 pszValue = pState->argv[iThis + pState->cNonOptions + 1];
638 rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]);
639 pState->iNext++;
640 }
641 else /* same argument. */
642 pszValue = &pszArgThis[cchLong + 1];
643 }
644 }
645
646 /*
647 * Set up the ValueUnion.
648 */
649 int rc = rtGetOptProcessValue(pOpt->fFlags, pszValue, pValueUnion);
650 if (RT_FAILURE(rc))
651 return rc;
652 }
653 else if (fShort)
654 {
655 /*
656 * Deal with "compressed" short option lists, correcting the next
657 * state variables for the start and end cases.
658 */
659 if (pszArgThis[2])
660 {
661 if (!pState->pszNextShort)
662 {
663 /* start */
664 pState->pszNextShort = &pszArgThis[2];
665 pState->iNext--;
666 }
667 }
668 else if (pState->pszNextShort)
669 {
670 /* end */
671 pState->pszNextShort = NULL;
672 pState->iNext++;
673 }
674 }
675 else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX)
676 {
677 size_t cchLong = strlen(pOpt->pszLong);
678 if (pszArgThis[cchLong] == '\0')
679 return VERR_GETOPT_INDEX_MISSING;
680
681 uint32_t uIndex;
682 char *pszRet = NULL;
683 if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)
684 pState->uIndex = uIndex;
685 else
686 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */
687 }
688
689 pState->pDef = pOpt;
690 return pOpt->iShort;
691 }
692
693 /*
694 * Not a known option argument. If it starts with a switch char (-) we'll
695 * fail with unknown option, and if it doesn't we'll return it as a non-option.
696 */
697 if (*pszArgThis == '-')
698 {
699 pValueUnion->psz = pszArgThis;
700 return VERR_GETOPT_UNKNOWN_OPTION;
701 }
702
703 pValueUnion->psz = pszArgThis;
704 return VINF_GETOPT_NOT_OPTION;
705}
706RT_EXPORT_SYMBOL(RTGetOpt);
707
708
709RTDECL(int) RTGetOptFetchValue(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion, uint32_t fFlags)
710{
711 /*
712 * Validate input.
713 */
714 PCRTGETOPTDEF pOpt = pState->pDef;
715 AssertReturn(pOpt, VERR_GETOPT_UNKNOWN_OPTION);
716 AssertReturn(!(fFlags & ~RTGETOPT_VALID_MASK), VERR_INVALID_PARAMETER);
717 AssertReturn((fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING, VERR_INVALID_PARAMETER);
718
719 /*
720 * Make sure the union is completely cleared out, whatever happens below.
721 */
722 pValueUnion->u64 = 0;
723 pValueUnion->pDef = NULL;
724
725 /*
726 * Pop off the next argument and convert it into a value union.
727 */
728 if (pState->iNext >= pState->argc)
729 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
730 int iThis = pState->iNext++;
731 const char *pszValue = pState->argv[iThis + (pState->cNonOptions != INT32_MAX ? pState->cNonOptions : 0)];
732 pValueUnion->pDef = pOpt; /* in case of no value or error. */
733
734 if (pState->cNonOptions && pState->cNonOptions != INT32_MAX)
735 rtGetOptMoveArgvEntries(&pState->argv[iThis], &pState->argv[iThis + pState->cNonOptions]);
736
737 return rtGetOptProcessValue(fFlags, pszValue, pValueUnion);
738}
739RT_EXPORT_SYMBOL(RTGetOptFetchValue);
740
741
742RTDECL(RTEXITCODE) RTGetOptPrintError(int ch, PCRTGETOPTUNION pValueUnion)
743{
744 if (ch == VINF_GETOPT_NOT_OPTION)
745 RTMsgError("Invalid parameter: %s", pValueUnion->psz);
746 else if (ch > 0)
747 {
748 if (RT_C_IS_GRAPH(ch))
749 RTMsgError("Unhandled option: -%c", ch);
750 else
751 RTMsgError("Unhandled option: %i (%#x)", ch, ch);
752 }
753 else if (ch == VERR_GETOPT_UNKNOWN_OPTION)
754 RTMsgError("Unknown option: '%s'", pValueUnion->psz);
755 else if (pValueUnion->pDef)
756 RTMsgError("%s: %Rrs\n", pValueUnion->pDef->pszLong, ch);
757 else
758 RTMsgError("%Rrs\n", ch);
759
760 return RTEXITCODE_SYNTAX;
761}
762RT_EXPORT_SYMBOL(RTGetOptPrintError);
763
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