No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / file / dist / src / getopt_long.c
blob0533173a1f8a8a03698ffddf43224b5f8cc16860
1 /* $NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt 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 #include "file.h"
34 #ifndef lint
35 #if 0
36 FILE_RCSID("@(#)$File: getopt_long.c,v 1.6 2009/02/13 18:48:05 christos Exp $")
37 #else
38 __RCSID("$NetBSD$");
39 #endif
40 #endif /* lint */
42 #include <assert.h>
43 #ifdef HAVE_ERR_H
44 #include <err.h>
45 #else
46 #define warnx printf
47 #endif
48 #include <errno.h>
49 #if defined(HAVE_GETOPT_H) && defined(HAVE_STRUCT_OPTION)
50 #include <getopt.h>
51 #else
52 #include "mygetopt.h"
53 #endif
54 #include <stdlib.h>
55 #include <string.h>
57 #define REPLACE_GETOPT
59 #ifndef _DIAGASSERT
60 #define _DIAGASSERT assert
61 #endif
63 #ifdef REPLACE_GETOPT
64 #ifdef __weak_alias
65 __weak_alias(getopt,_getopt)
66 #endif
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
73 static int optreset;
74 #endif
76 #ifdef __weak_alias
77 __weak_alias(getopt_long,_getopt_long)
78 #endif
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 == '-')
88 /* return values */
89 #define BADCH (int)'?'
90 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
91 || (*options == ':') ? (int)':' : (int)'?')
92 #define INORDER (int)1
94 #define EMSG ""
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) */
106 /* Error messages */
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.
118 static int
119 gcd(a, b)
120 int a;
121 int b;
123 int c;
125 c = a % b;
126 while (c != 0) {
127 a = b;
128 b = c;
129 c = a % b;
132 return 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
138 * in each block).
140 static void
141 permute_args(panonopt_start, panonopt_end, opt_end, nargv)
142 int panonopt_start;
143 int panonopt_end;
144 int opt_end;
145 char **nargv;
147 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
148 char *swap;
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;
162 pos = cstart;
163 for (j = 0; j < cyclelen; j++) {
164 if (pos >= panonopt_end)
165 pos -= nnonopts;
166 else
167 pos += nopts;
168 swap = nargv[pos];
169 nargv[pos] = nargv[cstart];
170 nargv[cstart] = swap;
176 * getopt_internal --
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).
180 static int
181 getopt_internal(nargc, nargv, options)
182 int nargc;
183 char **nargv;
184 const char *options;
186 char *oli; /* option letter list index */
187 int optchar;
189 _DIAGASSERT(nargv != NULL);
190 _DIAGASSERT(options != NULL);
192 optarg = 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.
199 if (optind == 0)
200 optind = 1;
202 if (optreset)
203 nonopt_start = nonopt_end = -1;
204 start:
205 if (optreset || !*place) { /* update scanning pointer */
206 optreset = 0;
207 if (optind >= nargc) { /* end of argument vector */
208 place = EMSG;
209 if (nonopt_end != -1) {
210 /* do permutation, if we have to */
211 permute_args(nonopt_start, nonopt_end,
212 optind, nargv);
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;
223 return -1;
225 if ((*(place = nargv[optind]) != '-')
226 || (place[1] == '\0')) { /* found non-option */
227 place = EMSG;
228 if (IN_ORDER) {
230 * GNU extension:
231 * return non-option as argument to option 1
233 optarg = nargv[optind++];
234 return INORDER;
236 if (!PERMUTE) {
238 * if no permutation wanted, stop parsing
239 * at first non-option
241 return -1;
243 /* do permutation */
244 if (nonopt_start == -1)
245 nonopt_start = optind;
246 else if (nonopt_end != -1) {
247 permute_args(nonopt_start, nonopt_end,
248 optind, nargv);
249 nonopt_start = optind -
250 (nonopt_end - nonopt_start);
251 nonopt_end = -1;
253 optind++;
254 /* process next argument */
255 goto start;
257 if (nonopt_start != -1 && nonopt_end == -1)
258 nonopt_end = optind;
259 if (place[1] && *++place == '-') { /* found "--" */
260 place++;
261 return -2;
264 if ((optchar = (int)*place++) == (int)':' ||
265 (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
266 /* option letter unknown or ':' */
267 if (!*place)
268 ++optind;
269 if (PRINT_ERROR)
270 warnx(illoptchar, optchar);
271 optopt = optchar;
272 return BADCH;
274 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
275 /* XXX: what if no long options provided (called by getopt)? */
276 if (*place)
277 return -2;
279 if (++optind >= nargc) { /* no arg */
280 place = EMSG;
281 if (PRINT_ERROR)
282 warnx(recargchar, optchar);
283 optopt = optchar;
284 return BADARG;
285 } else /* white space */
286 place = nargv[optind];
288 * Handle -W arg the same as --arg (which causes getopt to
289 * stop parsing).
291 return -2;
293 if (*++oli != ':') { /* doesn't take argument */
294 if (!*place)
295 ++optind;
296 } else { /* takes (optional) argument */
297 optarg = NULL;
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 */
303 place = EMSG;
304 if (PRINT_ERROR)
305 warnx(recargchar, optchar);
306 optopt = optchar;
307 return BADARG;
308 } else
309 optarg = nargv[optind];
311 place = EMSG;
312 ++optind;
314 /* dump back option letter */
315 return optchar;
318 #ifdef REPLACE_GETOPT
320 * getopt --
321 * Parse argc/argv argument vector.
323 * [eventually this will replace the real getopt]
326 getopt(nargc, nargv, options)
327 int nargc;
328 char * const *nargv;
329 const char *options;
331 int retval;
333 _DIAGASSERT(nargv != NULL);
334 _DIAGASSERT(options != NULL);
336 retval = getopt_internal(nargc, (char **)nargv, options);
337 if (retval == -2) {
338 ++optind;
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,
345 (char **)nargv);
346 optind -= nonopt_end - nonopt_start;
348 nonopt_start = nonopt_end = -1;
349 retval = -1;
351 return retval;
353 #endif
356 * getopt_long --
357 * Parse argc/argv argument vector.
360 getopt_long(nargc, nargv, options, long_options, idx)
361 int nargc;
362 char * const *nargv;
363 const char *options;
364 const struct option *long_options;
365 int *idx;
367 int retval;
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);
380 if (retval == -2) {
381 char *current_argv, *has_equal;
382 size_t current_argv_len;
383 int i, ambiguous, match;
385 current_argv = (char *)place;
386 match = -1;
387 ambiguous = 0;
389 optind++;
390 place = EMSG;
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;
403 return -1;
405 if ((has_equal = strchr(current_argv, '=')) != NULL) {
406 /* argument found (--option=arg) */
407 current_argv_len = has_equal - current_argv;
408 has_equal++;
409 } else
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,
415 current_argv_len))
416 continue;
418 if (strlen(long_options[i].name) ==
419 (unsigned)current_argv_len) {
420 /* exact match */
421 match = i;
422 ambiguous = 0;
423 break;
425 if (match == -1) /* partial match */
426 match = i;
427 else if (!IDENTICAL_INTERPRETATION(i, match))
428 ambiguous = 1;
430 if (ambiguous) {
431 /* ambiguous abbreviation */
432 if (PRINT_ERROR)
433 warnx(ambig, (int)current_argv_len,
434 current_argv);
435 optopt = 0;
436 return BADCH;
438 if (match != -1) { /* option found */
439 if (long_options[match].has_arg == no_argument
440 && has_equal) {
441 if (PRINT_ERROR)
442 warnx(noarg, (int)current_argv_len,
443 current_argv);
445 * XXX: GNU sets optopt to val regardless of
446 * flag
448 if (long_options[match].flag == NULL)
449 optopt = long_options[match].val;
450 else
451 optopt = 0;
452 return BADARG;
454 if (long_options[match].has_arg == required_argument ||
455 long_options[match].has_arg == optional_argument) {
456 if (has_equal)
457 optarg = has_equal;
458 else if (long_options[match].has_arg ==
459 required_argument) {
461 * optional argument doesn't use
462 * next nargv
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
473 if (PRINT_ERROR)
474 warnx(recargstring, current_argv);
476 * XXX: GNU sets optopt to val regardless
477 * of flag
479 if (long_options[match].flag == NULL)
480 optopt = long_options[match].val;
481 else
482 optopt = 0;
483 --optind;
484 return BADARG;
486 } else { /* unknown option */
487 if (PRINT_ERROR)
488 warnx(illoptstring, current_argv);
489 optopt = 0;
490 return BADCH;
492 if (long_options[match].flag) {
493 *long_options[match].flag = long_options[match].val;
494 retval = 0;
495 } else
496 retval = long_options[match].val;
497 if (idx)
498 *idx = match;
500 return retval;
501 #undef IDENTICAL_INTERPRETATION