VirtualBox

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

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

and return the correct index.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1/* $Id: getopt.cpp 8927 2008-05-19 17:43:01Z 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/getopt.h>
35#include <iprt/err.h>
36#include <iprt/string.h>
37#include <iprt/assert.h>
38#include <iprt/ctype.h>
39
40
41
42RTDECL(int) RTGetOpt(int argc, char **argv, PCRTOPTIONDEF paOptions, size_t cOptions, int *piThis, PRTOPTIONUNION pValueUnion)
43{
44 pValueUnion->u64 = 0;
45 pValueUnion->pDef = NULL;
46
47 if ( !piThis
48 || *piThis >= argc
49 )
50 return 0;
51
52 int iThis = (*piThis)++;
53 const char *pszArgThis = argv[iThis];
54
55 if (*pszArgThis == '-')
56 {
57/** @todo implement '--'. */
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
63 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)
64 {
65 /*
66 * A value is required with the argument. We're trying to very
67 * understanding here and will permit any of the following:
68 * -svalue, -s:value, -s=value,
69 * -s value, -s: value, -s= value
70 * (Ditto for long options.)
71 */
72 bool fShort = false;
73 size_t cchLong = 2;
74 if ( ( paOptions[i].pszLong
75 && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong)))
76 && ( pszArgThis[cchLong] == '\0'
77 || pszArgThis[cchLong] == ':'
78 || pszArgThis[cchLong] == '=')
79 )
80 || (fShort = (pszArgThis[1] == paOptions[i].iShort))
81 )
82 {
83 pValueUnion->pDef = &paOptions[i]; /* in case of error. */
84
85 /*
86 * Find the argument value
87 */
88 const char *pszValue;
89 if ( fShort
90 ? pszArgThis[2] == '\0'
91 || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0')
92 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0')
93 {
94 if (iThis + 1 >= argc)
95 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING;
96 pszValue = argv[iThis + 1];
97 (*piThis)++;
98 }
99 else /* same argument. */
100 pszValue = fShort
101 ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')]
102 : &pszArgThis[cchLong + 1];
103
104 /*
105 * Transform into a option value as requested.
106 * If decimal conversion fails, we'll check for "0x<xdigit>" and
107 * try a 16 based conversion. We will not interpret any of the
108 * generic ints as octals.
109 */
110 switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC))
111 {
112 case RTGETOPT_REQ_STRING:
113 pValueUnion->psz = pszValue;
114 break;
115
116#define MY_INT_CASE(req,type,memb,convfn) \
117 case req: \
118 { \
119 type Value; \
120 if ( convfn(pszValue, 10, &Value) != VINF_SUCCESS \
121 && ( pszValue[0] != '0' \
122 || (pszValue[1] != 'x' && pszValue[1] != 'X') \
123 || !RT_C_IS_XDIGIT(pszValue[2]) \
124 || convfn(pszValue, 16, &Value) != VINF_SUCCESS ) ) \
125 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
126 pValueUnion->memb = Value; \
127 break; \
128 }
129#define MY_BASE_INT_CASE(req,type,memb,convfn,base) \
130 case req: \
131 { \
132 type Value; \
133 if (convfn(pszValue, base, &Value) != VINF_SUCCESS) \
134 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; \
135 pValueUnion->memb = Value; \
136 break; \
137 }
138
139 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full)
140 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full)
141 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full)
142 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full)
143 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full)
144 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full)
145 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full)
146 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full)
147
148 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16)
149 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16)
150 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16)
151 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16)
152 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16)
153 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16)
154 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16)
155 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16)
156
157 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10)
158 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10)
159 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10)
160 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10)
161 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10)
162 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10)
163 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10)
164 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10)
165
166 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8)
167 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8)
168 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8)
169 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8)
170 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8)
171 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8)
172 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8)
173 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8)
174#undef MY_INT_CASE
175#undef MY_BASE_INT_CASE
176
177 default:
178 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags));
179 return VERR_INTERNAL_ERROR;
180 }
181 return paOptions[i].iShort;
182 }
183 }
184 else if ( ( paOptions[i].pszLong
185 && !strcmp(pszArgThis, paOptions[i].pszLong))
186 || ( pszArgThis[1] == paOptions[i].iShort
187 && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */
188 )
189 {
190 pValueUnion->pDef = &paOptions[i];
191 return paOptions[i].iShort;
192 }
193 }
194
195
196 return VERR_GETOPT_UNKNOWN_OPTION;
197 }
198
199 /*
200 * Not an option.
201 */
202 (*piThis)--;
203 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when
204 * encountering the first argument. */
205
206 return 0;
207}
208
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