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