getopt_long.c revision 1.14 1 1.14 tv /* $NetBSD: getopt_long.c,v 1.14 2002/01/29 10:20:30 tv 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.14 tv __RCSID("$NetBSD: getopt_long.c,v 1.14 2002/01/29 10:20:30 tv 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.1 mcr #include <stdlib.h>
49 1.1 mcr #include <string.h>
50 1.5 christos #include <getopt.h>
51 1.4 christos
52 1.14 tv #if HAVE_ERR_H || !HAVE_CONFIG_H
53 1.14 tv #include <err.h>
54 1.14 tv #endif
55 1.14 tv
56 1.4 christos #ifdef REPLACE_GETOPT
57 1.4 christos #ifdef __weak_alias
58 1.4 christos __weak_alias(getopt,_getopt)
59 1.4 christos #endif
60 1.4 christos int opterr = 1; /* if error message should be printed */
61 1.4 christos int optind = 1; /* index into parent argv vector */
62 1.4 christos int optopt = '?'; /* character checked for validity */
63 1.4 christos int optreset; /* reset getopt */
64 1.4 christos char *optarg; /* argument associated with option */
65 1.14 tv #elif HAVE_CONFIG_H
66 1.14 tv static int optreset;
67 1.4 christos #endif
68 1.1 mcr
69 1.4 christos #ifdef __weak_alias
70 1.4 christos __weak_alias(getopt_long,_getopt_long)
71 1.4 christos #endif
72 1.2 lukem
73 1.2 lukem
74 1.4 christos #define IGNORE_FIRST (*options == '-' || *options == '+')
75 1.4 christos #define PRINT_ERROR ((opterr) && ((*options != ':') \
76 1.4 christos || (IGNORE_FIRST && options[1] != ':')))
77 1.4 christos #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
78 1.4 christos #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
79 1.4 christos /* XXX: GNU ignores PC if *options == '-' */
80 1.4 christos #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
81 1.1 mcr
82 1.4 christos /* return values */
83 1.1 mcr #define BADCH (int)'?'
84 1.9 wiz #define BADARG ((IGNORE_FIRST && options[1] == ':') \
85 1.9 wiz || (*options == ':') ? (int)':' : (int)'?')
86 1.4 christos #define INORDER (int)1
87 1.4 christos
88 1.1 mcr #define EMSG ""
89 1.1 mcr
90 1.4 christos static int getopt_internal __P((int, char * const *, const char *));
91 1.4 christos static int gcd __P((int, int));
92 1.4 christos static void permute_args __P((int, int, int, char * const *));
93 1.4 christos
94 1.4 christos static char *place = EMSG; /* option letter processing */
95 1.4 christos
96 1.4 christos /* XXX: set optreset to 1 rather than these two */
97 1.4 christos static int nonopt_start = -1; /* first non option argument (for permute) */
98 1.4 christos static int nonopt_end = -1; /* first option after non options (for permute) */
99 1.4 christos
100 1.4 christos /* Error messages */
101 1.6 nathanw static const char recargchar[] = "option requires an argument -- %c";
102 1.6 nathanw static const char recargstring[] = "option requires an argument -- %s";
103 1.4 christos static const char ambig[] = "ambiguous option -- %.*s";
104 1.4 christos static const char noarg[] = "option doesn't take an argument -- %.*s";
105 1.12 joda static const char illoptchar[] = "unknown option -- %c";
106 1.12 joda static const char illoptstring[] = "unknown option -- %s";
107 1.4 christos
108 1.4 christos
109 1.1 mcr /*
110 1.4 christos * Compute the greatest common divisor of a and b.
111 1.4 christos */
112 1.4 christos static int
113 1.4 christos gcd(a, b)
114 1.4 christos int a;
115 1.4 christos int b;
116 1.4 christos {
117 1.4 christos int c;
118 1.4 christos
119 1.4 christos c = a % b;
120 1.4 christos while (c != 0) {
121 1.4 christos a = b;
122 1.4 christos b = c;
123 1.4 christos c = a % b;
124 1.4 christos }
125 1.4 christos
126 1.4 christos return b;
127 1.4 christos }
128 1.4 christos
129 1.4 christos /*
130 1.4 christos * Exchange the block from nonopt_start to nonopt_end with the block
131 1.4 christos * from nonopt_end to opt_end (keeping the same order of arguments
132 1.4 christos * in each block).
133 1.4 christos */
134 1.4 christos static void
135 1.13 lukem permute_args(panonopt_start, panonopt_end, opt_end, nargv)
136 1.13 lukem int panonopt_start;
137 1.13 lukem int panonopt_end;
138 1.4 christos int opt_end;
139 1.4 christos char * const *nargv;
140 1.4 christos {
141 1.4 christos int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
142 1.4 christos char *swap;
143 1.4 christos
144 1.10 lukem _DIAGASSERT(nargv != NULL);
145 1.10 lukem
146 1.4 christos /*
147 1.4 christos * compute lengths of blocks and number and size of cycles
148 1.4 christos */
149 1.13 lukem nnonopts = panonopt_end - panonopt_start;
150 1.13 lukem nopts = opt_end - panonopt_end;
151 1.4 christos ncycle = gcd(nnonopts, nopts);
152 1.13 lukem cyclelen = (opt_end - panonopt_start) / ncycle;
153 1.4 christos
154 1.4 christos for (i = 0; i < ncycle; i++) {
155 1.13 lukem cstart = panonopt_end+i;
156 1.4 christos pos = cstart;
157 1.4 christos for (j = 0; j < cyclelen; j++) {
158 1.13 lukem if (pos >= panonopt_end)
159 1.4 christos pos -= nnonopts;
160 1.4 christos else
161 1.4 christos pos += nopts;
162 1.4 christos swap = nargv[pos];
163 1.4 christos /* LINTED const cast */
164 1.4 christos ((char **) nargv)[pos] = nargv[cstart];
165 1.4 christos /* LINTED const cast */
166 1.4 christos ((char **)nargv)[cstart] = swap;
167 1.4 christos }
168 1.4 christos }
169 1.4 christos }
170 1.4 christos
171 1.4 christos /*
172 1.4 christos * getopt_internal --
173 1.4 christos * Parse argc/argv argument vector. Called by user level routines.
174 1.4 christos * Returns -2 if -- is found (can be long option or end of options marker).
175 1.1 mcr */
176 1.4 christos static int
177 1.4 christos getopt_internal(nargc, nargv, options)
178 1.1 mcr int nargc;
179 1.1 mcr char * const *nargv;
180 1.4 christos const char *options;
181 1.1 mcr {
182 1.1 mcr char *oli; /* option letter list index */
183 1.4 christos int optchar;
184 1.1 mcr
185 1.2 lukem _DIAGASSERT(nargv != NULL);
186 1.4 christos _DIAGASSERT(options != NULL);
187 1.4 christos
188 1.4 christos optarg = NULL;
189 1.8 thorpej
190 1.8 thorpej /*
191 1.8 thorpej * XXX Some programs (like rsyncd) expect to be able to
192 1.8 thorpej * XXX re-initialize optind to 0 and have getopt_long(3)
193 1.8 thorpej * XXX properly function again. Work around this braindamage.
194 1.8 thorpej */
195 1.8 thorpej if (optind == 0)
196 1.8 thorpej optind = 1;
197 1.2 lukem
198 1.4 christos if (optreset)
199 1.4 christos nonopt_start = nonopt_end = -1;
200 1.4 christos start:
201 1.1 mcr if (optreset || !*place) { /* update scanning pointer */
202 1.1 mcr optreset = 0;
203 1.4 christos if (optind >= nargc) { /* end of argument vector */
204 1.4 christos place = EMSG;
205 1.4 christos if (nonopt_end != -1) {
206 1.4 christos /* do permutation, if we have to */
207 1.4 christos permute_args(nonopt_start, nonopt_end,
208 1.4 christos optind, nargv);
209 1.4 christos optind -= nonopt_end - nonopt_start;
210 1.4 christos }
211 1.4 christos else if (nonopt_start != -1) {
212 1.4 christos /*
213 1.4 christos * If we skipped non-options, set optind
214 1.4 christos * to the first of them.
215 1.4 christos */
216 1.4 christos optind = nonopt_start;
217 1.4 christos }
218 1.4 christos nonopt_start = nonopt_end = -1;
219 1.4 christos return -1;
220 1.4 christos }
221 1.9 wiz if ((*(place = nargv[optind]) != '-')
222 1.9 wiz || (place[1] == '\0')) { /* found non-option */
223 1.1 mcr place = EMSG;
224 1.4 christos if (IN_ORDER) {
225 1.4 christos /*
226 1.4 christos * GNU extension:
227 1.4 christos * return non-option as argument to option 1
228 1.4 christos */
229 1.4 christos optarg = nargv[optind++];
230 1.4 christos return INORDER;
231 1.4 christos }
232 1.4 christos if (!PERMUTE) {
233 1.4 christos /*
234 1.4 christos * if no permutation wanted, stop parsing
235 1.4 christos * at first non-option
236 1.4 christos */
237 1.4 christos return -1;
238 1.4 christos }
239 1.4 christos /* do permutation */
240 1.4 christos if (nonopt_start == -1)
241 1.4 christos nonopt_start = optind;
242 1.4 christos else if (nonopt_end != -1) {
243 1.4 christos permute_args(nonopt_start, nonopt_end,
244 1.4 christos optind, nargv);
245 1.4 christos nonopt_start = optind -
246 1.4 christos (nonopt_end - nonopt_start);
247 1.4 christos nonopt_end = -1;
248 1.4 christos }
249 1.4 christos optind++;
250 1.4 christos /* process next argument */
251 1.4 christos goto start;
252 1.1 mcr }
253 1.4 christos if (nonopt_start != -1 && nonopt_end == -1)
254 1.4 christos nonopt_end = optind;
255 1.1 mcr if (place[1] && *++place == '-') { /* found "--" */
256 1.4 christos place++;
257 1.4 christos return -2;
258 1.4 christos }
259 1.4 christos }
260 1.4 christos if ((optchar = (int)*place++) == (int)':' ||
261 1.4 christos (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
262 1.4 christos /* option letter unknown or ':' */
263 1.4 christos if (!*place)
264 1.4 christos ++optind;
265 1.4 christos if (PRINT_ERROR)
266 1.6 nathanw warnx(illoptchar, optchar);
267 1.4 christos optopt = optchar;
268 1.4 christos return BADCH;
269 1.4 christos }
270 1.4 christos if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
271 1.4 christos /* XXX: what if no long options provided (called by getopt)? */
272 1.4 christos if (*place)
273 1.4 christos return -2;
274 1.4 christos
275 1.4 christos if (++optind >= nargc) { /* no arg */
276 1.1 mcr place = EMSG;
277 1.4 christos if (PRINT_ERROR)
278 1.6 nathanw warnx(recargchar, optchar);
279 1.4 christos optopt = optchar;
280 1.4 christos return BADARG;
281 1.4 christos } else /* white space */
282 1.4 christos place = nargv[optind];
283 1.1 mcr /*
284 1.4 christos * Handle -W arg the same as --arg (which causes getopt to
285 1.4 christos * stop parsing).
286 1.1 mcr */
287 1.4 christos return -2;
288 1.4 christos }
289 1.4 christos if (*++oli != ':') { /* doesn't take argument */
290 1.1 mcr if (!*place)
291 1.1 mcr ++optind;
292 1.4 christos } else { /* takes (optional) argument */
293 1.1 mcr optarg = NULL;
294 1.1 mcr if (*place) /* no white space */
295 1.1 mcr optarg = place;
296 1.4 christos /* XXX: disable test for :: if PC? (GNU doesn't) */
297 1.4 christos else if (oli[1] != ':') { /* arg not optional */
298 1.4 christos if (++optind >= nargc) { /* no arg */
299 1.4 christos place = EMSG;
300 1.4 christos if (PRINT_ERROR)
301 1.6 nathanw warnx(recargchar, optchar);
302 1.4 christos optopt = optchar;
303 1.4 christos return BADARG;
304 1.4 christos } else
305 1.4 christos optarg = nargv[optind];
306 1.4 christos }
307 1.1 mcr place = EMSG;
308 1.1 mcr ++optind;
309 1.1 mcr }
310 1.4 christos /* dump back option letter */
311 1.4 christos return optchar;
312 1.1 mcr }
313 1.1 mcr
314 1.4 christos #ifdef REPLACE_GETOPT
315 1.1 mcr /*
316 1.1 mcr * getopt --
317 1.1 mcr * Parse argc/argv argument vector.
318 1.4 christos *
319 1.4 christos * [eventually this will replace the real getopt]
320 1.1 mcr */
321 1.1 mcr int
322 1.4 christos getopt(nargc, nargv, options)
323 1.1 mcr int nargc;
324 1.1 mcr char * const *nargv;
325 1.4 christos const char *options;
326 1.1 mcr {
327 1.1 mcr int retval;
328 1.10 lukem
329 1.10 lukem _DIAGASSERT(nargv != NULL);
330 1.10 lukem _DIAGASSERT(options != NULL);
331 1.1 mcr
332 1.4 christos if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
333 1.4 christos ++optind;
334 1.4 christos /*
335 1.4 christos * We found an option (--), so if we skipped non-options,
336 1.4 christos * we have to permute.
337 1.4 christos */
338 1.4 christos if (nonopt_end != -1) {
339 1.4 christos permute_args(nonopt_start, nonopt_end, optind,
340 1.4 christos nargv);
341 1.4 christos optind -= nonopt_end - nonopt_start;
342 1.4 christos }
343 1.4 christos nonopt_start = nonopt_end = -1;
344 1.1 mcr retval = -1;
345 1.1 mcr }
346 1.4 christos return retval;
347 1.1 mcr }
348 1.1 mcr #endif
349 1.1 mcr
350 1.1 mcr /*
351 1.1 mcr * getopt_long --
352 1.1 mcr * Parse argc/argv argument vector.
353 1.1 mcr */
354 1.1 mcr int
355 1.4 christos getopt_long(nargc, nargv, options, long_options, idx)
356 1.2 lukem int nargc;
357 1.4 christos char * const *nargv;
358 1.4 christos const char *options;
359 1.4 christos const struct option *long_options;
360 1.4 christos int *idx;
361 1.1 mcr {
362 1.1 mcr int retval;
363 1.1 mcr
364 1.2 lukem _DIAGASSERT(nargv != NULL);
365 1.4 christos _DIAGASSERT(options != NULL);
366 1.2 lukem _DIAGASSERT(long_options != NULL);
367 1.4 christos /* idx may be NULL */
368 1.2 lukem
369 1.1 mcr if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
370 1.4 christos char *current_argv, *has_equal;
371 1.4 christos size_t current_argv_len;
372 1.4 christos int i, match;
373 1.1 mcr
374 1.4 christos current_argv = place;
375 1.4 christos match = -1;
376 1.4 christos
377 1.4 christos optind++;
378 1.4 christos place = EMSG;
379 1.4 christos
380 1.4 christos if (*current_argv == '\0') { /* found "--" */
381 1.4 christos /*
382 1.4 christos * We found an option (--), so if we skipped
383 1.4 christos * non-options, we have to permute.
384 1.4 christos */
385 1.4 christos if (nonopt_end != -1) {
386 1.4 christos permute_args(nonopt_start, nonopt_end,
387 1.4 christos optind, nargv);
388 1.4 christos optind -= nonopt_end - nonopt_start;
389 1.4 christos }
390 1.4 christos nonopt_start = nonopt_end = -1;
391 1.4 christos return -1;
392 1.1 mcr }
393 1.1 mcr if ((has_equal = strchr(current_argv, '=')) != NULL) {
394 1.4 christos /* argument found (--option=arg) */
395 1.1 mcr current_argv_len = has_equal - current_argv;
396 1.1 mcr has_equal++;
397 1.1 mcr } else
398 1.1 mcr current_argv_len = strlen(current_argv);
399 1.4 christos
400 1.4 christos for (i = 0; long_options[i].name; i++) {
401 1.4 christos /* find matching long option */
402 1.4 christos if (strncmp(current_argv, long_options[i].name,
403 1.4 christos current_argv_len))
404 1.1 mcr continue;
405 1.1 mcr
406 1.4 christos if (strlen(long_options[i].name) ==
407 1.4 christos (unsigned)current_argv_len) {
408 1.4 christos /* exact match */
409 1.1 mcr match = i;
410 1.1 mcr break;
411 1.1 mcr }
412 1.4 christos if (match == -1) /* partial match */
413 1.1 mcr match = i;
414 1.4 christos else {
415 1.4 christos /* ambiguous abbreviation */
416 1.4 christos if (PRINT_ERROR)
417 1.7 sommerfe warnx(ambig, (int)current_argv_len,
418 1.4 christos current_argv);
419 1.4 christos optopt = 0;
420 1.4 christos return BADCH;
421 1.4 christos }
422 1.1 mcr }
423 1.4 christos if (match != -1) { /* option found */
424 1.4 christos if (long_options[match].has_arg == no_argument
425 1.4 christos && has_equal) {
426 1.4 christos if (PRINT_ERROR)
427 1.7 sommerfe warnx(noarg, (int)current_argv_len,
428 1.4 christos current_argv);
429 1.4 christos /*
430 1.4 christos * XXX: GNU sets optopt to val regardless of
431 1.4 christos * flag
432 1.4 christos */
433 1.4 christos if (long_options[match].flag == NULL)
434 1.4 christos optopt = long_options[match].val;
435 1.4 christos else
436 1.4 christos optopt = 0;
437 1.4 christos return BADARG;
438 1.4 christos }
439 1.1 mcr if (long_options[match].has_arg == required_argument ||
440 1.1 mcr long_options[match].has_arg == optional_argument) {
441 1.1 mcr if (has_equal)
442 1.1 mcr optarg = has_equal;
443 1.4 christos else if (long_options[match].has_arg ==
444 1.4 christos required_argument) {
445 1.4 christos /*
446 1.4 christos * optional argument doesn't use
447 1.4 christos * next nargv
448 1.4 christos */
449 1.1 mcr optarg = nargv[optind++];
450 1.4 christos }
451 1.1 mcr }
452 1.1 mcr if ((long_options[match].has_arg == required_argument)
453 1.1 mcr && (optarg == NULL)) {
454 1.1 mcr /*
455 1.4 christos * Missing argument; leading ':'
456 1.1 mcr * indicates no error should be generated
457 1.1 mcr */
458 1.4 christos if (PRINT_ERROR)
459 1.6 nathanw warnx(recargstring, current_argv);
460 1.4 christos /*
461 1.4 christos * XXX: GNU sets optopt to val regardless
462 1.4 christos * of flag
463 1.4 christos */
464 1.4 christos if (long_options[match].flag == NULL)
465 1.4 christos optopt = long_options[match].val;
466 1.4 christos else
467 1.4 christos optopt = 0;
468 1.4 christos --optind;
469 1.4 christos return BADARG;
470 1.4 christos }
471 1.4 christos } else { /* unknown option */
472 1.4 christos if (PRINT_ERROR)
473 1.6 nathanw warnx(illoptstring, current_argv);
474 1.4 christos optopt = 0;
475 1.4 christos return BADCH;
476 1.1 mcr }
477 1.1 mcr if (long_options[match].flag) {
478 1.1 mcr *long_options[match].flag = long_options[match].val;
479 1.1 mcr retval = 0;
480 1.1 mcr } else
481 1.1 mcr retval = long_options[match].val;
482 1.4 christos if (idx)
483 1.4 christos *idx = match;
484 1.1 mcr }
485 1.4 christos return retval;
486 1.1 mcr }
487