1
/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
4
* Copyright (c) 2000 The NetBSD Foundation, Inc.
7
* This code is derived from software contributed to The NetBSD Foundation
8
* by Dieter Baron and Thomas Klausner.
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. All advertising materials mentioning features or use of this software
19
* must display the following acknowledgement:
20
* This product includes software developed by the NetBSD
21
* Foundation, Inc. and its contributors.
22
* 4. Neither the name of The NetBSD Foundation nor the names of its
23
* contributors may be used to endorse or promote products derived
24
* from this software without specific prior written permission.
26
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
* POSSIBILITY OF SUCH DAMAGE.
47
#define REPLACE_GETOPT
49
#define _DIAGASSERT(x) do {} while (0)
53
__weak_alias(getopt,_getopt)
55
int opterr = 1; /* if error message should be printed */
56
int optind = 1; /* index into parent argv vector */
57
int optopt = '?'; /* character checked for validity */
58
int optreset; /* reset getopt */
59
char *optarg; /* argument associated with option */
63
__weak_alias(getopt_long,_getopt_long)
66
char *__progname = "x264";
68
#define IGNORE_FIRST (*options == '-' || *options == '+')
69
#define PRINT_ERROR ((opterr) && ((*options != ':') \
70
|| (IGNORE_FIRST && options[1] != ':')))
72
#define IS_POSIXLY_CORRECT (getenv("POSIXLY_INCORRECT_GETOPT") == NULL)
74
#define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
75
/* XXX: GNU ignores PC if *options == '-' */
76
#define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
79
#define BADCH (int)'?'
80
#define BADARG ((IGNORE_FIRST && options[1] == ':') \
81
|| (*options == ':') ? (int)':' : (int)'?')
82
#define INORDER (int)1
86
static int getopt_internal (int, char * const *, const char *);
87
static int gcd (int, int);
88
static void permute_args (int, int, int, char * const *);
90
static char *place = EMSG; /* option letter processing */
92
/* XXX: set optreset to 1 rather than these two */
93
static int nonopt_start = -1; /* first non option argument (for permute) */
94
static int nonopt_end = -1; /* first option after non options (for permute) */
97
static const char recargchar[] = "option requires an argument -- %c";
98
static const char recargstring[] = "option requires an argument -- %s";
99
static const char ambig[] = "ambiguous option -- %.*s";
100
static const char noarg[] = "option doesn't take an argument -- %.*s";
101
static const char illoptchar[] = "unknown option -- %c";
102
static const char illoptstring[] = "unknown option -- %s";
105
_vwarnx(const char *fmt, va_list ap)
107
(void)fprintf(stderr, "%s: ", __progname);
109
(void)vfprintf(stderr, fmt, ap);
110
(void)fprintf(stderr, "\n");
114
warnx(const char *fmt, ...)
123
* Compute the greatest common divisor of a and b.
143
* Exchange the block from nonopt_start to nonopt_end with the block
144
* from nonopt_end to opt_end (keeping the same order of arguments
148
permute_args(panonopt_start, panonopt_end, opt_end, nargv)
154
int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
157
_DIAGASSERT(nargv != NULL);
160
* compute lengths of blocks and number and size of cycles
162
nnonopts = panonopt_end - panonopt_start;
163
nopts = opt_end - panonopt_end;
164
ncycle = gcd(nnonopts, nopts);
165
cyclelen = (opt_end - panonopt_start) / ncycle;
167
for (i = 0; i < ncycle; i++) {
168
cstart = panonopt_end+i;
170
for (j = 0; j < cyclelen; j++) {
171
if (pos >= panonopt_end)
176
/* LINTED const cast */
177
((char **) nargv)[pos] = nargv[cstart];
178
/* LINTED const cast */
179
((char **)nargv)[cstart] = swap;
186
* Parse argc/argv argument vector. Called by user level routines.
187
* Returns -2 if -- is found (can be long option or end of options marker).
190
getopt_internal(nargc, nargv, options)
195
char *oli; /* option letter list index */
198
_DIAGASSERT(nargv != NULL);
199
_DIAGASSERT(options != NULL);
204
* XXX Some programs (like rsyncd) expect to be able to
205
* XXX re-initialize optind to 0 and have getopt_long(3)
206
* XXX properly function again. Work around this braindamage.
212
nonopt_start = nonopt_end = -1;
214
if (optreset || !*place) { /* update scanning pointer */
216
if (optind >= nargc) { /* end of argument vector */
218
if (nonopt_end != -1) {
219
/* do permutation, if we have to */
220
permute_args(nonopt_start, nonopt_end,
222
optind -= nonopt_end - nonopt_start;
224
else if (nonopt_start != -1) {
226
* If we skipped non-options, set optind
227
* to the first of them.
229
optind = nonopt_start;
231
nonopt_start = nonopt_end = -1;
234
if ((*(place = nargv[optind]) != '-')
235
|| (place[1] == '\0')) { /* found non-option */
240
* return non-option as argument to option 1
242
optarg = nargv[optind++];
247
* if no permutation wanted, stop parsing
248
* at first non-option
253
if (nonopt_start == -1)
254
nonopt_start = optind;
255
else if (nonopt_end != -1) {
256
permute_args(nonopt_start, nonopt_end,
258
nonopt_start = optind -
259
(nonopt_end - nonopt_start);
263
/* process next argument */
266
if (nonopt_start != -1 && nonopt_end == -1)
268
if (place[1] && *++place == '-') { /* found "--" */
273
if ((optchar = (int)*place++) == (int)':' ||
274
(oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
275
/* option letter unknown or ':' */
279
warnx(illoptchar, optchar);
283
if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
284
/* XXX: what if no long options provided (called by getopt)? */
288
if (++optind >= nargc) { /* no arg */
291
warnx(recargchar, optchar);
294
} else /* white space */
295
place = nargv[optind];
297
* Handle -W arg the same as --arg (which causes getopt to
302
if (*++oli != ':') { /* doesn't take argument */
305
} else { /* takes (optional) argument */
307
if (*place) /* no white space */
309
/* XXX: disable test for :: if PC? (GNU doesn't) */
310
else if (oli[1] != ':') { /* arg not optional */
311
if (++optind >= nargc) { /* no arg */
314
warnx(recargchar, optchar);
318
optarg = nargv[optind];
323
/* dump back option letter */
327
#ifdef REPLACE_GETOPT
330
* Parse argc/argv argument vector.
332
* [eventually this will replace the real getopt]
335
getopt(nargc, nargv, options)
342
_DIAGASSERT(nargv != NULL);
343
_DIAGASSERT(options != NULL);
345
if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
348
* We found an option (--), so if we skipped non-options,
349
* we have to permute.
351
if (nonopt_end != -1) {
352
permute_args(nonopt_start, nonopt_end, optind,
354
optind -= nonopt_end - nonopt_start;
356
nonopt_start = nonopt_end = -1;
365
* Parse argc/argv argument vector.
368
getopt_long(nargc, nargv, options, long_options, idx)
372
const struct option *long_options;
377
_DIAGASSERT(nargv != NULL);
378
_DIAGASSERT(options != NULL);
379
_DIAGASSERT(long_options != NULL);
380
/* idx may be NULL */
382
if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
383
char *current_argv, *has_equal;
384
size_t current_argv_len;
387
current_argv = place;
393
if (*current_argv == '\0') { /* found "--" */
395
* We found an option (--), so if we skipped
396
* non-options, we have to permute.
398
if (nonopt_end != -1) {
399
permute_args(nonopt_start, nonopt_end,
401
optind -= nonopt_end - nonopt_start;
403
nonopt_start = nonopt_end = -1;
406
if ((has_equal = strchr(current_argv, '=')) != NULL) {
407
/* argument found (--option=arg) */
408
current_argv_len = has_equal - current_argv;
411
current_argv_len = strlen(current_argv);
413
for (i = 0; long_options[i].name; i++) {
414
/* find matching long option */
415
if (strncmp(current_argv, long_options[i].name,
419
if (strlen(long_options[i].name) ==
420
(unsigned)current_argv_len) {
425
if (match == -1) /* partial match */
428
/* ambiguous abbreviation */
430
warnx(ambig, (int)current_argv_len,
436
if (match != -1) { /* option found */
437
if (long_options[match].has_arg == no_argument
440
warnx(noarg, (int)current_argv_len,
443
* XXX: GNU sets optopt to val regardless of
446
if (long_options[match].flag == NULL)
447
optopt = long_options[match].val;
452
if (long_options[match].has_arg == required_argument ||
453
long_options[match].has_arg == optional_argument) {
456
else if (long_options[match].has_arg ==
459
* optional argument doesn't use
462
optarg = nargv[optind++];
465
if ((long_options[match].has_arg == required_argument)
466
&& (optarg == NULL)) {
468
* Missing argument; leading ':'
469
* indicates no error should be generated
472
warnx(recargstring, current_argv);
474
* XXX: GNU sets optopt to val regardless
477
if (long_options[match].flag == NULL)
478
optopt = long_options[match].val;
484
} else { /* unknown option */
486
warnx(illoptstring, current_argv);
490
if (long_options[match].flag) {
491
*long_options[match].flag = long_options[match].val;
494
retval = long_options[match].val;