VirtualBox

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

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

Rebranding: replacing more innotek strings.

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