1 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
2 /* $FreeBSD: src/lib/libc/stdlib/getopt_long.c,v 1.2 2002/10/16 22:18:42 alfred Exp $ */
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Dieter Baron and Thomas Klausner.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 #include "getopt_win.h"
47 /* Windows needs warnx(). We change the definition though:
48 * 1. (another) global is defined, opterrmsg, which holds the error message
49 * 2. errors are always printed out on stderr w/o the program name
50 * Note that opterrmsg always gets set no matter what opterr is set to. The
51 * error message will not be printed if opterr is 0 as usual.
57 GETOPT_API extern char opterrmsg[128];
58 char opterrmsg[128]; /* last error message is stored here */
60 static void warnx(int print_error, const char *fmt, ...)
65 _vsnprintf(opterrmsg, 128, fmt, ap);
70 fprintf(stderr, opterrmsg);
71 fprintf(stderr, "\n");
78 /* not part of the original file */
80 #define _DIAGASSERT(X)
83 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
84 #define REPLACE_GETOPT
89 __weak_alias(getopt,_getopt)
91 int opterr = 1; /* if error message should be printed */
92 int optind = 1; /* index into parent argv vector */
93 int optopt = '?'; /* character checked for validity */
94 int optreset; /* reset getopt */
95 char *optarg; /* argument associated with option */
96 #elif HAVE_CONFIG_H && !HAVE_DECL_OPTRESET
101 __weak_alias(getopt_long,_getopt_long)
104 #if !HAVE_GETOPT_LONG
105 #define IGNORE_FIRST (*options == '-' || *options == '+')
106 #define PRINT_ERROR ((opterr) && ((*options != ':') \
107 || (IGNORE_FIRST && options[1] != ':')))
108 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
109 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
110 /* XXX: GNU ignores PC if *options == '-' */
111 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
114 #define BADCH (int)'?'
115 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
116 || (*options == ':') ? (int)':' : (int)'?')
117 #define INORDER (int)1
121 static int getopt_internal(int, char * const *, const char *);
122 static int gcd(int, int);
123 static void permute_args(int, int, int, char * const *);
125 static char *place = EMSG; /* option letter processing */
127 /* XXX: set optreset to 1 rather than these two */
128 static int nonopt_start = -1; /* first non option argument (for permute) */
129 static int nonopt_end = -1; /* first option after non options (for permute) */
132 static const char recargchar[] = "option requires an argument -- %c";
133 static const char recargstring[] = "option requires an argument -- %s";
134 static const char ambig[] = "ambiguous option -- %.*s";
135 static const char noarg[] = "option doesn't take an argument -- %.*s";
136 static const char illoptchar[] = "unknown option -- %c";
137 static const char illoptstring[] = "unknown option -- %s";
141 * Compute the greatest common divisor of a and b.
161 * Exchange the block from nonopt_start to nonopt_end with the block
162 * from nonopt_end to opt_end (keeping the same order of arguments
166 permute_args(panonopt_start, panonopt_end, opt_end, nargv)
172 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
175 _DIAGASSERT(nargv != NULL);
178 * compute lengths of blocks and number and size of cycles
180 nnonopts = panonopt_end - panonopt_start;
181 nopts = opt_end - panonopt_end;
182 ncycle = gcd(nnonopts, nopts);
183 cyclelen = (opt_end - panonopt_start) / ncycle;
185 for (i = 0; i < ncycle; i++) {
186 cstart = panonopt_end+i;
188 for (j = 0; j < cyclelen; j++) {
189 if (pos >= panonopt_end)
194 /* LINTED const cast */
195 ((char **) nargv)[pos] = nargv[cstart];
196 /* LINTED const cast */
197 ((char **)nargv)[cstart] = swap;
204 * Parse argc/argv argument vector. Called by user level routines.
205 * Returns -2 if -- is found (can be long option or end of options marker).
208 getopt_internal(nargc, nargv, options)
213 char *oli; /* option letter list index */
216 _DIAGASSERT(nargv != NULL);
217 _DIAGASSERT(options != NULL);
222 * XXX Some programs (like rsyncd) expect to be able to
223 * XXX re-initialize optind to 0 and have getopt_long(3)
224 * XXX properly function again. Work around this braindamage.
230 nonopt_start = nonopt_end = -1;
232 if (optreset || !*place) { /* update scanning pointer */
234 if (optind >= nargc) { /* end of argument vector */
236 if (nonopt_end != -1) {
237 /* do permutation, if we have to */
238 permute_args(nonopt_start, nonopt_end,
240 optind -= nonopt_end - nonopt_start;
242 else if (nonopt_start != -1) {
244 * If we skipped non-options, set optind
245 * to the first of them.
247 optind = nonopt_start;
249 nonopt_start = nonopt_end = -1;
252 if ((*(place = nargv[optind]) != '-')
253 || (place[1] == '\0')) { /* found non-option */
258 * return non-option as argument to option 1
260 optarg = nargv[optind++];
265 * if no permutation wanted, stop parsing
266 * at first non-option
271 if (nonopt_start == -1)
272 nonopt_start = optind;
273 else if (nonopt_end != -1) {
274 permute_args(nonopt_start, nonopt_end,
276 nonopt_start = optind -
277 (nonopt_end - nonopt_start);
281 /* process next argument */
284 if (nonopt_start != -1 && nonopt_end == -1)
286 if (place[1] && *++place == '-') { /* found "--" */
291 if ((optchar = (int)*place++) == (int)':' ||
292 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
293 /* option letter unknown or ':' */
298 warnx(illoptchar, optchar);
300 warnx(PRINT_ERROR, illoptchar, optchar);
305 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
306 /* XXX: what if no long options provided (called by getopt)? */
310 if (++optind >= nargc) { /* no arg */
314 warnx(recargchar, optchar);
316 warnx(PRINT_ERROR, recargchar, optchar);
320 } else /* white space */
321 place = nargv[optind];
323 * Handle -W arg the same as --arg (which causes getopt to
328 if (*++oli != ':') { /* doesn't take argument */
331 } else { /* takes (optional) argument */
333 if (*place) /* no white space */
335 /* XXX: disable test for :: if PC? (GNU doesn't) */
336 else if (oli[1] != ':') { /* arg not optional */
337 if (++optind >= nargc) { /* no arg */
341 warnx(recargchar, optchar);
343 warnx(PRINT_ERROR, recargchar, optchar);
348 optarg = nargv[optind];
353 /* dump back option letter */
357 #ifdef REPLACE_GETOPT
360 * Parse argc/argv argument vector.
362 * [eventually this will replace the real getopt]
365 getopt(nargc, nargv, options)
372 _DIAGASSERT(nargv != NULL);
373 _DIAGASSERT(options != NULL);
375 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
378 * We found an option (--), so if we skipped non-options,
379 * we have to permute.
381 if (nonopt_end != -1) {
382 permute_args(nonopt_start, nonopt_end, optind,
384 optind -= nonopt_end - nonopt_start;
386 nonopt_start = nonopt_end = -1;
395 * Parse argc/argv argument vector.
398 getopt_long(nargc, nargv, options, long_options, idx)
402 const struct option *long_options;
407 _DIAGASSERT(nargv != NULL);
408 _DIAGASSERT(options != NULL);
409 _DIAGASSERT(long_options != NULL);
410 /* idx may be NULL */
412 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
413 char *current_argv, *has_equal;
414 size_t current_argv_len;
417 current_argv = place;
423 if (*current_argv == '\0') { /* found "--" */
425 * We found an option (--), so if we skipped
426 * non-options, we have to permute.
428 if (nonopt_end != -1) {
429 permute_args(nonopt_start, nonopt_end,
431 optind -= nonopt_end - nonopt_start;
433 nonopt_start = nonopt_end = -1;
436 if ((has_equal = strchr(current_argv, '=')) != NULL) {
437 /* argument found (--option=arg) */
438 current_argv_len = has_equal - current_argv;
441 current_argv_len = strlen(current_argv);
443 for (i = 0; long_options[i].name; i++) {
444 /* find matching long option */
445 if (strncmp(current_argv, long_options[i].name,
449 if (strlen(long_options[i].name) ==
450 (unsigned)current_argv_len) {
455 if (match == -1) /* partial match */
458 /* ambiguous abbreviation */
461 warnx(ambig, (int)current_argv_len,
464 warnx(PRINT_ERROR, ambig, (int)current_argv_len,
471 if (match != -1) { /* option found */
472 if (long_options[match].has_arg == no_argument
476 warnx(noarg, (int)current_argv_len,
479 warnx(PRINT_ERROR, noarg, (int)current_argv_len,
483 * XXX: GNU sets optopt to val regardless of
486 if (long_options[match].flag == NULL)
487 optopt = long_options[match].val;
492 if (long_options[match].has_arg == required_argument ||
493 long_options[match].has_arg == optional_argument) {
496 else if (long_options[match].has_arg ==
499 * optional argument doesn't use
502 optarg = nargv[optind++];
505 if ((long_options[match].has_arg == required_argument)
506 && (optarg == NULL)) {
508 * Missing argument; leading ':'
509 * indicates no error should be generated
513 warnx(recargstring, current_argv);
515 warnx(PRINT_ERROR, recargstring, current_argv);
518 * XXX: GNU sets optopt to val regardless
521 if (long_options[match].flag == NULL)
522 optopt = long_options[match].val;
528 } else { /* unknown option */
531 warnx(illoptstring, current_argv);
533 warnx(PRINT_ERROR, illoptstring, current_argv);
538 if (long_options[match].flag) {
539 *long_options[match].flag = long_options[match].val;
542 retval = long_options[match].val;
548 #endif /* !GETOPT_LONG */