1 /* $OpenBSD: getopt_long.c,v 1.21 2006/09/22 17:22:05 millert 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.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the NetBSD
41 * Foundation, Inc. and its contributors.
42 * 4. Neither the name of The NetBSD Foundation nor the names of its
43 * contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
62 #endif /* LIBC_SCCS and not lint */
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
73 #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
75 #if 0 /* we prefer to keep our getopt(3) */
76 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
80 int opterr = 1; /* if error message should be printed */
81 int optind = 1; /* index into parent argv vector */
82 int optopt = '?'; /* character checked for validity */
83 int optreset; /* reset getopt */
84 char *optarg; /* argument associated with option */
87 #define PRINT_ERROR ((opterr) && (*options != ':'))
89 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
90 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
91 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
94 #define BADCH (int)'?'
95 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
96 #define INORDER (int)1
100 #ifdef GNU_COMPATIBLE
101 #define NO_PREFIX (-1)
107 static int getopt_internal(int, char * const *, const char *,
108 const struct option *, int *, int);
109 static int parse_long_options(char * const *, const char *,
110 const struct option *, int *, int, int);
111 static int gcd(int, int);
112 static void permute_args(int, int, int, char * const *);
114 static char *place = EMSG; /* option letter processing */
116 /* XXX: set optreset to 1 rather than these two */
117 static int nonopt_start = -1; /* first non option argument (for permute) */
118 static int nonopt_end = -1; /* first option after non options (for permute) */
121 static const char recargchar[] = "option requires an argument -- %c";
122 static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
123 #ifdef GNU_COMPATIBLE
124 static int dash_prefix = NO_PREFIX;
125 static const char gnuoptchar[] = "invalid option -- %c";
127 static const char recargstring[] = "option `%s%s' requires an argument";
128 static const char ambig[] = "option `%s%.*s' is ambiguous";
129 static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
130 static const char illoptstring[] = "unrecognized option `%s%s'";
132 static const char recargstring[] = "option requires an argument -- %s";
133 static const char ambig[] = "ambiguous option -- %.*s";
134 static const char noarg[] = "option doesn't take an argument -- %.*s";
135 static const char illoptstring[] = "unknown option -- %s";
139 * Compute the greatest common divisor of a and b.
157 * Exchange the block from nonopt_start to nonopt_end with the block
158 * from nonopt_end to opt_end (keeping the same order of arguments
162 permute_args(int panonopt_start, int panonopt_end, int opt_end,
165 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
169 * compute lengths of blocks and number and size of cycles
171 nnonopts = panonopt_end - panonopt_start;
172 nopts = opt_end - panonopt_end;
173 ncycle = gcd(nnonopts, nopts);
174 cyclelen = (opt_end - panonopt_start) / ncycle;
176 for (i = 0; i < ncycle; i++) {
177 cstart = panonopt_end+i;
179 for (j = 0; j < cyclelen; j++) {
180 if (pos >= panonopt_end)
185 /* LINTED const cast */
186 ((char **) nargv)[pos] = nargv[cstart];
187 /* LINTED const cast */
188 ((char **)nargv)[cstart] = swap;
194 * parse_long_options --
195 * Parse long options in argc/argv argument vector.
196 * Returns -1 if short_too is set and the option does not match long_options.
199 parse_long_options(char * const *nargv, const char *options,
200 const struct option *long_options, int *idx, int short_too, int flags)
202 char *current_argv, *has_equal;
203 #ifdef GNU_COMPATIBLE
206 size_t current_argv_len;
207 int i, match, exact_match, second_partial_match;
209 current_argv = place;
210 #ifdef GNU_COMPATIBLE
211 switch (dash_prefix) {
219 current_dash = "-W ";
228 second_partial_match = 0;
232 if ((has_equal = strchr(current_argv, '=')) != NULL) {
233 /* argument found (--option=arg) */
234 current_argv_len = has_equal - current_argv;
237 current_argv_len = strlen(current_argv);
239 for (i = 0; long_options[i].name; i++) {
240 /* find matching long option */
241 if (strncmp(current_argv, long_options[i].name,
245 if (strlen(long_options[i].name) == current_argv_len) {
252 * If this is a known short option, don't allow
253 * a partial match of a single character.
255 if (short_too && current_argv_len == 1)
258 if (match == -1) /* first partial match */
260 else if ((flags & FLAG_LONGONLY) ||
261 long_options[i].has_arg !=
262 long_options[match].has_arg ||
263 long_options[i].flag != long_options[match].flag ||
264 long_options[i].val != long_options[match].val)
265 second_partial_match = 1;
267 if (!exact_match && second_partial_match) {
268 /* ambiguous abbreviation */
271 #ifdef GNU_COMPATIBLE
274 (int)current_argv_len,
279 if (match != -1) { /* option found */
280 if (long_options[match].has_arg == no_argument
284 #ifdef GNU_COMPATIBLE
287 (int)current_argv_len,
290 * XXX: GNU sets optopt to val regardless of flag
292 if (long_options[match].flag == NULL)
293 optopt = long_options[match].val;
296 #ifdef GNU_COMPATIBLE
302 if (long_options[match].has_arg == required_argument ||
303 long_options[match].has_arg == optional_argument) {
306 else if (long_options[match].has_arg ==
309 * optional argument doesn't use next nargv
311 optarg = nargv[optind++];
314 if ((long_options[match].has_arg == required_argument)
315 && (optarg == NULL)) {
317 * Missing argument; leading ':' indicates no error
318 * should be generated.
322 #ifdef GNU_COMPATIBLE
327 * XXX: GNU sets optopt to val regardless of flag
329 if (long_options[match].flag == NULL)
330 optopt = long_options[match].val;
336 } else { /* unknown option */
343 #ifdef GNU_COMPATIBLE
352 if (long_options[match].flag) {
353 *long_options[match].flag = long_options[match].val;
356 return (long_options[match].val);
361 * Parse argc/argv argument vector. Called by user level routines.
364 getopt_internal(int nargc, char * const *nargv, const char *options,
365 const struct option *long_options, int *idx, int flags)
367 char *oli; /* option letter list index */
368 int optchar, short_too;
369 int posixly_correct; /* no static, can be changed on the fly */
375 * Disable GNU extensions if POSIXLY_CORRECT is set or options
376 * string begins with a '+'.
378 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
379 #ifdef GNU_COMPATIBLE
381 flags |= FLAG_ALLARGS;
382 else if (posixly_correct || *options == '+')
383 flags &= ~FLAG_PERMUTE;
385 if (posixly_correct || *options == '+')
386 flags &= ~FLAG_PERMUTE;
387 else if (*options == '-')
388 flags |= FLAG_ALLARGS;
390 if (*options == '+' || *options == '-')
394 * XXX Some GNU programs (like cvs) set optind to 0 instead of
395 * XXX using optreset. Work around this braindamage.
398 optind = optreset = 1;
402 nonopt_start = nonopt_end = -1;
404 if (optreset || !*place) { /* update scanning pointer */
406 if (optind >= nargc) { /* end of argument vector */
408 if (nonopt_end != -1) {
409 /* do permutation, if we have to */
410 permute_args(nonopt_start, nonopt_end,
412 optind -= nonopt_end - nonopt_start;
414 else if (nonopt_start != -1) {
416 * If we skipped non-options, set optind
417 * to the first of them.
419 optind = nonopt_start;
421 nonopt_start = nonopt_end = -1;
424 if (*(place = nargv[optind]) != '-' ||
425 #ifdef GNU_COMPATIBLE
428 (place[1] == '\0' && strchr(options, '-') == NULL)) {
430 place = EMSG; /* found non-option */
431 if (flags & FLAG_ALLARGS) {
434 * return non-option as argument to option 1
436 optarg = nargv[optind++];
439 if (!(flags & FLAG_PERMUTE)) {
441 * If no permutation wanted, stop parsing
442 * at first non-option.
447 if (nonopt_start == -1)
448 nonopt_start = optind;
449 else if (nonopt_end != -1) {
450 permute_args(nonopt_start, nonopt_end,
452 nonopt_start = optind -
453 (nonopt_end - nonopt_start);
457 /* process next argument */
460 if (nonopt_start != -1 && nonopt_end == -1)
464 * If we have "-" do nothing, if "--" we are done.
466 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
470 * We found an option (--), so if we skipped
471 * non-options, we have to permute.
473 if (nonopt_end != -1) {
474 permute_args(nonopt_start, nonopt_end,
476 optind -= nonopt_end - nonopt_start;
478 nonopt_start = nonopt_end = -1;
484 * Check long options if:
485 * 1) we were passed some
486 * 2) the arg is not just "-"
487 * 3) either the arg starts with -- we are getopt_long_only()
489 if (long_options != NULL && place != nargv[optind] &&
490 (*place == '-' || (flags & FLAG_LONGONLY))) {
492 #ifdef GNU_COMPATIBLE
493 dash_prefix = D_PREFIX;
496 place++; /* --foo long option */
497 #ifdef GNU_COMPATIBLE
498 dash_prefix = DD_PREFIX;
500 } else if (*place != ':' && strchr(options, *place) != NULL)
501 short_too = 1; /* could be short option too */
503 optchar = parse_long_options(nargv, options, long_options,
504 idx, short_too, flags);
511 if ((optchar = (int)*place++) == (int)':' ||
512 (optchar == (int)'-' && *place != '\0') ||
513 (oli = strchr(options, optchar)) == NULL) {
515 * If the user specified "-" and '-' isn't listed in
516 * options, return -1 (non-option) as per POSIX.
517 * Otherwise, it is an unknown option character (or ':').
519 if (optchar == (int)'-' && *place == '\0')
523 #ifdef GNU_COMPATIBLE
525 warnx(posixly_correct ? illoptchar : gnuoptchar,
529 warnx(illoptchar, optchar);
534 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
536 if (*place) /* no space */
538 else if (++optind >= nargc) { /* no arg */
541 warnx(recargchar, optchar);
544 } else /* white space */
545 place = nargv[optind];
546 #ifdef GNU_COMPATIBLE
547 dash_prefix = W_PREFIX;
549 optchar = parse_long_options(nargv, options, long_options,
554 if (*++oli != ':') { /* doesn't take argument */
557 } else { /* takes (optional) argument */
559 if (*place) /* no white space */
561 else if (oli[1] != ':') { /* arg not optional */
562 if (++optind >= nargc) { /* no arg */
565 warnx(recargchar, optchar);
569 optarg = nargv[optind];
574 /* dump back option letter */
578 #ifdef REPLACE_GETOPT
581 * Parse argc/argv argument vector.
583 * [eventually this will replace the BSD getopt]
586 getopt(int nargc, char * const *nargv, const char *options)
590 * We don't pass FLAG_PERMUTE to getopt_internal() since
591 * the BSD getopt(3) (unlike GNU) has never done this.
593 * Furthermore, since many privileged programs call getopt()
594 * before dropping privileges it makes sense to keep things
595 * as simple (and bug-free) as possible.
597 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
599 #endif /* REPLACE_GETOPT */
603 * Parse argc/argv argument vector.
606 getopt_long(int nargc, char * const *nargv, const char *options,
607 const struct option *long_options, int *idx)
610 return (getopt_internal(nargc, nargv, options, long_options, idx,
615 * getopt_long_only --
616 * Parse argc/argv argument vector.
619 getopt_long_only(int nargc, char * const *nargv, const char *options,
620 const struct option *long_options, int *idx)
623 return (getopt_internal(nargc, nargv, options, long_options, idx,
624 FLAG_PERMUTE|FLAG_LONGONLY));