1 /* $NetBSD: getopt_long.c,v 1.27 2015/09/01 19:39:57 kamil 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.27 2015/09/01 19:39:57 kamil 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(int nargc
, char * const *nargv
, const char *options
)
314 _DIAGASSERT(nargv
!= NULL
);
315 _DIAGASSERT(options
!= NULL
);
317 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
321 * We found an option (--), so if we skipped non-options,
322 * we have to permute.
324 if (nonopt_end
!= -1) {
325 permute_args(nonopt_start
, nonopt_end
, optind
,
327 optind
-= nonopt_end
- nonopt_start
;
329 nonopt_start
= nonopt_end
= -1;
338 * Parse argc/argv argument vector.
341 getopt_long(int nargc
, char * const *nargv
, const char *options
,
342 const struct option
*long_options
, int *idx
)
346 #define IDENTICAL_INTERPRETATION(_x, _y) \
347 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
348 long_options[(_x)].flag == long_options[(_y)].flag && \
349 long_options[(_x)].val == long_options[(_y)].val)
351 _DIAGASSERT(nargv
!= NULL
);
352 _DIAGASSERT(options
!= NULL
);
353 _DIAGASSERT(long_options
!= NULL
);
354 /* idx may be NULL */
356 retval
= getopt_internal(nargc
, __UNCONST(nargv
), options
);
358 char *current_argv
, *has_equal
;
359 size_t current_argv_len
;
360 int i
, ambiguous
, match
;
362 current_argv
= __UNCONST(place
);
369 if (*current_argv
== '\0') { /* found "--" */
371 * We found an option (--), so if we skipped
372 * non-options, we have to permute.
374 if (nonopt_end
!= -1) {
375 permute_args(nonopt_start
, nonopt_end
,
376 optind
, __UNCONST(nargv
));
377 optind
-= nonopt_end
- nonopt_start
;
379 nonopt_start
= nonopt_end
= -1;
382 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
383 /* argument found (--option=arg) */
384 current_argv_len
= has_equal
- current_argv
;
387 current_argv_len
= strlen(current_argv
);
389 for (i
= 0; long_options
[i
].name
; i
++) {
390 /* find matching long option */
391 if (strncmp(current_argv
, long_options
[i
].name
,
395 if (strlen(long_options
[i
].name
) ==
396 (unsigned)current_argv_len
) {
402 if (match
== -1) /* partial match */
404 else if (!IDENTICAL_INTERPRETATION(i
, match
))
408 /* ambiguous abbreviation */
410 warnx(ambig
, (int)current_argv_len
,
415 if (match
!= -1) { /* option found */
416 if (long_options
[match
].has_arg
== no_argument
419 warnx(noarg
, (int)current_argv_len
,
422 * XXX: GNU sets optopt to val regardless of
425 if (long_options
[match
].flag
== NULL
)
426 optopt
= long_options
[match
].val
;
431 if (long_options
[match
].has_arg
== required_argument
||
432 long_options
[match
].has_arg
== optional_argument
) {
435 else if (long_options
[match
].has_arg
==
438 * optional argument doesn't use
441 optarg
= nargv
[optind
++];
444 if ((long_options
[match
].has_arg
== required_argument
)
445 && (optarg
== NULL
)) {
447 * Missing argument; leading ':'
448 * indicates no error should be generated
451 warnx(recargstring
, current_argv
);
453 * XXX: GNU sets optopt to val regardless
456 if (long_options
[match
].flag
== NULL
)
457 optopt
= long_options
[match
].val
;
463 } else { /* unknown option */
465 warnx(illoptstring
, current_argv
);
469 if (long_options
[match
].flag
) {
470 *long_options
[match
].flag
= long_options
[match
].val
;
473 retval
= long_options
[match
].val
;
478 #undef IDENTICAL_INTERPRETATION