2 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Dieter Baron and Thomas Klausner.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
44 /* Windows needs warnx(). We change the definition though:
45 * 1. (another) global is defined, opterrmsg, which holds the error message
46 * 2. errors are always printed out on stderr w/o the program name
47 * Note that opterrmsg always gets set no matter what opterr is set to. The
48 * error message will not be printed if opterr is 0 as usual.
54 extern char opterrmsg[128];
55 char opterrmsg[128]; /* last error message is stored here */
57 static void warnx(int print_error, const char *fmt, ...)
62 _vsnprintf(opterrmsg, 128, fmt, ap);
67 fprintf(stderr, opterrmsg);
68 fprintf(stderr, "\n");
74 /* not part of the original file */
76 #define _DIAGASSERT(X)
79 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
80 #define REPLACE_GETOPT
83 int opterr = 1; /* if error message should be printed */
84 int optind = 1; /* index into parent argv vector */
85 int optopt = '?'; /* character checked for validity */
86 int optreset; /* reset getopt */
87 char *optarg; /* argument associated with option */
90 #define IGNORE_FIRST (*options == '-' || *options == '+')
91 #define PRINT_ERROR ((opterr) && ((*options != ':') \
92 || (IGNORE_FIRST && options[1] != ':')))
93 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
94 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
95 /* XXX: GNU ignores PC if *options == '-' */
96 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
99 #define BADCH (int)'?'
100 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
101 || (*options == ':') ? (int)':' : (int)'?')
102 #define INORDER (int)1
106 static int getopt_internal(int, char * const *, const char *);
107 static int gcd(int, int);
108 static void permute_args(int, int, int, char * const *);
110 static char *place = EMSG; /* option letter processing */
112 /* XXX: set optreset to 1 rather than these two */
113 static int nonopt_start = -1; /* first non option argument (for permute) */
114 static int nonopt_end = -1; /* first option after non options (for permute) */
117 static const char recargchar[] = "option requires an argument -- %c";
118 static const char recargstring[] = "option requires an argument -- %s";
119 static const char ambig[] = "ambiguous option -- %.*s";
120 static const char noarg[] = "option doesn't take an argument -- %.*s";
121 static const char illoptchar[] = "unknown option -- %c";
122 static const char illoptstring[] = "unknown option -- %s";
126 * Compute the greatest common divisor of a and b.
144 * Exchange the block from nonopt_start to nonopt_end with the block
145 * from nonopt_end to opt_end (keeping the same order of arguments
149 permute_args(int panonopt_start, int panonopt_end,
150 int opt_end, char * const *nargv)
152 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
155 _DIAGASSERT(nargv != NULL);
158 * compute lengths of blocks and number and size of cycles
160 nnonopts = panonopt_end - panonopt_start;
161 nopts = opt_end - panonopt_end;
162 ncycle = gcd(nnonopts, nopts);
163 cyclelen = (opt_end - panonopt_start) / ncycle;
165 for (i = 0; i < ncycle; i++) {
166 cstart = panonopt_end+i;
168 for (j = 0; j < cyclelen; j++) {
169 if (pos >= panonopt_end)
174 /* LINTED const cast */
175 ((char **) nargv)[pos] = nargv[cstart];
176 /* LINTED const cast */
177 ((char **)nargv)[cstart] = swap;
184 * Parse argc/argv argument vector. Called by user level routines.
185 * Returns -2 if -- is found (can be long option or end of options marker).
188 getopt_internal(int nargc, char * const *nargv, const char *options)
190 char *oli; /* option letter list index */
193 _DIAGASSERT(nargv != NULL);
194 _DIAGASSERT(options != NULL);
199 * XXX Some programs (like rsyncd) expect to be able to
200 * XXX re-initialize optind to 0 and have getopt_long(3)
201 * XXX properly function again. Work around this braindamage.
207 nonopt_start = nonopt_end = -1;
209 if (optreset || !*place) { /* update scanning pointer */
211 if (optind >= nargc) { /* end of argument vector */
213 if (nonopt_end != -1) {
214 /* do permutation, if we have to */
215 permute_args(nonopt_start, nonopt_end,
217 optind -= nonopt_end - nonopt_start;
219 else if (nonopt_start != -1) {
221 * If we skipped non-options, set optind
222 * to the first of them.
224 optind = nonopt_start;
226 nonopt_start = nonopt_end = -1;
229 if ((*(place = nargv[optind]) != '-')
230 || (place[1] == '\0')) { /* found non-option */
235 * return non-option as argument to option 1
237 optarg = nargv[optind++];
242 * if no permutation wanted, stop parsing
243 * at first non-option
248 if (nonopt_start == -1)
249 nonopt_start = optind;
250 else if (nonopt_end != -1) {
251 permute_args(nonopt_start, nonopt_end,
253 nonopt_start = optind -
254 (nonopt_end - nonopt_start);
258 /* process next argument */
261 if (nonopt_start != -1 && nonopt_end == -1)
263 if (place[1] && *++place == '-') { /* found "--" */
268 if ((optchar = (int)*place++) == (int)':' ||
269 (oli = (char *) strchr(options + (IGNORE_FIRST ? 1 : 0),
271 /* option letter unknown or ':' */
276 warnx(illoptchar, optchar);
278 warnx(PRINT_ERROR, illoptchar, optchar);
283 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
284 /* XXX: what if no long options provided (called by getopt)? */
288 if (++optind >= nargc) { /* no arg */
292 warnx(recargchar, optchar);
294 warnx(PRINT_ERROR, 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 */
319 warnx(recargchar, optchar);
321 warnx(PRINT_ERROR, recargchar, optchar);
326 optarg = nargv[optind];
331 /* dump back option letter */
337 * Parse argc/argv argument vector.
339 * [eventually this will replace the real getopt]
342 getopt(int nargc, char * const *nargv, const char *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;
368 * Parse argc/argv argument vector.
371 getopt_long(int nargc,
374 const struct option *long_options,
379 _DIAGASSERT(nargv != NULL);
380 _DIAGASSERT(options != NULL);
381 _DIAGASSERT(long_options != NULL);
382 /* idx may be NULL */
384 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
385 char *current_argv, *has_equal;
386 size_t current_argv_len;
389 current_argv = place;
395 if (*current_argv == '\0') { /* found "--" */
397 * We found an option (--), so if we skipped
398 * non-options, we have to permute.
400 if (nonopt_end != -1) {
401 permute_args(nonopt_start, nonopt_end,
403 optind -= nonopt_end - nonopt_start;
405 nonopt_start = nonopt_end = -1;
408 if ((has_equal = strchr(current_argv, '=')) != NULL) {
409 /* argument found (--option=arg) */
410 current_argv_len = has_equal - current_argv;
413 current_argv_len = strlen(current_argv);
415 for (i = 0; long_options[i].name; i++) {
416 /* find matching long option */
417 if (strncmp(current_argv, long_options[i].name,
421 if (strlen(long_options[i].name) ==
422 (unsigned)current_argv_len) {
427 if (match == -1) /* partial match */
430 /* ambiguous abbreviation */
433 warnx(ambig, (int)current_argv_len,
436 warnx(PRINT_ERROR, ambig, (int)current_argv_len,
443 if (match != -1) { /* option found */
444 if (long_options[match].has_arg == no_argument
448 warnx(noarg, (int)current_argv_len,
451 warnx(PRINT_ERROR, noarg, (int)current_argv_len,
455 * XXX: GNU sets optopt to val regardless of
458 if (long_options[match].flag == NULL)
459 optopt = long_options[match].val;
464 if (long_options[match].has_arg == required_argument ||
465 long_options[match].has_arg == optional_argument) {
468 else if (long_options[match].has_arg ==
471 * optional argument doesn't use
474 optarg = nargv[optind++];
477 if ((long_options[match].has_arg == required_argument)
478 && (optarg == NULL)) {
480 * Missing argument; leading ':'
481 * indicates no error should be generated
485 warnx(recargstring, current_argv);
487 warnx(PRINT_ERROR, recargstring, current_argv);
490 * XXX: GNU sets optopt to val regardless
493 if (long_options[match].flag == NULL)
494 optopt = long_options[match].val;
500 } else { /* unknown option */
503 warnx(illoptstring, current_argv);
505 warnx(PRINT_ERROR, illoptstring, current_argv);
510 if (long_options[match].flag) {
511 *long_options[match].flag = long_options[match].val;
514 retval = long_options[match].val;
520 #endif /* !GETOPT_LONG */