getopt_long.c revision 1.1 1 1.1 christos /* $OpenBSD: getopt_long.c,v 1.22 2006/10/04 21:29:04 jmc Exp $ */
2 1.1 christos /* NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 2002 Todd C. Miller <Todd.Miller (at) courtesan.com>
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos *
19 1.1 christos * Sponsored in part by the Defense Advanced Research Projects
20 1.1 christos * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 1.1 christos * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 1.1 christos */
23 1.1 christos /*-
24 1.1 christos * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 1.1 christos * All rights reserved.
26 1.1 christos *
27 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
28 1.1 christos * by Dieter Baron and Thomas Klausner.
29 1.1 christos *
30 1.1 christos * Redistribution and use in source and binary forms, with or without
31 1.1 christos * modification, are permitted provided that the following conditions
32 1.1 christos * are met:
33 1.1 christos * 1. Redistributions of source code must retain the above copyright
34 1.1 christos * notice, this list of conditions and the following disclaimer.
35 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
36 1.1 christos * notice, this list of conditions and the following disclaimer in the
37 1.1 christos * documentation and/or other materials provided with the distribution.
38 1.1 christos *
39 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
50 1.1 christos */
51 1.1 christos
52 1.1 christos
53 1.1 christos #include <errno.h>
54 1.1 christos #include "getopt_long.h"
55 1.1 christos #include <stdlib.h>
56 1.1 christos #include <stdio.h>
57 1.1 christos #include <string.h>
58 1.1 christos #include <stdarg.h>
59 1.1 christos
60 1.1 christos #define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
61 1.1 christos
62 1.1 christos #define PRINT_ERROR ((opterr) && (*options != ':'))
63 1.1 christos
64 1.1 christos #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
65 1.1 christos #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
66 1.1 christos #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
67 1.1 christos
68 1.1 christos /* return values */
69 1.1 christos #define BADCH (int)'?'
70 1.1 christos #define BADARG ((*options == ':') ? (int)':' : (int)'?')
71 1.1 christos #define INORDER (int)1
72 1.1 christos
73 1.1 christos #define EMSG ""
74 1.1 christos
75 1.1 christos #ifdef GNU_COMPATIBLE
76 1.1 christos #define NO_PREFIX (-1)
77 1.1 christos #define D_PREFIX 0
78 1.1 christos #define DD_PREFIX 1
79 1.1 christos #define W_PREFIX 2
80 1.1 christos #endif
81 1.1 christos
82 1.1 christos char *optarg;
83 1.1 christos int optind, opterr = 1, optopt;
84 1.1 christos
85 1.1 christos static int getopt_internal(int, char * const *, const char *,
86 1.1 christos const struct option *, int *, int);
87 1.1 christos static int parse_long_options(char * const *, const char *,
88 1.1 christos const struct option *, int *, int, int);
89 1.1 christos static int gcd(int, int);
90 1.1 christos static void permute_args(int, int, int, char * const *);
91 1.1 christos
92 1.1 christos static const char *place = EMSG; /* option letter processing */
93 1.1 christos
94 1.1 christos static int nonopt_start = -1; /* first non option argument (for permute) */
95 1.1 christos static int nonopt_end = -1; /* first option after non options (for permute) */
96 1.1 christos
97 1.1 christos /* Error messages */
98 1.1 christos static const char recargchar[] = "option requires an argument -- %c";
99 1.1 christos static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
100 1.1 christos #ifdef GNU_COMPATIBLE
101 1.1 christos static int dash_prefix = NO_PREFIX;
102 1.1 christos static const char gnuoptchar[] = "invalid option -- %c";
103 1.1 christos
104 1.1 christos static const char recargstring[] = "option `%s%s' requires an argument";
105 1.1 christos static const char ambig[] = "option `%s%.*s' is ambiguous";
106 1.1 christos static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
107 1.1 christos static const char illoptstring[] = "unrecognized option `%s%s'";
108 1.1 christos #else
109 1.1 christos static const char recargstring[] = "option requires an argument -- %s";
110 1.1 christos static const char ambig[] = "ambiguous option -- %.*s";
111 1.1 christos static const char noarg[] = "option doesn't take an argument -- %.*s";
112 1.1 christos static const char illoptstring[] = "unknown option -- %s";
113 1.1 christos #endif
114 1.1 christos
115 1.1 christos /*
116 1.1 christos * Compute the greatest common divisor of a and b.
117 1.1 christos */
118 1.1 christos static int
119 1.1 christos gcd(int a, int b)
120 1.1 christos {
121 1.1 christos int c;
122 1.1 christos
123 1.1 christos c = a % b;
124 1.1 christos while (c != 0) {
125 1.1 christos a = b;
126 1.1 christos b = c;
127 1.1 christos c = a % b;
128 1.1 christos }
129 1.1 christos
130 1.1 christos return (b);
131 1.1 christos }
132 1.1 christos
133 1.1 christos /*
134 1.1 christos * Exchange the block from nonopt_start to nonopt_end with the block
135 1.1 christos * from nonopt_end to opt_end (keeping the same order of arguments
136 1.1 christos * in each block).
137 1.1 christos */
138 1.1 christos static void
139 1.1 christos permute_args(int panonopt_start, int panonopt_end, int opt_end,
140 1.1 christos char * const *nargv)
141 1.1 christos {
142 1.1 christos int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
143 1.1 christos char *swap;
144 1.1 christos
145 1.1 christos /*
146 1.1 christos * compute lengths of blocks and number and size of cycles
147 1.1 christos */
148 1.1 christos nnonopts = panonopt_end - panonopt_start;
149 1.1 christos nopts = opt_end - panonopt_end;
150 1.1 christos ncycle = gcd(nnonopts, nopts);
151 1.1 christos cyclelen = (opt_end - panonopt_start) / ncycle;
152 1.1 christos
153 1.1 christos for (i = 0; i < ncycle; i++) {
154 1.1 christos cstart = panonopt_end+i;
155 1.1 christos pos = cstart;
156 1.1 christos for (j = 0; j < cyclelen; j++) {
157 1.1 christos if (pos >= panonopt_end)
158 1.1 christos pos -= nnonopts;
159 1.1 christos else
160 1.1 christos pos += nopts;
161 1.1 christos swap = nargv[pos];
162 1.1 christos /* LINTED const cast */
163 1.1 christos ((char **) nargv)[pos] = nargv[cstart];
164 1.1 christos /* LINTED const cast */
165 1.1 christos ((char **)nargv)[cstart] = swap;
166 1.1 christos }
167 1.1 christos }
168 1.1 christos }
169 1.1 christos
170 1.1 christos static void
171 1.1 christos warnx(const char *fmt, ...)
172 1.1 christos {
173 1.1 christos extern char *program_name;
174 1.1 christos va_list ap;
175 1.1 christos
176 1.1 christos va_start(ap, fmt);
177 1.1 christos fprintf(stderr, "%s: ", program_name);
178 1.1 christos vfprintf(stderr, fmt, ap);
179 1.1 christos fprintf(stderr, "\n");
180 1.1 christos va_end(ap);
181 1.1 christos }
182 1.1 christos
183 1.1 christos /*
184 1.1 christos * parse_long_options --
185 1.1 christos * Parse long options in argc/argv argument vector.
186 1.1 christos * Returns -1 if short_too is set and the option does not match long_options.
187 1.1 christos */
188 1.1 christos static int
189 1.1 christos parse_long_options(char * const *nargv, const char *options,
190 1.1 christos const struct option *long_options, int *idx, int short_too, int flags)
191 1.1 christos {
192 1.1 christos const char *current_argv, *has_equal;
193 1.1 christos #ifdef GNU_COMPATIBLE
194 1.1 christos const char *current_dash;
195 1.1 christos #endif
196 1.1 christos size_t current_argv_len;
197 1.1 christos int i, match, exact_match, second_partial_match;
198 1.1 christos
199 1.1 christos current_argv = place;
200 1.1 christos #ifdef GNU_COMPATIBLE
201 1.1 christos switch (dash_prefix) {
202 1.1 christos case D_PREFIX:
203 1.1 christos current_dash = "-";
204 1.1 christos break;
205 1.1 christos case DD_PREFIX:
206 1.1 christos current_dash = "--";
207 1.1 christos break;
208 1.1 christos case W_PREFIX:
209 1.1 christos current_dash = "-W ";
210 1.1 christos break;
211 1.1 christos default:
212 1.1 christos current_dash = "";
213 1.1 christos break;
214 1.1 christos }
215 1.1 christos #endif
216 1.1 christos match = -1;
217 1.1 christos exact_match = 0;
218 1.1 christos second_partial_match = 0;
219 1.1 christos
220 1.1 christos optind++;
221 1.1 christos
222 1.1 christos if ((has_equal = strchr(current_argv, '=')) != NULL) {
223 1.1 christos /* argument found (--option=arg) */
224 1.1 christos current_argv_len = has_equal - current_argv;
225 1.1 christos has_equal++;
226 1.1 christos } else
227 1.1 christos current_argv_len = strlen(current_argv);
228 1.1 christos
229 1.1 christos for (i = 0; long_options[i].name; i++) {
230 1.1 christos /* find matching long option */
231 1.1 christos if (strncmp(current_argv, long_options[i].name,
232 1.1 christos current_argv_len))
233 1.1 christos continue;
234 1.1 christos
235 1.1 christos if (strlen(long_options[i].name) == current_argv_len) {
236 1.1 christos /* exact match */
237 1.1 christos match = i;
238 1.1 christos exact_match = 1;
239 1.1 christos break;
240 1.1 christos }
241 1.1 christos /*
242 1.1 christos * If this is a known short option, don't allow
243 1.1 christos * a partial match of a single character.
244 1.1 christos */
245 1.1 christos if (short_too && current_argv_len == 1)
246 1.1 christos continue;
247 1.1 christos
248 1.1 christos if (match == -1) /* first partial match */
249 1.1 christos match = i;
250 1.1 christos else if ((flags & FLAG_LONGONLY) ||
251 1.1 christos long_options[i].has_arg !=
252 1.1 christos long_options[match].has_arg ||
253 1.1 christos long_options[i].flag != long_options[match].flag ||
254 1.1 christos long_options[i].val != long_options[match].val)
255 1.1 christos second_partial_match = 1;
256 1.1 christos }
257 1.1 christos if (!exact_match && second_partial_match) {
258 1.1 christos /* ambiguous abbreviation */
259 1.1 christos if (PRINT_ERROR)
260 1.1 christos warnx(ambig,
261 1.1 christos #ifdef GNU_COMPATIBLE
262 1.1 christos current_dash,
263 1.1 christos #endif
264 1.1 christos (int)current_argv_len,
265 1.1 christos current_argv);
266 1.1 christos optopt = 0;
267 1.1 christos return (BADCH);
268 1.1 christos }
269 1.1 christos if (match != -1) { /* option found */
270 1.1 christos if (long_options[match].has_arg == no_argument
271 1.1 christos && has_equal) {
272 1.1 christos if (PRINT_ERROR)
273 1.1 christos warnx(noarg,
274 1.1 christos #ifdef GNU_COMPATIBLE
275 1.1 christos current_dash,
276 1.1 christos #endif
277 1.1 christos (int)current_argv_len,
278 1.1 christos current_argv);
279 1.1 christos /*
280 1.1 christos * XXX: GNU sets optopt to val regardless of flag
281 1.1 christos */
282 1.1 christos if (long_options[match].flag == NULL)
283 1.1 christos optopt = long_options[match].val;
284 1.1 christos else
285 1.1 christos optopt = 0;
286 1.1 christos #ifdef GNU_COMPATIBLE
287 1.1 christos return (BADCH);
288 1.1 christos #else
289 1.1 christos return (BADARG);
290 1.1 christos #endif
291 1.1 christos }
292 1.1 christos if (long_options[match].has_arg == required_argument ||
293 1.1 christos long_options[match].has_arg == optional_argument) {
294 1.1 christos if (has_equal)
295 1.1 christos optarg = (char *)has_equal;
296 1.1 christos else if (long_options[match].has_arg ==
297 1.1 christos required_argument) {
298 1.1 christos /*
299 1.1 christos * optional argument doesn't use next nargv
300 1.1 christos */
301 1.1 christos optarg = nargv[optind++];
302 1.1 christos }
303 1.1 christos }
304 1.1 christos if ((long_options[match].has_arg == required_argument)
305 1.1 christos && (optarg == NULL)) {
306 1.1 christos /*
307 1.1 christos * Missing argument; leading ':' indicates no error
308 1.1 christos * should be generated.
309 1.1 christos */
310 1.1 christos if (PRINT_ERROR)
311 1.1 christos warnx(recargstring,
312 1.1 christos #ifdef GNU_COMPATIBLE
313 1.1 christos current_dash,
314 1.1 christos #endif
315 1.1 christos current_argv);
316 1.1 christos /*
317 1.1 christos * XXX: GNU sets optopt to val regardless of flag
318 1.1 christos */
319 1.1 christos if (long_options[match].flag == NULL)
320 1.1 christos optopt = long_options[match].val;
321 1.1 christos else
322 1.1 christos optopt = 0;
323 1.1 christos --optind;
324 1.1 christos return (BADARG);
325 1.1 christos }
326 1.1 christos } else { /* unknown option */
327 1.1 christos if (short_too) {
328 1.1 christos --optind;
329 1.1 christos return (-1);
330 1.1 christos }
331 1.1 christos if (PRINT_ERROR)
332 1.1 christos warnx(illoptstring,
333 1.1 christos #ifdef GNU_COMPATIBLE
334 1.1 christos current_dash,
335 1.1 christos #endif
336 1.1 christos current_argv);
337 1.1 christos optopt = 0;
338 1.1 christos return (BADCH);
339 1.1 christos }
340 1.1 christos if (idx)
341 1.1 christos *idx = match;
342 1.1 christos if (long_options[match].flag) {
343 1.1 christos *long_options[match].flag = long_options[match].val;
344 1.1 christos return (0);
345 1.1 christos } else
346 1.1 christos return (long_options[match].val);
347 1.1 christos }
348 1.1 christos
349 1.1 christos /*
350 1.1 christos * getopt_internal --
351 1.1 christos * Parse argc/argv argument vector. Called by user level routines.
352 1.1 christos */
353 1.1 christos static int
354 1.1 christos getopt_internal(int nargc, char * const *nargv, const char *options,
355 1.1 christos const struct option *long_options, int *idx, int flags)
356 1.1 christos {
357 1.1 christos char *oli; /* option letter list index */
358 1.1 christos int optchar, short_too;
359 1.1 christos int posixly_correct; /* no static, can be changed on the fly */
360 1.1 christos
361 1.1 christos if (options == NULL)
362 1.1 christos return (-1);
363 1.1 christos
364 1.1 christos /*
365 1.1 christos * Disable GNU extensions if POSIXLY_CORRECT is set or options
366 1.1 christos * string begins with a '+'.
367 1.1 christos */
368 1.1 christos posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
369 1.1 christos #ifdef GNU_COMPATIBLE
370 1.1 christos if (*options == '-')
371 1.1 christos flags |= FLAG_ALLARGS;
372 1.1 christos else if (posixly_correct || *options == '+')
373 1.1 christos flags &= ~FLAG_PERMUTE;
374 1.1 christos #else
375 1.1 christos if (posixly_correct || *options == '+')
376 1.1 christos flags &= ~FLAG_PERMUTE;
377 1.1 christos else if (*options == '-')
378 1.1 christos flags |= FLAG_ALLARGS;
379 1.1 christos #endif
380 1.1 christos if (*options == '+' || *options == '-')
381 1.1 christos options++;
382 1.1 christos
383 1.1 christos /*
384 1.1 christos * XXX Some GNU programs (like cvs) set optind to 0 instead of
385 1.1 christos * XXX using optreset. Work around this braindamage.
386 1.1 christos */
387 1.1 christos if (optind == 0)
388 1.1 christos optind = 1;
389 1.1 christos
390 1.1 christos optarg = NULL;
391 1.1 christos start:
392 1.1 christos if (!*place) { /* update scanning pointer */
393 1.1 christos if (optind >= nargc) { /* end of argument vector */
394 1.1 christos place = EMSG;
395 1.1 christos if (nonopt_end != -1) {
396 1.1 christos /* do permutation, if we have to */
397 1.1 christos permute_args(nonopt_start, nonopt_end,
398 1.1 christos optind, nargv);
399 1.1 christos optind -= nonopt_end - nonopt_start;
400 1.1 christos }
401 1.1 christos else if (nonopt_start != -1) {
402 1.1 christos /*
403 1.1 christos * If we skipped non-options, set optind
404 1.1 christos * to the first of them.
405 1.1 christos */
406 1.1 christos optind = nonopt_start;
407 1.1 christos }
408 1.1 christos nonopt_start = nonopt_end = -1;
409 1.1 christos return (-1);
410 1.1 christos }
411 1.1 christos if (*(place = nargv[optind]) != '-' ||
412 1.1 christos #ifdef GNU_COMPATIBLE
413 1.1 christos place[1] == '\0') {
414 1.1 christos #else
415 1.1 christos (place[1] == '\0' && strchr(options, '-') == NULL)) {
416 1.1 christos #endif
417 1.1 christos place = EMSG; /* found non-option */
418 1.1 christos if (flags & FLAG_ALLARGS) {
419 1.1 christos /*
420 1.1 christos * GNU extension:
421 1.1 christos * return non-option as argument to option 1
422 1.1 christos */
423 1.1 christos optarg = nargv[optind++];
424 1.1 christos return (INORDER);
425 1.1 christos }
426 1.1 christos if (!(flags & FLAG_PERMUTE)) {
427 1.1 christos /*
428 1.1 christos * If no permutation wanted, stop parsing
429 1.1 christos * at first non-option.
430 1.1 christos */
431 1.1 christos return (-1);
432 1.1 christos }
433 1.1 christos /* do permutation */
434 1.1 christos if (nonopt_start == -1)
435 1.1 christos nonopt_start = optind;
436 1.1 christos else if (nonopt_end != -1) {
437 1.1 christos permute_args(nonopt_start, nonopt_end,
438 1.1 christos optind, nargv);
439 1.1 christos nonopt_start = optind -
440 1.1 christos (nonopt_end - nonopt_start);
441 1.1 christos nonopt_end = -1;
442 1.1 christos }
443 1.1 christos optind++;
444 1.1 christos /* process next argument */
445 1.1 christos goto start;
446 1.1 christos }
447 1.1 christos if (nonopt_start != -1 && nonopt_end == -1)
448 1.1 christos nonopt_end = optind;
449 1.1 christos
450 1.1 christos /*
451 1.1 christos * If we have "-" do nothing, if "--" we are done.
452 1.1 christos */
453 1.1 christos if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
454 1.1 christos optind++;
455 1.1 christos place = EMSG;
456 1.1 christos /*
457 1.1 christos * We found an option (--), so if we skipped
458 1.1 christos * non-options, we have to permute.
459 1.1 christos */
460 1.1 christos if (nonopt_end != -1) {
461 1.1 christos permute_args(nonopt_start, nonopt_end,
462 1.1 christos optind, nargv);
463 1.1 christos optind -= nonopt_end - nonopt_start;
464 1.1 christos }
465 1.1 christos nonopt_start = nonopt_end = -1;
466 1.1 christos return (-1);
467 1.1 christos }
468 1.1 christos }
469 1.1 christos
470 1.1 christos /*
471 1.1 christos * Check long options if:
472 1.1 christos * 1) we were passed some
473 1.1 christos * 2) the arg is not just "-"
474 1.1 christos * 3) either the arg starts with -- we are getopt_long_only()
475 1.1 christos */
476 1.1 christos if (long_options != NULL && place != nargv[optind] &&
477 1.1 christos (*place == '-' || (flags & FLAG_LONGONLY))) {
478 1.1 christos short_too = 0;
479 1.1 christos #ifdef GNU_COMPATIBLE
480 1.1 christos dash_prefix = D_PREFIX;
481 1.1 christos #endif
482 1.1 christos if (*place == '-') {
483 1.1 christos place++; /* --foo long option */
484 1.1 christos #ifdef GNU_COMPATIBLE
485 1.1 christos dash_prefix = DD_PREFIX;
486 1.1 christos #endif
487 1.1 christos } else if (*place != ':' && strchr(options, *place) != NULL)
488 1.1 christos short_too = 1; /* could be short option too */
489 1.1 christos
490 1.1 christos optchar = parse_long_options(nargv, options, long_options,
491 1.1 christos idx, short_too, flags);
492 1.1 christos if (optchar != -1) {
493 1.1 christos place = EMSG;
494 1.1 christos return (optchar);
495 1.1 christos }
496 1.1 christos }
497 1.1 christos
498 1.1 christos if ((optchar = (int)*place++) == (int)':' ||
499 1.1 christos (optchar == (int)'-' && *place != '\0') ||
500 1.1 christos (oli = strchr(options, optchar)) == NULL) {
501 1.1 christos /*
502 1.1 christos * If the user specified "-" and '-' isn't listed in
503 1.1 christos * options, return -1 (non-option) as per POSIX.
504 1.1 christos * Otherwise, it is an unknown option character (or ':').
505 1.1 christos */
506 1.1 christos if (optchar == (int)'-' && *place == '\0')
507 1.1 christos return (-1);
508 1.1 christos if (!*place)
509 1.1 christos ++optind;
510 1.1 christos #ifdef GNU_COMPATIBLE
511 1.1 christos if (PRINT_ERROR)
512 1.1 christos warnx(posixly_correct ? illoptchar : gnuoptchar,
513 1.1 christos optchar);
514 1.1 christos #else
515 1.1 christos if (PRINT_ERROR)
516 1.1 christos warnx(illoptchar, optchar);
517 1.1 christos #endif
518 1.1 christos optopt = optchar;
519 1.1 christos return (BADCH);
520 1.1 christos }
521 1.1 christos if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
522 1.1 christos /* -W long-option */
523 1.1 christos if (*place) /* no space */
524 1.1 christos /* NOTHING */;
525 1.1 christos else if (++optind >= nargc) { /* no arg */
526 1.1 christos place = EMSG;
527 1.1 christos if (PRINT_ERROR)
528 1.1 christos warnx(recargchar, optchar);
529 1.1 christos optopt = optchar;
530 1.1 christos return (BADARG);
531 1.1 christos } else /* white space */
532 1.1 christos place = nargv[optind];
533 1.1 christos #ifdef GNU_COMPATIBLE
534 1.1 christos dash_prefix = W_PREFIX;
535 1.1 christos #endif
536 1.1 christos optchar = parse_long_options(nargv, options, long_options,
537 1.1 christos idx, 0, flags);
538 1.1 christos place = EMSG;
539 1.1 christos return (optchar);
540 1.1 christos }
541 1.1 christos if (*++oli != ':') { /* doesn't take argument */
542 1.1 christos if (!*place)
543 1.1 christos ++optind;
544 1.1 christos } else { /* takes (optional) argument */
545 1.1 christos optarg = NULL;
546 1.1 christos if (*place) /* no white space */
547 1.1 christos optarg = (char *)place;
548 1.1 christos else if (oli[1] != ':') { /* arg not optional */
549 1.1 christos if (++optind >= nargc) { /* no arg */
550 1.1 christos place = EMSG;
551 1.1 christos if (PRINT_ERROR)
552 1.1 christos warnx(recargchar, optchar);
553 1.1 christos optopt = optchar;
554 1.1 christos return (BADARG);
555 1.1 christos } else
556 1.1 christos optarg = nargv[optind];
557 1.1 christos }
558 1.1 christos place = EMSG;
559 1.1 christos ++optind;
560 1.1 christos }
561 1.1 christos /* dump back option letter */
562 1.1 christos return (optchar);
563 1.1 christos }
564 1.1 christos
565 1.1 christos #ifdef REPLACE_GETOPT
566 1.1 christos /*
567 1.1 christos * getopt --
568 1.1 christos * Parse argc/argv argument vector.
569 1.1 christos *
570 1.1 christos * [eventually this will replace the BSD getopt]
571 1.1 christos */
572 1.1 christos int
573 1.1 christos getopt(int nargc, char * const *nargv, const char *options)
574 1.1 christos {
575 1.1 christos
576 1.1 christos /*
577 1.1 christos * We don't pass FLAG_PERMUTE to getopt_internal() since
578 1.1 christos * the BSD getopt(3) (unlike GNU) has never done this.
579 1.1 christos *
580 1.1 christos * Furthermore, since many privileged programs call getopt()
581 1.1 christos * before dropping privileges it makes sense to keep things
582 1.1 christos * as simple (and bug-free) as possible.
583 1.1 christos */
584 1.1 christos return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
585 1.1 christos }
586 1.1 christos #endif /* REPLACE_GETOPT */
587 1.1 christos
588 1.1 christos /*
589 1.1 christos * getopt_long --
590 1.1 christos * Parse argc/argv argument vector.
591 1.1 christos */
592 1.1 christos int
593 1.1 christos getopt_long(int nargc, char * const *nargv, const char *options,
594 1.1 christos const struct option *long_options, int *idx)
595 1.1 christos {
596 1.1 christos
597 1.1 christos return (getopt_internal(nargc, nargv, options, long_options, idx,
598 1.1 christos FLAG_PERMUTE));
599 1.1 christos }
600 1.1 christos
601 1.1 christos /*
602 1.1 christos * getopt_long_only --
603 1.1 christos * Parse argc/argv argument vector.
604 1.1 christos */
605 1.1 christos int
606 1.1 christos getopt_long_only(int nargc, char * const *nargv, const char *options,
607 1.1 christos const struct option *long_options, int *idx)
608 1.1 christos {
609 1.1 christos
610 1.1 christos return (getopt_internal(nargc, nargv, options, long_options, idx,
611 1.1 christos FLAG_PERMUTE|FLAG_LONGONLY));
612 1.1 christos }
613