VirtualBox

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

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

RTGetOpt: cleanup, restored @todo and returning 0 on first non-option.

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