xref: /openbsd-src/lib/libc/stdlib/getopt_long.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: getopt_long.c,v 1.14 2003/06/17 21:56:24 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  * 3. All advertising materials mentioning features or use of this software
39  *    must display the following acknowledgement:
40  *        This product includes software developed by the NetBSD
41  *        Foundation, Inc. and its contributors.
42  * 4. Neither the name of The NetBSD Foundation nor the names of its
43  *    contributors may be used to endorse or promote products derived
44  *    from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56  * POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #if defined(LIBC_SCCS) && !defined(lint)
60 static char *rcsid = "$OpenBSD: getopt_long.c,v 1.14 2003/06/17 21:56:24 millert Exp $";
61 #endif /* LIBC_SCCS and not lint */
62 
63 #include <err.h>
64 #include <errno.h>
65 #include <getopt.h>
66 #include <stdlib.h>
67 #include <string.h>
68 
69 #define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
70 
71 #ifdef REPLACE_GETOPT
72 int	opterr = 1;		/* if error message should be printed */
73 int	optind = 1;		/* index into parent argv vector */
74 int	optopt = '?';		/* character checked for validity */
75 int	optreset;		/* reset getopt */
76 char    *optarg;		/* argument associated with option */
77 #endif
78 
79 #define PRINT_ERROR	((opterr) && (*options != ':'))
80 
81 #define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
82 #define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
83 #define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
84 
85 /* return values */
86 #define	BADCH		(int)'?'
87 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
88 #define	INORDER 	(int)1
89 
90 #define	EMSG		""
91 
92 static int getopt_internal(int, char * const *, const char *,
93 			   const struct option *, int *, int);
94 static int parse_long_options(char * const *, const char *,
95 			      const struct option *, int *, int);
96 static int gcd(int, int);
97 static void permute_args(int, int, int, char * const *);
98 
99 static char *place = EMSG; /* option letter processing */
100 
101 /* XXX: set optreset to 1 rather than these two */
102 static int nonopt_start = -1; /* first non option argument (for permute) */
103 static int nonopt_end = -1;   /* first option after non options (for permute) */
104 
105 /* Error messages */
106 static const char recargchar[] = "option requires an argument -- %c";
107 static const char recargstring[] = "option requires an argument -- %s";
108 static const char ambig[] = "ambiguous option -- %.*s";
109 static const char noarg[] = "option doesn't take an argument -- %.*s";
110 static const char illoptchar[] = "unknown option -- %c";
111 static const char illoptstring[] = "unknown option -- %s";
112 
113 /*
114  * Compute the greatest common divisor of a and b.
115  */
116 static int
117 gcd(int a, int b)
118 {
119 	int c;
120 
121 	c = a % b;
122 	while (c != 0) {
123 		a = b;
124 		b = c;
125 		c = a % b;
126 	}
127 
128 	return (b);
129 }
130 
131 /*
132  * Exchange the block from nonopt_start to nonopt_end with the block
133  * from nonopt_end to opt_end (keeping the same order of arguments
134  * in each block).
135  */
136 static void
137 permute_args(int panonopt_start, int panonopt_end, int opt_end,
138 	char * const *nargv)
139 {
140 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
141 	char *swap;
142 
143 	/*
144 	 * compute lengths of blocks and number and size of cycles
145 	 */
146 	nnonopts = panonopt_end - panonopt_start;
147 	nopts = opt_end - panonopt_end;
148 	ncycle = gcd(nnonopts, nopts);
149 	cyclelen = (opt_end - panonopt_start) / ncycle;
150 
151 	for (i = 0; i < ncycle; i++) {
152 		cstart = panonopt_end+i;
153 		pos = cstart;
154 		for (j = 0; j < cyclelen; j++) {
155 			if (pos >= panonopt_end)
156 				pos -= nnonopts;
157 			else
158 				pos += nopts;
159 			swap = nargv[pos];
160 			/* LINTED const cast */
161 			((char **) nargv)[pos] = nargv[cstart];
162 			/* LINTED const cast */
163 			((char **)nargv)[cstart] = swap;
164 		}
165 	}
166 }
167 
168 /*
169  * parse_long_options --
170  *	Parse long options in argc/argv argument vector.
171  * Returns -1 if short_too is set and the option does not match long_options.
172  */
173 static int
174 parse_long_options(char * const *nargv, const char *options,
175 	const struct option *long_options, int *idx, int short_too)
176 {
177 	char *current_argv, *has_equal;
178 	size_t current_argv_len;
179 	int i, match;
180 
181 	current_argv = place;
182 	match = -1;
183 
184 	optind++;
185 
186 	if ((has_equal = strchr(current_argv, '=')) != NULL) {
187 		/* argument found (--option=arg) */
188 		current_argv_len = has_equal - current_argv;
189 		has_equal++;
190 	} else
191 		current_argv_len = strlen(current_argv);
192 
193 	for (i = 0; long_options[i].name; i++) {
194 		/* find matching long option */
195 		if (strncmp(current_argv, long_options[i].name,
196 		    current_argv_len))
197 			continue;
198 
199 		if (strlen(long_options[i].name) == current_argv_len) {
200 			/* exact match */
201 			match = i;
202 			break;
203 		}
204 		/*
205 		 * If this is a known short option, don't allow
206 		 * a partial match of a single character.
207 		 */
208 		if (short_too && current_argv_len == 1)
209 			continue;
210 
211 		if (match == -1)	/* partial match */
212 			match = i;
213 		else {
214 			/* ambiguous abbreviation */
215 			if (PRINT_ERROR)
216 				warnx(ambig, (int)current_argv_len,
217 				     current_argv);
218 			optopt = 0;
219 			return (BADCH);
220 		}
221 	}
222 	if (match != -1) {		/* option found */
223 		if (long_options[match].has_arg == no_argument
224 		    && has_equal) {
225 			if (PRINT_ERROR)
226 				warnx(noarg, (int)current_argv_len,
227 				     current_argv);
228 			/*
229 			 * XXX: GNU sets optopt to val regardless of flag
230 			 */
231 			if (long_options[match].flag == NULL)
232 				optopt = long_options[match].val;
233 			else
234 				optopt = 0;
235 			return (BADARG);
236 		}
237 		if (long_options[match].has_arg == required_argument ||
238 		    long_options[match].has_arg == optional_argument) {
239 			if (has_equal)
240 				optarg = has_equal;
241 			else if (long_options[match].has_arg ==
242 			    required_argument) {
243 				/*
244 				 * optional argument doesn't use next nargv
245 				 */
246 				optarg = nargv[optind++];
247 			}
248 		}
249 		if ((long_options[match].has_arg == required_argument)
250 		    && (optarg == NULL)) {
251 			/*
252 			 * Missing argument; leading ':' indicates no error
253 			 * should be generated.
254 			 */
255 			if (PRINT_ERROR)
256 				warnx(recargstring,
257 				    current_argv);
258 			/*
259 			 * XXX: GNU sets optopt to val regardless of flag
260 			 */
261 			if (long_options[match].flag == NULL)
262 				optopt = long_options[match].val;
263 			else
264 				optopt = 0;
265 			--optind;
266 			return (BADARG);
267 		}
268 	} else {			/* unknown option */
269 		if (short_too) {
270 			--optind;
271 			return (-1);
272 		}
273 		if (PRINT_ERROR)
274 			warnx(illoptstring, current_argv);
275 		optopt = 0;
276 		return (BADCH);
277 	}
278 	if (idx)
279 		*idx = match;
280 	if (long_options[match].flag) {
281 		*long_options[match].flag = long_options[match].val;
282 		return (0);
283 	} else
284 		return (long_options[match].val);
285 }
286 
287 /*
288  * getopt_internal --
289  *	Parse argc/argv argument vector.  Called by user level routines.
290  */
291 static int
292 getopt_internal(int nargc, char * const *nargv, const char *options,
293 	const struct option *long_options, int *idx, int flags)
294 {
295 	char *oli;				/* option letter list index */
296 	int optchar, short_too;
297 	static int posixly_correct = -1;
298 
299 	if (options == NULL)
300 		return (-1);
301 
302 	/*
303 	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
304 	 * string begins with a '+'.
305 	 */
306 	if (posixly_correct == -1)
307 		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
308 	if (posixly_correct || *options == '+')
309 		flags &= ~FLAG_PERMUTE;
310 	else if (*options == '-')
311 		flags |= FLAG_ALLARGS;
312 	if (*options == '+' || *options == '-')
313 		options++;
314 
315 	/*
316 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
317 	 * XXX using optreset.  Work around this braindamage.
318 	 */
319 	if (optind == 0)
320 		optind = optreset = 1;
321 
322 	optarg = NULL;
323 	if (optreset)
324 		nonopt_start = nonopt_end = -1;
325 start:
326 	if (optreset || !*place) {		/* update scanning pointer */
327 		optreset = 0;
328 		if (optind >= nargc) {          /* end of argument vector */
329 			place = EMSG;
330 			if (nonopt_end != -1) {
331 				/* do permutation, if we have to */
332 				permute_args(nonopt_start, nonopt_end,
333 				    optind, nargv);
334 				optind -= nonopt_end - nonopt_start;
335 			}
336 			else if (nonopt_start != -1) {
337 				/*
338 				 * If we skipped non-options, set optind
339 				 * to the first of them.
340 				 */
341 				optind = nonopt_start;
342 			}
343 			nonopt_start = nonopt_end = -1;
344 			return (-1);
345 		}
346 		if (*(place = nargv[optind]) != '-' ||
347 		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
348 			place = EMSG;		/* found non-option */
349 			if (flags & FLAG_ALLARGS) {
350 				/*
351 				 * GNU extension:
352 				 * return non-option as argument to option 1
353 				 */
354 				optarg = nargv[optind++];
355 				return (INORDER);
356 			}
357 			if (!(flags & FLAG_PERMUTE)) {
358 				/*
359 				 * If no permutation wanted, stop parsing
360 				 * at first non-option.
361 				 */
362 				return (-1);
363 			}
364 			/* do permutation */
365 			if (nonopt_start == -1)
366 				nonopt_start = optind;
367 			else if (nonopt_end != -1) {
368 				permute_args(nonopt_start, nonopt_end,
369 				    optind, nargv);
370 				nonopt_start = optind -
371 				    (nonopt_end - nonopt_start);
372 				nonopt_end = -1;
373 			}
374 			optind++;
375 			/* process next argument */
376 			goto start;
377 		}
378 		if (nonopt_start != -1 && nonopt_end == -1)
379 			nonopt_end = optind;
380 
381 		/*
382 		 * Check for "--" or "--foo" with no long options
383 		 * but if place is simply "-" leave it unmolested.
384 		 */
385 		if (place[1] != '\0' && *++place == '-' &&
386 		    (place[1] == '\0' || long_options == NULL)) {
387 			optind++;
388 			place = EMSG;
389 			/*
390 			 * We found an option (--), so if we skipped
391 			 * non-options, we have to permute.
392 			 */
393 			if (nonopt_end != -1) {
394 				permute_args(nonopt_start, nonopt_end,
395 				    optind, nargv);
396 				optind -= nonopt_end - nonopt_start;
397 			}
398 			nonopt_start = nonopt_end = -1;
399 			return (-1);
400 		}
401 	}
402 
403 	/*
404 	 * Check long options if:
405 	 *  1) we were passed some
406 	 *  2) the arg is not just "-"
407 	 *  3) either the arg starts with -- we are getopt_long_only()
408 	 */
409 	if (long_options != NULL && place != nargv[optind] &&
410 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
411 		short_too = 0;
412 		if (*place == '-')
413 			place++;		/* --foo long option */
414 		else if (*place != ':' && strchr(options, *place) != NULL)
415 			short_too = 1;		/* could be short option too */
416 
417 		optchar = parse_long_options(nargv, options, long_options,
418 		    idx, short_too);
419 		if (optchar != -1) {
420 			place = EMSG;
421 			return (optchar);
422 		}
423 	}
424 
425 	if ((optchar = (int)*place++) == (int)':' ||
426 	    (oli = strchr(options, optchar)) == NULL) {
427 		/*
428 		 * If the user didn't specify '-' as an option,
429 		 * assume it means -1 as POSIX specifies.
430 		 */
431 		if (optchar == (int)'-')
432 			return (-1);
433 		/* option letter unknown or ':' */
434 		if (!*place)
435 			++optind;
436 		if (PRINT_ERROR)
437 			warnx(illoptchar, optchar);
438 		optopt = optchar;
439 		return (BADCH);
440 	}
441 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
442 		/* -W long-option */
443 		if (*place)			/* no space */
444 			/* NOTHING */;
445 		else if (++optind >= nargc) {	/* no arg */
446 			place = EMSG;
447 			if (PRINT_ERROR)
448 				warnx(recargchar, optchar);
449 			optopt = optchar;
450 			return (BADARG);
451 		} else				/* white space */
452 			place = nargv[optind];
453 		optchar = parse_long_options(nargv, options, long_options,
454 		    idx, 0);
455 		place = EMSG;
456 		return (optchar);
457 	}
458 	if (*++oli != ':') {			/* doesn't take argument */
459 		if (!*place)
460 			++optind;
461 	} else {				/* takes (optional) argument */
462 		optarg = NULL;
463 		if (*place)			/* no white space */
464 			optarg = place;
465 		/* XXX: disable test for :: if PC? (GNU doesn't) */
466 		else if (oli[1] != ':') {	/* arg not optional */
467 			if (++optind >= nargc) {	/* no arg */
468 				place = EMSG;
469 				if (PRINT_ERROR)
470 					warnx(recargchar, optchar);
471 				optopt = optchar;
472 				return (BADARG);
473 			} else
474 				optarg = nargv[optind];
475 		}
476 		place = EMSG;
477 		++optind;
478 	}
479 	/* dump back option letter */
480 	return (optchar);
481 }
482 
483 #ifdef REPLACE_GETOPT
484 /*
485  * getopt --
486  *	Parse argc/argv argument vector.
487  *
488  * [eventually this will replace the BSD getopt]
489  */
490 int
491 getopt(int nargc, char * const *nargv, const char *options)
492 {
493 
494 	/*
495 	 * We dont' pass FLAG_PERMUTE to getopt_internal() since
496 	 * the BSD getopt(3) (unlike GNU) has never done this.
497 	 *
498 	 * Furthermore, since many privileged programs call getopt()
499 	 * before dropping privileges it makes sense to keep things
500 	 * as simple (and bug-free) as possible.
501 	 */
502 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
503 }
504 #endif /* REPLACE_GETOPT */
505 
506 /*
507  * getopt_long --
508  *	Parse argc/argv argument vector.
509  */
510 int
511 getopt_long(nargc, nargv, options, long_options, idx)
512 	int nargc;
513 	char * const *nargv;
514 	const char *options;
515 	const struct option *long_options;
516 	int *idx;
517 {
518 
519 	return (getopt_internal(nargc, nargv, options, long_options, idx,
520 	    FLAG_PERMUTE));
521 }
522 
523 /*
524  * getopt_long_only --
525  *	Parse argc/argv argument vector.
526  */
527 int
528 getopt_long_only(nargc, nargv, options, long_options, idx)
529 	int nargc;
530 	char * const *nargv;
531 	const char *options;
532 	const struct option *long_options;
533 	int *idx;
534 {
535 
536 	return (getopt_internal(nargc, nargv, options, long_options, idx,
537 	    FLAG_PERMUTE|FLAG_LONGONLY));
538 }
539