1 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Dieter Baron and Thomas Klausner.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
47 #define REPLACE_GETOPT
49 #define _DIAGASSERT(x) do {} while (0)
53 __weak_alias(getopt,_getopt)
55 int opterr = 1; /* if error message should be printed */
56 int optind = 1; /* index into parent argv vector */
57 int optopt = '?'; /* character checked for validity */
58 int optreset; /* reset getopt */
59 char *optarg; /* argument associated with option */
63 __weak_alias(getopt_long,_getopt_long)
67 #define __progname __argv[0]
69 extern char *__progname;
72 #define IGNORE_FIRST (*options == '-' || *options == '+')
73 #define PRINT_ERROR ((opterr) && ((*options != ':') \
74 || (IGNORE_FIRST && options[1] != ':')))
76 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_INCORRECT_GETOPT") == NULL)
78 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
79 /* XXX: GNU ignores PC if *options == '-' */
80 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
83 #define BADCH (int)'?'
84 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
85 || (*options == ':') ? (int)':' : (int)'?')
86 #define INORDER (int)1
90 static int getopt_internal (int, char * const *, const char *);
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 _vwarnx(const char *fmt, va_list ap)
111 (void)fprintf(stderr, "%s: ", __progname);
113 (void)vfprintf(stderr, fmt, ap);
114 (void)fprintf(stderr, "\n");
118 warnx(const char *fmt, ...)
127 * Compute the greatest common divisor of a and b.
147 * Exchange the block from nonopt_start to nonopt_end with the block
148 * from nonopt_end to opt_end (keeping the same order of arguments
152 permute_args(panonopt_start, panonopt_end, opt_end, nargv)
158 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
161 _DIAGASSERT(nargv != NULL);
164 * compute lengths of blocks and number and size of cycles
166 nnonopts = panonopt_end - panonopt_start;
167 nopts = opt_end - panonopt_end;
168 ncycle = gcd(nnonopts, nopts);
169 cyclelen = (opt_end - panonopt_start) / ncycle;
171 for (i = 0; i < ncycle; i++) {
172 cstart = panonopt_end+i;
174 for (j = 0; j < cyclelen; j++) {
175 if (pos >= panonopt_end)
180 /* LINTED const cast */
181 ((char **) nargv)[pos] = nargv[cstart];
182 /* LINTED const cast */
183 ((char **)nargv)[cstart] = swap;
190 * Parse argc/argv argument vector. Called by user level routines.
191 * Returns -2 if -- is found (can be long option or end of options marker).
194 getopt_internal(nargc, nargv, options)
199 char *oli; /* option letter list index */
202 _DIAGASSERT(nargv != NULL);
203 _DIAGASSERT(options != NULL);
208 * XXX Some programs (like rsyncd) expect to be able to
209 * XXX re-initialize optind to 0 and have getopt_long(3)
210 * XXX properly function again. Work around this braindamage.
216 nonopt_start = nonopt_end = -1;
218 if (optreset || !*place) { /* update scanning pointer */
220 if (optind >= nargc) { /* end of argument vector */
222 if (nonopt_end != -1) {
223 /* do permutation, if we have to */
224 permute_args(nonopt_start, nonopt_end,
226 optind -= nonopt_end - nonopt_start;
228 else if (nonopt_start != -1) {
230 * If we skipped non-options, set optind
231 * to the first of them.
233 optind = nonopt_start;
235 nonopt_start = nonopt_end = -1;
238 if ((*(place = nargv[optind]) != '-')
239 || (place[1] == '\0')) { /* found non-option */
244 * return non-option as argument to option 1
246 optarg = nargv[optind++];
251 * if no permutation wanted, stop parsing
252 * at first non-option
257 if (nonopt_start == -1)
258 nonopt_start = optind;
259 else if (nonopt_end != -1) {
260 permute_args(nonopt_start, nonopt_end,
262 nonopt_start = optind -
263 (nonopt_end - nonopt_start);
267 /* process next argument */
270 if (nonopt_start != -1 && nonopt_end == -1)
272 if (place[1] && *++place == '-') { /* found "--" */
277 if ((optchar = (int)*place++) == (int)':' ||
278 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
279 /* option letter unknown or ':' */
283 warnx(illoptchar, optchar);
287 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
288 /* XXX: what if no long options provided (called by getopt)? */
292 if (++optind >= nargc) { /* no arg */
295 warnx(recargchar, optchar);
298 } else /* white space */
299 place = nargv[optind];
301 * Handle -W arg the same as --arg (which causes getopt to
306 if (*++oli != ':') { /* doesn't take argument */
309 } else { /* takes (optional) argument */
311 if (*place) /* no white space */
313 /* XXX: disable test for :: if PC? (GNU doesn't) */
314 else if (oli[1] != ':') { /* arg not optional */
315 if (++optind >= nargc) { /* no arg */
318 warnx(recargchar, optchar);
322 optarg = nargv[optind];
327 /* dump back option letter */
331 #ifdef REPLACE_GETOPT
334 * Parse argc/argv argument vector.
336 * [eventually this will replace the real getopt]
339 getopt(nargc, nargv, options)
346 _DIAGASSERT(nargv != NULL);
347 _DIAGASSERT(options != NULL);
349 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
352 * We found an option (--), so if we skipped non-options,
353 * we have to permute.
355 if (nonopt_end != -1) {
356 permute_args(nonopt_start, nonopt_end, optind,
358 optind -= nonopt_end - nonopt_start;
360 nonopt_start = nonopt_end = -1;
369 * Parse argc/argv argument vector.
372 getopt_long(nargc, nargv, options, long_options, idx)
376 const struct option *long_options;
381 _DIAGASSERT(nargv != NULL);
382 _DIAGASSERT(options != NULL);
383 _DIAGASSERT(long_options != NULL);
384 /* idx may be NULL */
386 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
387 char *current_argv, *has_equal;
388 size_t current_argv_len;
391 current_argv = place;
397 if (*current_argv == '\0') { /* found "--" */
399 * We found an option (--), so if we skipped
400 * non-options, we have to permute.
402 if (nonopt_end != -1) {
403 permute_args(nonopt_start, nonopt_end,
405 optind -= nonopt_end - nonopt_start;
407 nonopt_start = nonopt_end = -1;
410 if ((has_equal = strchr(current_argv, '=')) != NULL) {
411 /* argument found (--option=arg) */
412 current_argv_len = has_equal - current_argv;
415 current_argv_len = strlen(current_argv);
417 for (i = 0; long_options[i].name; i++) {
418 /* find matching long option */
419 if (strncmp(current_argv, long_options[i].name,
423 if (strlen(long_options[i].name) ==
424 (unsigned)current_argv_len) {
429 if (match == -1) /* partial match */
432 /* ambiguous abbreviation */
434 warnx(ambig, (int)current_argv_len,
440 if (match != -1) { /* option found */
441 if (long_options[match].has_arg == no_argument
444 warnx(noarg, (int)current_argv_len,
447 * XXX: GNU sets optopt to val regardless of
450 if (long_options[match].flag == NULL)
451 optopt = long_options[match].val;
456 if (long_options[match].has_arg == required_argument ||
457 long_options[match].has_arg == optional_argument) {
460 else if (long_options[match].has_arg ==
463 * optional argument doesn't use
466 optarg = nargv[optind++];
469 if ((long_options[match].has_arg == required_argument)
470 && (optarg == NULL)) {
472 * Missing argument; leading ':'
473 * indicates no error should be generated
476 warnx(recargstring, current_argv);
478 * XXX: GNU sets optopt to val regardless
481 if (long_options[match].flag == NULL)
482 optopt = long_options[match].val;
488 } else { /* unknown option */
490 warnx(illoptstring, current_argv);
494 if (long_options[match].flag) {
495 *long_options[match].flag = long_options[match].val;
498 retval = long_options[match].val;