1 /* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */
2 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
6 * Copyright (c) 2008 coresystems GmbH
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * Sponsored in part by the Defense Advanced Research Projects
21 * Agency (DARPA) and Air Force Research Laboratory, Air Force
22 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
25 * Copyright (c) 2000 The NetBSD Foundation, Inc.
26 * All rights reserved.
28 * This code is derived from software contributed to The NetBSD Foundation
29 * by Dieter Baron and Thomas Klausner.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
40 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
41 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
44 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
50 * POSSIBILITY OF SUCH DAMAGE.
57 #include <libpayload.h>
59 #define warnx(x...) printf(x)
62 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
65 int opterr
= 1; /* if error message should be printed */
66 int optind
= 1; /* index into parent argv vector */
67 int optopt
= '?'; /* character checked for validity */
68 int optreset
; /* reset getopt */
69 char *optarg
; /* argument associated with option */
71 int posixly_correct
= 0;
74 #define PRINT_ERROR ((opterr) && (*options != ':'))
76 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
77 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
78 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
81 #define BADCH (int)'?'
82 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
83 #define INORDER (int)1
85 #define EMSG (char*)""
87 static int getopt_internal(int, char * const *, const char *,
88 const struct option
*, int *, int);
89 static int parse_long_options(char * const *, const char *,
90 const struct option
*, int *, int);
91 static int gcd(int, int);
92 static void permute_args(int, int, int, char * const *);
94 static char *place
= EMSG
; /* option letter processing */
96 /* XXX: set optreset to 1 rather than these two */
97 static int nonopt_start
= -1; /* first non option argument (for permute) */
98 static int nonopt_end
= -1; /* first option after non options (for permute) */
101 static const char recargchar
[] = "option requires an argument -- %c";
102 static const char recargstring
[] = "option requires an argument -- %s";
103 static const char ambig
[] = "ambiguous option -- %.*s";
104 static const char noarg
[] = "option doesn't take an argument -- %.*s";
105 static const char illoptchar
[] = "unknown option -- %c";
106 static const char illoptstring
[] = "unknown option -- %s";
109 * Compute the greatest common divisor of a and b.
127 * Exchange the block from nonopt_start to nonopt_end with the block
128 * from nonopt_end to opt_end (keeping the same order of arguments
132 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
135 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
139 * compute lengths of blocks and number and size of cycles
141 nnonopts
= panonopt_end
- panonopt_start
;
142 nopts
= opt_end
- panonopt_end
;
143 ncycle
= gcd(nnonopts
, nopts
);
144 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
146 for (i
= 0; i
< ncycle
; i
++) {
147 cstart
= panonopt_end
+i
;
149 for (j
= 0; j
< cyclelen
; j
++) {
150 if (pos
>= panonopt_end
)
155 /* LINTED const cast */
156 ((char **) nargv
)[pos
] = nargv
[cstart
];
157 /* LINTED const cast */
158 ((char **)nargv
)[cstart
] = swap
;
164 * parse_long_options --
165 * Parse long options in argc/argv argument vector.
166 * Returns -1 if short_too is set and the option does not match long_options.
169 parse_long_options(char * const *nargv
, const char *options
,
170 const struct option
*long_options
, int *idx
, int short_too
)
172 char *current_argv
, *has_equal
;
173 size_t current_argv_len
;
176 current_argv
= place
;
181 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
182 /* argument found (--option=arg) */
183 current_argv_len
= has_equal
- current_argv
;
186 current_argv_len
= strlen(current_argv
);
188 for (i
= 0; long_options
[i
].name
; i
++) {
189 /* find matching long option */
190 if (strncmp(current_argv
, long_options
[i
].name
,
194 if (strlen(long_options
[i
].name
) == current_argv_len
) {
200 * If this is a known short option, don't allow
201 * a partial match of a single character.
203 if (short_too
&& current_argv_len
== 1)
206 if (match
== -1) /* partial match */
209 /* ambiguous abbreviation */
211 warnx(ambig
, (int)current_argv_len
,
217 if (match
!= -1) { /* option found */
218 if (long_options
[match
].has_arg
== no_argument
221 warnx(noarg
, (int)current_argv_len
,
224 * XXX: GNU sets optopt to val regardless of flag
226 if (long_options
[match
].flag
== NULL
)
227 optopt
= long_options
[match
].val
;
232 if (long_options
[match
].has_arg
== required_argument
||
233 long_options
[match
].has_arg
== optional_argument
) {
236 else if (long_options
[match
].has_arg
==
239 * optional argument doesn't use next nargv
241 optarg
= nargv
[optind
++];
244 if ((long_options
[match
].has_arg
== required_argument
)
245 && (optarg
== NULL
)) {
247 * Missing argument; leading ':' indicates no error
248 * should be generated.
254 * XXX: GNU sets optopt to val regardless of flag
256 if (long_options
[match
].flag
== NULL
)
257 optopt
= long_options
[match
].val
;
263 } else { /* unknown option */
269 warnx(illoptstring
, current_argv
);
275 if (long_options
[match
].flag
) {
276 *long_options
[match
].flag
= long_options
[match
].val
;
279 return (long_options
[match
].val
);
284 * Parse argc/argv argument vector. Called by user level routines.
287 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
288 const struct option
*long_options
, int *idx
, int flags
)
290 char *oli
; /* option letter list index */
291 int optchar
, short_too
;
297 * Disable GNU extensions if posixly_correct is set or options
298 * string begins with a '+'.
300 if (posixly_correct
|| *options
== '+')
301 flags
&= ~FLAG_PERMUTE
;
302 else if (*options
== '-')
303 flags
|= FLAG_ALLARGS
;
304 if (*options
== '+' || *options
== '-')
308 * XXX Some GNU programs (like cvs) set optind to 0 instead of
309 * XXX using optreset. Work around this braindamage.
312 optind
= optreset
= 1;
316 nonopt_start
= nonopt_end
= -1;
318 if (optreset
|| !*place
) { /* update scanning pointer */
320 if (optind
>= nargc
) { /* end of argument vector */
322 if (nonopt_end
!= -1) {
323 /* do permutation, if we have to */
324 permute_args(nonopt_start
, nonopt_end
,
326 optind
-= nonopt_end
- nonopt_start
;
328 else if (nonopt_start
!= -1) {
330 * If we skipped non-options, set optind
331 * to the first of them.
333 optind
= nonopt_start
;
335 nonopt_start
= nonopt_end
= -1;
338 if (*(place
= nargv
[optind
]) != '-' ||
339 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
340 place
= EMSG
; /* found non-option */
341 if (flags
& FLAG_ALLARGS
) {
344 * return non-option as argument to option 1
346 optarg
= nargv
[optind
++];
349 if (!(flags
& FLAG_PERMUTE
)) {
351 * If no permutation wanted, stop parsing
352 * at first non-option.
357 if (nonopt_start
== -1)
358 nonopt_start
= optind
;
359 else if (nonopt_end
!= -1) {
360 permute_args(nonopt_start
, nonopt_end
,
362 nonopt_start
= optind
-
363 (nonopt_end
- nonopt_start
);
367 /* process next argument */
370 if (nonopt_start
!= -1 && nonopt_end
== -1)
374 * If we have "-" do nothing, if "--" we are done.
376 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
380 * We found an option (--), so if we skipped
381 * non-options, we have to permute.
383 if (nonopt_end
!= -1) {
384 permute_args(nonopt_start
, nonopt_end
,
386 optind
-= nonopt_end
- nonopt_start
;
388 nonopt_start
= nonopt_end
= -1;
394 * Check long options if:
395 * 1) we were passed some
396 * 2) the arg is not just "-"
397 * 3) either the arg starts with -- we are getopt_long_only()
399 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
400 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
403 place
++; /* --foo long option */
404 else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
405 short_too
= 1; /* could be short option too */
407 optchar
= parse_long_options(nargv
, options
, long_options
,
415 if ((optchar
= (int)*place
++) == (int)':' ||
416 (optchar
== (int)'-' && *place
!= '\0') ||
417 (oli
= strchr(options
, optchar
)) == NULL
) {
419 * If the user specified "-" and '-' isn't listed in
420 * options, return -1 (non-option) as per POSIX.
421 * Otherwise, it is an unknown option character (or ':').
423 if (optchar
== (int)'-' && *place
== '\0')
428 warnx(illoptchar
, optchar
);
432 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
434 if (*place
) /* no space */
436 else if (++optind
>= nargc
) { /* no arg */
439 warnx(recargchar
, optchar
);
442 } else /* white space */
443 place
= nargv
[optind
];
444 optchar
= parse_long_options(nargv
, options
, long_options
,
449 if (*++oli
!= ':') { /* doesn't take argument */
452 } else { /* takes (optional) argument */
454 if (*place
) /* no white space */
456 else if (oli
[1] != ':') { /* arg not optional */
457 if (++optind
>= nargc
) { /* no arg */
460 warnx(recargchar
, optchar
);
464 optarg
= nargv
[optind
];
469 /* dump back option letter */
473 #ifdef REPLACE_GETOPT
476 * Parse argc/argv argument vector.
478 * [eventually this will replace the BSD getopt]
481 getopt(int nargc
, char * const *nargv
, const char *options
)
485 * We don't pass FLAG_PERMUTE to getopt_internal() since
486 * the BSD getopt(3) (unlike GNU) has never done this.
488 * Furthermore, since many privileged programs call getopt()
489 * before dropping privileges it makes sense to keep things
490 * as simple (and bug-free) as possible.
492 return (getopt_internal(nargc
, nargv
, options
, NULL
, NULL
, 0));
494 #endif /* REPLACE_GETOPT */
498 * Parse argc/argv argument vector.
501 getopt_long(int nargc
, char * const *nargv
, const char *options
,
502 const struct option
*long_options
, int *idx
)
505 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
510 * getopt_long_only --
511 * Parse argc/argv argument vector.
514 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
515 const struct option
*long_options
, int *idx
)
518 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
519 FLAG_PERMUTE
|FLAG_LONGONLY
));