1 /* $NetBSD: getopt_long.c,v 1.25 2009/03/20 14:05:54 joerg 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.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: getopt_long.c,v 1.25 2009/03/20 14:05:54 joerg Exp $");
39 #include "namespace.h"
44 #if HAVE_NBTOOL_CONFIG_H
45 #include "compat_getopt.h"
52 #if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
53 #define REPLACE_GETOPT
58 __weak_alias(getopt
,_getopt
)
60 int opterr
= 1; /* if error message should be printed */
61 int optind
= 1; /* index into parent argv vector */
62 int optopt
= '?'; /* character checked for validity */
63 int optreset
; /* reset getopt */
64 char *optarg
; /* argument associated with option */
65 #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
70 __weak_alias(getopt_long
,_getopt_long
)
73 #define IGNORE_FIRST (*options == '-' || *options == '+')
74 #define PRINT_ERROR ((opterr) && ((*options != ':') \
75 || (IGNORE_FIRST && options[1] != ':')))
76 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
77 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
78 /* XXX: GNU ignores PC if *options == '-' */
79 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
82 #define BADCH (int)'?'
83 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
84 || (*options == ':') ? (int)':' : (int)'?')
85 #define INORDER (int)1
89 static int getopt_internal(int, char **, const char *);
90 static int gcd(int, int);
91 static void permute_args(int, int, int, char **);
93 static const char *place
= EMSG
; /* option letter processing */
95 /* XXX: set optreset to 1 rather than these two */
96 static int nonopt_start
= -1; /* first non option argument (for permute) */
97 static int nonopt_end
= -1; /* first option after non options (for permute) */
100 static const char recargchar
[] = "option requires an argument -- %c";
101 static const char recargstring
[] = "option requires an argument -- %s";
102 static const char ambig
[] = "ambiguous option -- %.*s";
103 static const char noarg
[] = "option doesn't take an argument -- %.*s";
104 static const char illoptchar
[] = "unknown option -- %c";
105 static const char illoptstring
[] = "unknown option -- %s";
109 * Compute the greatest common divisor of a and b.
127 * Exchange the block from nonopt_start to nonopt_end with the block
128 * from nonopt_end to opt_end (keeping the same order of arguments
132 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
, char **nargv
)
134 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
137 _DIAGASSERT(nargv
!= NULL
);
140 * compute lengths of blocks and number and size of cycles
142 nnonopts
= panonopt_end
- panonopt_start
;
143 nopts
= opt_end
- panonopt_end
;
144 ncycle
= gcd(nnonopts
, nopts
);
145 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
147 for (i
= 0; i
< ncycle
; i
++) {
148 cstart
= panonopt_end
+i
;
150 for (j
= 0; j
< cyclelen
; j
++) {
151 if (pos
>= panonopt_end
)
156 nargv
[pos
] = nargv
[cstart
];
157 nargv
[cstart
] = swap
;
164 * Parse argc/argv argument vector. Called by user level routines.
165 * Returns -2 if -- is found (can be long option or end of options marker).
168 getopt_internal(int nargc
, char **nargv
, const char *options
)
170 char *oli
; /* option letter list index */
173 _DIAGASSERT(nargv
!= NULL
);
174 _DIAGASSERT(options
!= NULL
);
179 * XXX Some programs (like rsyncd) expect to be able to
180 * XXX re-initialize optind to 0 and have getopt_long(3)
181 * XXX properly function again. Work around this braindamage.
187 nonopt_start
= nonopt_end
= -1;
189 if (optreset
|| !*place
) { /* update scanning pointer */
191 if (optind
>= nargc
) { /* end of argument vector */
193 if (nonopt_end
!= -1) {
194 /* do permutation, if we have to */
195 permute_args(nonopt_start
, nonopt_end
,
197 optind
-= nonopt_end
- nonopt_start
;
199 else if (nonopt_start
!= -1) {
201 * If we skipped non-options, set optind
202 * to the first of them.
204 optind
= nonopt_start
;
206 nonopt_start
= nonopt_end
= -1;
209 if ((*(place
= nargv
[optind
]) != '-')
210 || (place
[1] == '\0')) { /* found non-option */
215 * return non-option as argument to option 1
217 optarg
= nargv
[optind
++];
222 * if no permutation wanted, stop parsing
223 * at first non-option
228 if (nonopt_start
== -1)
229 nonopt_start
= optind
;
230 else if (nonopt_end
!= -1) {
231 permute_args(nonopt_start
, nonopt_end
,
233 nonopt_start
= optind
-
234 (nonopt_end
- nonopt_start
);
238 /* process next argument */
241 if (nonopt_start
!= -1 && nonopt_end
== -1)
243 if (place
[1] && *++place
== '-') { /* found "--" */
248 if ((optchar
= (int)*place
++) == (int)':' ||
249 (oli
= strchr(options
+ (IGNORE_FIRST
? 1 : 0), optchar
)) == NULL
) {
250 /* option letter unknown or ':' */
254 warnx(illoptchar
, optchar
);
258 if (optchar
== 'W' && oli
[1] == ';') { /* -W long-option */
259 /* XXX: what if no long options provided (called by getopt)? */
263 if (++optind
>= nargc
) { /* no arg */
266 warnx(recargchar
, optchar
);
269 } else /* white space */
270 place
= nargv
[optind
];
272 * Handle -W arg the same as --arg (which causes getopt to
277 if (*++oli
!= ':') { /* doesn't take argument */
280 } else { /* takes (optional) argument */
282 if (*place
) /* no white space */
283 optarg
= __UNCONST(place
);
284 /* XXX: disable test for :: if PC? (GNU doesn't) */
285 else if (oli
[1] != ':') { /* arg not optional */
286 if (++optind
>= nargc
) { /* no arg */
289 warnx(recargchar
, optchar
);
293 optarg
= nargv
[optind
];
298 /* dump back option letter */
302 #ifdef REPLACE_GETOPT
305 * Parse argc/argv argument vector.
307 * [eventually this will replace the real getopt]
310 getopt(nargc
, nargv
, options
)
317 _DIAGASSERT(nargv
!= NULL
);
318 _DIAGASSERT(options
!= NULL
);
320 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
324 * We found an option (--), so if we skipped non-options,
325 * we have to permute.
327 if (nonopt_end
!= -1) {
328 permute_args(nonopt_start
, nonopt_end
, optind
,
330 optind
-= nonopt_end
- nonopt_start
;
332 nonopt_start
= nonopt_end
= -1;
341 * Parse argc/argv argument vector.
344 getopt_long(int nargc
, char * const *nargv
, const char *options
,
345 const struct option
*long_options
, int *idx
)
349 #define IDENTICAL_INTERPRETATION(_x, _y) \
350 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
351 long_options[(_x)].flag == long_options[(_y)].flag && \
352 long_options[(_x)].val == long_options[(_y)].val)
354 _DIAGASSERT(nargv
!= NULL
);
355 _DIAGASSERT(options
!= NULL
);
356 _DIAGASSERT(long_options
!= NULL
);
357 /* idx may be NULL */
359 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
361 char *current_argv
, *has_equal
;
362 size_t current_argv_len
;
363 int i
, ambiguous
, match
;
365 current_argv
= __UNCONST(place
);
372 if (*current_argv
== '\0') { /* found "--" */
374 * We found an option (--), so if we skipped
375 * non-options, we have to permute.
377 if (nonopt_end
!= -1) {
378 permute_args(nonopt_start
, nonopt_end
,
379 optind
, __UNCONST(nargv
));
380 optind
-= nonopt_end
- nonopt_start
;
382 nonopt_start
= nonopt_end
= -1;
385 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
386 /* argument found (--option=arg) */
387 current_argv_len
= has_equal
- current_argv
;
390 current_argv_len
= strlen(current_argv
);
392 for (i
= 0; long_options
[i
].name
; i
++) {
393 /* find matching long option */
394 if (strncmp(current_argv
, long_options
[i
].name
,
398 if (strlen(long_options
[i
].name
) ==
399 (unsigned)current_argv_len
) {
405 if (match
== -1) /* partial match */
407 else if (!IDENTICAL_INTERPRETATION(i
, match
))
411 /* ambiguous abbreviation */
413 warnx(ambig
, (int)current_argv_len
,
418 if (match
!= -1) { /* option found */
419 if (long_options
[match
].has_arg
== no_argument
422 warnx(noarg
, (int)current_argv_len
,
425 * XXX: GNU sets optopt to val regardless of
428 if (long_options
[match
].flag
== NULL
)
429 optopt
= long_options
[match
].val
;
434 if (long_options
[match
].has_arg
== required_argument
||
435 long_options
[match
].has_arg
== optional_argument
) {
438 else if (long_options
[match
].has_arg
==
441 * optional argument doesn't use
444 optarg
= nargv
[optind
++];
447 if ((long_options
[match
].has_arg
== required_argument
)
448 && (optarg
== NULL
)) {
450 * Missing argument; leading ':'
451 * indicates no error should be generated
454 warnx(recargstring
, current_argv
);
456 * XXX: GNU sets optopt to val regardless
459 if (long_options
[match
].flag
== NULL
)
460 optopt
= long_options
[match
].val
;
466 } else { /* unknown option */
468 warnx(illoptstring
, current_argv
);
472 if (long_options
[match
].flag
) {
473 *long_options
[match
].flag
= long_options
[match
].val
;
476 retval
= long_options
[match
].val
;
481 #undef IDENTICAL_INTERPRETATION