Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / stdlib / getopt_long.c
blob0b84943834578114dd0f417b7ee8ce1b301b80ef
1 /* $NetBSD: getopt_long.c,v 1.24 2007/11/09 03:29:20 christos Exp $ */
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
12 * are met:
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"
34 #endif
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: getopt_long.c,v 1.24 2007/11/09 03:29:20 christos Exp $");
39 #include "namespace.h"
41 #include <assert.h>
42 #include <err.h>
43 #include <errno.h>
44 #if HAVE_NBTOOL_CONFIG_H
45 #include "compat_getopt.h"
46 #else
47 #include <getopt.h>
48 #endif
49 #include <stdlib.h>
50 #include <string.h>
52 #if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
53 #define REPLACE_GETOPT
54 #endif
56 #ifdef REPLACE_GETOPT
57 #ifdef __weak_alias
58 __weak_alias(getopt,_getopt)
59 #endif
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
66 static int optreset;
67 #endif
69 #ifdef __weak_alias
70 __weak_alias(getopt_long,_getopt_long)
71 #endif
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 == '-')
81 /* return values */
82 #define BADCH (int)'?'
83 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
84 || (*options == ':') ? (int)':' : (int)'?')
85 #define INORDER (int)1
87 #define EMSG ""
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) */
99 /* Error messages */
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.
111 static int
112 gcd(int a, int b)
114 int c;
116 c = a % b;
117 while (c != 0) {
118 a = b;
119 b = c;
120 c = a % b;
123 return 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
129 * in each block).
131 static void
132 permute_args(int panonopt_start, int panonopt_end, int opt_end, char **nargv)
134 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
135 char *swap;
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;
149 pos = cstart;
150 for (j = 0; j < cyclelen; j++) {
151 if (pos >= panonopt_end)
152 pos -= nnonopts;
153 else
154 pos += nopts;
155 swap = nargv[pos];
156 nargv[pos] = nargv[cstart];
157 nargv[cstart] = swap;
163 * getopt_internal --
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).
167 static int
168 getopt_internal(int nargc, char **nargv, const char *options)
170 char *oli; /* option letter list index */
171 int optchar;
173 _DIAGASSERT(nargv != NULL);
174 _DIAGASSERT(options != NULL);
176 optarg = 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.
183 if (optind == 0)
184 optind = 1;
186 if (optreset)
187 nonopt_start = nonopt_end = -1;
188 start:
189 if (optreset || !*place) { /* update scanning pointer */
190 optreset = 0;
191 if (optind >= nargc) { /* end of argument vector */
192 place = EMSG;
193 if (nonopt_end != -1) {
194 /* do permutation, if we have to */
195 permute_args(nonopt_start, nonopt_end,
196 optind, nargv);
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;
207 return -1;
209 if ((*(place = nargv[optind]) != '-')
210 || (place[1] == '\0')) { /* found non-option */
211 place = EMSG;
212 if (IN_ORDER) {
214 * GNU extension:
215 * return non-option as argument to option 1
217 optarg = nargv[optind++];
218 return INORDER;
220 if (!PERMUTE) {
222 * if no permutation wanted, stop parsing
223 * at first non-option
225 return -1;
227 /* do permutation */
228 if (nonopt_start == -1)
229 nonopt_start = optind;
230 else if (nonopt_end != -1) {
231 permute_args(nonopt_start, nonopt_end,
232 optind, nargv);
233 nonopt_start = optind -
234 (nonopt_end - nonopt_start);
235 nonopt_end = -1;
237 optind++;
238 /* process next argument */
239 goto start;
241 if (nonopt_start != -1 && nonopt_end == -1)
242 nonopt_end = optind;
243 if (place[1] && *++place == '-') { /* found "--" */
244 place++;
245 return -2;
248 if ((optchar = (int)*place++) == (int)':' ||
249 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
250 /* option letter unknown or ':' */
251 if (!*place)
252 ++optind;
253 if (PRINT_ERROR)
254 warnx(illoptchar, optchar);
255 optopt = optchar;
256 return BADCH;
258 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
259 /* XXX: what if no long options provided (called by getopt)? */
260 if (*place)
261 return -2;
263 if (++optind >= nargc) { /* no arg */
264 place = EMSG;
265 if (PRINT_ERROR)
266 warnx(recargchar, optchar);
267 optopt = optchar;
268 return BADARG;
269 } else /* white space */
270 place = nargv[optind];
272 * Handle -W arg the same as --arg (which causes getopt to
273 * stop parsing).
275 return -2;
277 if (*++oli != ':') { /* doesn't take argument */
278 if (!*place)
279 ++optind;
280 } else { /* takes (optional) argument */
281 optarg = NULL;
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 */
287 place = EMSG;
288 if (PRINT_ERROR)
289 warnx(recargchar, optchar);
290 optopt = optchar;
291 return BADARG;
292 } else
293 optarg = nargv[optind];
295 place = EMSG;
296 ++optind;
298 /* dump back option letter */
299 return optchar;
302 #ifdef REPLACE_GETOPT
304 * getopt --
305 * Parse argc/argv argument vector.
307 * [eventually this will replace the real getopt]
310 getopt(nargc, nargv, options)
311 int nargc;
312 char * const *nargv;
313 const char *options;
315 int retval;
317 _DIAGASSERT(nargv != NULL);
318 _DIAGASSERT(options != NULL);
320 retval = getopt_internal(nargc, __UNCONST(nargv), options);
321 if (retval == -2) {
322 ++optind;
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,
329 nargv);
330 optind -= nonopt_end - nonopt_start;
332 nonopt_start = nonopt_end = -1;
333 retval = -1;
335 return retval;
337 #endif
340 * getopt_long --
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)
347 int retval;
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);
360 if (retval == -2) {
361 char *current_argv, *has_equal;
362 size_t current_argv_len;
363 int i, ambiguous, match;
365 current_argv = __UNCONST(place);
366 match = -1;
367 ambiguous = 0;
369 optind++;
370 place = EMSG;
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;
383 return -1;
385 if ((has_equal = strchr(current_argv, '=')) != NULL) {
386 /* argument found (--option=arg) */
387 current_argv_len = has_equal - current_argv;
388 has_equal++;
389 } else
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,
395 current_argv_len))
396 continue;
398 if (strlen(long_options[i].name) ==
399 (unsigned)current_argv_len) {
400 /* exact match */
401 match = i;
402 ambiguous = 0;
403 break;
405 if (match == -1) /* partial match */
406 match = i;
407 else if (!IDENTICAL_INTERPRETATION(i, match))
408 ambiguous = 1;
410 if (ambiguous) {
411 /* ambiguous abbreviation */
412 if (PRINT_ERROR)
413 warnx(ambig, (int)current_argv_len,
414 current_argv);
415 optopt = 0;
416 return BADCH;
418 if (match != -1) { /* option found */
419 if (long_options[match].has_arg == no_argument
420 && has_equal) {
421 if (PRINT_ERROR)
422 warnx(noarg, (int)current_argv_len,
423 current_argv);
425 * XXX: GNU sets optopt to val regardless of
426 * flag
428 if (long_options[match].flag == NULL)
429 optopt = long_options[match].val;
430 else
431 optopt = 0;
432 return BADARG;
434 if (long_options[match].has_arg == required_argument ||
435 long_options[match].has_arg == optional_argument) {
436 if (has_equal)
437 optarg = has_equal;
438 else if (long_options[match].has_arg ==
439 required_argument) {
441 * optional argument doesn't use
442 * next nargv
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
453 if (PRINT_ERROR)
454 warnx(recargstring, current_argv);
456 * XXX: GNU sets optopt to val regardless
457 * of flag
459 if (long_options[match].flag == NULL)
460 optopt = long_options[match].val;
461 else
462 optopt = 0;
463 --optind;
464 return BADARG;
466 } else { /* unknown option */
467 if (PRINT_ERROR)
468 warnx(illoptstring, current_argv);
469 optopt = 0;
470 return BADCH;
472 if (long_options[match].flag) {
473 *long_options[match].flag = long_options[match].val;
474 retval = 0;
475 } else
476 retval = long_options[match].val;
477 if (idx)
478 *idx = match;
480 return retval;
481 #undef IDENTICAL_INTERPRETATION