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