1 /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
2 /* $FreeBSD: src/lib/libc/stdlib/getopt_long.c,v 1.2 2002/10/16 22:18:42 alfred Exp $ */
5 * Copyright (c) 2000 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Dieter Baron and Thomas Klausner.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
44 #ifdef PLATFORM_WINDOWS
46 #include "pbd/msvc_pbd.h" // Defines 'GETOPT_API'
48 /* Windows needs warnx(). We change the definition though:
49 * 1. (another) global is defined, opterrmsg, which holds the error message
50 * 2. errors are always printed out on stderr w/o the program name
51 * Note that opterrmsg always gets set no matter what opterr is set to. The
52 * error message will not be printed if opterr is 0 as usual.
59 GETOPT_API extern char opterrmsg[128];
60 char opterrmsg[128]; /* last error message is stored here */
62 static void warnx(int print_error, const char *fmt, ...)
67 _vsnprintf(opterrmsg, 128, fmt, ap);
72 fprintf(stderr, opterrmsg);
73 fprintf(stderr, "\n");
77 #endif /*PLATFORM_WINDOWS*/
79 /* not part of the original file */
81 #define _DIAGASSERT(X)
84 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
85 #define REPLACE_GETOPT
90 __weak_alias(getopt,_getopt)
92 int opterr = 1; /* if error message should be printed */
93 int optind = 1; /* index into parent argv vector */
94 int optopt = '?'; /* character checked for validity */
95 int optreset; /* reset getopt */
96 char *optarg; /* argument associated with option */
97 #elif HAVE_CONFIG_H && !HAVE_DECL_OPTRESET
102 __weak_alias(getopt_long,_getopt_long)
105 #if !HAVE_GETOPT_LONG
106 #define IGNORE_FIRST (*options == '-' || *options == '+')
107 #define PRINT_ERROR ((opterr) && ((*options != ':') \
108 || (IGNORE_FIRST && options[1] != ':')))
109 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
110 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
111 /* XXX: GNU ignores PC if *options == '-' */
112 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
115 #define BADCH (int)'?'
116 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
117 || (*options == ':') ? (int)':' : (int)'?')
118 #define INORDER (int)1
122 static int getopt_internal(int, char * const *, const char *);
123 static int gcd(int, int);
124 static void permute_args(int, int, int, char * const *);
126 static char *place = EMSG; /* option letter processing */
128 /* XXX: set optreset to 1 rather than these two */
129 static int nonopt_start = -1; /* first non option argument (for permute) */
130 static int nonopt_end = -1; /* first option after non options (for permute) */
133 static const char recargchar[] = "option requires an argument -- %c";
134 static const char recargstring[] = "option requires an argument -- %s";
135 static const char ambig[] = "ambiguous option -- %.*s";
136 static const char noarg[] = "option doesn't take an argument -- %.*s";
137 static const char illoptchar[] = "unknown option -- %c";
138 static const char illoptstring[] = "unknown option -- %s";
142 * Compute the greatest common divisor of a and b.
160 * Exchange the block from nonopt_start to nonopt_end with the block
161 * from nonopt_end to opt_end (keeping the same order of arguments
165 permute_args(int panonopt_start,
170 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
173 _DIAGASSERT(nargv != NULL);
176 * compute lengths of blocks and number and size of cycles
178 nnonopts = panonopt_end - panonopt_start;
179 nopts = opt_end - panonopt_end;
180 ncycle = gcd(nnonopts, nopts);
181 cyclelen = (opt_end - panonopt_start) / ncycle;
183 for (i = 0; i < ncycle; i++) {
184 cstart = panonopt_end+i;
186 for (j = 0; j < cyclelen; j++) {
187 if (pos >= panonopt_end)
192 /* LINTED const cast */
193 ((char **) nargv)[pos] = nargv[cstart];
194 /* LINTED const cast */
195 ((char **)nargv)[cstart] = swap;
202 * Parse argc/argv argument vector. Called by user level routines.
203 * Returns -2 if -- is found (can be long option or end of options marker).
206 getopt_internal(int nargc,
210 char *oli; /* option letter list index */
213 _DIAGASSERT(nargv != NULL);
214 _DIAGASSERT(options != NULL);
219 * XXX Some programs (like rsyncd) expect to be able to
220 * XXX re-initialize optind to 0 and have getopt_long(3)
221 * XXX properly function again. Work around this braindamage.
227 nonopt_start = nonopt_end = -1;
229 if (optreset || !*place) { /* update scanning pointer */
231 if (optind >= nargc) { /* end of argument vector */
233 if (nonopt_end != -1) {
234 /* do permutation, if we have to */
235 permute_args(nonopt_start, nonopt_end,
237 optind -= nonopt_end - nonopt_start;
239 else if (nonopt_start != -1) {
241 * If we skipped non-options, set optind
242 * to the first of them.
244 optind = nonopt_start;
246 nonopt_start = nonopt_end = -1;
249 if ((*(place = nargv[optind]) != '-')
250 || (place[1] == '\0')) { /* found non-option */
255 * return non-option as argument to option 1
257 optarg = nargv[optind++];
262 * if no permutation wanted, stop parsing
263 * at first non-option
268 if (nonopt_start == -1)
269 nonopt_start = optind;
270 else if (nonopt_end != -1) {
271 permute_args(nonopt_start, nonopt_end,
273 nonopt_start = optind -
274 (nonopt_end - nonopt_start);
278 /* process next argument */
281 if (nonopt_start != -1 && nonopt_end == -1)
283 if (place[1] && *++place == '-') { /* found "--" */
288 if ((optchar = (int)*place++) == (int)':' ||
289 (oli = (char*)strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
290 /* option letter unknown or ':' */
293 #ifndef PLATFORM_WINDOWS
295 warnx(illoptchar, optchar);
297 warnx(PRINT_ERROR, illoptchar, optchar);
302 if (optchar == 'W' && oli[1] == ';') { /* -W long-option */
303 /* XXX: what if no long options provided (called by getopt)? */
307 if (++optind >= nargc) { /* no arg */
309 #ifndef PLATFORM_WINDOWS
311 warnx(recargchar, optchar);
313 warnx(PRINT_ERROR, recargchar, optchar);
317 } else /* white space */
318 place = nargv[optind];
320 * Handle -W arg the same as --arg (which causes getopt to
325 if (*++oli != ':') { /* doesn't take argument */
328 } else { /* takes (optional) argument */
330 if (*place) /* no white space */
332 /* XXX: disable test for :: if PC? (GNU doesn't) */
333 else if (oli[1] != ':') { /* arg not optional */
334 if (++optind >= nargc) { /* no arg */
336 #ifndef PLATFORM_WINDOWS
338 warnx(recargchar, optchar);
340 warnx(PRINT_ERROR, recargchar, optchar);
345 optarg = nargv[optind];
350 /* dump back option letter */
354 __BEGIN_DECLS // Added by JE - 31-01-2010
355 #ifdef REPLACE_GETOPT
358 * Parse argc/argv argument vector.
360 * [eventually this will replace the real getopt]
362 GETOPT_API int // 'GETOPT_API' declaration added by JE - 31-01-2010
369 _DIAGASSERT(nargv != NULL);
370 _DIAGASSERT(options != NULL);
372 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
375 * We found an option (--), so if we skipped non-options,
376 * we have to permute.
378 if (nonopt_end != -1) {
379 permute_args(nonopt_start, nonopt_end, optind,
381 optind -= nonopt_end - nonopt_start;
383 nonopt_start = nonopt_end = -1;
392 * Parse argc/argv argument vector.
394 GETOPT_API int // 'GETOPT_API' declaration added by JE - 31-01-2010
395 getopt_long(int nargc,
398 const struct option *long_options,
403 _DIAGASSERT(nargv != NULL);
404 _DIAGASSERT(options != NULL);
405 _DIAGASSERT(long_options != NULL);
406 /* idx may be NULL */
408 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
409 char *current_argv, *has_equal;
410 size_t current_argv_len;
413 current_argv = place;
419 if (*current_argv == '\0') { /* found "--" */
421 * We found an option (--), so if we skipped
422 * non-options, we have to permute.
424 if (nonopt_end != -1) {
425 permute_args(nonopt_start, nonopt_end,
427 optind -= nonopt_end - nonopt_start;
429 nonopt_start = nonopt_end = -1;
432 if ((has_equal = strchr(current_argv, '=')) != NULL) {
433 /* argument found (--option=arg) */
434 current_argv_len = has_equal - current_argv;
437 current_argv_len = strlen(current_argv);
439 for (i = 0; long_options[i].name; i++) {
440 /* find matching long option */
441 if (strncmp(current_argv, long_options[i].name,
445 if (strlen(long_options[i].name) ==
446 (unsigned)current_argv_len) {
451 if (match == -1) /* partial match */
454 /* ambiguous abbreviation */
455 #ifndef PLATFORM_WINDOWS
457 warnx(ambig, (int)current_argv_len,
460 warnx(PRINT_ERROR, ambig, (int)current_argv_len,
467 if (match != -1) { /* option found */
468 if (long_options[match].has_arg == no_argument
470 #ifndef PLATFORM_WINDOWS
472 warnx(noarg, (int)current_argv_len,
475 warnx(PRINT_ERROR, noarg, (int)current_argv_len,
479 * XXX: GNU sets optopt to val regardless of
482 if (long_options[match].flag == NULL)
483 optopt = long_options[match].val;
488 if (long_options[match].has_arg == required_argument ||
489 long_options[match].has_arg == optional_argument) {
492 else if (long_options[match].has_arg ==
495 * optional argument doesn't use
498 optarg = nargv[optind++];
501 if ((long_options[match].has_arg == required_argument)
502 && (optarg == NULL)) {
504 * Missing argument; leading ':'
505 * indicates no error should be generated
507 #ifndef PLATFORM_WINDOWS
509 warnx(recargstring, current_argv);
511 warnx(PRINT_ERROR, recargstring, current_argv);
514 * XXX: GNU sets optopt to val regardless
517 if (long_options[match].flag == NULL)
518 optopt = long_options[match].val;
524 } else { /* unknown option */
525 #ifndef PLATFORM_WINDOWS
527 warnx(illoptstring, current_argv);
529 warnx(PRINT_ERROR, illoptstring, current_argv);
534 if (long_options[match].flag) {
535 *long_options[match].flag = long_options[match].val;
538 retval = long_options[match].val;
544 __END_DECLS // Added by JE - 31-01-2010
546 #endif /* !GETOPT_LONG */