VirtualBox

source: vbox/trunk/include/iprt/getopt.h@ 17555

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

IPRT/RTGetOpt: Added RTGETOPT_REQ_MACADDR.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/** @file
2 * IPRT - Command Line Parsing.
3 */
4
5/*
6 * Copyright (C) 2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_getopt_h
31#define ___iprt_getopt_h
32
33
34#include <iprt/cdefs.h>
35#include <iprt/types.h>
36
37__BEGIN_DECLS
38
39/** @defgroup grp_rt_getopt RTGetOpt - Command Line Parsing
40 * @ingroup grp_rt
41 * @{
42 */
43
44/** @name RTGETOPTDEF::fFlags
45 *
46 * @remarks When neither of the RTGETOPT_FLAG_HEX, RTGETOPT_FLAG_OCT and RTGETOPT_FLAG_DEC
47 * flags are specified with a integer value format, RTGetOpt will default to
48 * decimal but recognize the 0x prefix when present. RTGetOpt will not look for
49 * for the octal prefix (0).
50 * @{ */
51/** Requires no extra argument.
52 * (Can be assumed to be 0 for ever.) */
53#define RTGETOPT_REQ_NOTHING 0
54/** A value is required or error will be returned. */
55#define RTGETOPT_REQ_STRING 1
56/** The value must be a valid signed 8-bit integer or an error will be returned. */
57#define RTGETOPT_REQ_INT8 2
58/** The value must be a valid unsigned 8-bit integer or an error will be returned. */
59#define RTGETOPT_REQ_UINT8 3
60/** The value must be a valid signed 16-bit integer or an error will be returned. */
61#define RTGETOPT_REQ_INT16 4
62/** The value must be a valid unsigned 16-bit integer or an error will be returned. */
63#define RTGETOPT_REQ_UINT16 5
64/** The value must be a valid signed 32-bit integer or an error will be returned. */
65#define RTGETOPT_REQ_INT32 6
66/** The value must be a valid unsigned 32-bit integer or an error will be returned. */
67#define RTGETOPT_REQ_UINT32 7
68/** The value must be a valid signed 64-bit integer or an error will be returned. */
69#define RTGETOPT_REQ_INT64 8
70/** The value must be a valid unsigned 64-bit integer or an error will be returned. */
71#define RTGETOPT_REQ_UINT64 9
72/** The value must be a valid IPv4 address.
73 * (Not a name, but 4 values in the 0..255 range with dots separating them). */
74#define RTGETOPT_REQ_IPV4ADDR 10
75#if 0
76/** The value must be a valid IPv4 CIDR.
77 * As with RTGETOPT_REQ_IPV4ADDR, no name.
78 * @todo Mix CIDR with types.h or/and net.h first and find a way to make the
79 * mask optional like with ifconfig. See RTCidrStrToIPv4. */
80#define RTGETOPT_REQ_IPV4CIDR 11
81#endif
82/** The value must be a valid ethernet MAC address. */
83#define RTGETOPT_REQ_MACADDR 14
84/** The mask of the valid required types. */
85#define RTGETOPT_REQ_MASK 15
86/** Treat the value as hexadecimal - only applicable with the RTGETOPT_REQ_*INT*. */
87#define RTGETOPT_FLAG_HEX RT_BIT(16)
88/** Treat the value as octal - only applicable with the RTGETOPT_REQ_*INT*. */
89#define RTGETOPT_FLAG_OCT RT_BIT(17)
90/** Treat the value as decimal - only applicable with the RTGETOPT_REQ_*INT*. */
91#define RTGETOPT_FLAG_DEC RT_BIT(18)
92/** Mask of valid bits - for validation. */
93#define RTGETOPT_VALID_MASK ( RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC )
94/** @} */
95
96/**
97 * An option definition.
98 */
99typedef struct RTGETOPTDEF
100{
101 /** The long option.
102 * This is optional */
103 const char *pszLong;
104 /** The short option character.
105 * This doesn't have to be a character, it may also be a \#define or enum value if
106 * there isn't any short version of this option. */
107 int iShort;
108 /** The flags (RTGETOPT_*). */
109 unsigned fFlags;
110} RTGETOPTDEF;
111/** Pointer to an option definition. */
112typedef RTGETOPTDEF *PRTGETOPTDEF;
113/** Pointer to an const option definition. */
114typedef const RTGETOPTDEF *PCRTGETOPTDEF;
115
116/**
117 * Option argument union.
118 *
119 * What ends up here depends on argument format in the option definition.
120 *
121 * @remarks Integers will bet put in the \a i and \a u members and sign/zero extended
122 * according to the signedness indicated by the \a fFlags. So, you can choose
123 * use which ever of the integer members for accessing the value regardless
124 * of restrictions indicated in the \a fFlags.
125 */
126typedef union RTGETOPTUNION
127{
128 /** Pointer to the definition on failure or when the option doesn't take an argument.
129 * This can be NULL for some errors. */
130 PCRTGETOPTDEF pDef;
131 /** A RTGETOPT_REQ_STRING option argument. */
132 const char *psz;
133
134#if !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86)
135# error "PORTME: big-endian systems will need to fix the layout here to get the next two fields working right"
136#endif
137
138 /** A RTGETOPT_REQ_INT8 option argument. */
139 int8_t i8;
140 /** A RTGETOPT_REQ_UINT8 option argument . */
141 uint8_t u8;
142 /** A RTGETOPT_REQ_INT16 option argument. */
143 int16_t i16;
144 /** A RTGETOPT_REQ_UINT16 option argument . */
145 uint16_t u16;
146 /** A RTGETOPT_REQ_INT16 option argument. */
147 int32_t i32;
148 /** A RTGETOPT_REQ_UINT32 option argument . */
149 uint32_t u32;
150 /** A RTGETOPT_REQ_INT64 option argument. */
151 int64_t i64;
152 /** A RTGETOPT_REQ_UINT64 option argument. */
153 uint64_t u64;
154#ifdef ___iprt_net_h
155 /** A RTGETOPT_REQ_IPV4ADDR option argument. */
156 RTNETADDRIPV4 IPv4Addr;
157#endif
158 /** A RTGETOPT_REQ_MACADDR option argument. */
159 RTMAC MacAddr;
160 /** A signed integer value. */
161 int64_t i;
162 /** An unsigned integer value. */
163 uint64_t u;
164} RTGETOPTUNION;
165/** Pointer to an option argument union. */
166typedef RTGETOPTUNION *PRTGETOPTUNION;
167/** Pointer to a const option argument union. */
168typedef RTGETOPTUNION const *PCRTGETOPTUNION;
169
170
171/**
172 * RTGetOpt state.
173 */
174typedef struct RTGETOPTSTATE
175{
176 /** The next argument. */
177 int iNext;
178 /** Argument array. */
179 char **argv;
180 /** Number of items in argv. */
181 int argc;
182 /** Option definition array. */
183 PCRTGETOPTDEF paOptions;
184 /** Number of items in paOptions. */
185 size_t cOptions;
186 /** The next short option.
187 * (For parsing ls -latrT4 kind of option lists.) */
188 const char *pszNextShort;
189 /* More members will be added later for dealing with initial
190 call, optional sorting, '--' and so on. */
191} RTGETOPTSTATE;
192/** Pointer to RTGetOpt state. */
193typedef RTGETOPTSTATE *PRTGETOPTSTATE;
194
195
196/**
197 * Initialize the RTGetOpt state.
198 *
199 * The passed in argument vector may be sorted if fFlags indicates that this is
200 * desired (to be implemented).
201 *
202 * @returns VINF_SUCCESS, VERR_INVALID_PARAMETER or VERR_INVALID_POINTER.
203 * @param pState The state.
204 *
205 * @param argc Argument count, to be copied from what comes in with
206 * main().
207 * @param argv Argument array, to be copied from what comes in with
208 * main(). This may end up being modified by the
209 * option/argument sorting.
210 * @param paOptions Array of RTGETOPTDEF structures, which must specify what
211 * options are understood by the program.
212 * @param cOptions Number of array items passed in with paOptions.
213 * @param iFirst The argument to start with (in argv).
214 * @param fFlags The flags. MBZ for now.
215 */
216RTDECL(int) RTGetOptInit(PRTGETOPTSTATE pState, int argc, char **argv,
217 PCRTGETOPTDEF paOptions, size_t cOptions,
218 int iFirst, uint32_t fFlags);
219
220/**
221 * Command line argument parser, handling both long and short options and checking
222 * argument formats, if desired.
223 *
224 * This is to be called in a loop until it returns 0 (meaning that all options
225 * were parsed) or a negative value (meaning that an error occured). How non-option
226 * arguments are dealt with depends on the flags passed to RTGetOptInit. The default
227 * (fFlags = 0) is to return VINF_GETOPT_NOT_OPTION with pValueUnion->psz pointing to
228 * the argument string.
229 *
230 * For example, for a program which takes the following options:
231 *
232 * --optwithstring (or -s) and a string argument;
233 * --optwithint (or -i) and a 32-bit signed integer argument;
234 * --verbose (or -v) with no arguments,
235 *
236 * code would look something like this:
237 *
238 * @code
239 * int main(int argc, char *argv[])
240 * {
241 * static const RTGETOPTDEF s_aOptions[] =
242 * {
243 * { "--optwithstring", 's', RTGETOPT_REQ_STRING },
244 * { "--optwithint", 'i', RTGETOPT_REQ_INT32 },
245 * { "--verbose", 'v', 0 },
246 * };
247 *
248 * int ch;
249 * int i = 1;
250 * RTGETOPTUNION ValueUnion;
251 * RTGETOPTSTATE GetState;
252 * RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
253 * while ((ch = RTGetOpt(&GetState, &ValueUnion)))
254 * {
255 * // for options that require an argument, ValueUnion has received the value
256 * switch (ch)
257 * {
258 * case 's': // --optwithstring or -s
259 * // string argument, copy ValueUnion.psz
260 * break;
261 *
262 * case 'i': // --optwithint or -i
263 * // integer argument, copy ValueUnion.i32
264 * break;
265 *
266 * case 'v': // --verbose or -v
267 * g_fOptVerbose = true;
268 * break;
269 *
270 * case VINF_GETOPT_NOT_OPTION:
271 * // handle non-option argument in ValueUnion.psz.
272 * break;
273 *
274 * default:
275 * if (ch > 0)
276 * Error("missing case: %c\n", ch);
277 * else if (ValueUnion.pDef)
278 * Error("%s: %Rrs", ValueUnion.pDef->pszLong, ch);
279 * else
280 * Error("%Rrs", ch);
281 * return 1;
282 * }
283 * }
284 *
285 * return 0;
286 * }
287 * @endcode
288 *
289 * @returns 0 when done parsing.
290 * @returns IPRT error status on parse error.
291 * @returns VINF_GETOPT_NOT_OPTION when encountering a non-option argument and
292 * RTGETOPT_FLAG_SORT was not specified. pValueUnion->psz points to the
293 * argument string.
294 * @returns VERR_GETOPT_UNKNOWN_OPTION when encountering an unknown option.
295 * pValueUnion->psz points to the option string.
296 * @returns VERR_GETOPT_REQUIRED_ARGUMENT_MISSING and pValueUnion->pDef if
297 * a required argument (aka value) was missing for an option.
298 * @returns VERR_GETOPT_INVALID_ARGUMENT_FORMAT and pValueUnion->pDef if
299 * argument (aka value) conversion failed.
300 *
301 * @param pState The state previously initialized with RTGetOptInit.
302 * @param pValueUnion Union with value; in the event of an error, psz member
303 * points to erroneous parameter; otherwise, for options
304 * that require an argument, this contains the value of
305 * that argument, depending on the type that is required.
306 */
307RTDECL(int) RTGetOpt(PRTGETOPTSTATE pState, PRTGETOPTUNION pValueUnion);
308
309/** @} */
310
311__END_DECLS
312
313#endif
314
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