1 /* SPDX-License-Identifier: ISC AND BSD-2-Clause
2 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Sponsored in part by the Defense Advanced Research Projects
5 * Agency (DARPA) and Air Force Research Laboratory, Air Force
6 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
9 * Copyright (c) 2000 The NetBSD Foundation, Inc.
10 * All rights reserved.
12 * This code is derived from software contributed to The NetBSD Foundation
13 * by Dieter Baron and Thomas Klausner.
18 #ifdef NEED_USUAL_GETOPT
23 const char *optarg; /* argument associated with option */
24 int opterr = 1; /* if error message should be printed */
25 int optind = 1; /* index into parent argv vector */
26 int optopt = '?'; /* character checked for validity */
28 static void pass(const char *a) {(void) a; }
29 #define warnx(a, ...) pass(a)
31 #define PRINT_ERROR ((opterr) && (*options != ':'))
33 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
34 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
35 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
38 #define BADCH ((int)'?')
39 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
44 static const char *place = EMSG; /* option letter processing */
46 /* XXX: set optreset to 1 rather than these two */
47 static int nonopt_start = -1; /* first non option argument (for permute) */
48 static int nonopt_end = -1; /* first option after non options (for permute) */
51 static const char recargchar[] = "option requires an argument -- %c";
52 static const char recargstring[] = "option requires an argument -- %s";
53 static const char ambig[] = "ambiguous option -- %.*s";
54 static const char noarg[] = "option doesn't take an argument -- %.*s";
55 static const char illoptchar[] = "unknown option -- %c";
56 static const char illoptstring[] = "unknown option -- %s";
59 * Compute the greatest common divisor of a and b.
77 * Exchange the block from nonopt_start to nonopt_end with the block
78 * from nonopt_end to opt_end (keeping the same order of arguments
82 permute_args(int panonopt_start, int panonopt_end, int opt_end,
85 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
89 * compute lengths of blocks and number and size of cycles
91 nnonopts = panonopt_end - panonopt_start;
92 nopts = opt_end - panonopt_end;
93 ncycle = gcd(nnonopts, nopts);
94 cyclelen = (opt_end - panonopt_start) / ncycle;
96 for (i = 0; i < ncycle; i++) {
97 cstart = panonopt_end+i;
99 for (j = 0; j < cyclelen; j++) {
100 if (pos >= panonopt_end)
105 /* LINTED const cast */
106 ((char **) nargv)[pos] = nargv[cstart];
107 /* LINTED const cast */
108 ((char **)nargv)[cstart] = swap;
114 * parse_long_options --
115 * Parse long options in argc/argv argument vector.
116 * Returns -1 if short_too is set and the option does not match long_options.
119 parse_long_options(char **nargv, const char *options,
120 const struct option *long_options, int *idx, int short_too)
122 const char *current_argv;
124 size_t current_argv_len;
127 current_argv = place;
132 has_equal = strchr(current_argv, '=');
133 if (has_equal != NULL) {
134 /* argument found (--option=arg) */
135 current_argv_len = has_equal - current_argv;
138 current_argv_len = strlen(current_argv);
140 for (i = 0; long_options[i].name; i++) {
141 /* find matching long option */
142 if (strncmp(current_argv, long_options[i].name,
146 if (strlen(long_options[i].name) == current_argv_len) {
152 * If this is a known short option, don't allow
153 * a partial match of a single character.
155 if (short_too && current_argv_len == 1)
158 if (match == -1) /* partial match */
161 /* ambiguous abbreviation */
163 warnx(ambig, (int)current_argv_len,
169 if (match != -1) { /* option found */
170 if (long_options[match].has_arg == no_argument
173 warnx(noarg, (int)current_argv_len,
176 * XXX: GNU sets optopt to val regardless of flag
178 if (long_options[match].flag == NULL)
179 optopt = long_options[match].val;
184 if (long_options[match].has_arg == required_argument ||
185 long_options[match].has_arg == optional_argument) {
188 else if (long_options[match].has_arg ==
191 * optional argument doesn't use next nargv
193 optarg = nargv[optind++];
196 if ((long_options[match].has_arg == required_argument)
197 && (optarg == NULL)) {
199 * Missing argument; leading ':' indicates no error
200 * should be generated.
206 * XXX: GNU sets optopt to val regardless of flag
208 if (long_options[match].flag == NULL)
209 optopt = long_options[match].val;
215 } else { /* unknown option */
221 warnx(illoptstring, current_argv);
227 if (long_options[match].flag) {
228 *long_options[match].flag = long_options[match].val;
231 return (long_options[match].val);
236 * Parse argc/argv argument vector. Called by user level routines.
239 getopt_internal(int nargc, char **nargv, const char *options,
240 const struct option *long_options, int *idx, int flags)
242 char *oli; /* option letter list index */
243 int optchar, short_too;
244 static int posixly_correct = -1;
253 * Disable GNU extensions if POSIXLY_CORRECT is set or options
254 * string begins with a '+'.
256 if (posixly_correct == -1)
257 posixly_correct = _dupenv_s(&buf, &len, "POSIXLY_CORRECT");
258 if (!posixly_correct || *options == '+')
259 flags &= ~FLAG_PERMUTE;
260 else if (*options == '-')
261 flags |= FLAG_ALLARGS;
262 if (*options == '+' || *options == '-')
264 if (!posixly_correct)
270 optind = optreset = 1;
274 nonopt_start = nonopt_end = -1;
276 if (optreset || !*place) { /* update scanning pointer */
278 if (optind >= nargc) { /* end of argument vector */
280 if (nonopt_end != -1) {
281 /* do permutation, if we have to */
282 permute_args(nonopt_start, nonopt_end,
284 optind -= nonopt_end - nonopt_start;
285 } else if (nonopt_start != -1) {
287 * If we skipped non-options, set optind
288 * to the first of them.
290 optind = nonopt_start;
292 nonopt_start = nonopt_end = -1;
295 place = nargv[optind];
297 (place[1] == '\0' && strchr(options, '-') == NULL)) {
298 place = EMSG; /* found non-option */
299 if (flags & FLAG_ALLARGS) {
302 * return non-option as argument to option 1
304 optarg = nargv[optind++];
307 if (!(flags & FLAG_PERMUTE)) {
309 * If no permutation wanted, stop parsing
310 * at first non-option.
315 if (nonopt_start == -1)
316 nonopt_start = optind;
317 else if (nonopt_end != -1) {
318 permute_args(nonopt_start, nonopt_end,
320 nonopt_start = optind -
321 (nonopt_end - nonopt_start);
325 /* process next argument */
328 if (nonopt_start != -1 && nonopt_end == -1)
332 * If we have "-" do nothing, if "--" we are done.
334 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
338 * We found an option (--), so if we skipped
339 * non-options, we have to permute.
341 if (nonopt_end != -1) {
342 permute_args(nonopt_start, nonopt_end,
344 optind -= nonopt_end - nonopt_start;
346 nonopt_start = nonopt_end = -1;
352 * Check long options if:
353 * 1) we were passed some
354 * 2) the arg is not just "-"
355 * 3) either the arg starts with -- we are getopt_long_only()
357 if (long_options != NULL && place != nargv[optind] &&
358 (*place == '-' || (flags & FLAG_LONGONLY))) {
361 place++; /* --foo long option */
362 else if (*place != ':' && strchr(options, *place) != NULL)
363 short_too = 1; /* could be short option too */
365 optchar = parse_long_options(nargv, options, long_options,
373 optchar = (int)*place++;
374 oli = strchr(options, optchar);
375 if (optchar == (int)':' ||
376 (optchar == (int)'-' && *place != '\0') ||
379 * If the user specified "-" and '-' isn't listed in
380 * options, return -1 (non-option) as per POSIX.
381 * Otherwise, it is an unknown option character (or ':').
383 if (optchar == (int)'-' && *place == '\0')
388 warnx(illoptchar, optchar);
392 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
396 else if (++optind >= nargc) { /* no arg */
399 warnx(recargchar, optchar);
403 place = nargv[optind];
404 optchar = parse_long_options(nargv, options, long_options,
409 if (*++oli != ':') { /* doesn't take argument */
412 } else { /* takes (optional) argument */
414 if (*place) /* no white space */
416 else if (oli[1] != ':') { /* arg not optional */
417 if (++optind >= nargc) { /* no arg */
420 warnx(recargchar, optchar);
424 optarg = nargv[optind];
429 /* dump back option letter */
435 * Parse argc/argv argument vector.
438 getopt(int nargc, char *nargv[], const char *options)
440 return getopt_internal(nargc, nargv, options, NULL, NULL,
446 * Parse argc/argv argument vector.
449 getopt_long(int nargc, char *nargv[], const char *options,
450 const struct option *long_options, int *idx)
453 return (getopt_internal(nargc, nargv, options, long_options, idx,
458 * getopt_long_only --
459 * Parse argc/argv argument vector.
462 getopt_long_only(int nargc, char *nargv[], const char *options,
463 const struct option *long_options, int *idx)
466 return (getopt_internal(nargc, nargv, options, long_options, idx,
467 FLAG_PERMUTE|FLAG_LONGONLY));
470 #endif /* NEED_USUAL_GETOPT */