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