1298207c3SAndrey A. Chernov /* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */
2a35a7e76SEric Melville /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3a35a7e76SEric Melville
4829a229dSAndrey A. Chernov /*
5829a229dSAndrey A. Chernov * Copyright (c) 2002 Todd C. Miller <[email protected]>
6829a229dSAndrey A. Chernov *
7829a229dSAndrey A. Chernov * Permission to use, copy, modify, and distribute this software for any
8829a229dSAndrey A. Chernov * purpose with or without fee is hereby granted, provided that the above
9829a229dSAndrey A. Chernov * copyright notice and this permission notice appear in all copies.
10829a229dSAndrey A. Chernov *
11829a229dSAndrey A. Chernov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12829a229dSAndrey A. Chernov * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13829a229dSAndrey A. Chernov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14829a229dSAndrey A. Chernov * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15829a229dSAndrey A. Chernov * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16829a229dSAndrey A. Chernov * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17829a229dSAndrey A. Chernov * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18829a229dSAndrey A. Chernov *
19829a229dSAndrey A. Chernov * Sponsored in part by the Defense Advanced Research Projects
20829a229dSAndrey A. Chernov * Agency (DARPA) and Air Force Research Laboratory, Air Force
21829a229dSAndrey A. Chernov * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22829a229dSAndrey A. Chernov */
23a35a7e76SEric Melville /*-
24a35a7e76SEric Melville * Copyright (c) 2000 The NetBSD Foundation, Inc.
25a35a7e76SEric Melville * All rights reserved.
26a35a7e76SEric Melville *
27a35a7e76SEric Melville * This code is derived from software contributed to The NetBSD Foundation
28a35a7e76SEric Melville * by Dieter Baron and Thomas Klausner.
29a35a7e76SEric Melville *
30a35a7e76SEric Melville * Redistribution and use in source and binary forms, with or without
31a35a7e76SEric Melville * modification, are permitted provided that the following conditions
32a35a7e76SEric Melville * are met:
33a35a7e76SEric Melville * 1. Redistributions of source code must retain the above copyright
34a35a7e76SEric Melville * notice, this list of conditions and the following disclaimer.
35a35a7e76SEric Melville * 2. Redistributions in binary form must reproduce the above copyright
36a35a7e76SEric Melville * notice, this list of conditions and the following disclaimer in the
37a35a7e76SEric Melville * documentation and/or other materials provided with the distribution.
38a35a7e76SEric Melville *
39a35a7e76SEric Melville * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40a35a7e76SEric Melville * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41a35a7e76SEric Melville * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42a35a7e76SEric Melville * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43a35a7e76SEric Melville * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44a35a7e76SEric Melville * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45a35a7e76SEric Melville * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46a35a7e76SEric Melville * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47a35a7e76SEric Melville * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48a35a7e76SEric Melville * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49a35a7e76SEric Melville * POSSIBILITY OF SUCH DAMAGE.
50a35a7e76SEric Melville */
51a35a7e76SEric Melville
52829a229dSAndrey A. Chernov #if 0
53a35a7e76SEric Melville #if defined(LIBC_SCCS) && !defined(lint)
54829a229dSAndrey A. Chernov static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $";
55a35a7e76SEric Melville #endif /* LIBC_SCCS and not lint */
56829a229dSAndrey A. Chernov #endif
57829a229dSAndrey A. Chernov #include <sys/cdefs.h>
58829a229dSAndrey A. Chernov __FBSDID("$FreeBSD$");
59a35a7e76SEric Melville
60a35a7e76SEric Melville #include <err.h>
61a35a7e76SEric Melville #include <errno.h>
62a35a7e76SEric Melville #include <getopt.h>
63a35a7e76SEric Melville #include <stdlib.h>
64a35a7e76SEric Melville #include <string.h>
65a35a7e76SEric Melville
66f2fd86b7SAndrey A. Chernov #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
67f2fd86b7SAndrey A. Chernov
68d3ff3b5fSAndrey A. Chernov #if 0 /* we prefer to keep our getopt(3) */
69829a229dSAndrey A. Chernov #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
70a35a7e76SEric Melville #endif
71a35a7e76SEric Melville
72a35a7e76SEric Melville #ifdef REPLACE_GETOPT
73a35a7e76SEric Melville int opterr = 1; /* if error message should be printed */
74a35a7e76SEric Melville int optind = 1; /* index into parent argv vector */
75a35a7e76SEric Melville int optopt = '?'; /* character checked for validity */
76a35a7e76SEric Melville int optreset; /* reset getopt */
77a35a7e76SEric Melville char *optarg; /* argument associated with option */
78a35a7e76SEric Melville #endif
79a35a7e76SEric Melville
80829a229dSAndrey A. Chernov #define PRINT_ERROR ((opterr) && (*options != ':'))
81a35a7e76SEric Melville
82829a229dSAndrey A. Chernov #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
83829a229dSAndrey A. Chernov #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
84829a229dSAndrey A. Chernov #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
85a35a7e76SEric Melville
86a35a7e76SEric Melville /* return values */
87a35a7e76SEric Melville #define BADCH (int)'?'
88829a229dSAndrey A. Chernov #define BADARG ((*options == ':') ? (int)':' : (int)'?')
89a35a7e76SEric Melville #define INORDER (int)1
90a35a7e76SEric Melville
91*0348c8fcSAlex Richardson static char EMSG[] = "";
92a35a7e76SEric Melville
939f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
949f06a99eSAndrey A. Chernov #define NO_PREFIX (-1)
959f06a99eSAndrey A. Chernov #define D_PREFIX 0
969f06a99eSAndrey A. Chernov #define DD_PREFIX 1
979f06a99eSAndrey A. Chernov #define W_PREFIX 2
989f06a99eSAndrey A. Chernov #endif
999f06a99eSAndrey A. Chernov
100829a229dSAndrey A. Chernov static int getopt_internal(int, char * const *, const char *,
101829a229dSAndrey A. Chernov const struct option *, int *, int);
102829a229dSAndrey A. Chernov static int parse_long_options(char * const *, const char *,
103ed4fbbd5SAndrey A. Chernov const struct option *, int *, int, int);
104e6fc380cSAlfred Perlstein static int gcd(int, int);
105e6fc380cSAlfred Perlstein static void permute_args(int, int, int, char * const *);
106a35a7e76SEric Melville
107a35a7e76SEric Melville static char *place = EMSG; /* option letter processing */
108a35a7e76SEric Melville
109a35a7e76SEric Melville /* XXX: set optreset to 1 rather than these two */
110a35a7e76SEric Melville static int nonopt_start = -1; /* first non option argument (for permute) */
111a35a7e76SEric Melville static int nonopt_end = -1; /* first option after non options (for permute) */
112a35a7e76SEric Melville
113a35a7e76SEric Melville /* Error messages */
114a35a7e76SEric Melville static const char recargchar[] = "option requires an argument -- %c";
1159f06a99eSAndrey A. Chernov static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
116f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
1179f06a99eSAndrey A. Chernov static int dash_prefix = NO_PREFIX;
118f2fd86b7SAndrey A. Chernov static const char gnuoptchar[] = "invalid option -- %c";
119f2fd86b7SAndrey A. Chernov
1209f06a99eSAndrey A. Chernov static const char recargstring[] = "option `%s%s' requires an argument";
1219f06a99eSAndrey A. Chernov static const char ambig[] = "option `%s%.*s' is ambiguous";
1229f06a99eSAndrey A. Chernov static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
1239f06a99eSAndrey A. Chernov static const char illoptstring[] = "unrecognized option `%s%s'";
124f2fd86b7SAndrey A. Chernov #else
125a35a7e76SEric Melville static const char recargstring[] = "option requires an argument -- %s";
126a35a7e76SEric Melville static const char ambig[] = "ambiguous option -- %.*s";
127a35a7e76SEric Melville static const char noarg[] = "option doesn't take an argument -- %.*s";
128a35a7e76SEric Melville static const char illoptstring[] = "unknown option -- %s";
129f2fd86b7SAndrey A. Chernov #endif
130a35a7e76SEric Melville
131a35a7e76SEric Melville /*
132a35a7e76SEric Melville * Compute the greatest common divisor of a and b.
133a35a7e76SEric Melville */
134a35a7e76SEric Melville static int
gcd(int a,int b)135829a229dSAndrey A. Chernov gcd(int a, int b)
136a35a7e76SEric Melville {
137a35a7e76SEric Melville int c;
138a35a7e76SEric Melville
139a35a7e76SEric Melville c = a % b;
140a35a7e76SEric Melville while (c != 0) {
141a35a7e76SEric Melville a = b;
142a35a7e76SEric Melville b = c;
143a35a7e76SEric Melville c = a % b;
144a35a7e76SEric Melville }
145a35a7e76SEric Melville
146829a229dSAndrey A. Chernov return (b);
147a35a7e76SEric Melville }
148a35a7e76SEric Melville
149a35a7e76SEric Melville /*
150a35a7e76SEric Melville * Exchange the block from nonopt_start to nonopt_end with the block
151a35a7e76SEric Melville * from nonopt_end to opt_end (keeping the same order of arguments
152a35a7e76SEric Melville * in each block).
153a35a7e76SEric Melville */
154a35a7e76SEric Melville static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)155829a229dSAndrey A. Chernov permute_args(int panonopt_start, int panonopt_end, int opt_end,
156829a229dSAndrey A. Chernov char * const *nargv)
157a35a7e76SEric Melville {
158a35a7e76SEric Melville int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
159a35a7e76SEric Melville char *swap;
160a35a7e76SEric Melville
161a35a7e76SEric Melville /*
162a35a7e76SEric Melville * compute lengths of blocks and number and size of cycles
163a35a7e76SEric Melville */
164a35a7e76SEric Melville nnonopts = panonopt_end - panonopt_start;
165a35a7e76SEric Melville nopts = opt_end - panonopt_end;
166a35a7e76SEric Melville ncycle = gcd(nnonopts, nopts);
167a35a7e76SEric Melville cyclelen = (opt_end - panonopt_start) / ncycle;
168a35a7e76SEric Melville
169a35a7e76SEric Melville for (i = 0; i < ncycle; i++) {
170a35a7e76SEric Melville cstart = panonopt_end+i;
171a35a7e76SEric Melville pos = cstart;
172a35a7e76SEric Melville for (j = 0; j < cyclelen; j++) {
173a35a7e76SEric Melville if (pos >= panonopt_end)
174a35a7e76SEric Melville pos -= nnonopts;
175a35a7e76SEric Melville else
176a35a7e76SEric Melville pos += nopts;
177a35a7e76SEric Melville swap = nargv[pos];
178a35a7e76SEric Melville /* LINTED const cast */
179a35a7e76SEric Melville ((char **) nargv)[pos] = nargv[cstart];
180a35a7e76SEric Melville /* LINTED const cast */
181a35a7e76SEric Melville ((char **)nargv)[cstart] = swap;
182a35a7e76SEric Melville }
183a35a7e76SEric Melville }
184a35a7e76SEric Melville }
185a35a7e76SEric Melville
186a35a7e76SEric Melville /*
187829a229dSAndrey A. Chernov * parse_long_options --
188829a229dSAndrey A. Chernov * Parse long options in argc/argv argument vector.
189829a229dSAndrey A. Chernov * Returns -1 if short_too is set and the option does not match long_options.
190a35a7e76SEric Melville */
191a35a7e76SEric Melville static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,int flags)192829a229dSAndrey A. Chernov parse_long_options(char * const *nargv, const char *options,
193ed4fbbd5SAndrey A. Chernov const struct option *long_options, int *idx, int short_too, int flags)
194a35a7e76SEric Melville {
195829a229dSAndrey A. Chernov char *current_argv, *has_equal;
1969f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
197*0348c8fcSAlex Richardson const char *current_dash;
1989f06a99eSAndrey A. Chernov #endif
199829a229dSAndrey A. Chernov size_t current_argv_len;
200ed4fbbd5SAndrey A. Chernov int i, match, exact_match, second_partial_match;
201a35a7e76SEric Melville
202829a229dSAndrey A. Chernov current_argv = place;
2039f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2049f06a99eSAndrey A. Chernov switch (dash_prefix) {
2059f06a99eSAndrey A. Chernov case D_PREFIX:
2069f06a99eSAndrey A. Chernov current_dash = "-";
2079f06a99eSAndrey A. Chernov break;
2089f06a99eSAndrey A. Chernov case DD_PREFIX:
2099f06a99eSAndrey A. Chernov current_dash = "--";
2109f06a99eSAndrey A. Chernov break;
2119f06a99eSAndrey A. Chernov case W_PREFIX:
2129f06a99eSAndrey A. Chernov current_dash = "-W ";
2139f06a99eSAndrey A. Chernov break;
2149f06a99eSAndrey A. Chernov default:
2159f06a99eSAndrey A. Chernov current_dash = "";
2169f06a99eSAndrey A. Chernov break;
2179f06a99eSAndrey A. Chernov }
2189f06a99eSAndrey A. Chernov #endif
219829a229dSAndrey A. Chernov match = -1;
220ed4fbbd5SAndrey A. Chernov exact_match = 0;
221ed4fbbd5SAndrey A. Chernov second_partial_match = 0;
222a35a7e76SEric Melville
223829a229dSAndrey A. Chernov optind++;
224829a229dSAndrey A. Chernov
225829a229dSAndrey A. Chernov if ((has_equal = strchr(current_argv, '=')) != NULL) {
226829a229dSAndrey A. Chernov /* argument found (--option=arg) */
227829a229dSAndrey A. Chernov current_argv_len = has_equal - current_argv;
228829a229dSAndrey A. Chernov has_equal++;
229829a229dSAndrey A. Chernov } else
230829a229dSAndrey A. Chernov current_argv_len = strlen(current_argv);
231829a229dSAndrey A. Chernov
232829a229dSAndrey A. Chernov for (i = 0; long_options[i].name; i++) {
233829a229dSAndrey A. Chernov /* find matching long option */
234829a229dSAndrey A. Chernov if (strncmp(current_argv, long_options[i].name,
235829a229dSAndrey A. Chernov current_argv_len))
236829a229dSAndrey A. Chernov continue;
237829a229dSAndrey A. Chernov
238829a229dSAndrey A. Chernov if (strlen(long_options[i].name) == current_argv_len) {
239829a229dSAndrey A. Chernov /* exact match */
240829a229dSAndrey A. Chernov match = i;
241ed4fbbd5SAndrey A. Chernov exact_match = 1;
242829a229dSAndrey A. Chernov break;
243829a229dSAndrey A. Chernov }
244829a229dSAndrey A. Chernov /*
245829a229dSAndrey A. Chernov * If this is a known short option, don't allow
246829a229dSAndrey A. Chernov * a partial match of a single character.
247829a229dSAndrey A. Chernov */
248f853699aSAndrey A. Chernov if (short_too && current_argv_len == 1)
249829a229dSAndrey A. Chernov continue;
250829a229dSAndrey A. Chernov
251ed4fbbd5SAndrey A. Chernov if (match == -1) /* first partial match */
252829a229dSAndrey A. Chernov match = i;
253ed4fbbd5SAndrey A. Chernov else if ((flags & FLAG_LONGONLY) ||
254ed4fbbd5SAndrey A. Chernov long_options[i].has_arg !=
255ed4fbbd5SAndrey A. Chernov long_options[match].has_arg ||
256ed4fbbd5SAndrey A. Chernov long_options[i].flag != long_options[match].flag ||
257ed4fbbd5SAndrey A. Chernov long_options[i].val != long_options[match].val)
258ed4fbbd5SAndrey A. Chernov second_partial_match = 1;
259ed4fbbd5SAndrey A. Chernov }
260ed4fbbd5SAndrey A. Chernov if (!exact_match && second_partial_match) {
261829a229dSAndrey A. Chernov /* ambiguous abbreviation */
262829a229dSAndrey A. Chernov if (PRINT_ERROR)
2639f06a99eSAndrey A. Chernov warnx(ambig,
2649f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2659f06a99eSAndrey A. Chernov current_dash,
2669f06a99eSAndrey A. Chernov #endif
2679f06a99eSAndrey A. Chernov (int)current_argv_len,
268829a229dSAndrey A. Chernov current_argv);
269829a229dSAndrey A. Chernov optopt = 0;
270829a229dSAndrey A. Chernov return (BADCH);
271829a229dSAndrey A. Chernov }
272829a229dSAndrey A. Chernov if (match != -1) { /* option found */
273829a229dSAndrey A. Chernov if (long_options[match].has_arg == no_argument
274829a229dSAndrey A. Chernov && has_equal) {
275829a229dSAndrey A. Chernov if (PRINT_ERROR)
2769f06a99eSAndrey A. Chernov warnx(noarg,
2779f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
2789f06a99eSAndrey A. Chernov current_dash,
2799f06a99eSAndrey A. Chernov #endif
2809f06a99eSAndrey A. Chernov (int)current_argv_len,
281829a229dSAndrey A. Chernov current_argv);
282829a229dSAndrey A. Chernov /*
283829a229dSAndrey A. Chernov * XXX: GNU sets optopt to val regardless of flag
284829a229dSAndrey A. Chernov */
285829a229dSAndrey A. Chernov if (long_options[match].flag == NULL)
286829a229dSAndrey A. Chernov optopt = long_options[match].val;
287829a229dSAndrey A. Chernov else
288829a229dSAndrey A. Chernov optopt = 0;
28988485399SAndrey A. Chernov #ifdef GNU_COMPATIBLE
29088485399SAndrey A. Chernov return (BADCH);
29188485399SAndrey A. Chernov #else
292829a229dSAndrey A. Chernov return (BADARG);
29388485399SAndrey A. Chernov #endif
294829a229dSAndrey A. Chernov }
295829a229dSAndrey A. Chernov if (long_options[match].has_arg == required_argument ||
296829a229dSAndrey A. Chernov long_options[match].has_arg == optional_argument) {
297829a229dSAndrey A. Chernov if (has_equal)
298829a229dSAndrey A. Chernov optarg = has_equal;
299829a229dSAndrey A. Chernov else if (long_options[match].has_arg ==
300829a229dSAndrey A. Chernov required_argument) {
301829a229dSAndrey A. Chernov /*
302829a229dSAndrey A. Chernov * optional argument doesn't use next nargv
303829a229dSAndrey A. Chernov */
304829a229dSAndrey A. Chernov optarg = nargv[optind++];
305829a229dSAndrey A. Chernov }
306829a229dSAndrey A. Chernov }
307829a229dSAndrey A. Chernov if ((long_options[match].has_arg == required_argument)
308829a229dSAndrey A. Chernov && (optarg == NULL)) {
309829a229dSAndrey A. Chernov /*
310829a229dSAndrey A. Chernov * Missing argument; leading ':' indicates no error
311829a229dSAndrey A. Chernov * should be generated.
312829a229dSAndrey A. Chernov */
313829a229dSAndrey A. Chernov if (PRINT_ERROR)
314829a229dSAndrey A. Chernov warnx(recargstring,
3159f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3169f06a99eSAndrey A. Chernov current_dash,
3179f06a99eSAndrey A. Chernov #endif
318829a229dSAndrey A. Chernov current_argv);
319829a229dSAndrey A. Chernov /*
320829a229dSAndrey A. Chernov * XXX: GNU sets optopt to val regardless of flag
321829a229dSAndrey A. Chernov */
322829a229dSAndrey A. Chernov if (long_options[match].flag == NULL)
323829a229dSAndrey A. Chernov optopt = long_options[match].val;
324829a229dSAndrey A. Chernov else
325829a229dSAndrey A. Chernov optopt = 0;
326829a229dSAndrey A. Chernov --optind;
327829a229dSAndrey A. Chernov return (BADARG);
328829a229dSAndrey A. Chernov }
329829a229dSAndrey A. Chernov } else { /* unknown option */
330829a229dSAndrey A. Chernov if (short_too) {
331829a229dSAndrey A. Chernov --optind;
332829a229dSAndrey A. Chernov return (-1);
333829a229dSAndrey A. Chernov }
334829a229dSAndrey A. Chernov if (PRINT_ERROR)
3359f06a99eSAndrey A. Chernov warnx(illoptstring,
3369f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
3379f06a99eSAndrey A. Chernov current_dash,
3389f06a99eSAndrey A. Chernov #endif
3399f06a99eSAndrey A. Chernov current_argv);
340829a229dSAndrey A. Chernov optopt = 0;
341829a229dSAndrey A. Chernov return (BADCH);
342829a229dSAndrey A. Chernov }
343829a229dSAndrey A. Chernov if (idx)
344829a229dSAndrey A. Chernov *idx = match;
345829a229dSAndrey A. Chernov if (long_options[match].flag) {
346829a229dSAndrey A. Chernov *long_options[match].flag = long_options[match].val;
347829a229dSAndrey A. Chernov return (0);
348829a229dSAndrey A. Chernov } else
349829a229dSAndrey A. Chernov return (long_options[match].val);
350829a229dSAndrey A. Chernov }
351a35a7e76SEric Melville
352a35a7e76SEric Melville /*
353829a229dSAndrey A. Chernov * getopt_internal --
354829a229dSAndrey A. Chernov * Parse argc/argv argument vector. Called by user level routines.
355829a229dSAndrey A. Chernov */
356829a229dSAndrey A. Chernov static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)357829a229dSAndrey A. Chernov getopt_internal(int nargc, char * const *nargv, const char *options,
358829a229dSAndrey A. Chernov const struct option *long_options, int *idx, int flags)
359829a229dSAndrey A. Chernov {
360829a229dSAndrey A. Chernov char *oli; /* option letter list index */
361829a229dSAndrey A. Chernov int optchar, short_too;
362298207c3SAndrey A. Chernov static int posixly_correct = -1;
363829a229dSAndrey A. Chernov
364829a229dSAndrey A. Chernov if (options == NULL)
365829a229dSAndrey A. Chernov return (-1);
366829a229dSAndrey A. Chernov
367829a229dSAndrey A. Chernov /*
368829a229dSAndrey A. Chernov * XXX Some GNU programs (like cvs) set optind to 0 instead of
369829a229dSAndrey A. Chernov * XXX using optreset. Work around this braindamage.
370a35a7e76SEric Melville */
371a35a7e76SEric Melville if (optind == 0)
372829a229dSAndrey A. Chernov optind = optreset = 1;
373a35a7e76SEric Melville
374298207c3SAndrey A. Chernov /*
375298207c3SAndrey A. Chernov * Disable GNU extensions if POSIXLY_CORRECT is set or options
376298207c3SAndrey A. Chernov * string begins with a '+'.
377298207c3SAndrey A. Chernov */
378298207c3SAndrey A. Chernov if (posixly_correct == -1 || optreset)
379298207c3SAndrey A. Chernov posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
380298207c3SAndrey A. Chernov if (*options == '-')
381298207c3SAndrey A. Chernov flags |= FLAG_ALLARGS;
382298207c3SAndrey A. Chernov else if (posixly_correct || *options == '+')
383298207c3SAndrey A. Chernov flags &= ~FLAG_PERMUTE;
384298207c3SAndrey A. Chernov if (*options == '+' || *options == '-')
385298207c3SAndrey A. Chernov options++;
386298207c3SAndrey A. Chernov
387829a229dSAndrey A. Chernov optarg = NULL;
388a35a7e76SEric Melville if (optreset)
389a35a7e76SEric Melville nonopt_start = nonopt_end = -1;
390a35a7e76SEric Melville start:
391a35a7e76SEric Melville if (optreset || !*place) { /* update scanning pointer */
392a35a7e76SEric Melville optreset = 0;
393a35a7e76SEric Melville if (optind >= nargc) { /* end of argument vector */
394a35a7e76SEric Melville place = EMSG;
395a35a7e76SEric Melville if (nonopt_end != -1) {
396a35a7e76SEric Melville /* do permutation, if we have to */
397a35a7e76SEric Melville permute_args(nonopt_start, nonopt_end,
398a35a7e76SEric Melville optind, nargv);
399a35a7e76SEric Melville optind -= nonopt_end - nonopt_start;
400a35a7e76SEric Melville }
401a35a7e76SEric Melville else if (nonopt_start != -1) {
402a35a7e76SEric Melville /*
403a35a7e76SEric Melville * If we skipped non-options, set optind
404a35a7e76SEric Melville * to the first of them.
405a35a7e76SEric Melville */
406a35a7e76SEric Melville optind = nonopt_start;
407a35a7e76SEric Melville }
408a35a7e76SEric Melville nonopt_start = nonopt_end = -1;
409829a229dSAndrey A. Chernov return (-1);
410a35a7e76SEric Melville }
411829a229dSAndrey A. Chernov if (*(place = nargv[optind]) != '-' ||
4123700175bSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4133700175bSAndrey A. Chernov place[1] == '\0') {
4143700175bSAndrey A. Chernov #else
415829a229dSAndrey A. Chernov (place[1] == '\0' && strchr(options, '-') == NULL)) {
4163700175bSAndrey A. Chernov #endif
417829a229dSAndrey A. Chernov place = EMSG; /* found non-option */
418829a229dSAndrey A. Chernov if (flags & FLAG_ALLARGS) {
419a35a7e76SEric Melville /*
420a35a7e76SEric Melville * GNU extension:
421a35a7e76SEric Melville * return non-option as argument to option 1
422a35a7e76SEric Melville */
423a35a7e76SEric Melville optarg = nargv[optind++];
424829a229dSAndrey A. Chernov return (INORDER);
425a35a7e76SEric Melville }
426829a229dSAndrey A. Chernov if (!(flags & FLAG_PERMUTE)) {
427a35a7e76SEric Melville /*
428829a229dSAndrey A. Chernov * If no permutation wanted, stop parsing
429829a229dSAndrey A. Chernov * at first non-option.
430a35a7e76SEric Melville */
431829a229dSAndrey A. Chernov return (-1);
432a35a7e76SEric Melville }
433a35a7e76SEric Melville /* do permutation */
434a35a7e76SEric Melville if (nonopt_start == -1)
435a35a7e76SEric Melville nonopt_start = optind;
436a35a7e76SEric Melville else if (nonopt_end != -1) {
437a35a7e76SEric Melville permute_args(nonopt_start, nonopt_end,
438a35a7e76SEric Melville optind, nargv);
439a35a7e76SEric Melville nonopt_start = optind -
440a35a7e76SEric Melville (nonopt_end - nonopt_start);
441a35a7e76SEric Melville nonopt_end = -1;
442a35a7e76SEric Melville }
443a35a7e76SEric Melville optind++;
444a35a7e76SEric Melville /* process next argument */
445a35a7e76SEric Melville goto start;
446a35a7e76SEric Melville }
447a35a7e76SEric Melville if (nonopt_start != -1 && nonopt_end == -1)
448a35a7e76SEric Melville nonopt_end = optind;
449829a229dSAndrey A. Chernov
450829a229dSAndrey A. Chernov /*
451829a229dSAndrey A. Chernov * If we have "-" do nothing, if "--" we are done.
452829a229dSAndrey A. Chernov */
453829a229dSAndrey A. Chernov if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
454829a229dSAndrey A. Chernov optind++;
455829a229dSAndrey A. Chernov place = EMSG;
456829a229dSAndrey A. Chernov /*
457829a229dSAndrey A. Chernov * We found an option (--), so if we skipped
458829a229dSAndrey A. Chernov * non-options, we have to permute.
459829a229dSAndrey A. Chernov */
460829a229dSAndrey A. Chernov if (nonopt_end != -1) {
461829a229dSAndrey A. Chernov permute_args(nonopt_start, nonopt_end,
462829a229dSAndrey A. Chernov optind, nargv);
463829a229dSAndrey A. Chernov optind -= nonopt_end - nonopt_start;
464829a229dSAndrey A. Chernov }
465829a229dSAndrey A. Chernov nonopt_start = nonopt_end = -1;
466829a229dSAndrey A. Chernov return (-1);
467a35a7e76SEric Melville }
468a35a7e76SEric Melville }
469829a229dSAndrey A. Chernov
470829a229dSAndrey A. Chernov /*
471829a229dSAndrey A. Chernov * Check long options if:
472829a229dSAndrey A. Chernov * 1) we were passed some
473829a229dSAndrey A. Chernov * 2) the arg is not just "-"
474829a229dSAndrey A. Chernov * 3) either the arg starts with -- we are getopt_long_only()
475829a229dSAndrey A. Chernov */
476829a229dSAndrey A. Chernov if (long_options != NULL && place != nargv[optind] &&
477829a229dSAndrey A. Chernov (*place == '-' || (flags & FLAG_LONGONLY))) {
478829a229dSAndrey A. Chernov short_too = 0;
4799f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4809f06a99eSAndrey A. Chernov dash_prefix = D_PREFIX;
4819f06a99eSAndrey A. Chernov #endif
4829f06a99eSAndrey A. Chernov if (*place == '-') {
483829a229dSAndrey A. Chernov place++; /* --foo long option */
484253b638eSKyle Evans if (*place == '\0')
485253b638eSKyle Evans return (BADARG); /* malformed option */
4869f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
4879f06a99eSAndrey A. Chernov dash_prefix = DD_PREFIX;
4889f06a99eSAndrey A. Chernov #endif
4899f06a99eSAndrey A. Chernov } else if (*place != ':' && strchr(options, *place) != NULL)
490829a229dSAndrey A. Chernov short_too = 1; /* could be short option too */
491829a229dSAndrey A. Chernov
492829a229dSAndrey A. Chernov optchar = parse_long_options(nargv, options, long_options,
493ed4fbbd5SAndrey A. Chernov idx, short_too, flags);
494829a229dSAndrey A. Chernov if (optchar != -1) {
495829a229dSAndrey A. Chernov place = EMSG;
496829a229dSAndrey A. Chernov return (optchar);
497829a229dSAndrey A. Chernov }
498829a229dSAndrey A. Chernov }
499829a229dSAndrey A. Chernov
500a35a7e76SEric Melville if ((optchar = (int)*place++) == (int)':' ||
501829a229dSAndrey A. Chernov (optchar == (int)'-' && *place != '\0') ||
502829a229dSAndrey A. Chernov (oli = strchr(options, optchar)) == NULL) {
503829a229dSAndrey A. Chernov /*
504829a229dSAndrey A. Chernov * If the user specified "-" and '-' isn't listed in
505829a229dSAndrey A. Chernov * options, return -1 (non-option) as per POSIX.
506829a229dSAndrey A. Chernov * Otherwise, it is an unknown option character (or ':').
507829a229dSAndrey A. Chernov */
508829a229dSAndrey A. Chernov if (optchar == (int)'-' && *place == '\0')
509829a229dSAndrey A. Chernov return (-1);
510a35a7e76SEric Melville if (!*place)
511a35a7e76SEric Melville ++optind;
512f2fd86b7SAndrey A. Chernov #ifdef GNU_COMPATIBLE
513f2fd86b7SAndrey A. Chernov if (PRINT_ERROR)
514f2fd86b7SAndrey A. Chernov warnx(posixly_correct ? illoptchar : gnuoptchar,
515f2fd86b7SAndrey A. Chernov optchar);
516f2fd86b7SAndrey A. Chernov #else
517a35a7e76SEric Melville if (PRINT_ERROR)
518a35a7e76SEric Melville warnx(illoptchar, optchar);
519f2fd86b7SAndrey A. Chernov #endif
520a35a7e76SEric Melville optopt = optchar;
521829a229dSAndrey A. Chernov return (BADCH);
522a35a7e76SEric Melville }
523829a229dSAndrey A. Chernov if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
524829a229dSAndrey A. Chernov /* -W long-option */
525829a229dSAndrey A. Chernov if (*place) /* no space */
526829a229dSAndrey A. Chernov /* NOTHING */;
527829a229dSAndrey A. Chernov else if (++optind >= nargc) { /* no arg */
528a35a7e76SEric Melville place = EMSG;
529a35a7e76SEric Melville if (PRINT_ERROR)
530a35a7e76SEric Melville warnx(recargchar, optchar);
531a35a7e76SEric Melville optopt = optchar;
532829a229dSAndrey A. Chernov return (BADARG);
533a35a7e76SEric Melville } else /* white space */
534a35a7e76SEric Melville place = nargv[optind];
5359f06a99eSAndrey A. Chernov #ifdef GNU_COMPATIBLE
5369f06a99eSAndrey A. Chernov dash_prefix = W_PREFIX;
5379f06a99eSAndrey A. Chernov #endif
538829a229dSAndrey A. Chernov optchar = parse_long_options(nargv, options, long_options,
539ed4fbbd5SAndrey A. Chernov idx, 0, flags);
540829a229dSAndrey A. Chernov place = EMSG;
541829a229dSAndrey A. Chernov return (optchar);
542a35a7e76SEric Melville }
543a35a7e76SEric Melville if (*++oli != ':') { /* doesn't take argument */
544a35a7e76SEric Melville if (!*place)
545a35a7e76SEric Melville ++optind;
546a35a7e76SEric Melville } else { /* takes (optional) argument */
547a35a7e76SEric Melville optarg = NULL;
548a35a7e76SEric Melville if (*place) /* no white space */
549a35a7e76SEric Melville optarg = place;
550a35a7e76SEric Melville else if (oli[1] != ':') { /* arg not optional */
551a35a7e76SEric Melville if (++optind >= nargc) { /* no arg */
552a35a7e76SEric Melville place = EMSG;
553a35a7e76SEric Melville if (PRINT_ERROR)
554a35a7e76SEric Melville warnx(recargchar, optchar);
555a35a7e76SEric Melville optopt = optchar;
556829a229dSAndrey A. Chernov return (BADARG);
557a35a7e76SEric Melville } else
558a35a7e76SEric Melville optarg = nargv[optind];
559f27c7b47SAndrey A. Chernov }
560a35a7e76SEric Melville place = EMSG;
561a35a7e76SEric Melville ++optind;
562a35a7e76SEric Melville }
563a35a7e76SEric Melville /* dump back option letter */
564829a229dSAndrey A. Chernov return (optchar);
565a35a7e76SEric Melville }
566a35a7e76SEric Melville
567a35a7e76SEric Melville #ifdef REPLACE_GETOPT
568a35a7e76SEric Melville /*
569a35a7e76SEric Melville * getopt --
570a35a7e76SEric Melville * Parse argc/argv argument vector.
571a35a7e76SEric Melville *
572829a229dSAndrey A. Chernov * [eventually this will replace the BSD getopt]
573a35a7e76SEric Melville */
574a35a7e76SEric Melville int
575829a229dSAndrey A. Chernov getopt(int nargc, char * const *nargv, const char *options)
576a35a7e76SEric Melville {
577a35a7e76SEric Melville
578a35a7e76SEric Melville /*
579829a229dSAndrey A. Chernov * We don't pass FLAG_PERMUTE to getopt_internal() since
580829a229dSAndrey A. Chernov * the BSD getopt(3) (unlike GNU) has never done this.
581829a229dSAndrey A. Chernov *
582829a229dSAndrey A. Chernov * Furthermore, since many privileged programs call getopt()
583829a229dSAndrey A. Chernov * before dropping privileges it makes sense to keep things
584829a229dSAndrey A. Chernov * as simple (and bug-free) as possible.
585a35a7e76SEric Melville */
586829a229dSAndrey A. Chernov return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
587a35a7e76SEric Melville }
588829a229dSAndrey A. Chernov #endif /* REPLACE_GETOPT */
589a35a7e76SEric Melville
590a35a7e76SEric Melville /*
591a35a7e76SEric Melville * getopt_long --
592a35a7e76SEric Melville * Parse argc/argv argument vector.
593a35a7e76SEric Melville */
594a35a7e76SEric Melville int
595d3ff3b5fSAndrey A. Chernov getopt_long(int nargc, char * const *nargv, const char *options,
596d3ff3b5fSAndrey A. Chernov const struct option *long_options, int *idx)
597a35a7e76SEric Melville {
598a35a7e76SEric Melville
599829a229dSAndrey A. Chernov return (getopt_internal(nargc, nargv, options, long_options, idx,
600829a229dSAndrey A. Chernov FLAG_PERMUTE));
601829a229dSAndrey A. Chernov }
602a35a7e76SEric Melville
603a35a7e76SEric Melville /*
604829a229dSAndrey A. Chernov * getopt_long_only --
605829a229dSAndrey A. Chernov * Parse argc/argv argument vector.
606a35a7e76SEric Melville */
607829a229dSAndrey A. Chernov int
608d3ff3b5fSAndrey A. Chernov getopt_long_only(int nargc, char * const *nargv, const char *options,
609d3ff3b5fSAndrey A. Chernov const struct option *long_options, int *idx)
610829a229dSAndrey A. Chernov {
611a35a7e76SEric Melville
612829a229dSAndrey A. Chernov return (getopt_internal(nargc, nargv, options, long_options, idx,
613829a229dSAndrey A. Chernov FLAG_PERMUTE|FLAG_LONGONLY));
614a35a7e76SEric Melville }
615