xref: /openbsd-src/lib/libc/stdlib/getopt_long.c (revision 33b4f39fbeffad07bc3206f173cff9f3c9901cd1)
1 /*	$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 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.16 2004/02/04 18:17:25 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 		 * If we have "-" do nothing, if "--" we are done.
383 		 */
384 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
385 			optind++;
386 			place = EMSG;
387 			/*
388 			 * We found an option (--), so if we skipped
389 			 * non-options, we have to permute.
390 			 */
391 			if (nonopt_end != -1) {
392 				permute_args(nonopt_start, nonopt_end,
393 				    optind, nargv);
394 				optind -= nonopt_end - nonopt_start;
395 			}
396 			nonopt_start = nonopt_end = -1;
397 			return (-1);
398 		}
399 	}
400 
401 	/*
402 	 * Check long options if:
403 	 *  1) we were passed some
404 	 *  2) the arg is not just "-"
405 	 *  3) either the arg starts with -- we are getopt_long_only()
406 	 */
407 	if (long_options != NULL && place != nargv[optind] &&
408 	    (*place == '-' || (flags & FLAG_LONGONLY))) {
409 		short_too = 0;
410 		if (*place == '-')
411 			place++;		/* --foo long option */
412 		else if (*place != ':' && strchr(options, *place) != NULL)
413 			short_too = 1;		/* could be short option too */
414 
415 		optchar = parse_long_options(nargv, options, long_options,
416 		    idx, short_too);
417 		if (optchar != -1) {
418 			place = EMSG;
419 			return (optchar);
420 		}
421 	}
422 
423 	if ((optchar = (int)*place++) == (int)':' ||
424 	    optchar == (int)'-' && *place != '\0' ||
425 	    (oli = strchr(options, optchar)) == NULL) {
426 		/*
427 		 * If the user specified "-" and  '-' isn't listed in
428 		 * options, return -1 (non-option) as per POSIX.
429 		 * Otherwise, it is an unknown option character (or ':').
430 		 */
431 		if (optchar == (int)'-' && *place == '\0')
432 			return (-1);
433 		if (!*place)
434 			++optind;
435 		if (PRINT_ERROR)
436 			warnx(illoptchar, optchar);
437 		optopt = optchar;
438 		return (BADCH);
439 	}
440 	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
441 		/* -W long-option */
442 		if (*place)			/* no space */
443 			/* NOTHING */;
444 		else if (++optind >= nargc) {	/* no arg */
445 			place = EMSG;
446 			if (PRINT_ERROR)
447 				warnx(recargchar, optchar);
448 			optopt = optchar;
449 			return (BADARG);
450 		} else				/* white space */
451 			place = nargv[optind];
452 		optchar = parse_long_options(nargv, options, long_options,
453 		    idx, 0);
454 		place = EMSG;
455 		return (optchar);
456 	}
457 	if (*++oli != ':') {			/* doesn't take argument */
458 		if (!*place)
459 			++optind;
460 	} else {				/* takes (optional) argument */
461 		optarg = NULL;
462 		if (*place)			/* no white space */
463 			optarg = place;
464 		/* XXX: disable test for :: if PC? (GNU doesn't) */
465 		else if (oli[1] != ':') {	/* arg not optional */
466 			if (++optind >= nargc) {	/* no arg */
467 				place = EMSG;
468 				if (PRINT_ERROR)
469 					warnx(recargchar, optchar);
470 				optopt = optchar;
471 				return (BADARG);
472 			} else
473 				optarg = nargv[optind];
474 		} else if (!(flags & FLAG_PERMUTE)) {
475 			/*
476 			 * If permutation is disabled, we can accept an
477 			 * optional arg separated by whitespace.
478 			 */
479 			if (optind + 1 < nargc)
480 				optarg = nargv[++optind];
481 		}
482 		place = EMSG;
483 		++optind;
484 	}
485 	/* dump back option letter */
486 	return (optchar);
487 }
488 
489 #ifdef REPLACE_GETOPT
490 /*
491  * getopt --
492  *	Parse argc/argv argument vector.
493  *
494  * [eventually this will replace the BSD getopt]
495  */
496 int
497 getopt(int nargc, char * const *nargv, const char *options)
498 {
499 
500 	/*
501 	 * We dont' pass FLAG_PERMUTE to getopt_internal() since
502 	 * the BSD getopt(3) (unlike GNU) has never done this.
503 	 *
504 	 * Furthermore, since many privileged programs call getopt()
505 	 * before dropping privileges it makes sense to keep things
506 	 * as simple (and bug-free) as possible.
507 	 */
508 	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
509 }
510 #endif /* REPLACE_GETOPT */
511 
512 /*
513  * getopt_long --
514  *	Parse argc/argv argument vector.
515  */
516 int
517 getopt_long(nargc, nargv, options, long_options, idx)
518 	int nargc;
519 	char * const *nargv;
520 	const char *options;
521 	const struct option *long_options;
522 	int *idx;
523 {
524 
525 	return (getopt_internal(nargc, nargv, options, long_options, idx,
526 	    FLAG_PERMUTE));
527 }
528 
529 /*
530  * getopt_long_only --
531  *	Parse argc/argv argument vector.
532  */
533 int
534 getopt_long_only(nargc, nargv, options, long_options, idx)
535 	int nargc;
536 	char * const *nargv;
537 	const char *options;
538 	const struct option *long_options;
539 	int *idx;
540 {
541 
542 	return (getopt_internal(nargc, nargv, options, long_options, idx,
543 	    FLAG_PERMUTE|FLAG_LONGONLY));
544 }
545