]>
The Tcpdump Group git mirrors - tcpdump/blob - missing/getopt_long.c
1 /* $OpenBSD: getopt_long.c,v 1.22 2006/10/04 21:29:04 jmc 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>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 * All rights reserved.
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by Dieter Baron and Thomas Klausner.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
54 #include "getopt_long.h"
60 #include "diag-control.h"
62 #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
64 #define PRINT_ERROR ((opterr) && (*options != ':'))
66 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
67 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
68 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
71 #define BADCH (int)'?'
72 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
73 #define INORDER (int)1
78 #define NO_PREFIX (-1)
85 int optind
, opterr
= 1, optopt
;
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, int);
91 static int gcd(int, int);
92 static void permute_args(int, int, int, char * const *);
94 static const char *place
= EMSG
; /* option letter processing */
96 static int nonopt_start
= -1; /* first non option argument (for permute) */
97 static int nonopt_end
= -1; /* first option after non options (for permute) */
100 static const char recargchar
[] = "option requires an argument -- %c";
101 static const char illoptchar
[] = "illegal option -- %c"; /* From P1003.2 */
102 #ifdef GNU_COMPATIBLE
103 static int dash_prefix
= NO_PREFIX
;
104 static const char gnuoptchar
[] = "invalid option -- %c";
106 static const char recargstring
[] = "option `%s%s' requires an argument";
107 static const char ambig
[] = "option `%s%.*s' is ambiguous";
108 static const char noarg
[] = "option `%s%.*s' doesn't allow an argument";
109 static const char illoptstring
[] = "unrecognized option `%s%s'";
111 static const char recargstring
[] = "option requires an argument -- %s";
112 static const char ambig
[] = "ambiguous option -- %.*s";
113 static const char noarg
[] = "option doesn't take an argument -- %.*s";
114 static const char illoptstring
[] = "unknown option -- %s";
118 * Compute the greatest common divisor of a and b.
136 * Exchange the block from nonopt_start to nonopt_end with the block
137 * from nonopt_end to opt_end (keeping the same order of arguments
141 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
144 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
148 * compute lengths of blocks and number and size of cycles
150 nnonopts
= panonopt_end
- panonopt_start
;
151 nopts
= opt_end
- panonopt_end
;
152 ncycle
= gcd(nnonopts
, nopts
);
153 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
155 for (i
= 0; i
< ncycle
; i
++) {
156 cstart
= panonopt_end
+i
;
158 for (j
= 0; j
< cyclelen
; j
++) {
159 if (pos
>= panonopt_end
)
164 * This is annoying - I guess the
165 * "char * const argv[]" in the declaration
166 * of getopt() - and thus getopt_long() -
167 * means that it makes a promise not to
168 * shuffle the arguments, but here we are,
169 * shuffling the arguments.
171 * (No, it's not a promise that it won't
172 * modify any of the argument strings.
173 * It's a promise that it won't modify
174 * the array of pointers to the argument
177 * So squelch the cast warnings.
181 /* LINTED const cast */
182 ((char **) nargv
)[pos
] = nargv
[cstart
];
183 /* LINTED const cast */
184 ((char **)nargv
)[cstart
] = swap
;
191 warnx(const char *fmt
, ...)
193 extern char *program_name
;
197 fprintf(stderr
, "%s: ", program_name
);
198 vfprintf(stderr
, fmt
, ap
);
199 fprintf(stderr
, "\n");
204 * parse_long_options --
205 * Parse long options in argc/argv argument vector.
206 * Returns -1 if short_too is set and the option does not match long_options.
209 parse_long_options(char * const *nargv
, const char *options
,
210 const struct option
*long_options
, int *idx
, int short_too
, int flags
)
212 const char *current_argv
, *has_equal
;
213 #ifdef GNU_COMPATIBLE
214 const char *current_dash
;
216 size_t current_argv_len
;
217 int i
, match
, exact_match
, second_partial_match
;
219 current_argv
= place
;
220 #ifdef GNU_COMPATIBLE
221 switch (dash_prefix
) {
229 current_dash
= "-W ";
238 second_partial_match
= 0;
242 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
243 /* argument found (--option=arg) */
244 current_argv_len
= has_equal
- current_argv
;
247 current_argv_len
= strlen(current_argv
);
249 for (i
= 0; long_options
[i
].name
; i
++) {
250 /* find matching long option */
251 if (strncmp(current_argv
, long_options
[i
].name
,
255 if (strlen(long_options
[i
].name
) == current_argv_len
) {
262 * If this is a known short option, don't allow
263 * a partial match of a single character.
265 if (short_too
&& current_argv_len
== 1)
268 if (match
== -1) /* first partial match */
270 else if ((flags
& FLAG_LONGONLY
) ||
271 long_options
[i
].has_arg
!=
272 long_options
[match
].has_arg
||
273 long_options
[i
].flag
!= long_options
[match
].flag
||
274 long_options
[i
].val
!= long_options
[match
].val
)
275 second_partial_match
= 1;
277 if (!exact_match
&& second_partial_match
) {
278 /* ambiguous abbreviation */
281 #ifdef GNU_COMPATIBLE
284 (int)current_argv_len
,
289 if (match
!= -1) { /* option found */
290 if (long_options
[match
].has_arg
== no_argument
294 #ifdef GNU_COMPATIBLE
297 (int)current_argv_len
,
300 * XXX: GNU sets optopt to val regardless of flag
302 if (long_options
[match
].flag
== NULL
)
303 optopt
= long_options
[match
].val
;
306 #ifdef GNU_COMPATIBLE
312 if (long_options
[match
].has_arg
== required_argument
||
313 long_options
[match
].has_arg
== optional_argument
) {
316 optarg
= (char *)has_equal
;
317 else if (long_options
[match
].has_arg
==
320 * optional argument doesn't use next nargv
322 optarg
= nargv
[optind
++];
326 if ((long_options
[match
].has_arg
== required_argument
)
327 && (optarg
== NULL
)) {
329 * Missing argument; leading ':' indicates no error
330 * should be generated.
334 #ifdef GNU_COMPATIBLE
339 * XXX: GNU sets optopt to val regardless of flag
341 if (long_options
[match
].flag
== NULL
)
342 optopt
= long_options
[match
].val
;
348 } else { /* unknown option */
355 #ifdef GNU_COMPATIBLE
364 if (long_options
[match
].flag
) {
365 *long_options
[match
].flag
= long_options
[match
].val
;
368 return (long_options
[match
].val
);
373 * Parse argc/argv argument vector. Called by user level routines.
376 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
377 const struct option
*long_options
, int *idx
, int flags
)
379 char *oli
; /* option letter list index */
380 int optchar
, short_too
;
381 int posixly_correct
; /* no static, can be changed on the fly */
387 * Disable GNU extensions if POSIXLY_CORRECT is set or options
388 * string begins with a '+'.
390 posixly_correct
= (getenv("POSIXLY_CORRECT") != NULL
);
391 #ifdef GNU_COMPATIBLE
393 flags
|= FLAG_ALLARGS
;
394 else if (posixly_correct
|| *options
== '+')
395 flags
&= ~FLAG_PERMUTE
;
397 if (posixly_correct
|| *options
== '+')
398 flags
&= ~FLAG_PERMUTE
;
399 else if (*options
== '-')
400 flags
|= FLAG_ALLARGS
;
402 if (*options
== '+' || *options
== '-')
406 * XXX Some GNU programs (like cvs) set optind to 0 instead of
407 * XXX using optreset. Work around this braindamage.
414 if (!*place
) { /* update scanning pointer */
415 if (optind
>= nargc
) { /* end of argument vector */
417 if (nonopt_end
!= -1) {
418 /* do permutation, if we have to */
419 permute_args(nonopt_start
, nonopt_end
,
421 optind
-= nonopt_end
- nonopt_start
;
423 else if (nonopt_start
!= -1) {
425 * If we skipped non-options, set optind
426 * to the first of them.
428 optind
= nonopt_start
;
430 nonopt_start
= nonopt_end
= -1;
433 if (*(place
= nargv
[optind
]) != '-' ||
434 #ifdef GNU_COMPATIBLE
437 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
439 place
= EMSG
; /* found non-option */
440 if (flags
& FLAG_ALLARGS
) {
443 * return non-option as argument to option 1
445 optarg
= nargv
[optind
++];
448 if (!(flags
& FLAG_PERMUTE
)) {
450 * If no permutation wanted, stop parsing
451 * at first non-option.
456 if (nonopt_start
== -1)
457 nonopt_start
= optind
;
458 else if (nonopt_end
!= -1) {
459 permute_args(nonopt_start
, nonopt_end
,
461 nonopt_start
= optind
-
462 (nonopt_end
- nonopt_start
);
466 /* process next argument */
469 if (nonopt_start
!= -1 && nonopt_end
== -1)
473 * If we have "-" do nothing, if "--" we are done.
475 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
479 * We found an option (--), so if we skipped
480 * non-options, we have to permute.
482 if (nonopt_end
!= -1) {
483 permute_args(nonopt_start
, nonopt_end
,
485 optind
-= nonopt_end
- nonopt_start
;
487 nonopt_start
= nonopt_end
= -1;
493 * Check long options if:
494 * 1) we were passed some
495 * 2) the arg is not just "-"
496 * 3) either the arg starts with -- we are getopt_long_only()
498 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
499 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
501 #ifdef GNU_COMPATIBLE
502 dash_prefix
= D_PREFIX
;
505 place
++; /* --foo long option */
506 #ifdef GNU_COMPATIBLE
507 dash_prefix
= DD_PREFIX
;
509 } else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
510 short_too
= 1; /* could be short option too */
512 optchar
= parse_long_options(nargv
, options
, long_options
,
513 idx
, short_too
, flags
);
520 if ((optchar
= (int)*place
++) == (int)':' ||
521 (optchar
== (int)'-' && *place
!= '\0') ||
522 (oli
= strchr(options
, optchar
)) == NULL
) {
524 * If the user specified "-" and '-' isn't listed in
525 * options, return -1 (non-option) as per POSIX.
526 * Otherwise, it is an unknown option character (or ':').
528 if (optchar
== (int)'-' && *place
== '\0')
532 #ifdef GNU_COMPATIBLE
534 warnx(posixly_correct
? illoptchar
: gnuoptchar
,
538 warnx(illoptchar
, optchar
);
543 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
545 if (*place
) /* no space */
547 else if (++optind
>= nargc
) { /* no arg */
550 warnx(recargchar
, optchar
);
553 } else /* white space */
554 place
= nargv
[optind
];
555 #ifdef GNU_COMPATIBLE
556 dash_prefix
= W_PREFIX
;
558 optchar
= parse_long_options(nargv
, options
, long_options
,
563 if (*++oli
!= ':') { /* doesn't take argument */
566 } else { /* takes (optional) argument */
568 if (*place
) { /* no white space */
570 optarg
= (char *)place
;
572 } else if (oli
[1] != ':') { /* arg not optional */
573 if (++optind
>= nargc
) { /* no arg */
576 warnx(recargchar
, optchar
);
580 optarg
= nargv
[optind
];
585 /* dump back option letter */
589 #ifdef REPLACE_GETOPT
592 * Parse argc/argv argument vector.
594 * [eventually this will replace the BSD getopt]
597 getopt(int nargc
, char * const *nargv
, const char *options
)
601 * We don't pass FLAG_PERMUTE to getopt_internal() since
602 * the BSD getopt(3) (unlike GNU) has never done this.
604 * Furthermore, since many privileged programs call getopt()
605 * before dropping privileges it makes sense to keep things
606 * as simple (and bug-free) as possible.
608 return (getopt_internal(nargc
, nargv
, options
, NULL
, NULL
, 0));
610 #endif /* REPLACE_GETOPT */
614 * Parse argc/argv argument vector.
617 getopt_long(int nargc
, char * const *nargv
, const char *options
,
618 const struct option
*long_options
, int *idx
)
621 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
626 * getopt_long_only --
627 * Parse argc/argv argument vector.
630 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
631 const struct option
*long_options
, int *idx
)
634 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
635 FLAG_PERMUTE
|FLAG_LONGONLY
));