getopt_long.c revision 1.21.4.1 1 1.21.4.1 matt /* $NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt Exp $ */
2 1.4 christos
3 1.4 christos /*-
4 1.4 christos * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.4 christos * All rights reserved.
6 1.4 christos *
7 1.4 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.4 christos * by Dieter Baron and Thomas Klausner.
9 1.1 mcr *
10 1.1 mcr * Redistribution and use in source and binary forms, with or without
11 1.1 mcr * modification, are permitted provided that the following conditions
12 1.1 mcr * are met:
13 1.1 mcr * 1. Redistributions of source code must retain the above copyright
14 1.1 mcr * notice, this list of conditions and the following disclaimer.
15 1.1 mcr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mcr * notice, this list of conditions and the following disclaimer in the
17 1.1 mcr * documentation and/or other materials provided with the distribution.
18 1.1 mcr *
19 1.4 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.4 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.4 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.4 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.4 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.4 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.4 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.4 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.4 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.4 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.4 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mcr */
31 1.1 mcr
32 1.17 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.17 jmc #include "nbtool_config.h"
34 1.17 jmc #endif
35 1.17 jmc
36 1.2 lukem #include <sys/cdefs.h>
37 1.4 christos #if defined(LIBC_SCCS) && !defined(lint)
38 1.21.4.1 matt __RCSID("$NetBSD: getopt_long.c,v 1.21.4.1 2008/01/09 01:34:14 matt Exp $");
39 1.4 christos #endif /* LIBC_SCCS and not lint */
40 1.2 lukem
41 1.5 christos #include "namespace.h"
42 1.5 christos
43 1.2 lukem #include <assert.h>
44 1.15 tv #include <err.h>
45 1.2 lukem #include <errno.h>
46 1.17 jmc #if HAVE_NBTOOL_CONFIG_H
47 1.17 jmc #include "compat_getopt.h"
48 1.17 jmc #else
49 1.15 tv #include <getopt.h>
50 1.17 jmc #endif
51 1.1 mcr #include <stdlib.h>
52 1.1 mcr #include <string.h>
53 1.4 christos
54 1.16 lukem #if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
55 1.15 tv #define REPLACE_GETOPT
56 1.14 tv #endif
57 1.14 tv
58 1.4 christos #ifdef REPLACE_GETOPT
59 1.4 christos #ifdef __weak_alias
60 1.4 christos __weak_alias(getopt,_getopt)
61 1.4 christos #endif
62 1.4 christos int opterr = 1; /* if error message should be printed */
63 1.4 christos int optind = 1; /* index into parent argv vector */
64 1.4 christos int optopt = '?'; /* character checked for validity */
65 1.4 christos int optreset; /* reset getopt */
66 1.4 christos char *optarg; /* argument associated with option */
67 1.16 lukem #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET
68 1.14 tv static int optreset;
69 1.4 christos #endif
70 1.1 mcr
71 1.4 christos #ifdef __weak_alias
72 1.4 christos __weak_alias(getopt_long,_getopt_long)
73 1.4 christos #endif
74 1.2 lukem
75 1.4 christos #define IGNORE_FIRST (*options == '-' || *options == '+')
76 1.4 christos #define PRINT_ERROR ((opterr) && ((*options != ':') \
77 1.4 christos || (IGNORE_FIRST && options[1] != ':')))
78 1.4 christos #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
79 1.4 christos #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
80 1.4 christos /* XXX: GNU ignores PC if *options == '-' */
81 1.4 christos #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
82 1.1 mcr
83 1.4 christos /* return values */
84 1.1 mcr #define BADCH (int)'?'
85 1.9 wiz #define BADARG ((IGNORE_FIRST && options[1] == ':') \
86 1.9 wiz || (*options == ':') ? (int)':' : (int)'?')
87 1.4 christos #define INORDER (int)1
88 1.4 christos
89 1.1 mcr #define EMSG ""
90 1.1 mcr
91 1.19 yamt static int getopt_internal __P((int, char **, const char *));
92 1.4 christos static int gcd __P((int, int));
93 1.19 yamt static void permute_args __P((int, int, int, char **));
94 1.4 christos
95 1.18 christos static const char *place = EMSG; /* option letter processing */
96 1.4 christos
97 1.4 christos /* XXX: set optreset to 1 rather than these two */
98 1.4 christos static int nonopt_start = -1; /* first non option argument (for permute) */
99 1.4 christos static int nonopt_end = -1; /* first option after non options (for permute) */
100 1.4 christos
101 1.4 christos /* Error messages */
102 1.6 nathanw static const char recargchar[] = "option requires an argument -- %c";
103 1.6 nathanw static const char recargstring[] = "option requires an argument -- %s";
104 1.4 christos static const char ambig[] = "ambiguous option -- %.*s";
105 1.4 christos static const char noarg[] = "option doesn't take an argument -- %.*s";
106 1.12 joda static const char illoptchar[] = "unknown option -- %c";
107 1.12 joda static const char illoptstring[] = "unknown option -- %s";
108 1.4 christos
109 1.4 christos
110 1.1 mcr /*
111 1.4 christos * Compute the greatest common divisor of a and b.
112 1.4 christos */
113 1.4 christos static int
114 1.4 christos gcd(a, b)
115 1.4 christos int a;
116 1.4 christos int b;
117 1.4 christos {
118 1.4 christos int c;
119 1.4 christos
120 1.4 christos c = a % b;
121 1.4 christos while (c != 0) {
122 1.4 christos a = b;
123 1.4 christos b = c;
124 1.4 christos c = a % b;
125 1.4 christos }
126 1.4 christos
127 1.4 christos return b;
128 1.4 christos }
129 1.4 christos
130 1.4 christos /*
131 1.4 christos * Exchange the block from nonopt_start to nonopt_end with the block
132 1.4 christos * from nonopt_end to opt_end (keeping the same order of arguments
133 1.4 christos * in each block).
134 1.4 christos */
135 1.4 christos static void
136 1.13 lukem permute_args(panonopt_start, panonopt_end, opt_end, nargv)
137 1.13 lukem int panonopt_start;
138 1.13 lukem int panonopt_end;
139 1.4 christos int opt_end;
140 1.19 yamt char **nargv;
141 1.4 christos {
142 1.4 christos int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
143 1.4 christos char *swap;
144 1.4 christos
145 1.10 lukem _DIAGASSERT(nargv != NULL);
146 1.10 lukem
147 1.4 christos /*
148 1.4 christos * compute lengths of blocks and number and size of cycles
149 1.4 christos */
150 1.13 lukem nnonopts = panonopt_end - panonopt_start;
151 1.13 lukem nopts = opt_end - panonopt_end;
152 1.4 christos ncycle = gcd(nnonopts, nopts);
153 1.13 lukem cyclelen = (opt_end - panonopt_start) / ncycle;
154 1.4 christos
155 1.4 christos for (i = 0; i < ncycle; i++) {
156 1.13 lukem cstart = panonopt_end+i;
157 1.4 christos pos = cstart;
158 1.4 christos for (j = 0; j < cyclelen; j++) {
159 1.13 lukem if (pos >= panonopt_end)
160 1.4 christos pos -= nnonopts;
161 1.4 christos else
162 1.4 christos pos += nopts;
163 1.4 christos swap = nargv[pos];
164 1.19 yamt nargv[pos] = nargv[cstart];
165 1.19 yamt nargv[cstart] = swap;
166 1.4 christos }
167 1.4 christos }
168 1.4 christos }
169 1.4 christos
170 1.4 christos /*
171 1.4 christos * getopt_internal --
172 1.4 christos * Parse argc/argv argument vector. Called by user level routines.
173 1.4 christos * Returns -2 if -- is found (can be long option or end of options marker).
174 1.1 mcr */
175 1.4 christos static int
176 1.4 christos getopt_internal(nargc, nargv, options)
177 1.1 mcr int nargc;
178 1.19 yamt char **nargv;
179 1.4 christos const char *options;
180 1.1 mcr {
181 1.1 mcr char *oli; /* option letter list index */
182 1.4 christos int optchar;
183 1.1 mcr
184 1.2 lukem _DIAGASSERT(nargv != NULL);
185 1.4 christos _DIAGASSERT(options != NULL);
186 1.4 christos
187 1.4 christos optarg = NULL;
188 1.8 thorpej
189 1.8 thorpej /*
190 1.8 thorpej * XXX Some programs (like rsyncd) expect to be able to
191 1.8 thorpej * XXX re-initialize optind to 0 and have getopt_long(3)
192 1.8 thorpej * XXX properly function again. Work around this braindamage.
193 1.8 thorpej */
194 1.8 thorpej if (optind == 0)
195 1.8 thorpej optind = 1;
196 1.2 lukem
197 1.4 christos if (optreset)
198 1.4 christos nonopt_start = nonopt_end = -1;
199 1.4 christos start:
200 1.1 mcr if (optreset || !*place) { /* update scanning pointer */
201 1.1 mcr optreset = 0;
202 1.4 christos if (optind >= nargc) { /* end of argument vector */
203 1.4 christos place = EMSG;
204 1.4 christos if (nonopt_end != -1) {
205 1.4 christos /* do permutation, if we have to */
206 1.4 christos permute_args(nonopt_start, nonopt_end,
207 1.4 christos optind, nargv);
208 1.4 christos optind -= nonopt_end - nonopt_start;
209 1.4 christos }
210 1.4 christos else if (nonopt_start != -1) {
211 1.4 christos /*
212 1.4 christos * If we skipped non-options, set optind
213 1.4 christos * to the first of them.
214 1.4 christos */
215 1.4 christos optind = nonopt_start;
216 1.4 christos }
217 1.4 christos nonopt_start = nonopt_end = -1;
218 1.4 christos return -1;
219 1.4 christos }
220 1.9 wiz if ((*(place = nargv[optind]) != '-')
221 1.9 wiz || (place[1] == '\0')) { /* found non-option */
222 1.1 mcr place = EMSG;
223 1.4 christos if (IN_ORDER) {
224 1.4 christos /*
225 1.4 christos * GNU extension:
226 1.4 christos * return non-option as argument to option 1
227 1.4 christos */
228 1.4 christos optarg = nargv[optind++];
229 1.4 christos return INORDER;
230 1.4 christos }
231 1.4 christos if (!PERMUTE) {
232 1.4 christos /*
233 1.4 christos * if no permutation wanted, stop parsing
234 1.4 christos * at first non-option
235 1.4 christos */
236 1.4 christos return -1;
237 1.4 christos }
238 1.4 christos /* do permutation */
239 1.4 christos if (nonopt_start == -1)
240 1.4 christos nonopt_start = optind;
241 1.4 christos else if (nonopt_end != -1) {
242 1.4 christos permute_args(nonopt_start, nonopt_end,
243 1.4 christos optind, nargv);
244 1.4 christos nonopt_start = optind -
245 1.4 christos (nonopt_end - nonopt_start);
246 1.4 christos nonopt_end = -1;
247 1.4 christos }
248 1.4 christos optind++;
249 1.4 christos /* process next argument */
250 1.4 christos goto start;
251 1.1 mcr }
252 1.4 christos if (nonopt_start != -1 && nonopt_end == -1)
253 1.4 christos nonopt_end = optind;
254 1.1 mcr if (place[1] && *++place == '-') { /* found "--" */
255 1.4 christos place++;
256 1.4 christos return -2;
257 1.4 christos }
258 1.4 christos }
259 1.4 christos if ((optchar = (int)*place++) == (int)':' ||
260 1.4 christos (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
261 1.4 christos /* option letter unknown or ':' */
262 1.4 christos if (!*place)
263 1.4 christos ++optind;
264 1.4 christos if (PRINT_ERROR)
265 1.6 nathanw warnx(illoptchar, optchar);
266 1.4 christos optopt = optchar;
267 1.4 christos return BADCH;
268 1.4 christos }
269 1.4 christos if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
270 1.4 christos /* XXX: what if no long options provided (called by getopt)? */
271 1.4 christos if (*place)
272 1.4 christos return -2;
273 1.4 christos
274 1.4 christos if (++optind >= nargc) { /* no arg */
275 1.1 mcr place = EMSG;
276 1.4 christos if (PRINT_ERROR)
277 1.6 nathanw warnx(recargchar, optchar);
278 1.4 christos optopt = optchar;
279 1.4 christos return BADARG;
280 1.4 christos } else /* white space */
281 1.4 christos place = nargv[optind];
282 1.1 mcr /*
283 1.4 christos * Handle -W arg the same as --arg (which causes getopt to
284 1.4 christos * stop parsing).
285 1.1 mcr */
286 1.4 christos return -2;
287 1.4 christos }
288 1.4 christos if (*++oli != ':') { /* doesn't take argument */
289 1.1 mcr if (!*place)
290 1.1 mcr ++optind;
291 1.4 christos } else { /* takes (optional) argument */
292 1.1 mcr optarg = NULL;
293 1.1 mcr if (*place) /* no white space */
294 1.18 christos optarg = __UNCONST(place);
295 1.4 christos /* XXX: disable test for :: if PC? (GNU doesn't) */
296 1.4 christos else if (oli[1] != ':') { /* arg not optional */
297 1.4 christos if (++optind >= nargc) { /* no arg */
298 1.4 christos place = EMSG;
299 1.4 christos if (PRINT_ERROR)
300 1.6 nathanw warnx(recargchar, optchar);
301 1.4 christos optopt = optchar;
302 1.4 christos return BADARG;
303 1.4 christos } else
304 1.4 christos optarg = nargv[optind];
305 1.4 christos }
306 1.1 mcr place = EMSG;
307 1.1 mcr ++optind;
308 1.1 mcr }
309 1.4 christos /* dump back option letter */
310 1.4 christos return optchar;
311 1.1 mcr }
312 1.1 mcr
313 1.4 christos #ifdef REPLACE_GETOPT
314 1.1 mcr /*
315 1.1 mcr * getopt --
316 1.1 mcr * Parse argc/argv argument vector.
317 1.4 christos *
318 1.4 christos * [eventually this will replace the real getopt]
319 1.1 mcr */
320 1.1 mcr int
321 1.4 christos getopt(nargc, nargv, options)
322 1.1 mcr int nargc;
323 1.1 mcr char * const *nargv;
324 1.4 christos const char *options;
325 1.1 mcr {
326 1.1 mcr int retval;
327 1.10 lukem
328 1.10 lukem _DIAGASSERT(nargv != NULL);
329 1.10 lukem _DIAGASSERT(options != NULL);
330 1.1 mcr
331 1.19 yamt retval = getopt_internal(nargc, __UNCONST(nargv), options);
332 1.19 yamt if (retval == -2) {
333 1.4 christos ++optind;
334 1.4 christos /*
335 1.4 christos * We found an option (--), so if we skipped non-options,
336 1.4 christos * we have to permute.
337 1.4 christos */
338 1.4 christos if (nonopt_end != -1) {
339 1.4 christos permute_args(nonopt_start, nonopt_end, optind,
340 1.4 christos nargv);
341 1.4 christos optind -= nonopt_end - nonopt_start;
342 1.4 christos }
343 1.4 christos nonopt_start = nonopt_end = -1;
344 1.1 mcr retval = -1;
345 1.1 mcr }
346 1.4 christos return retval;
347 1.1 mcr }
348 1.1 mcr #endif
349 1.1 mcr
350 1.1 mcr /*
351 1.1 mcr * getopt_long --
352 1.1 mcr * Parse argc/argv argument vector.
353 1.1 mcr */
354 1.1 mcr int
355 1.4 christos getopt_long(nargc, nargv, options, long_options, idx)
356 1.2 lukem int nargc;
357 1.4 christos char * const *nargv;
358 1.4 christos const char *options;
359 1.4 christos const struct option *long_options;
360 1.4 christos int *idx;
361 1.1 mcr {
362 1.1 mcr int retval;
363 1.1 mcr
364 1.21 ginsbach #define IDENTICAL_INTERPRETATION(_x, _y) \
365 1.21 ginsbach (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
366 1.21 ginsbach long_options[(_x)].flag == long_options[(_y)].flag && \
367 1.21 ginsbach long_options[(_x)].val == long_options[(_y)].val)
368 1.21 ginsbach
369 1.2 lukem _DIAGASSERT(nargv != NULL);
370 1.4 christos _DIAGASSERT(options != NULL);
371 1.2 lukem _DIAGASSERT(long_options != NULL);
372 1.4 christos /* idx may be NULL */
373 1.2 lukem
374 1.19 yamt retval = getopt_internal(nargc, __UNCONST(nargv), options);
375 1.19 yamt if (retval == -2) {
376 1.4 christos char *current_argv, *has_equal;
377 1.4 christos size_t current_argv_len;
378 1.21 ginsbach int i, ambiguous, match;
379 1.1 mcr
380 1.18 christos current_argv = __UNCONST(place);
381 1.4 christos match = -1;
382 1.21 ginsbach ambiguous = 0;
383 1.4 christos
384 1.4 christos optind++;
385 1.4 christos place = EMSG;
386 1.4 christos
387 1.4 christos if (*current_argv == '\0') { /* found "--" */
388 1.4 christos /*
389 1.4 christos * We found an option (--), so if we skipped
390 1.4 christos * non-options, we have to permute.
391 1.4 christos */
392 1.4 christos if (nonopt_end != -1) {
393 1.4 christos permute_args(nonopt_start, nonopt_end,
394 1.19 yamt optind, __UNCONST(nargv));
395 1.4 christos optind -= nonopt_end - nonopt_start;
396 1.4 christos }
397 1.4 christos nonopt_start = nonopt_end = -1;
398 1.4 christos return -1;
399 1.1 mcr }
400 1.1 mcr if ((has_equal = strchr(current_argv, '=')) != NULL) {
401 1.4 christos /* argument found (--option=arg) */
402 1.1 mcr current_argv_len = has_equal - current_argv;
403 1.1 mcr has_equal++;
404 1.1 mcr } else
405 1.1 mcr current_argv_len = strlen(current_argv);
406 1.4 christos
407 1.4 christos for (i = 0; long_options[i].name; i++) {
408 1.4 christos /* find matching long option */
409 1.4 christos if (strncmp(current_argv, long_options[i].name,
410 1.4 christos current_argv_len))
411 1.1 mcr continue;
412 1.1 mcr
413 1.4 christos if (strlen(long_options[i].name) ==
414 1.4 christos (unsigned)current_argv_len) {
415 1.4 christos /* exact match */
416 1.1 mcr match = i;
417 1.21 ginsbach ambiguous = 0;
418 1.1 mcr break;
419 1.1 mcr }
420 1.4 christos if (match == -1) /* partial match */
421 1.1 mcr match = i;
422 1.21 ginsbach else if (!IDENTICAL_INTERPRETATION(i, match))
423 1.21 ginsbach ambiguous = 1;
424 1.21 ginsbach }
425 1.21 ginsbach if (ambiguous) {
426 1.21 ginsbach /* ambiguous abbreviation */
427 1.21 ginsbach if (PRINT_ERROR)
428 1.21 ginsbach warnx(ambig, (int)current_argv_len,
429 1.21 ginsbach current_argv);
430 1.21 ginsbach optopt = 0;
431 1.21 ginsbach return BADCH;
432 1.1 mcr }
433 1.4 christos if (match != -1) { /* option found */
434 1.4 christos if (long_options[match].has_arg == no_argument
435 1.4 christos && has_equal) {
436 1.4 christos if (PRINT_ERROR)
437 1.7 sommerfe warnx(noarg, (int)current_argv_len,
438 1.4 christos current_argv);
439 1.4 christos /*
440 1.4 christos * XXX: GNU sets optopt to val regardless of
441 1.4 christos * flag
442 1.4 christos */
443 1.4 christos if (long_options[match].flag == NULL)
444 1.4 christos optopt = long_options[match].val;
445 1.4 christos else
446 1.4 christos optopt = 0;
447 1.4 christos return BADARG;
448 1.4 christos }
449 1.1 mcr if (long_options[match].has_arg == required_argument ||
450 1.1 mcr long_options[match].has_arg == optional_argument) {
451 1.1 mcr if (has_equal)
452 1.1 mcr optarg = has_equal;
453 1.4 christos else if (long_options[match].has_arg ==
454 1.4 christos required_argument) {
455 1.4 christos /*
456 1.4 christos * optional argument doesn't use
457 1.4 christos * next nargv
458 1.4 christos */
459 1.1 mcr optarg = nargv[optind++];
460 1.4 christos }
461 1.1 mcr }
462 1.1 mcr if ((long_options[match].has_arg == required_argument)
463 1.1 mcr && (optarg == NULL)) {
464 1.1 mcr /*
465 1.4 christos * Missing argument; leading ':'
466 1.1 mcr * indicates no error should be generated
467 1.1 mcr */
468 1.4 christos if (PRINT_ERROR)
469 1.6 nathanw warnx(recargstring, current_argv);
470 1.4 christos /*
471 1.4 christos * XXX: GNU sets optopt to val regardless
472 1.4 christos * of flag
473 1.4 christos */
474 1.4 christos if (long_options[match].flag == NULL)
475 1.4 christos optopt = long_options[match].val;
476 1.4 christos else
477 1.4 christos optopt = 0;
478 1.4 christos --optind;
479 1.4 christos return BADARG;
480 1.4 christos }
481 1.4 christos } else { /* unknown option */
482 1.4 christos if (PRINT_ERROR)
483 1.6 nathanw warnx(illoptstring, current_argv);
484 1.4 christos optopt = 0;
485 1.4 christos return BADCH;
486 1.1 mcr }
487 1.1 mcr if (long_options[match].flag) {
488 1.1 mcr *long_options[match].flag = long_options[match].val;
489 1.1 mcr retval = 0;
490 1.1 mcr } else
491 1.1 mcr retval = long_options[match].val;
492 1.4 christos if (idx)
493 1.4 christos *idx = match;
494 1.1 mcr }
495 1.4 christos return retval;
496 1.21 ginsbach #undef IDENTICAL_INTERPRETATION
497 1.1 mcr }
498