GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/stdlib/getopt_long.c Lines: 48 183 26.2 %
Date: 2017-11-07 Branches: 43 182 23.6 %

Line Branch Exec Source
1
/*	$OpenBSD: getopt_long.c,v 1.29 2015/10/01 02:32:07 guenther 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
#include <err.h>
53
#include <errno.h>
54
#include <getopt.h>
55
#include <stdlib.h>
56
#include <string.h>
57
58
int	opterr = 1;		/* if error message should be printed */
59
int	optind = 1;		/* index into parent argv vector */
60
int	optopt = '?';		/* character checked for validity */
61
int	optreset;		/* reset getopt */
62
char    *optarg;		/* argument associated with option */
63
64
#if 0
65
/* DEF_* only work on initialized (non-COMMON) variables */
66
DEF_WEAK(opterr);
67
DEF_WEAK(optind);
68
DEF_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
84
static int getopt_internal(int, char * const *, const char *,
85
			   const struct option *, int *, int);
86
static int parse_long_options(char * const *, const char *,
87
			      const struct option *, int *, int, int);
88
static int gcd(int, int);
89
static void permute_args(int, int, int, char * const *);
90
91
static char *place = EMSG; /* option letter processing */
92
93
/* XXX: set optreset to 1 rather than these two */
94
static int nonopt_start = -1; /* first non option argument (for permute) */
95
static int nonopt_end = -1;   /* first option after non options (for permute) */
96
97
/* Error messages */
98
static const char recargchar[] = "option requires an argument -- %c";
99
static const char recargstring[] = "option requires an argument -- %s";
100
static const char ambig[] = "ambiguous option -- %.*s";
101
static const char noarg[] = "option doesn't take an argument -- %.*s";
102
static const char illoptchar[] = "unknown option -- %c";
103
static const char illoptstring[] = "unknown option -- %s";
104
105
/*
106
 * Compute the greatest common divisor of a and b.
107
 */
108
static int
109
gcd(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
 */
128
static void
129
permute_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
 */
163
static int
164
parse_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
 */
288
static int
289
getopt_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
56556
	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
28278
	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
28278
	if (posixly_correct == -1 || optreset)
311
14702
		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
312
28278
	if (*options == '-')
313
		flags |= FLAG_ALLARGS;
314

56556
	else if (posixly_correct || *options == '+')
315
		flags &= ~FLAG_PERMUTE;
316

56556
	if (*options == '+' || *options == '-')
317
		options++;
318
319
28278
	optarg = NULL;
320
28278
	if (optreset)
321
120
		nonopt_start = nonopt_end = -1;
322
start:
323

56436
	if (optreset || !*place) {		/* update scanning pointer */
324
25925
		optreset = 0;
325
25925
		if (optind >= nargc) {          /* end of argument vector */
326
2753
			place = EMSG;
327
2753
			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
2753
			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
2753
			nonopt_start = nonopt_end = -1;
341
2753
			return (-1);
342
		}
343

23172
		if (*(place = nargv[optind]) != '-' ||
344
11267
		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
345
11905
			place = EMSG;		/* found non-option */
346
11905
			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
11905
			if (!(flags & FLAG_PERMUTE)) {
355
				/*
356
				 * If no permutation wanted, stop parsing
357
				 * at first non-option.
358
				 */
359
11905
				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
11267
		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

22558
		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
382
24
			optind++;
383
24
			place = EMSG;
384
			/*
385
			 * We found an option (--), so if we skipped
386
			 * non-options, we have to permute.
387
			 */
388
24
			if (nonopt_end != -1) {
389
				permute_args(nonopt_start, nonopt_end,
390
				    optind, nargv);
391
				optind -= nonopt_end - nonopt_start;
392
			}
393
24
			nonopt_start = nonopt_end = -1;
394
24
			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

13596
	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

27192
	if ((optchar = (int)*place++) == (int)':' ||
421

13596
	    (optchar == (int)'-' && *place != '\0') ||
422
13596
	    (oli = strchr(options, optchar)) == NULL) {
423
		/*
424
		 * If the user specified "-" and  '-' isn't listed in
425
		 * options, return -1 (non-option) as per POSIX.
426
		 * Otherwise, it is an unknown option character (or ':').
427
		 */
428
		if (optchar == (int)'-' && *place == '\0')
429
			return (-1);
430
		if (!*place)
431
			++optind;
432
		if (PRINT_ERROR)
433
			warnx(illoptchar, optchar);
434
		optopt = optchar;
435
		return (BADCH);
436
	}
437

13596
	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
438
		/* -W long-option */
439
		if (*place)			/* no space */
440
			/* NOTHING */;
441
		else if (++optind >= nargc) {	/* no arg */
442
			place = EMSG;
443
			if (PRINT_ERROR)
444
				warnx(recargchar, optchar);
445
			optopt = optchar;
446
			return (BADARG);
447
		} else				/* white space */
448
			place = nargv[optind];
449
		optchar = parse_long_options(nargv, options, long_options,
450
		    idx, 0, flags);
451
		place = EMSG;
452
		return (optchar);
453
	}
454
13596
	if (*++oli != ':') {			/* doesn't take argument */
455
9577
		if (!*place)
456
			++optind;
457
	} else {				/* takes (optional) argument */
458
4019
		optarg = NULL;
459
4019
		if (*place)			/* no white space */
460
			optarg = place;
461
4017
		else if (oli[1] != ':') {	/* arg not optional */
462
4017
			if (++optind >= nargc) {	/* no arg */
463
				place = EMSG;
464
				if (PRINT_ERROR)
465
					warnx(recargchar, optchar);
466
				optopt = optchar;
467
				return (BADARG);
468
			} else
469
4017
				optarg = nargv[optind];
470
4017
		}
471
8038
		place = EMSG;
472
		++optind;
473
	}
474
	/* dump back option letter */
475
24839
	return (optchar);
476
28278
}
477
478
/*
479
 * getopt --
480
 *	Parse argc/argv argument vector.
481
 *
482
 * [eventually this will replace the BSD getopt]
483
 */
484
int
485
getopt(int nargc, char * const *nargv, const char *options)
486
{
487
488
	/*
489
	 * We don't pass FLAG_PERMUTE to getopt_internal() since
490
	 * the BSD getopt(3) (unlike GNU) has never done this.
491
	 *
492
	 * Furthermore, since many privileged programs call getopt()
493
	 * before dropping privileges it makes sense to keep things
494
	 * as simple (and bug-free) as possible.
495
	 */
496
56556
	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
497
}
498
499
/*
500
 * getopt_long --
501
 *	Parse argc/argv argument vector.
502
 */
503
int
504
getopt_long(int nargc, char * const *nargv, const char *options,
505
    const struct option *long_options, int *idx)
506
{
507
508
	return (getopt_internal(nargc, nargv, options, long_options, idx,
509
	    FLAG_PERMUTE));
510
}
511
512
/*
513
 * getopt_long_only --
514
 *	Parse argc/argv argument vector.
515
 */
516
int
517
getopt_long_only(int nargc, char * const *nargv, const char *options,
518
    const struct option *long_options, int *idx)
519
{
520
521
	return (getopt_internal(nargc, nargv, options, long_options, idx,
522
	    FLAG_PERMUTE|FLAG_LONGONLY));
523
}