]>
git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/getopt_long.c
1 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
2 /* $FreeBSD: src/lib/libc/stdlib/getopt_long.c,v 1.2 2002/10/16 22:18:42 alfred Exp $ */
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Dieter Baron and Thomas Klausner.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 __RCSID("$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $");
43 #endif /* LIBC_SCCS and not lint */
45 #include "namespace.h"
54 /* not part of the original file */
56 #define _DIAGASSERT(X)
59 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
60 #define REPLACE_GETOPT
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_CONFIG_H && !HAVE_DECL_OPTRESET
77 __weak_alias(getopt_long
,_getopt_long
)
81 #define IGNORE_FIRST (*options == '-' || *options == '+')
82 #define PRINT_ERROR ((opterr) && ((*options != ':') \
83 || (IGNORE_FIRST && options[1] != ':')))
84 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
85 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
86 /* XXX: GNU ignores PC if *options == '-' */
87 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
90 #define BADCH (int)'?'
91 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
92 || (*options == ':') ? (int)':' : (int)'?')
93 #define INORDER (int)1
97 static int getopt_internal(int, char * const *, const char *);
98 static int gcd(int, int);
99 static void permute_args(int, int, int, char * const *);
101 static char *place
= EMSG
; /* option letter processing */
103 /* XXX: set optreset to 1 rather than these two */
104 static int nonopt_start
= -1; /* first non option argument (for permute) */
105 static int nonopt_end
= -1; /* first option after non options (for permute) */
108 static const char recargchar
[] = "option requires an argument -- %c";
109 static const char recargstring
[] = "option requires an argument -- %s";
110 static const char ambig
[] = "ambiguous option -- %.*s";
111 static const char noarg
[] = "option doesn't take an argument -- %.*s";
112 static const char illoptchar
[] = "unknown option -- %c";
113 static const char illoptstring
[] = "unknown option -- %s";
117 * Compute the greatest common divisor of a and b.
137 * Exchange the block from nonopt_start to nonopt_end with the block
138 * from nonopt_end to opt_end (keeping the same order of arguments
142 permute_args(panonopt_start
, panonopt_end
, opt_end
, nargv
)
148 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
151 _DIAGASSERT(nargv
!= NULL
);
154 * compute lengths of blocks and number and size of cycles
156 nnonopts
= panonopt_end
- panonopt_start
;
157 nopts
= opt_end
- panonopt_end
;
158 ncycle
= gcd(nnonopts
, nopts
);
159 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
161 for (i
= 0; i
< ncycle
; i
++) {
162 cstart
= panonopt_end
+i
;
164 for (j
= 0; j
< cyclelen
; j
++) {
165 if (pos
>= panonopt_end
)
170 /* LINTED const cast */
171 ((char **) nargv
)[pos
] = nargv
[cstart
];
172 /* LINTED const cast */
173 ((char **)nargv
)[cstart
] = swap
;
180 * Parse argc/argv argument vector. Called by user level routines.
181 * Returns -2 if -- is found (can be long option or end of options marker).
184 getopt_internal(nargc
, nargv
, options
)
189 char *oli
; /* option letter list index */
192 _DIAGASSERT(nargv
!= NULL
);
193 _DIAGASSERT(options
!= NULL
);
198 * XXX Some programs (like rsyncd) expect to be able to
199 * XXX re-initialize optind to 0 and have getopt_long(3)
200 * XXX properly function again. Work around this braindamage.
206 nonopt_start
= nonopt_end
= -1;
208 if (optreset
|| !*place
) { /* update scanning pointer */
210 if (optind
>= nargc
) { /* end of argument vector */
212 if (nonopt_end
!= -1) {
213 /* do permutation, if we have to */
214 permute_args(nonopt_start
, nonopt_end
,
216 optind
-= nonopt_end
- nonopt_start
;
218 else if (nonopt_start
!= -1) {
220 * If we skipped non-options, set optind
221 * to the first of them.
223 optind
= nonopt_start
;
225 nonopt_start
= nonopt_end
= -1;
228 if ((*(place
= nargv
[optind
]) != '-')
229 || (place
[1] == '\0')) { /* found non-option */
234 * return non-option as argument to option 1
236 optarg
= nargv
[optind
++];
241 * if no permutation wanted, stop parsing
242 * at first non-option
247 if (nonopt_start
== -1)
248 nonopt_start
= optind
;
249 else if (nonopt_end
!= -1) {
250 permute_args(nonopt_start
, nonopt_end
,
252 nonopt_start
= optind
-
253 (nonopt_end
- nonopt_start
);
257 /* process next argument */
260 if (nonopt_start
!= -1 && nonopt_end
== -1)
262 if (place
[1] && *++place
== '-') { /* found "--" */
267 if ((optchar
= (int)*place
++) == (int)':' ||
268 (oli
= strchr(options
+ (IGNORE_FIRST
? 1 : 0), optchar
)) == NULL
) {
269 /* option letter unknown or ':' */
273 warnx(illoptchar
, optchar
);
277 if (optchar
== 'W' && oli
[1] == ';') { /* -W long-option */
278 /* XXX: what if no long options provided (called by getopt)? */
282 if (++optind
>= nargc
) { /* no arg */
285 warnx(recargchar
, optchar
);
288 } else /* white space */
289 place
= nargv
[optind
];
291 * Handle -W arg the same as --arg (which causes getopt to
296 if (*++oli
!= ':') { /* doesn't take argument */
299 } else { /* takes (optional) argument */
301 if (*place
) /* no white space */
303 /* XXX: disable test for :: if PC? (GNU doesn't) */
304 else if (oli
[1] != ':') { /* arg not optional */
305 if (++optind
>= nargc
) { /* no arg */
308 warnx(recargchar
, optchar
);
312 optarg
= nargv
[optind
];
317 /* dump back option letter */
321 #ifdef REPLACE_GETOPT
324 * Parse argc/argv argument vector.
326 * [eventually this will replace the real getopt]
329 getopt(nargc
, nargv
, options
)
336 _DIAGASSERT(nargv
!= NULL
);
337 _DIAGASSERT(options
!= NULL
);
339 if ((retval
= getopt_internal(nargc
, nargv
, options
)) == -2) {
342 * We found an option (--), so if we skipped non-options,
343 * we have to permute.
345 if (nonopt_end
!= -1) {
346 permute_args(nonopt_start
, nonopt_end
, optind
,
348 optind
-= nonopt_end
- nonopt_start
;
350 nonopt_start
= nonopt_end
= -1;
359 * Parse argc/argv argument vector.
362 getopt_long(nargc
, nargv
, options
, long_options
, idx
)
366 const struct option
*long_options
;
371 _DIAGASSERT(nargv
!= NULL
);
372 _DIAGASSERT(options
!= NULL
);
373 _DIAGASSERT(long_options
!= NULL
);
374 /* idx may be NULL */
376 if ((retval
= getopt_internal(nargc
, nargv
, options
)) == -2) {
377 char *current_argv
, *has_equal
;
378 size_t current_argv_len
;
381 current_argv
= place
;
387 if (*current_argv
== '\0') { /* found "--" */
389 * We found an option (--), so if we skipped
390 * non-options, we have to permute.
392 if (nonopt_end
!= -1) {
393 permute_args(nonopt_start
, nonopt_end
,
395 optind
-= nonopt_end
- nonopt_start
;
397 nonopt_start
= nonopt_end
= -1;
400 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
401 /* argument found (--option=arg) */
402 current_argv_len
= has_equal
- current_argv
;
405 current_argv_len
= strlen(current_argv
);
407 for (i
= 0; long_options
[i
].name
; i
++) {
408 /* find matching long option */
409 if (strncmp(current_argv
, long_options
[i
].name
,
413 if (strlen(long_options
[i
].name
) ==
414 (unsigned)current_argv_len
) {
419 if (match
== -1) /* partial match */
422 /* ambiguous abbreviation */
424 warnx(ambig
, (int)current_argv_len
,
430 if (match
!= -1) { /* option found */
431 if (long_options
[match
].has_arg
== no_argument
434 warnx(noarg
, (int)current_argv_len
,
437 * XXX: GNU sets optopt to val regardless of
440 if (long_options
[match
].flag
== NULL
)
441 optopt
= long_options
[match
].val
;
446 if (long_options
[match
].has_arg
== required_argument
||
447 long_options
[match
].has_arg
== optional_argument
) {
450 else if (long_options
[match
].has_arg
==
453 * optional argument doesn't use
456 optarg
= nargv
[optind
++];
459 if ((long_options
[match
].has_arg
== required_argument
)
460 && (optarg
== NULL
)) {
462 * Missing argument; leading ':'
463 * indicates no error should be generated
466 warnx(recargstring
, current_argv
);
468 * XXX: GNU sets optopt to val regardless
471 if (long_options
[match
].flag
== NULL
)
472 optopt
= long_options
[match
].val
;
478 } else { /* unknown option */
480 warnx(illoptstring
, current_argv
);
484 if (long_options
[match
].flag
) {
485 *long_options
[match
].flag
= long_options
[match
].val
;
488 retval
= long_options
[match
].val
;
494 #endif /* !GETOPT_LONG */