xref: /freebsd/lib/libc/stdlib/getopt_long.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
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 <Todd.Miller@courtesan.com>
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 <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59 
60 #include <err.h>
61 #include <errno.h>
62 #include <getopt.h>
63 #include <stdlib.h>
64 #include <string.h>
65 
66 #define GNU_COMPATIBLE		/* Be more compatible, configure's use us! */
67 
68 #if 0				/* we prefer to keep our getopt(3) */
69 #define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
70 #endif
71 
72 #ifdef REPLACE_GETOPT
73 int	opterr = 1;		/* if error message should be printed */
74 int	optind = 1;		/* index into parent argv vector */
75 int	optopt = '?';		/* character checked for validity */
76 int	optreset;		/* reset getopt */
77 char    *optarg;		/* argument associated with option */
78 #endif
79 
80 #define PRINT_ERROR	((opterr) && (*options != ':'))
81 
82 #define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
83 #define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
84 #define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
85 
86 /* return values */
87 #define	BADCH		(int)'?'
88 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
89 #define	INORDER 	(int)1
90 
91 static char EMSG[] = "";
92 
93 #ifdef GNU_COMPATIBLE
94 #define NO_PREFIX	(-1)
95 #define D_PREFIX	0
96 #define DD_PREFIX	1
97 #define W_PREFIX	2
98 #endif
99 
100 static int getopt_internal(int, char * const *, const char *,
101 			   const struct option *, int *, int);
102 static int parse_long_options(char * const *, const char *,
103 			      const struct option *, int *, int, int);
104 static int gcd(int, int);
105 static void permute_args(int, int, int, char * const *);
106 
107 static char *place = EMSG; /* option letter processing */
108 
109 /* XXX: set optreset to 1 rather than these two */
110 static int nonopt_start = -1; /* first non option argument (for permute) */
111 static int nonopt_end = -1;   /* first option after non options (for permute) */
112 
113 /* Error messages */
114 static const char recargchar[] = "option requires an argument -- %c";
115 static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
116 #ifdef GNU_COMPATIBLE
117 static int dash_prefix = NO_PREFIX;
118 static const char gnuoptchar[] = "invalid option -- %c";
119 
120 static const char recargstring[] = "option `%s%s' requires an argument";
121 static const char ambig[] = "option `%s%.*s' is ambiguous";
122 static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
123 static const char illoptstring[] = "unrecognized option `%s%s'";
124 #else
125 static const char recargstring[] = "option requires an argument -- %s";
126 static const char ambig[] = "ambiguous option -- %.*s";
127 static const char noarg[] = "option doesn't take an argument -- %.*s";
128 static const char illoptstring[] = "unknown option -- %s";
129 #endif
130 
131 /*
132  * Compute the greatest common divisor of a and b.
133  */
134 static int
135 gcd(int a, int b)
136 {
137 	int c;
138 
139 	c = a % b;
140 	while (c != 0) {
141 		a = b;
142 		b = c;
143 		c = a % b;
144 	}
145 
146 	return (b);
147 }
148 
149 /*
150  * Exchange the block from nonopt_start to nonopt_end with the block
151  * from nonopt_end to opt_end (keeping the same order of arguments
152  * in each block).
153  */
154 static void
155 permute_args(int panonopt_start, int panonopt_end, int opt_end,
156 	char * const *nargv)
157 {
158 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
159 	char *swap;
160 
161 	/*
162 	 * compute lengths of blocks and number and size of cycles
163 	 */
164 	nnonopts = panonopt_end - panonopt_start;
165 	nopts = opt_end - panonopt_end;
166 	ncycle = gcd(nnonopts, nopts);
167 	cyclelen = (opt_end - panonopt_start) / ncycle;
168 
169 	for (i = 0; i < ncycle; i++) {
170 		cstart = panonopt_end+i;
171 		pos = cstart;
172 		for (j = 0; j < cyclelen; j++) {
173 			if (pos >= panonopt_end)
174 				pos -= nnonopts;
175 			else
176 				pos += nopts;
177 			swap = nargv[pos];
178 			/* LINTED const cast */
179 			((char **) nargv)[pos] = nargv[cstart];
180 			/* LINTED const cast */
181 			((char **)nargv)[cstart] = swap;
182 		}
183 	}
184 }
185 
186 /*
187  * parse_long_options --
188  *	Parse long options in argc/argv argument vector.
189  * Returns -1 if short_too is set and the option does not match long_options.
190  */
191 static int
192 parse_long_options(char * const *nargv, const char *options,
193 	const struct option *long_options, int *idx, int short_too, int flags)
194 {
195 	char *current_argv, *has_equal;
196 #ifdef GNU_COMPATIBLE
197 	const char *current_dash;
198 #endif
199 	size_t current_argv_len;
200 	int i, match, exact_match, second_partial_match;
201 
202 	current_argv = place;
203 #ifdef GNU_COMPATIBLE
204 	switch (dash_prefix) {
205 		case D_PREFIX:
206 			current_dash = "-";
207 			break;
208 		case DD_PREFIX:
209 			current_dash = "--";
210 			break;
211 		case W_PREFIX:
212 			current_dash = "-W ";
213 			break;
214 		default:
215 			current_dash = "";
216 			break;
217 	}
218 #endif
219 	match = -1;
220 	exact_match = 0;
221 	second_partial_match = 0;
222 
223 	optind++;
224 
225 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
226 		/* argument found (--option=arg) */
227 		current_argv_len = has_equal - current_argv;
228 		has_equal++;
229 	} else
230 		current_argv_len = strlen(current_argv);
231 
232 	for (i = 0; long_options[i].name; i++) {
233 		/* find matching long option */
234 		if (strncmp(current_argv, long_options[i].name,
235 		    current_argv_len))
236 			continue;
237 
238 		if (strlen(long_options[i].name) == current_argv_len) {
239 			/* exact match */
240 			match = i;
241 			exact_match = 1;
242 			break;
243 		}
244 		/*
245 		 * If this is a known short option, don't allow
246 		 * a partial match of a single character.
247 		 */
248 		if (short_too && current_argv_len == 1)
249 			continue;
250 
251 		if (match == -1)	/* first partial match */
252 			match = i;
253 		else if ((flags & FLAG_LONGONLY) ||
254 			 long_options[i].has_arg !=
255 			     long_options[match].has_arg ||
256 			 long_options[i].flag != long_options[match].flag ||
257 			 long_options[i].val != long_options[match].val)
258 			second_partial_match = 1;
259 	}
260 	if (!exact_match && second_partial_match) {
261 		/* ambiguous abbreviation */
262 		if (PRINT_ERROR)
263 			warnx(ambig,
264 #ifdef GNU_COMPATIBLE
265 			     current_dash,
266 #endif
267 			     (int)current_argv_len,
268 			     current_argv);
269 		optopt = 0;
270 		return (BADCH);
271 	}
272 	if (match != -1) {		/* option found */
273 		if (long_options[match].has_arg == no_argument
274 		    && has_equal) {
275 			if (PRINT_ERROR)
276 				warnx(noarg,
277 #ifdef GNU_COMPATIBLE
278 				     current_dash,
279 #endif
280 				     (int)current_argv_len,
281 				     current_argv);
282 			/*
283 			 * XXX: GNU sets optopt to val regardless of flag
284 			 */
285 			if (long_options[match].flag == NULL)
286 				optopt = long_options[match].val;
287 			else
288 				optopt = 0;
289 #ifdef GNU_COMPATIBLE
290 			return (BADCH);
291 #else
292 			return (BADARG);
293 #endif
294 		}
295 		if (long_options[match].has_arg == required_argument ||
296 		    long_options[match].has_arg == optional_argument) {
297 			if (has_equal)
298 				optarg = has_equal;
299 			else if (long_options[match].has_arg ==
300 			    required_argument) {
301 				/*
302 				 * optional argument doesn't use next nargv
303 				 */
304 				optarg = nargv[optind++];
305 			}
306 		}
307 		if ((long_options[match].has_arg == required_argument)
308 		    && (optarg == NULL)) {
309 			/*
310 			 * Missing argument; leading ':' indicates no error
311 			 * should be generated.
312 			 */
313 			if (PRINT_ERROR)
314 				warnx(recargstring,
315 #ifdef GNU_COMPATIBLE
316 				    current_dash,
317 #endif
318 				    current_argv);
319 			/*
320 			 * XXX: GNU sets optopt to val regardless of flag
321 			 */
322 			if (long_options[match].flag == NULL)
323 				optopt = long_options[match].val;
324 			else
325 				optopt = 0;
326 			--optind;
327 			return (BADARG);
328 		}
329 	} else {			/* unknown option */
330 		if (short_too) {
331 			--optind;
332 			return (-1);
333 		}
334 		if (PRINT_ERROR)
335 			warnx(illoptstring,
336 #ifdef GNU_COMPATIBLE
337 			      current_dash,
338 #endif
339 			      current_argv);
340 		optopt = 0;
341 		return (BADCH);
342 	}
343 	if (idx)
344 		*idx = match;
345 	if (long_options[match].flag) {
346 		*long_options[match].flag = long_options[match].val;
347 		return (0);
348 	} else
349 		return (long_options[match].val);
350 }
351 
352 /*
353  * getopt_internal --
354  *	Parse argc/argv argument vector.  Called by user level routines.
355  */
356 static int
357 getopt_internal(int nargc, char * const *nargv, const char *options,
358 	const struct option *long_options, int *idx, int flags)
359 {
360 	char *oli;				/* option letter list index */
361 	int optchar, short_too;
362 	static int posixly_correct = -1;
363 
364 	if (options == NULL)
365 		return (-1);
366 
367 	/*
368 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
369 	 * XXX using optreset.  Work around this braindamage.
370 	 */
371 	if (optind == 0)
372 		optind = optreset = 1;
373 
374 	/*
375 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
376 	 * string begins with a '+'.
377 	 */
378 	if (posixly_correct == -1 || optreset)
379 		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
380 	if (*options == '-')
381 		flags |= FLAG_ALLARGS;
382 	else if (posixly_correct || *options == '+')
383 		flags &= ~FLAG_PERMUTE;
384 	if (*options == '+' || *options == '-')
385 		options++;
386 
387 	optarg = NULL;
388 	if (optreset)
389 		nonopt_start = nonopt_end = -1;
390 start:
391 	if (optreset || !*place) {		/* update scanning pointer */
392 		optreset = 0;
393 		if (optind >= nargc) {          /* end of argument vector */
394 			place = EMSG;
395 			if (nonopt_end != -1) {
396 				/* do permutation, if we have to */
397 				permute_args(nonopt_start, nonopt_end,
398 				    optind, nargv);
399 				optind -= nonopt_end - nonopt_start;
400 			}
401 			else if (nonopt_start != -1) {
402 				/*
403 				 * If we skipped non-options, set optind
404 				 * to the first of them.
405 				 */
406 				optind = nonopt_start;
407 			}
408 			nonopt_start = nonopt_end = -1;
409 			return (-1);
410 		}
411 		if (*(place = nargv[optind]) != '-' ||
412 #ifdef GNU_COMPATIBLE
413 		    place[1] == '\0') {
414 #else
415 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
416 #endif
417 			place = EMSG;		/* found non-option */
418 			if (flags & FLAG_ALLARGS) {
419 				/*
420 				 * GNU extension:
421 				 * return non-option as argument to option 1
422 				 */
423 				optarg = nargv[optind++];
424 				return (INORDER);
425 			}
426 			if (!(flags & FLAG_PERMUTE)) {
427 				/*
428 				 * If no permutation wanted, stop parsing
429 				 * at first non-option.
430 				 */
431 				return (-1);
432 			}
433 			/* do permutation */
434 			if (nonopt_start == -1)
435 				nonopt_start = optind;
436 			else if (nonopt_end != -1) {
437 				permute_args(nonopt_start, nonopt_end,
438 				    optind, nargv);
439 				nonopt_start = optind -
440 				    (nonopt_end - nonopt_start);
441 				nonopt_end = -1;
442 			}
443 			optind++;
444 			/* process next argument */
445 			goto start;
446 		}
447 		if (nonopt_start != -1 && nonopt_end == -1)
448 			nonopt_end = optind;
449 
450 		/*
451 		 * If we have "-" do nothing, if "--" we are done.
452 		 */
453 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
454 			optind++;
455 			place = EMSG;
456 			/*
457 			 * We found an option (--), so if we skipped
458 			 * non-options, we have to permute.
459 			 */
460 			if (nonopt_end != -1) {
461 				permute_args(nonopt_start, nonopt_end,
462 				    optind, nargv);
463 				optind -= nonopt_end - nonopt_start;
464 			}
465 			nonopt_start = nonopt_end = -1;
466 			return (-1);
467 		}
468 	}
469 
470 	/*
471 	 * Check long options if:
472 	 *  1) we were passed some
473 	 *  2) the arg is not just "-"
474 	 *  3) either the arg starts with -- we are getopt_long_only()
475 	 */
476 	if (long_options != NULL && place != nargv[optind] &&
477 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
478 		short_too = 0;
479 #ifdef GNU_COMPATIBLE
480 		dash_prefix = D_PREFIX;
481 #endif
482 		if (*place == '-') {
483 			place++;		/* --foo long option */
484 			if (*place == '\0')
485 				return (BADARG);	/* malformed option */
486 #ifdef GNU_COMPATIBLE
487 			dash_prefix = DD_PREFIX;
488 #endif
489 		} else if (*place != ':' && strchr(options, *place) != NULL)
490 			short_too = 1;		/* could be short option too */
491 
492 		optchar = parse_long_options(nargv, options, long_options,
493 		    idx, short_too, flags);
494 		if (optchar != -1) {
495 			place = EMSG;
496 			return (optchar);
497 		}
498 	}
499 
500 	if ((optchar = (int)*place++) == (int)':' ||
501 	    (optchar == (int)'-' && *place != '\0') ||
502 	    (oli = strchr(options, optchar)) == NULL) {
503 		/*
504 		 * If the user specified "-" and  '-' isn't listed in
505 		 * options, return -1 (non-option) as per POSIX.
506 		 * Otherwise, it is an unknown option character (or ':').
507 		 */
508 		if (optchar == (int)'-' && *place == '\0')
509 			return (-1);
510 		if (!*place)
511 			++optind;
512 #ifdef GNU_COMPATIBLE
513 		if (PRINT_ERROR)
514 			warnx(posixly_correct ? illoptchar : gnuoptchar,
515 			      optchar);
516 #else
517 		if (PRINT_ERROR)
518 			warnx(illoptchar, optchar);
519 #endif
520 		optopt = optchar;
521 		return (BADCH);
522 	}
523 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
524 		/* -W long-option */
525 		if (*place)			/* no space */
526 			/* NOTHING */;
527 		else if (++optind >= nargc) {	/* no arg */
528 			place = EMSG;
529 			if (PRINT_ERROR)
530 				warnx(recargchar, optchar);
531 			optopt = optchar;
532 			return (BADARG);
533 		} else				/* white space */
534 			place = nargv[optind];
535 #ifdef GNU_COMPATIBLE
536 		dash_prefix = W_PREFIX;
537 #endif
538 		optchar = parse_long_options(nargv, options, long_options,
539 		    idx, 0, flags);
540 		place = EMSG;
541 		return (optchar);
542 	}
543 	if (*++oli != ':') {			/* doesn't take argument */
544 		if (!*place)
545 			++optind;
546 	} else {				/* takes (optional) argument */
547 		optarg = NULL;
548 		if (*place)			/* no white space */
549 			optarg = place;
550 		else if (oli[1] != ':') {	/* arg not optional */
551 			if (++optind >= nargc) {	/* no arg */
552 				place = EMSG;
553 				if (PRINT_ERROR)
554 					warnx(recargchar, optchar);
555 				optopt = optchar;
556 				return (BADARG);
557 			} else
558 				optarg = nargv[optind];
559 		}
560 		place = EMSG;
561 		++optind;
562 	}
563 	/* dump back option letter */
564 	return (optchar);
565 }
566 
567 #ifdef REPLACE_GETOPT
568 /*
569  * getopt --
570  *	Parse argc/argv argument vector.
571  *
572  * [eventually this will replace the BSD getopt]
573  */
574 int
575 getopt(int nargc, char * const *nargv, const char *options)
576 {
577 
578 	/*
579 	 * We don't pass FLAG_PERMUTE to getopt_internal() since
580 	 * the BSD getopt(3) (unlike GNU) has never done this.
581 	 *
582 	 * Furthermore, since many privileged programs call getopt()
583 	 * before dropping privileges it makes sense to keep things
584 	 * as simple (and bug-free) as possible.
585 	 */
586 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
587 }
588 #endif /* REPLACE_GETOPT */
589 
590 /*
591  * getopt_long --
592  *	Parse argc/argv argument vector.
593  */
594 int
595 getopt_long(int nargc, char * const *nargv, const char *options,
596 	const struct option *long_options, int *idx)
597 {
598 
599 	return (getopt_internal(nargc, nargv, options, long_options, idx,
600 	    FLAG_PERMUTE));
601 }
602 
603 /*
604  * getopt_long_only --
605  *	Parse argc/argv argument vector.
606  */
607 int
608 getopt_long_only(int nargc, char * const *nargv, const char *options,
609 	const struct option *long_options, int *idx)
610 {
611 
612 	return (getopt_internal(nargc, nargv, options, long_options, idx,
613 	    FLAG_PERMUTE|FLAG_LONGONLY));
614 }
615