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.
36 FILE_RCSID("@(#)$File: getopt_long.c,v 1.6 2009/02/13 18:48:05 christos Exp $")
49 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
57 #define REPLACE_GETOPT
60 #define _DIAGASSERT assert
65 __weak_alias(getopt
,_getopt
)
67 int opterr
= 1; /* if error message should be printed */
68 int optind
= 1; /* index into parent argv vector */
69 int optopt
= '?'; /* character checked for validity */
70 int optreset
; /* reset getopt */
71 char *optarg
; /* argument associated with option */
72 #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
77 __weak_alias(getopt_long
,_getopt_long
)
80 #define IGNORE_FIRST (*options == '-' || *options == '+')
81 #define PRINT_ERROR ((opterr) && ((*options != ':') \
82 || (IGNORE_FIRST && options[1] != ':')))
83 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
84 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
85 /* XXX: GNU ignores PC if *options == '-' */
86 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
89 #define BADCH (int)'?'
90 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
91 || (*options == ':') ? (int)':' : (int)'?')
92 #define INORDER (int)1
96 static int getopt_internal(int, char **, const char *);
97 static int gcd(int, int);
98 static void permute_args(int, int, int, char **);
100 static const char *place
= EMSG
; /* option letter processing */
102 /* XXX: set optreset to 1 rather than these two */
103 static int nonopt_start
= -1; /* first non option argument (for permute) */
104 static int nonopt_end
= -1; /* first option after non options (for permute) */
107 static const char recargchar
[] = "option requires an argument -- %c";
108 static const char recargstring
[] = "option requires an argument -- %s";
109 static const char ambig
[] = "ambiguous option -- %.*s";
110 static const char noarg
[] = "option doesn't take an argument -- %.*s";
111 static const char illoptchar
[] = "unknown option -- %c";
112 static const char illoptstring
[] = "unknown option -- %s";
116 * Compute the greatest common divisor of a and b.
136 * Exchange the block from nonopt_start to nonopt_end with the block
137 * from nonopt_end to opt_end (keeping the same order of arguments
141 permute_args(panonopt_start
, panonopt_end
, opt_end
, nargv
)
147 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
150 _DIAGASSERT(nargv
!= NULL
);
153 * compute lengths of blocks and number and size of cycles
155 nnonopts
= panonopt_end
- panonopt_start
;
156 nopts
= opt_end
- panonopt_end
;
157 ncycle
= gcd(nnonopts
, nopts
);
158 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
160 for (i
= 0; i
< ncycle
; i
++) {
161 cstart
= panonopt_end
+i
;
163 for (j
= 0; j
< cyclelen
; j
++) {
164 if (pos
>= panonopt_end
)
169 nargv
[pos
] = nargv
[cstart
];
170 nargv
[cstart
] = swap
;
177 * Parse argc/argv argument vector. Called by user level routines.
178 * Returns -2 if -- is found (can be long option or end of options marker).
181 getopt_internal(nargc
, nargv
, options
)
186 char *oli
; /* option letter list index */
189 _DIAGASSERT(nargv
!= NULL
);
190 _DIAGASSERT(options
!= NULL
);
195 * XXX Some programs (like rsyncd) expect to be able to
196 * XXX re-initialize optind to 0 and have getopt_long(3)
197 * XXX properly function again. Work around this braindamage.
203 nonopt_start
= nonopt_end
= -1;
205 if (optreset
|| !*place
) { /* update scanning pointer */
207 if (optind
>= nargc
) { /* end of argument vector */
209 if (nonopt_end
!= -1) {
210 /* do permutation, if we have to */
211 permute_args(nonopt_start
, nonopt_end
,
213 optind
-= nonopt_end
- nonopt_start
;
215 else if (nonopt_start
!= -1) {
217 * If we skipped non-options, set optind
218 * to the first of them.
220 optind
= nonopt_start
;
222 nonopt_start
= nonopt_end
= -1;
225 if ((*(place
= nargv
[optind
]) != '-')
226 || (place
[1] == '\0')) { /* found non-option */
231 * return non-option as argument to option 1
233 optarg
= nargv
[optind
++];
238 * if no permutation wanted, stop parsing
239 * at first non-option
244 if (nonopt_start
== -1)
245 nonopt_start
= optind
;
246 else if (nonopt_end
!= -1) {
247 permute_args(nonopt_start
, nonopt_end
,
249 nonopt_start
= optind
-
250 (nonopt_end
- nonopt_start
);
254 /* process next argument */
257 if (nonopt_start
!= -1 && nonopt_end
== -1)
259 if (place
[1] && *++place
== '-') { /* found "--" */
264 if ((optchar
= (int)*place
++) == (int)':' ||
265 (oli
= strchr(options
+ (IGNORE_FIRST
? 1 : 0), optchar
)) == NULL
) {
266 /* option letter unknown or ':' */
270 warnx(illoptchar
, optchar
);
274 if (optchar
== 'W' && oli
[1] == ';') { /* -W long-option */
275 /* XXX: what if no long options provided (called by getopt)? */
279 if (++optind
>= nargc
) { /* no arg */
282 warnx(recargchar
, optchar
);
285 } else /* white space */
286 place
= nargv
[optind
];
288 * Handle -W arg the same as --arg (which causes getopt to
293 if (*++oli
!= ':') { /* doesn't take argument */
296 } else { /* takes (optional) argument */
298 if (*place
) /* no white space */
299 optarg
= (char *)place
;
300 /* XXX: disable test for :: if PC? (GNU doesn't) */
301 else if (oli
[1] != ':') { /* arg not optional */
302 if (++optind
>= nargc
) { /* no arg */
305 warnx(recargchar
, optchar
);
309 optarg
= nargv
[optind
];
314 /* dump back option letter */
318 #ifdef REPLACE_GETOPT
321 * Parse argc/argv argument vector.
323 * [eventually this will replace the real getopt]
326 getopt(nargc
, nargv
, options
)
333 _DIAGASSERT(nargv
!= NULL
);
334 _DIAGASSERT(options
!= NULL
);
336 retval
= getopt_internal(nargc
, (char **)nargv
, options
);
340 * We found an option (--), so if we skipped non-options,
341 * we have to permute.
343 if (nonopt_end
!= -1) {
344 permute_args(nonopt_start
, nonopt_end
, optind
,
346 optind
-= nonopt_end
- nonopt_start
;
348 nonopt_start
= nonopt_end
= -1;
357 * Parse argc/argv argument vector.
360 getopt_long(nargc
, nargv
, options
, long_options
, idx
)
364 const struct option
*long_options
;
369 #define IDENTICAL_INTERPRETATION(_x, _y) \
370 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
371 long_options[(_x)].flag == long_options[(_y)].flag && \
372 long_options[(_x)].val == long_options[(_y)].val)
374 _DIAGASSERT(nargv
!= NULL
);
375 _DIAGASSERT(options
!= NULL
);
376 _DIAGASSERT(long_options
!= NULL
);
377 /* idx may be NULL */
379 retval
= getopt_internal(nargc
, (char **)nargv
, options
);
381 char *current_argv
, *has_equal
;
382 size_t current_argv_len
;
383 int i
, ambiguous
, match
;
385 current_argv
= (char *)place
;
392 if (*current_argv
== '\0') { /* found "--" */
394 * We found an option (--), so if we skipped
395 * non-options, we have to permute.
397 if (nonopt_end
!= -1) {
398 permute_args(nonopt_start
, nonopt_end
,
399 optind
, (char **)nargv
);
400 optind
-= nonopt_end
- nonopt_start
;
402 nonopt_start
= nonopt_end
= -1;
405 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
406 /* argument found (--option=arg) */
407 current_argv_len
= has_equal
- current_argv
;
410 current_argv_len
= strlen(current_argv
);
412 for (i
= 0; long_options
[i
].name
; i
++) {
413 /* find matching long option */
414 if (strncmp(current_argv
, long_options
[i
].name
,
418 if (strlen(long_options
[i
].name
) ==
419 (unsigned)current_argv_len
) {
425 if (match
== -1) /* partial match */
427 else if (!IDENTICAL_INTERPRETATION(i
, match
))
431 /* ambiguous abbreviation */
433 warnx(ambig
, (int)current_argv_len
,
438 if (match
!= -1) { /* option found */
439 if (long_options
[match
].has_arg
== no_argument
442 warnx(noarg
, (int)current_argv_len
,
445 * XXX: GNU sets optopt to val regardless of
448 if (long_options
[match
].flag
== NULL
)
449 optopt
= long_options
[match
].val
;
454 if (long_options
[match
].has_arg
== required_argument
||
455 long_options
[match
].has_arg
== optional_argument
) {
458 else if (long_options
[match
].has_arg
==
461 * optional argument doesn't use
464 optarg
= nargv
[optind
++];
467 if ((long_options
[match
].has_arg
== required_argument
)
468 && (optarg
== NULL
)) {
470 * Missing argument; leading ':'
471 * indicates no error should be generated
474 warnx(recargstring
, current_argv
);
476 * XXX: GNU sets optopt to val regardless
479 if (long_options
[match
].flag
== NULL
)
480 optopt
= long_options
[match
].val
;
486 } else { /* unknown option */
488 warnx(illoptstring
, current_argv
);
492 if (long_options
[match
].flag
) {
493 *long_options
[match
].flag
= long_options
[match
].val
;
496 retval
= long_options
[match
].val
;
501 #undef IDENTICAL_INTERPRETATION