1 /* $NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 FILE_RCSID("@(#)$File: getopt_long.c,v 1.6 2009/02/13 18:48:05 christos Exp $")
45 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
53 #define REPLACE_GETOPT
56 #define _DIAGASSERT assert
61 __weak_alias(getopt,_getopt)
63 int opterr = 1; /* if error message should be printed */
64 int optind = 1; /* index into parent argv vector */
65 int optopt = '?'; /* character checked for validity */
66 int optreset; /* reset getopt */
67 char *optarg; /* argument associated with option */
68 #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
73 __weak_alias(getopt_long,_getopt_long)
76 #define IGNORE_FIRST (*options == '-' || *options == '+')
77 #define PRINT_ERROR ((opterr) && ((*options != ':') \
78 || (IGNORE_FIRST && options[1] != ':')))
79 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
80 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
81 /* XXX: GNU ignores PC if *options == '-' */
82 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
85 #define BADCH (int)'?'
86 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
87 || (*options == ':') ? (int)':' : (int)'?')
88 #define INORDER (int)1
92 static int getopt_internal(int, char **, const char *);
93 static int gcd(int, int);
94 static void permute_args(int, int, int, char **);
96 static const char *place = EMSG; /* option letter processing */
98 /* XXX: set optreset to 1 rather than these two */
99 static int nonopt_start = -1; /* first non option argument (for permute) */
100 static int nonopt_end = -1; /* first option after non options (for permute) */
103 static const char recargchar[] = "option requires an argument -- %c";
104 static const char recargstring[] = "option requires an argument -- %s";
105 static const char ambig[] = "ambiguous option -- %.*s";
106 static const char noarg[] = "option doesn't take an argument -- %.*s";
107 static const char illoptchar[] = "unknown option -- %c";
108 static const char illoptstring[] = "unknown option -- %s";
112 * Compute the greatest common divisor of a and b.
132 * Exchange the block from nonopt_start to nonopt_end with the block
133 * from nonopt_end to opt_end (keeping the same order of arguments
137 permute_args(panonopt_start, panonopt_end, opt_end, nargv)
143 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
146 _DIAGASSERT(nargv != NULL);
149 * compute lengths of blocks and number and size of cycles
151 nnonopts = panonopt_end - panonopt_start;
152 nopts = opt_end - panonopt_end;
153 ncycle = gcd(nnonopts, nopts);
154 cyclelen = (opt_end - panonopt_start) / ncycle;
156 for (i = 0; i < ncycle; i++) {
157 cstart = panonopt_end+i;
159 for (j = 0; j < cyclelen; j++) {
160 if (pos >= panonopt_end)
165 nargv[pos] = nargv[cstart];
166 nargv[cstart] = swap;
173 * Parse argc/argv argument vector. Called by user level routines.
174 * Returns -2 if -- is found (can be long option or end of options marker).
177 getopt_internal(nargc, nargv, options)
182 char *oli; /* option letter list index */
185 _DIAGASSERT(nargv != NULL);
186 _DIAGASSERT(options != NULL);
191 * XXX Some programs (like rsyncd) expect to be able to
192 * XXX re-initialize optind to 0 and have getopt_long(3)
193 * XXX properly function again. Work around this braindamage.
199 nonopt_start = nonopt_end = -1;
201 if (optreset || !*place) { /* update scanning pointer */
203 if (optind >= nargc) { /* end of argument vector */
205 if (nonopt_end != -1) {
206 /* do permutation, if we have to */
207 permute_args(nonopt_start, nonopt_end,
209 optind -= nonopt_end - nonopt_start;
211 else if (nonopt_start != -1) {
213 * If we skipped non-options, set optind
214 * to the first of them.
216 optind = nonopt_start;
218 nonopt_start = nonopt_end = -1;
221 if ((*(place = nargv[optind]) != '-')
222 || (place[1] == '\0')) { /* found non-option */
227 * return non-option as argument to option 1
229 optarg = nargv[optind++];
234 * if no permutation wanted, stop parsing
235 * at first non-option
240 if (nonopt_start == -1)
241 nonopt_start = optind;
242 else if (nonopt_end != -1) {
243 permute_args(nonopt_start, nonopt_end,
245 nonopt_start = optind -
246 (nonopt_end - nonopt_start);
250 /* process next argument */
253 if (nonopt_start != -1 && nonopt_end == -1)
255 if (place[1] && *++place == '-') { /* found "--" */
260 if ((optchar = (int)*place++) == (int)':' ||
261 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
262 /* option letter unknown or ':' */
266 warnx(illoptchar, optchar);
270 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
271 /* XXX: what if no long options provided (called by getopt)? */
275 if (++optind >= nargc) { /* no arg */
278 warnx(recargchar, optchar);
281 } else /* white space */
282 place = nargv[optind];
284 * Handle -W arg the same as --arg (which causes getopt to
289 if (*++oli != ':') { /* doesn't take argument */
292 } else { /* takes (optional) argument */
294 if (*place) /* no white space */
295 optarg = (char *)place;
296 /* XXX: disable test for :: if PC? (GNU doesn't) */
297 else if (oli[1] != ':') { /* arg not optional */
298 if (++optind >= nargc) { /* no arg */
301 warnx(recargchar, optchar);
305 optarg = nargv[optind];
310 /* dump back option letter */
314 #ifdef REPLACE_GETOPT
317 * Parse argc/argv argument vector.
319 * [eventually this will replace the real getopt]
322 getopt(nargc, nargv, options)
329 _DIAGASSERT(nargv != NULL);
330 _DIAGASSERT(options != NULL);
332 retval = getopt_internal(nargc, (char **)nargv, options);
336 * We found an option (--), so if we skipped non-options,
337 * we have to permute.
339 if (nonopt_end != -1) {
340 permute_args(nonopt_start, nonopt_end, optind,
342 optind -= nonopt_end - nonopt_start;
344 nonopt_start = nonopt_end = -1;
353 * Parse argc/argv argument vector.
356 getopt_long(nargc, nargv, options, long_options, idx)
360 const struct option *long_options;
365 #define IDENTICAL_INTERPRETATION(_x, _y) \
366 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
367 long_options[(_x)].flag == long_options[(_y)].flag && \
368 long_options[(_x)].val == long_options[(_y)].val)
370 _DIAGASSERT(nargv != NULL);
371 _DIAGASSERT(options != NULL);
372 _DIAGASSERT(long_options != NULL);
373 /* idx may be NULL */
375 retval = getopt_internal(nargc, (char **)nargv, options);
377 char *current_argv, *has_equal;
378 size_t current_argv_len;
379 int i, ambiguous, match;
381 current_argv = (char *)place;
388 if (*current_argv == '\0') { /* found "--" */
390 * We found an option (--), so if we skipped
391 * non-options, we have to permute.
393 if (nonopt_end != -1) {
394 permute_args(nonopt_start, nonopt_end,
395 optind, (char **)nargv);
396 optind -= nonopt_end - nonopt_start;
398 nonopt_start = nonopt_end = -1;
401 if ((has_equal = strchr(current_argv, '=')) != NULL) {
402 /* argument found (--option=arg) */
403 current_argv_len = has_equal - current_argv;
406 current_argv_len = strlen(current_argv);
408 for (i = 0; long_options[i].name; i++) {
409 /* find matching long option */
410 if (strncmp(current_argv, long_options[i].name,
414 if (strlen(long_options[i].name) ==
415 (unsigned)current_argv_len) {
421 if (match == -1) /* partial match */
423 else if (!IDENTICAL_INTERPRETATION(i, match))
427 /* ambiguous abbreviation */
429 warnx(ambig, (int)current_argv_len,
434 if (match != -1) { /* option found */
435 if (long_options[match].has_arg == no_argument
438 warnx(noarg, (int)current_argv_len,
441 * XXX: GNU sets optopt to val regardless of
444 if (long_options[match].flag == NULL)
445 optopt = long_options[match].val;
450 if (long_options[match].has_arg == required_argument ||
451 long_options[match].has_arg == optional_argument) {
454 else if (long_options[match].has_arg ==
457 * optional argument doesn't use
460 optarg = nargv[optind++];
463 if ((long_options[match].has_arg == required_argument)
464 && (optarg == NULL)) {
466 * Missing argument; leading ':'
467 * indicates no error should be generated
470 warnx(recargstring, current_argv);
472 * XXX: GNU sets optopt to val regardless
475 if (long_options[match].flag == NULL)
476 optopt = long_options[match].val;
482 } else { /* unknown option */
484 warnx(illoptstring, current_argv);
488 if (long_options[match].flag) {
489 *long_options[match].flag = long_options[match].val;
492 retval = long_options[match].val;
497 #undef IDENTICAL_INTERPRETATION