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