getopt_long.c revision 1.6 1 1.6 nathanw /* $NetBSD: getopt_long.c,v 1.6 2000/06/09 21:35:53 nathanw Exp $ */
2 1.4 christos
3 1.4 christos /*-
4 1.4 christos * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.4 christos * All rights reserved.
6 1.4 christos *
7 1.4 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.4 christos * by Dieter Baron and Thomas Klausner.
9 1.1 mcr *
10 1.1 mcr * Redistribution and use in source and binary forms, with or without
11 1.1 mcr * modification, are permitted provided that the following conditions
12 1.1 mcr * are met:
13 1.1 mcr * 1. Redistributions of source code must retain the above copyright
14 1.1 mcr * notice, this list of conditions and the following disclaimer.
15 1.1 mcr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mcr * notice, this list of conditions and the following disclaimer in the
17 1.1 mcr * documentation and/or other materials provided with the distribution.
18 1.1 mcr * 3. All advertising materials mentioning features or use of this software
19 1.1 mcr * must display the following acknowledgement:
20 1.4 christos * This product includes software developed by the NetBSD
21 1.4 christos * Foundation, Inc. and its contributors.
22 1.4 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4 christos * contributors may be used to endorse or promote products derived
24 1.4 christos * from this software without specific prior written permission.
25 1.1 mcr *
26 1.4 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mcr */
38 1.1 mcr
39 1.2 lukem #include <sys/cdefs.h>
40 1.4 christos #if defined(LIBC_SCCS) && !defined(lint)
41 1.6 nathanw __RCSID("$NetBSD: getopt_long.c,v 1.6 2000/06/09 21:35:53 nathanw Exp $");
42 1.4 christos #endif /* LIBC_SCCS and not lint */
43 1.2 lukem
44 1.5 christos #include "namespace.h"
45 1.5 christos
46 1.2 lukem #include <assert.h>
47 1.2 lukem #include <errno.h>
48 1.4 christos #include <err.h>
49 1.1 mcr #include <stdlib.h>
50 1.1 mcr #include <string.h>
51 1.5 christos #include <getopt.h>
52 1.4 christos
53 1.4 christos #ifdef REPLACE_GETOPT
54 1.4 christos #ifdef __weak_alias
55 1.4 christos __weak_alias(getopt,_getopt)
56 1.4 christos #endif
57 1.4 christos int opterr = 1; /* if error message should be printed */
58 1.4 christos int optind = 1; /* index into parent argv vector */
59 1.4 christos int optopt = '?'; /* character checked for validity */
60 1.4 christos int optreset; /* reset getopt */
61 1.4 christos char *optarg; /* argument associated with option */
62 1.4 christos #endif
63 1.1 mcr
64 1.4 christos #ifdef __weak_alias
65 1.4 christos __weak_alias(getopt_long,_getopt_long)
66 1.4 christos #endif
67 1.2 lukem
68 1.2 lukem
69 1.4 christos #define IGNORE_FIRST (*options == '-' || *options == '+')
70 1.4 christos #define PRINT_ERROR ((opterr) && ((*options != ':') \
71 1.4 christos || (IGNORE_FIRST && options[1] != ':')))
72 1.4 christos #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
73 1.4 christos #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
74 1.4 christos /* XXX: GNU ignores PC if *options == '-' */
75 1.4 christos #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
76 1.1 mcr
77 1.4 christos /* return values */
78 1.1 mcr #define BADCH (int)'?'
79 1.1 mcr #define BADARG (int)':'
80 1.4 christos #define INORDER (int)1
81 1.4 christos
82 1.1 mcr #define EMSG ""
83 1.1 mcr
84 1.4 christos static int getopt_internal __P((int, char * const *, const char *));
85 1.4 christos static int gcd __P((int, int));
86 1.4 christos static void permute_args __P((int, int, int, char * const *));
87 1.4 christos
88 1.4 christos static char *place = EMSG; /* option letter processing */
89 1.4 christos
90 1.4 christos /* XXX: set optreset to 1 rather than these two */
91 1.4 christos static int nonopt_start = -1; /* first non option argument (for permute) */
92 1.4 christos static int nonopt_end = -1; /* first option after non options (for permute) */
93 1.4 christos
94 1.4 christos /* Error messages */
95 1.6 nathanw static const char recargchar[] = "option requires an argument -- %c";
96 1.6 nathanw static const char recargstring[] = "option requires an argument -- %s";
97 1.4 christos static const char ambig[] = "ambiguous option -- %.*s";
98 1.4 christos static const char noarg[] = "option doesn't take an argument -- %.*s";
99 1.6 nathanw static const char illoptchar[] = "illegal option -- %c";
100 1.6 nathanw static const char illoptstring[] = "illegal option -- %s";
101 1.4 christos
102 1.4 christos
103 1.4 christos extern char *__progname;
104 1.4 christos
105 1.1 mcr /*
106 1.4 christos * Compute the greatest common divisor of a and b.
107 1.4 christos */
108 1.4 christos static int
109 1.4 christos gcd(a, b)
110 1.4 christos int a;
111 1.4 christos int b;
112 1.4 christos {
113 1.4 christos int c;
114 1.4 christos
115 1.4 christos c = a % b;
116 1.4 christos while (c != 0) {
117 1.4 christos a = b;
118 1.4 christos b = c;
119 1.4 christos c = a % b;
120 1.4 christos }
121 1.4 christos
122 1.4 christos return b;
123 1.4 christos }
124 1.4 christos
125 1.4 christos /*
126 1.4 christos * Exchange the block from nonopt_start to nonopt_end with the block
127 1.4 christos * from nonopt_end to opt_end (keeping the same order of arguments
128 1.4 christos * in each block).
129 1.4 christos */
130 1.4 christos static void
131 1.4 christos permute_args(nonopt_start, nonopt_end, opt_end, nargv)
132 1.4 christos int nonopt_start;
133 1.4 christos int nonopt_end;
134 1.4 christos int opt_end;
135 1.4 christos char * const *nargv;
136 1.4 christos {
137 1.4 christos int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
138 1.4 christos char *swap;
139 1.4 christos
140 1.4 christos /*
141 1.4 christos * compute lengths of blocks and number and size of cycles
142 1.4 christos */
143 1.4 christos nnonopts = nonopt_end - nonopt_start;
144 1.4 christos nopts = opt_end - nonopt_end;
145 1.4 christos ncycle = gcd(nnonopts, nopts);
146 1.4 christos cyclelen = (opt_end - nonopt_start) / ncycle;
147 1.4 christos
148 1.4 christos for (i = 0; i < ncycle; i++) {
149 1.4 christos cstart = nonopt_end+i;
150 1.4 christos pos = cstart;
151 1.4 christos for (j = 0; j < cyclelen; j++) {
152 1.4 christos if (pos >= nonopt_end)
153 1.4 christos pos -= nnonopts;
154 1.4 christos else
155 1.4 christos pos += nopts;
156 1.4 christos swap = nargv[pos];
157 1.4 christos /* LINTED const cast */
158 1.4 christos ((char **) nargv)[pos] = nargv[cstart];
159 1.4 christos /* LINTED const cast */
160 1.4 christos ((char **)nargv)[cstart] = swap;
161 1.4 christos }
162 1.4 christos }
163 1.4 christos }
164 1.4 christos
165 1.4 christos /*
166 1.4 christos * getopt_internal --
167 1.4 christos * Parse argc/argv argument vector. Called by user level routines.
168 1.4 christos * Returns -2 if -- is found (can be long option or end of options marker).
169 1.1 mcr */
170 1.4 christos static int
171 1.4 christos getopt_internal(nargc, nargv, options)
172 1.1 mcr int nargc;
173 1.1 mcr char * const *nargv;
174 1.4 christos const char *options;
175 1.1 mcr {
176 1.1 mcr char *oli; /* option letter list index */
177 1.4 christos int optchar;
178 1.1 mcr
179 1.2 lukem _DIAGASSERT(nargv != NULL);
180 1.4 christos _DIAGASSERT(options != NULL);
181 1.4 christos
182 1.4 christos optarg = NULL;
183 1.2 lukem
184 1.4 christos if (optreset)
185 1.4 christos nonopt_start = nonopt_end = -1;
186 1.4 christos start:
187 1.1 mcr if (optreset || !*place) { /* update scanning pointer */
188 1.1 mcr optreset = 0;
189 1.4 christos if (optind >= nargc) { /* end of argument vector */
190 1.4 christos place = EMSG;
191 1.4 christos if (nonopt_end != -1) {
192 1.4 christos /* do permutation, if we have to */
193 1.4 christos permute_args(nonopt_start, nonopt_end,
194 1.4 christos optind, nargv);
195 1.4 christos optind -= nonopt_end - nonopt_start;
196 1.4 christos }
197 1.4 christos else if (nonopt_start != -1) {
198 1.4 christos /*
199 1.4 christos * If we skipped non-options, set optind
200 1.4 christos * to the first of them.
201 1.4 christos */
202 1.4 christos optind = nonopt_start;
203 1.4 christos }
204 1.4 christos nonopt_start = nonopt_end = -1;
205 1.4 christos return -1;
206 1.4 christos }
207 1.4 christos if (*(place = nargv[optind]) != '-') { /* found non-option */
208 1.1 mcr place = EMSG;
209 1.4 christos if (IN_ORDER) {
210 1.4 christos /*
211 1.4 christos * GNU extension:
212 1.4 christos * return non-option as argument to option 1
213 1.4 christos */
214 1.4 christos optarg = nargv[optind++];
215 1.4 christos return INORDER;
216 1.4 christos }
217 1.4 christos if (!PERMUTE) {
218 1.4 christos /*
219 1.4 christos * if no permutation wanted, stop parsing
220 1.4 christos * at first non-option
221 1.4 christos */
222 1.4 christos return -1;
223 1.4 christos }
224 1.4 christos /* do permutation */
225 1.4 christos if (nonopt_start == -1)
226 1.4 christos nonopt_start = optind;
227 1.4 christos else if (nonopt_end != -1) {
228 1.4 christos permute_args(nonopt_start, nonopt_end,
229 1.4 christos optind, nargv);
230 1.4 christos nonopt_start = optind -
231 1.4 christos (nonopt_end - nonopt_start);
232 1.4 christos nonopt_end = -1;
233 1.4 christos }
234 1.4 christos optind++;
235 1.4 christos /* process next argument */
236 1.4 christos goto start;
237 1.1 mcr }
238 1.4 christos if (nonopt_start != -1 && nonopt_end == -1)
239 1.4 christos nonopt_end = optind;
240 1.1 mcr if (place[1] && *++place == '-') { /* found "--" */
241 1.4 christos place++;
242 1.4 christos return -2;
243 1.4 christos }
244 1.4 christos }
245 1.4 christos if ((optchar = (int)*place++) == (int)':' ||
246 1.4 christos (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
247 1.4 christos /* option letter unknown or ':' */
248 1.4 christos if (!*place)
249 1.4 christos ++optind;
250 1.4 christos if (PRINT_ERROR)
251 1.6 nathanw warnx(illoptchar, optchar);
252 1.4 christos optopt = optchar;
253 1.4 christos return BADCH;
254 1.4 christos }
255 1.4 christos if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
256 1.4 christos /* XXX: what if no long options provided (called by getopt)? */
257 1.4 christos if (*place)
258 1.4 christos return -2;
259 1.4 christos
260 1.4 christos if (++optind >= nargc) { /* no arg */
261 1.1 mcr place = EMSG;
262 1.4 christos if (PRINT_ERROR)
263 1.6 nathanw warnx(recargchar, optchar);
264 1.4 christos optopt = optchar;
265 1.4 christos /* XXX: GNU returns '?' if options[0] != ':' */
266 1.4 christos return BADARG;
267 1.4 christos } else /* white space */
268 1.4 christos place = nargv[optind];
269 1.1 mcr /*
270 1.4 christos * Handle -W arg the same as --arg (which causes getopt to
271 1.4 christos * stop parsing).
272 1.1 mcr */
273 1.4 christos return -2;
274 1.4 christos }
275 1.4 christos if (*++oli != ':') { /* doesn't take argument */
276 1.1 mcr if (!*place)
277 1.1 mcr ++optind;
278 1.4 christos } else { /* takes (optional) argument */
279 1.1 mcr optarg = NULL;
280 1.1 mcr if (*place) /* no white space */
281 1.1 mcr optarg = place;
282 1.4 christos /* XXX: disable test for :: if PC? (GNU doesn't) */
283 1.4 christos else if (oli[1] != ':') { /* arg not optional */
284 1.4 christos if (++optind >= nargc) { /* no arg */
285 1.4 christos place = EMSG;
286 1.4 christos if (PRINT_ERROR)
287 1.6 nathanw warnx(recargchar, optchar);
288 1.4 christos optopt = optchar;
289 1.4 christos /* XXX: GNU returns '?' if options[0] != ':' */
290 1.4 christos return BADARG;
291 1.4 christos } else
292 1.4 christos optarg = nargv[optind];
293 1.4 christos }
294 1.1 mcr place = EMSG;
295 1.1 mcr ++optind;
296 1.1 mcr }
297 1.4 christos /* dump back option letter */
298 1.4 christos return optchar;
299 1.1 mcr }
300 1.1 mcr
301 1.4 christos #ifdef REPLACE_GETOPT
302 1.1 mcr /*
303 1.1 mcr * getopt --
304 1.1 mcr * Parse argc/argv argument vector.
305 1.4 christos *
306 1.4 christos * [eventually this will replace the real getopt]
307 1.1 mcr */
308 1.1 mcr int
309 1.4 christos getopt(nargc, nargv, options)
310 1.1 mcr int nargc;
311 1.1 mcr char * const *nargv;
312 1.4 christos const char *options;
313 1.1 mcr {
314 1.1 mcr int retval;
315 1.1 mcr
316 1.4 christos if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
317 1.4 christos ++optind;
318 1.4 christos /*
319 1.4 christos * We found an option (--), so if we skipped non-options,
320 1.4 christos * we have to permute.
321 1.4 christos */
322 1.4 christos if (nonopt_end != -1) {
323 1.4 christos permute_args(nonopt_start, nonopt_end, optind,
324 1.4 christos nargv);
325 1.4 christos optind -= nonopt_end - nonopt_start;
326 1.4 christos }
327 1.4 christos nonopt_start = nonopt_end = -1;
328 1.1 mcr retval = -1;
329 1.1 mcr }
330 1.4 christos return retval;
331 1.1 mcr }
332 1.1 mcr #endif
333 1.1 mcr
334 1.1 mcr /*
335 1.1 mcr * getopt_long --
336 1.1 mcr * Parse argc/argv argument vector.
337 1.1 mcr */
338 1.1 mcr int
339 1.4 christos getopt_long(nargc, nargv, options, long_options, idx)
340 1.2 lukem int nargc;
341 1.4 christos char * const *nargv;
342 1.4 christos const char *options;
343 1.4 christos const struct option *long_options;
344 1.4 christos int *idx;
345 1.1 mcr {
346 1.1 mcr int retval;
347 1.1 mcr
348 1.2 lukem _DIAGASSERT(nargv != NULL);
349 1.4 christos _DIAGASSERT(options != NULL);
350 1.2 lukem _DIAGASSERT(long_options != NULL);
351 1.4 christos /* idx may be NULL */
352 1.2 lukem
353 1.1 mcr if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
354 1.4 christos char *current_argv, *has_equal;
355 1.4 christos size_t current_argv_len;
356 1.4 christos int i, match;
357 1.1 mcr
358 1.4 christos current_argv = place;
359 1.4 christos match = -1;
360 1.4 christos
361 1.4 christos optind++;
362 1.4 christos place = EMSG;
363 1.4 christos
364 1.4 christos if (*current_argv == '\0') { /* found "--" */
365 1.4 christos /*
366 1.4 christos * We found an option (--), so if we skipped
367 1.4 christos * non-options, we have to permute.
368 1.4 christos */
369 1.4 christos if (nonopt_end != -1) {
370 1.4 christos permute_args(nonopt_start, nonopt_end,
371 1.4 christos optind, nargv);
372 1.4 christos optind -= nonopt_end - nonopt_start;
373 1.4 christos }
374 1.4 christos nonopt_start = nonopt_end = -1;
375 1.4 christos return -1;
376 1.1 mcr }
377 1.1 mcr if ((has_equal = strchr(current_argv, '=')) != NULL) {
378 1.4 christos /* argument found (--option=arg) */
379 1.1 mcr current_argv_len = has_equal - current_argv;
380 1.1 mcr has_equal++;
381 1.1 mcr } else
382 1.1 mcr current_argv_len = strlen(current_argv);
383 1.4 christos
384 1.4 christos for (i = 0; long_options[i].name; i++) {
385 1.4 christos /* find matching long option */
386 1.4 christos if (strncmp(current_argv, long_options[i].name,
387 1.4 christos current_argv_len))
388 1.1 mcr continue;
389 1.1 mcr
390 1.4 christos if (strlen(long_options[i].name) ==
391 1.4 christos (unsigned)current_argv_len) {
392 1.4 christos /* exact match */
393 1.1 mcr match = i;
394 1.1 mcr break;
395 1.1 mcr }
396 1.4 christos if (match == -1) /* partial match */
397 1.1 mcr match = i;
398 1.4 christos else {
399 1.4 christos /* ambiguous abbreviation */
400 1.4 christos if (PRINT_ERROR)
401 1.4 christos warnx(ambig, current_argv_len,
402 1.4 christos current_argv);
403 1.4 christos optopt = 0;
404 1.4 christos return BADCH;
405 1.4 christos }
406 1.1 mcr }
407 1.4 christos if (match != -1) { /* option found */
408 1.4 christos if (long_options[match].has_arg == no_argument
409 1.4 christos && has_equal) {
410 1.4 christos if (PRINT_ERROR)
411 1.4 christos warnx(noarg, current_argv_len,
412 1.4 christos current_argv);
413 1.4 christos /*
414 1.4 christos * XXX: GNU sets optopt to val regardless of
415 1.4 christos * flag
416 1.4 christos */
417 1.4 christos if (long_options[match].flag == NULL)
418 1.4 christos optopt = long_options[match].val;
419 1.4 christos else
420 1.4 christos optopt = 0;
421 1.4 christos /* XXX: GNU returns '?' if options[0] != ':' */
422 1.4 christos return BADARG;
423 1.4 christos }
424 1.1 mcr if (long_options[match].has_arg == required_argument ||
425 1.1 mcr long_options[match].has_arg == optional_argument) {
426 1.1 mcr if (has_equal)
427 1.1 mcr optarg = has_equal;
428 1.4 christos else if (long_options[match].has_arg ==
429 1.4 christos required_argument) {
430 1.4 christos /*
431 1.4 christos * optional argument doesn't use
432 1.4 christos * next nargv
433 1.4 christos */
434 1.1 mcr optarg = nargv[optind++];
435 1.4 christos }
436 1.1 mcr }
437 1.1 mcr if ((long_options[match].has_arg == required_argument)
438 1.1 mcr && (optarg == NULL)) {
439 1.1 mcr /*
440 1.4 christos * Missing argument; leading ':'
441 1.1 mcr * indicates no error should be generated
442 1.1 mcr */
443 1.4 christos if (PRINT_ERROR)
444 1.6 nathanw warnx(recargstring, current_argv);
445 1.4 christos /*
446 1.4 christos * XXX: GNU sets optopt to val regardless
447 1.4 christos * of flag
448 1.4 christos */
449 1.4 christos if (long_options[match].flag == NULL)
450 1.4 christos optopt = long_options[match].val;
451 1.4 christos else
452 1.4 christos optopt = 0;
453 1.4 christos /* XXX: GNU returns '?' if options[0] != ':' */
454 1.4 christos --optind;
455 1.4 christos return BADARG;
456 1.4 christos }
457 1.4 christos } else { /* unknown option */
458 1.4 christos if (PRINT_ERROR)
459 1.6 nathanw warnx(illoptstring, current_argv);
460 1.4 christos optopt = 0;
461 1.4 christos return BADCH;
462 1.1 mcr }
463 1.1 mcr if (long_options[match].flag) {
464 1.1 mcr *long_options[match].flag = long_options[match].val;
465 1.1 mcr retval = 0;
466 1.1 mcr } else
467 1.1 mcr retval = long_options[match].val;
468 1.4 christos if (idx)
469 1.4 christos *idx = match;
470 1.1 mcr }
471 1.4 christos return retval;
472 1.1 mcr }
473