]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/getopt_long.c
2 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Dieter Baron and Thomas Klausner.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
36 #include "openvswitch/vlog.h"
38 VLOG_DEFINE_THIS_MODULE(getopt_long
);
40 int opterr
= 1; /* if error message should be printed */
41 int optind
= 1; /* index into parent argv vector */
42 int optopt
= '?'; /* character checked for validity */
43 int optreset
; /* reset getopt */
44 char *optarg
; /* argument associated with option */
46 #define IGNORE_FIRST (*options == '-' || *options == '+')
47 #define PRINT_ERROR ((opterr) && ((*options != ':') \
48 || (IGNORE_FIRST && options[1] != ':')))
49 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
50 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
51 /* XXX: GNU ignores PC if *options == '-' */
52 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
55 #define BADCH (int)'?'
56 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
57 || (*options == ':') ? (int)':' : (int)'?')
58 #define INORDER (int)1
62 #define _DIAGASSERT(q) ovs_assert(q)
64 #define warnx VLOG_WARN
66 static int getopt_internal(int, char **, const char *);
67 static int gcd(int, int);
68 static void permute_args(int, int, int, char **);
70 static const char *place
= EMSG
; /* option letter processing */
72 /* XXX: set optreset to 1 rather than these two */
73 static int nonopt_start
= -1; /* first non option argument (for permute) */
74 static int nonopt_end
= -1; /* first option after non options (for permute) */
77 static const char recargchar
[] = "option requires an argument -- %c";
78 static const char recargstring
[] = "option requires an argument -- %s";
79 static const char ambig
[] = "ambiguous option -- %.*s";
80 static const char noarg
[] = "option doesn't take an argument -- %.*s";
81 static const char illoptchar
[] = "unknown option -- %c";
82 static const char illoptstring
[] = "unknown option -- %s";
86 * Compute the greatest common divisor of a and b.
104 * Exchange the block from nonopt_start to nonopt_end with the block
105 * from nonopt_end to opt_end (keeping the same order of arguments
109 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
, char **nargv
)
111 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
114 _DIAGASSERT(nargv
!= NULL
);
117 * compute lengths of blocks and number and size of cycles
119 nnonopts
= panonopt_end
- panonopt_start
;
120 nopts
= opt_end
- panonopt_end
;
121 ncycle
= gcd(nnonopts
, nopts
);
122 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
124 for (i
= 0; i
< ncycle
; i
++) {
125 cstart
= panonopt_end
+i
;
127 for (j
= 0; j
< cyclelen
; j
++) {
128 if (pos
>= panonopt_end
)
133 nargv
[pos
] = nargv
[cstart
];
134 nargv
[cstart
] = swap
;
141 * Parse argc/argv argument vector. Called by user level routines.
142 * Returns -2 if -- is found (can be long option or end of options marker).
145 getopt_internal(int nargc
, char **nargv
, const char *options
)
147 char *oli
; /* option letter list index */
150 _DIAGASSERT(nargv
!= NULL
);
151 _DIAGASSERT(options
!= NULL
);
156 * XXX Some programs (like rsyncd) expect to be able to
157 * XXX re-initialize optind to 0 and have getopt_long(3)
158 * XXX properly function again. Work around this braindamage.
164 nonopt_start
= nonopt_end
= -1;
166 if (optreset
|| !*place
) { /* update scanning pointer */
168 if (optind
>= nargc
) { /* end of argument vector */
170 if (nonopt_end
!= -1) {
171 /* do permutation, if we have to */
172 permute_args(nonopt_start
, nonopt_end
,
174 optind
-= nonopt_end
- nonopt_start
;
176 else if (nonopt_start
!= -1) {
178 * If we skipped non-options, set optind
179 * to the first of them.
181 optind
= nonopt_start
;
183 nonopt_start
= nonopt_end
= -1;
186 if ((*(place
= nargv
[optind
]) != '-')
187 || (place
[1] == '\0')) { /* found non-option */
192 * return non-option as argument to option 1
194 optarg
= nargv
[optind
++];
199 * if no permutation wanted, stop parsing
200 * at first non-option
205 if (nonopt_start
== -1)
206 nonopt_start
= optind
;
207 else if (nonopt_end
!= -1) {
208 permute_args(nonopt_start
, nonopt_end
,
210 nonopt_start
= optind
-
211 (nonopt_end
- nonopt_start
);
215 /* process next argument */
218 if (nonopt_start
!= -1 && nonopt_end
== -1)
220 if (place
[1] && *++place
== '-') { /* found "--" */
225 if ((optchar
= (int)*place
++) == (int)':' ||
226 (oli
= strchr(options
+ (IGNORE_FIRST
? 1 : 0), optchar
)) == NULL
) {
227 /* option letter unknown or ':' */
231 warnx(illoptchar
, optchar
);
235 if (optchar
== 'W' && oli
[1] == ';') { /* -W long-option */
236 /* XXX: what if no long options provided (called by getopt)? */
240 if (++optind
>= nargc
) { /* no arg */
243 warnx(recargchar
, optchar
);
246 } else /* white space */
247 place
= nargv
[optind
];
249 * Handle -W arg the same as --arg (which causes getopt to
254 if (*++oli
!= ':') { /* doesn't take argument */
257 } else { /* takes (optional) argument */
259 if (*place
) /* no white space */
260 optarg
= CONST_CAST(char *, place
);
261 /* XXX: disable test for :: if PC? (GNU doesn't) */
262 else if (oli
[1] != ':') { /* arg not optional */
263 if (++optind
>= nargc
) { /* no arg */
266 warnx(recargchar
, optchar
);
270 optarg
= nargv
[optind
];
275 /* dump back option letter */
279 #ifdef REPLACE_GETOPT
282 * Parse argc/argv argument vector.
284 * [eventually this will replace the real getopt]
287 getopt(nargc
, nargv
, options
)
294 _DIAGASSERT(nargv
!= NULL
);
295 _DIAGASSERT(options
!= NULL
);
297 retval
= getopt_internal(nargc
, CONST_CAST(char **, nargv
), options
);
301 * We found an option (--), so if we skipped non-options,
302 * we have to permute.
304 if (nonopt_end
!= -1) {
305 permute_args(nonopt_start
, nonopt_end
, optind
,
307 optind
-= nonopt_end
- nonopt_start
;
309 nonopt_start
= nonopt_end
= -1;
318 * Parse argc/argv argument vector.
321 getopt_long(int nargc
, char * const *nargv
, const char *options
,
322 const struct option
*long_options
, int *idx
)
326 #define IDENTICAL_INTERPRETATION(_x, _y) \
327 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
328 long_options[(_x)].flag == long_options[(_y)].flag && \
329 long_options[(_x)].val == long_options[(_y)].val)
331 _DIAGASSERT(nargv
!= NULL
);
332 _DIAGASSERT(options
!= NULL
);
333 _DIAGASSERT(long_options
!= NULL
);
334 /* idx may be NULL */
336 retval
= getopt_internal(nargc
, CONST_CAST(char **, nargv
), options
);
338 char *current_argv
, *has_equal
;
339 size_t current_argv_len
;
340 int i
, ambiguous
, match
;
342 current_argv
= CONST_CAST(char *, place
);
349 if (*current_argv
== '\0') { /* found "--" */
351 * We found an option (--), so if we skipped
352 * non-options, we have to permute.
354 if (nonopt_end
!= -1) {
355 permute_args(nonopt_start
, nonopt_end
,
356 optind
, CONST_CAST(char **, nargv
));
357 optind
-= nonopt_end
- nonopt_start
;
359 nonopt_start
= nonopt_end
= -1;
362 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
363 /* argument found (--option=arg) */
364 current_argv_len
= has_equal
- current_argv
;
367 current_argv_len
= strlen(current_argv
);
369 for (i
= 0; long_options
[i
].name
; i
++) {
370 /* find matching long option */
371 if (strncmp(current_argv
, long_options
[i
].name
,
375 if (strlen(long_options
[i
].name
) ==
376 (unsigned)current_argv_len
) {
382 if (match
== -1) /* partial match */
384 else if (!IDENTICAL_INTERPRETATION(i
, match
))
388 /* ambiguous abbreviation */
390 warnx(ambig
, (int)current_argv_len
,
395 if (match
!= -1) { /* option found */
396 if (long_options
[match
].has_arg
== no_argument
399 warnx(noarg
, (int)current_argv_len
,
402 * XXX: GNU sets optopt to val regardless of
405 if (long_options
[match
].flag
== NULL
)
406 optopt
= long_options
[match
].val
;
411 if (long_options
[match
].has_arg
== required_argument
||
412 long_options
[match
].has_arg
== optional_argument
) {
415 else if (long_options
[match
].has_arg
==
418 * optional argument doesn't use
421 optarg
= nargv
[optind
++];
424 if ((long_options
[match
].has_arg
== required_argument
)
425 && (optarg
== NULL
)) {
427 * Missing argument; leading ':'
428 * indicates no error should be generated
431 warnx(recargstring
, current_argv
);
433 * XXX: GNU sets optopt to val regardless
436 if (long_options
[match
].flag
== NULL
)
437 optopt
= long_options
[match
].val
;
443 } else { /* unknown option */
445 warnx(illoptstring
, current_argv
);
449 if (long_options
[match
].flag
) {
450 *long_options
[match
].flag
= long_options
[match
].val
;
453 retval
= long_options
[match
].val
;
458 #undef IDENTICAL_INTERPRETATION