]>
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.
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 __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
63 #define _DIAGASSERT(q) ovs_assert(q)
65 #define warnx VLOG_WARN
67 static int getopt_internal(int, char **, const char *);
68 static int gcd(int, int);
69 static void permute_args(int, int, int, char **);
71 static const char *place
= EMSG
; /* option letter processing */
73 /* XXX: set optreset to 1 rather than these two */
74 static int nonopt_start
= -1; /* first non option argument (for permute) */
75 static int nonopt_end
= -1; /* first option after non options (for permute) */
78 static const char recargchar
[] = "option requires an argument -- %c";
79 static const char recargstring
[] = "option requires an argument -- %s";
80 static const char ambig
[] = "ambiguous option -- %.*s";
81 static const char noarg
[] = "option doesn't take an argument -- %.*s";
82 static const char illoptchar
[] = "unknown option -- %c";
83 static const char illoptstring
[] = "unknown option -- %s";
87 * Compute the greatest common divisor of a and b.
105 * Exchange the block from nonopt_start to nonopt_end with the block
106 * from nonopt_end to opt_end (keeping the same order of arguments
110 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
, char **nargv
)
112 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
115 _DIAGASSERT(nargv
!= NULL
);
118 * compute lengths of blocks and number and size of cycles
120 nnonopts
= panonopt_end
- panonopt_start
;
121 nopts
= opt_end
- panonopt_end
;
122 ncycle
= gcd(nnonopts
, nopts
);
123 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
125 for (i
= 0; i
< ncycle
; i
++) {
126 cstart
= panonopt_end
+i
;
128 for (j
= 0; j
< cyclelen
; j
++) {
129 if (pos
>= panonopt_end
)
134 nargv
[pos
] = nargv
[cstart
];
135 nargv
[cstart
] = swap
;
142 * Parse argc/argv argument vector. Called by user level routines.
143 * Returns -2 if -- is found (can be long option or end of options marker).
146 getopt_internal(int nargc
, char **nargv
, const char *options
)
148 char *oli
; /* option letter list index */
151 _DIAGASSERT(nargv
!= NULL
);
152 _DIAGASSERT(options
!= NULL
);
157 * XXX Some programs (like rsyncd) expect to be able to
158 * XXX re-initialize optind to 0 and have getopt_long(3)
159 * XXX properly function again. Work around this braindamage.
165 nonopt_start
= nonopt_end
= -1;
167 if (optreset
|| !*place
) { /* update scanning pointer */
169 if (optind
>= nargc
) { /* end of argument vector */
171 if (nonopt_end
!= -1) {
172 /* do permutation, if we have to */
173 permute_args(nonopt_start
, nonopt_end
,
175 optind
-= nonopt_end
- nonopt_start
;
177 else if (nonopt_start
!= -1) {
179 * If we skipped non-options, set optind
180 * to the first of them.
182 optind
= nonopt_start
;
184 nonopt_start
= nonopt_end
= -1;
187 if ((*(place
= nargv
[optind
]) != '-')
188 || (place
[1] == '\0')) { /* found non-option */
193 * return non-option as argument to option 1
195 optarg
= nargv
[optind
++];
200 * if no permutation wanted, stop parsing
201 * at first non-option
206 if (nonopt_start
== -1)
207 nonopt_start
= optind
;
208 else if (nonopt_end
!= -1) {
209 permute_args(nonopt_start
, nonopt_end
,
211 nonopt_start
= optind
-
212 (nonopt_end
- nonopt_start
);
216 /* process next argument */
219 if (nonopt_start
!= -1 && nonopt_end
== -1)
221 if (place
[1] && *++place
== '-') { /* found "--" */
226 if ((optchar
= (int)*place
++) == (int)':' ||
227 (oli
= strchr(options
+ (IGNORE_FIRST
? 1 : 0), optchar
)) == NULL
) {
228 /* option letter unknown or ':' */
232 warnx(illoptchar
, optchar
);
236 if (optchar
== 'W' && oli
[1] == ';') { /* -W long-option */
237 /* XXX: what if no long options provided (called by getopt)? */
241 if (++optind
>= nargc
) { /* no arg */
244 warnx(recargchar
, optchar
);
247 } else /* white space */
248 place
= nargv
[optind
];
250 * Handle -W arg the same as --arg (which causes getopt to
255 if (*++oli
!= ':') { /* doesn't take argument */
258 } else { /* takes (optional) argument */
260 if (*place
) /* no white space */
261 optarg
= __UNCONST(place
);
262 /* XXX: disable test for :: if PC? (GNU doesn't) */
263 else if (oli
[1] != ':') { /* arg not optional */
264 if (++optind
>= nargc
) { /* no arg */
267 warnx(recargchar
, optchar
);
271 optarg
= nargv
[optind
];
276 /* dump back option letter */
280 #ifdef REPLACE_GETOPT
283 * Parse argc/argv argument vector.
285 * [eventually this will replace the real getopt]
288 getopt(nargc
, nargv
, options
)
295 _DIAGASSERT(nargv
!= NULL
);
296 _DIAGASSERT(options
!= NULL
);
298 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
302 * We found an option (--), so if we skipped non-options,
303 * we have to permute.
305 if (nonopt_end
!= -1) {
306 permute_args(nonopt_start
, nonopt_end
, optind
,
308 optind
-= nonopt_end
- nonopt_start
;
310 nonopt_start
= nonopt_end
= -1;
319 * Parse argc/argv argument vector.
322 getopt_long(int nargc
, char * const *nargv
, const char *options
,
323 const struct option
*long_options
, int *idx
)
327 #define IDENTICAL_INTERPRETATION(_x, _y) \
328 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
329 long_options[(_x)].flag == long_options[(_y)].flag && \
330 long_options[(_x)].val == long_options[(_y)].val)
332 _DIAGASSERT(nargv
!= NULL
);
333 _DIAGASSERT(options
!= NULL
);
334 _DIAGASSERT(long_options
!= NULL
);
335 /* idx may be NULL */
337 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
339 char *current_argv
, *has_equal
;
340 size_t current_argv_len
;
341 int i
, ambiguous
, match
;
343 current_argv
= __UNCONST(place
);
350 if (*current_argv
== '\0') { /* found "--" */
352 * We found an option (--), so if we skipped
353 * non-options, we have to permute.
355 if (nonopt_end
!= -1) {
356 permute_args(nonopt_start
, nonopt_end
,
357 optind
, __UNCONST(nargv
));
358 optind
-= nonopt_end
- nonopt_start
;
360 nonopt_start
= nonopt_end
= -1;
363 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
364 /* argument found (--option=arg) */
365 current_argv_len
= has_equal
- current_argv
;
368 current_argv_len
= strlen(current_argv
);
370 for (i
= 0; long_options
[i
].name
; i
++) {
371 /* find matching long option */
372 if (strncmp(current_argv
, long_options
[i
].name
,
376 if (strlen(long_options
[i
].name
) ==
377 (unsigned)current_argv_len
) {
383 if (match
== -1) /* partial match */
385 else if (!IDENTICAL_INTERPRETATION(i
, match
))
389 /* ambiguous abbreviation */
391 warnx(ambig
, (int)current_argv_len
,
396 if (match
!= -1) { /* option found */
397 if (long_options
[match
].has_arg
== no_argument
400 warnx(noarg
, (int)current_argv_len
,
403 * XXX: GNU sets optopt to val regardless of
406 if (long_options
[match
].flag
== NULL
)
407 optopt
= long_options
[match
].val
;
412 if (long_options
[match
].has_arg
== required_argument
||
413 long_options
[match
].has_arg
== optional_argument
) {
416 else if (long_options
[match
].has_arg
==
419 * optional argument doesn't use
422 optarg
= nargv
[optind
++];
425 if ((long_options
[match
].has_arg
== required_argument
)
426 && (optarg
== NULL
)) {
428 * Missing argument; leading ':'
429 * indicates no error should be generated
432 warnx(recargstring
, current_argv
);
434 * XXX: GNU sets optopt to val regardless
437 if (long_options
[match
].flag
== NULL
)
438 optopt
= long_options
[match
].val
;
444 } else { /* unknown option */
446 warnx(illoptstring
, current_argv
);
450 if (long_options
[match
].flag
) {
451 *long_options
[match
].flag
= long_options
[match
].val
;
454 retval
= long_options
[match
].val
;
459 #undef IDENTICAL_INTERPRETATION