1 /* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */
2 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3
4 /*
5 * Copyright (c) 2002 Todd C. Miller <[email protected]>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23 /*-
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by Dieter Baron and Thomas Klausner.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52 #if 0
53 #if defined(LIBC_SCCS) && !defined(lint)
54 static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
55 #endif /* LIBC_SCCS and not lint */
56 #endif
57 #include <err.h>
58 #include <errno.h>
59 #include <getopt.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
64
65 #if 0 /* we prefer to keep our getopt(3) */
66 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
67 #endif
68
69 #ifdef REPLACE_GETOPT
70 int opterr = 1; /* if error message should be printed */
71 int optind = 1; /* index into parent argv vector */
72 int optopt = '?'; /* character checked for validity */
73 int optreset; /* reset getopt */
74 char *optarg; /* argument associated with option */
75 #endif
76
77 #define PRINT_ERROR ((opterr) && (*options != ':'))
78
79 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
80 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
81 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
82
83 /* return values */
84 #define BADCH (int)'?'
85 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
86 #define INORDER (int)1
87
88 static char EMSG[] = "";
89
90 #ifdef GNU_COMPATIBLE
91 #define NO_PREFIX (-1)
92 #define D_PREFIX 0
93 #define DD_PREFIX 1
94 #define W_PREFIX 2
95 #endif
96
97 static int getopt_internal(int, char * const *, const char *,
98 const struct option *, int *, int);
99 static int parse_long_options(char * const *, const char *,
100 const struct option *, int *, int, int);
101 static int gcd(int, int);
102 static void permute_args(int, int, int, char * const *);
103
104 static char *place = EMSG; /* option letter processing */
105
106 /* XXX: set optreset to 1 rather than these two */
107 static int nonopt_start = -1; /* first non option argument (for permute) */
108 static int nonopt_end = -1; /* first option after non options (for permute) */
109
110 /* Error messages */
111 static const char recargchar[] = "option requires an argument -- %c";
112 static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
113 #ifdef GNU_COMPATIBLE
114 static int dash_prefix = NO_PREFIX;
115 static const char gnuoptchar[] = "invalid option -- %c";
116
117 static const char recargstring[] = "option `%s%s' requires an argument";
118 static const char ambig[] = "option `%s%.*s' is ambiguous";
119 static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
120 static const char illoptstring[] = "unrecognized option `%s%s'";
121 #else
122 static const char recargstring[] = "option requires an argument -- %s";
123 static const char ambig[] = "ambiguous option -- %.*s";
124 static const char noarg[] = "option doesn't take an argument -- %.*s";
125 static const char illoptstring[] = "unknown option -- %s";
126 #endif
127
128 /*
129 * Compute the greatest common divisor of a and b.
130 */
131 static int
gcd(int a,int b)132 gcd(int a, int b)
133 {
134 int c;
135
136 c = a % b;
137 while (c != 0) {
138 a = b;
139 b = c;
140 c = a % b;
141 }
142
143 return (b);
144 }
145
146 /*
147 * Exchange the block from nonopt_start to nonopt_end with the block
148 * from nonopt_end to opt_end (keeping the same order of arguments
149 * in each block).
150 */
151 static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)152 permute_args(int panonopt_start, int panonopt_end, int opt_end,
153 char * const *nargv)
154 {
155 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
156 char *swap;
157
158 /*
159 * compute lengths of blocks and number and size of cycles
160 */
161 nnonopts = panonopt_end - panonopt_start;
162 nopts = opt_end - panonopt_end;
163 ncycle = gcd(nnonopts, nopts);
164 cyclelen = (opt_end - panonopt_start) / ncycle;
165
166 for (i = 0; i < ncycle; i++) {
167 cstart = panonopt_end+i;
168 pos = cstart;
169 for (j = 0; j < cyclelen; j++) {
170 if (pos >= panonopt_end)
171 pos -= nnonopts;
172 else
173 pos += nopts;
174 swap = nargv[pos];
175 /* LINTED const cast */
176 ((char **) nargv)[pos] = nargv[cstart];
177 /* LINTED const cast */
178 ((char **)nargv)[cstart] = swap;
179 }
180 }
181 }
182
183 /*
184 * parse_long_options --
185 * Parse long options in argc/argv argument vector.
186 * Returns -1 if short_too is set and the option does not match long_options.
187 */
188 static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,int flags)189 parse_long_options(char * const *nargv, const char *options,
190 const struct option *long_options, int *idx, int short_too, int flags)
191 {
192 char *current_argv, *has_equal;
193 #ifdef GNU_COMPATIBLE
194 const char *current_dash;
195 #endif
196 size_t current_argv_len;
197 int i, match, exact_match, second_partial_match;
198
199 current_argv = place;
200 #ifdef GNU_COMPATIBLE
201 switch (dash_prefix) {
202 case D_PREFIX:
203 current_dash = "-";
204 break;
205 case DD_PREFIX:
206 current_dash = "--";
207 break;
208 case W_PREFIX:
209 current_dash = "-W ";
210 break;
211 default:
212 current_dash = "";
213 break;
214 }
215 #endif
216 match = -1;
217 exact_match = 0;
218 second_partial_match = 0;
219
220 optind++;
221
222 if ((has_equal = strchr(current_argv, '=')) != NULL) {
223 /* argument found (--option=arg) */
224 current_argv_len = has_equal - current_argv;
225 has_equal++;
226 } else
227 current_argv_len = strlen(current_argv);
228
229 for (i = 0; long_options[i].name; i++) {
230 /* find matching long option */
231 if (strncmp(current_argv, long_options[i].name,
232 current_argv_len))
233 continue;
234
235 if (strlen(long_options[i].name) == current_argv_len) {
236 /* exact match */
237 match = i;
238 exact_match = 1;
239 break;
240 }
241 /*
242 * If this is a known short option, don't allow
243 * a partial match of a single character.
244 */
245 if (short_too && current_argv_len == 1)
246 continue;
247
248 if (match == -1) /* first partial match */
249 match = i;
250 else if ((flags & FLAG_LONGONLY) ||
251 long_options[i].has_arg !=
252 long_options[match].has_arg ||
253 long_options[i].flag != long_options[match].flag ||
254 long_options[i].val != long_options[match].val)
255 second_partial_match = 1;
256 }
257 if (!exact_match && second_partial_match) {
258 /* ambiguous abbreviation */
259 if (PRINT_ERROR)
260 warnx(ambig,
261 #ifdef GNU_COMPATIBLE
262 current_dash,
263 #endif
264 (int)current_argv_len,
265 current_argv);
266 optopt = 0;
267 return (BADCH);
268 }
269 if (match != -1) { /* option found */
270 if (long_options[match].has_arg == no_argument
271 && has_equal) {
272 if (PRINT_ERROR)
273 warnx(noarg,
274 #ifdef GNU_COMPATIBLE
275 current_dash,
276 #endif
277 (int)current_argv_len,
278 current_argv);
279 /*
280 * XXX: GNU sets optopt to val regardless of flag
281 */
282 if (long_options[match].flag == NULL)
283 optopt = long_options[match].val;
284 else
285 optopt = 0;
286 #ifdef GNU_COMPATIBLE
287 return (BADCH);
288 #else
289 return (BADARG);
290 #endif
291 }
292 if (long_options[match].has_arg == required_argument ||
293 long_options[match].has_arg == optional_argument) {
294 if (has_equal)
295 optarg = has_equal;
296 else if (long_options[match].has_arg ==
297 required_argument) {
298 /*
299 * optional argument doesn't use next nargv
300 */
301 optarg = nargv[optind++];
302 }
303 }
304 if ((long_options[match].has_arg == required_argument)
305 && (optarg == NULL)) {
306 /*
307 * Missing argument; leading ':' indicates no error
308 * should be generated.
309 */
310 if (PRINT_ERROR)
311 warnx(recargstring,
312 #ifdef GNU_COMPATIBLE
313 current_dash,
314 #endif
315 current_argv);
316 /*
317 * XXX: GNU sets optopt to val regardless of flag
318 */
319 if (long_options[match].flag == NULL)
320 optopt = long_options[match].val;
321 else
322 optopt = 0;
323 --optind;
324 return (BADARG);
325 }
326 } else { /* unknown option */
327 if (short_too) {
328 --optind;
329 return (-1);
330 }
331 if (PRINT_ERROR)
332 warnx(illoptstring,
333 #ifdef GNU_COMPATIBLE
334 current_dash,
335 #endif
336 current_argv);
337 optopt = 0;
338 return (BADCH);
339 }
340 if (idx)
341 *idx = match;
342 if (long_options[match].flag) {
343 *long_options[match].flag = long_options[match].val;
344 return (0);
345 } else
346 return (long_options[match].val);
347 }
348
349 /*
350 * getopt_internal --
351 * Parse argc/argv argument vector. Called by user level routines.
352 */
353 static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)354 getopt_internal(int nargc, char * const *nargv, const char *options,
355 const struct option *long_options, int *idx, int flags)
356 {
357 char *oli; /* option letter list index */
358 int optchar, short_too;
359 static int posixly_correct = -1;
360
361 if (options == NULL)
362 return (-1);
363
364 /*
365 * XXX Some GNU programs (like cvs) set optind to 0 instead of
366 * XXX using optreset. Work around this braindamage.
367 */
368 if (optind == 0)
369 optind = optreset = 1;
370
371 /*
372 * Disable GNU extensions if POSIXLY_CORRECT is set or options
373 * string begins with a '+'.
374 */
375 if (posixly_correct == -1 || optreset)
376 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
377 if (*options == '-')
378 flags |= FLAG_ALLARGS;
379 else if (posixly_correct || *options == '+')
380 flags &= ~FLAG_PERMUTE;
381 if (*options == '+' || *options == '-')
382 options++;
383
384 optarg = NULL;
385 if (optreset)
386 nonopt_start = nonopt_end = -1;
387 start:
388 if (optreset || !*place) { /* update scanning pointer */
389 optreset = 0;
390 if (optind >= nargc) { /* end of argument vector */
391 place = EMSG;
392 if (nonopt_end != -1) {
393 /* do permutation, if we have to */
394 permute_args(nonopt_start, nonopt_end,
395 optind, nargv);
396 optind -= nonopt_end - nonopt_start;
397 }
398 else if (nonopt_start != -1) {
399 /*
400 * If we skipped non-options, set optind
401 * to the first of them.
402 */
403 optind = nonopt_start;
404 }
405 nonopt_start = nonopt_end = -1;
406 return (-1);
407 }
408 if (*(place = nargv[optind]) != '-' ||
409 #ifdef GNU_COMPATIBLE
410 place[1] == '\0') {
411 #else
412 (place[1] == '\0' && strchr(options, '-') == NULL)) {
413 #endif
414 place = EMSG; /* found non-option */
415 if (flags & FLAG_ALLARGS) {
416 /*
417 * GNU extension:
418 * return non-option as argument to option 1
419 */
420 optarg = nargv[optind++];
421 return (INORDER);
422 }
423 if (!(flags & FLAG_PERMUTE)) {
424 /*
425 * If no permutation wanted, stop parsing
426 * at first non-option.
427 */
428 return (-1);
429 }
430 /* do permutation */
431 if (nonopt_start == -1)
432 nonopt_start = optind;
433 else if (nonopt_end != -1) {
434 permute_args(nonopt_start, nonopt_end,
435 optind, nargv);
436 nonopt_start = optind -
437 (nonopt_end - nonopt_start);
438 nonopt_end = -1;
439 }
440 optind++;
441 /* process next argument */
442 goto start;
443 }
444 if (nonopt_start != -1 && nonopt_end == -1)
445 nonopt_end = optind;
446
447 /*
448 * If we have "-" do nothing, if "--" we are done.
449 */
450 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
451 optind++;
452 place = EMSG;
453 /*
454 * We found an option (--), so if we skipped
455 * non-options, we have to permute.
456 */
457 if (nonopt_end != -1) {
458 permute_args(nonopt_start, nonopt_end,
459 optind, nargv);
460 optind -= nonopt_end - nonopt_start;
461 }
462 nonopt_start = nonopt_end = -1;
463 return (-1);
464 }
465 }
466
467 /*
468 * Check long options if:
469 * 1) we were passed some
470 * 2) the arg is not just "-"
471 * 3) either the arg starts with -- we are getopt_long_only()
472 */
473 if (long_options != NULL && place != nargv[optind] &&
474 (*place == '-' || (flags & FLAG_LONGONLY))) {
475 short_too = 0;
476 #ifdef GNU_COMPATIBLE
477 dash_prefix = D_PREFIX;
478 #endif
479 if (*place == '-') {
480 place++; /* --foo long option */
481 if (*place == '\0')
482 return (BADARG); /* malformed option */
483 #ifdef GNU_COMPATIBLE
484 dash_prefix = DD_PREFIX;
485 #endif
486 } else if (*place != ':' && strchr(options, *place) != NULL)
487 short_too = 1; /* could be short option too */
488
489 optchar = parse_long_options(nargv, options, long_options,
490 idx, short_too, flags);
491 if (optchar != -1) {
492 place = EMSG;
493 return (optchar);
494 }
495 }
496
497 if ((optchar = (int)*place++) == (int)':' ||
498 (optchar == (int)'-' && *place != '\0') ||
499 (oli = strchr(options, optchar)) == NULL) {
500 /*
501 * If the user specified "-" and '-' isn't listed in
502 * options, return -1 (non-option) as per POSIX.
503 * Otherwise, it is an unknown option character (or ':').
504 */
505 if (optchar == (int)'-' && *place == '\0')
506 return (-1);
507 if (!*place)
508 ++optind;
509 #ifdef GNU_COMPATIBLE
510 if (PRINT_ERROR)
511 warnx(posixly_correct ? illoptchar : gnuoptchar,
512 optchar);
513 #else
514 if (PRINT_ERROR)
515 warnx(illoptchar, optchar);
516 #endif
517 optopt = optchar;
518 return (BADCH);
519 }
520 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
521 /* -W long-option */
522 if (*place) /* no space */
523 /* NOTHING */;
524 else if (++optind >= nargc) { /* no arg */
525 place = EMSG;
526 if (PRINT_ERROR)
527 warnx(recargchar, optchar);
528 optopt = optchar;
529 return (BADARG);
530 } else /* white space */
531 place = nargv[optind];
532 #ifdef GNU_COMPATIBLE
533 dash_prefix = W_PREFIX;
534 #endif
535 optchar = parse_long_options(nargv, options, long_options,
536 idx, 0, flags);
537 place = EMSG;
538 return (optchar);
539 }
540 if (*++oli != ':') { /* doesn't take argument */
541 if (!*place)
542 ++optind;
543 } else { /* takes (optional) argument */
544 optarg = NULL;
545 if (*place) /* no white space */
546 optarg = place;
547 else if (oli[1] != ':') { /* arg not optional */
548 if (++optind >= nargc) { /* no arg */
549 place = EMSG;
550 if (PRINT_ERROR)
551 warnx(recargchar, optchar);
552 optopt = optchar;
553 return (BADARG);
554 } else
555 optarg = nargv[optind];
556 }
557 place = EMSG;
558 ++optind;
559 }
560 /* dump back option letter */
561 return (optchar);
562 }
563
564 #ifdef REPLACE_GETOPT
565 /*
566 * getopt --
567 * Parse argc/argv argument vector.
568 *
569 * [eventually this will replace the BSD getopt]
570 */
571 int
572 getopt(int nargc, char * const *nargv, const char *options)
573 {
574
575 /*
576 * We don't pass FLAG_PERMUTE to getopt_internal() since
577 * the BSD getopt(3) (unlike GNU) has never done this.
578 *
579 * Furthermore, since many privileged programs call getopt()
580 * before dropping privileges it makes sense to keep things
581 * as simple (and bug-free) as possible.
582 */
583 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
584 }
585 #endif /* REPLACE_GETOPT */
586
587 /*
588 * getopt_long --
589 * Parse argc/argv argument vector.
590 */
591 int
592 getopt_long(int nargc, char * const *nargv, const char *options,
593 const struct option *long_options, int *idx)
594 {
595
596 return (getopt_internal(nargc, nargv, options, long_options, idx,
597 FLAG_PERMUTE));
598 }
599
600 /*
601 * getopt_long_only --
602 * Parse argc/argv argument vector.
603 */
604 int
605 getopt_long_only(int nargc, char * const *nargv, const char *options,
606 const struct option *long_options, int *idx)
607 {
608
609 return (getopt_internal(nargc, nargv, options, long_options, idx,
610 FLAG_PERMUTE|FLAG_LONGONLY));
611 }
612