dd8cd70495e8f433c7b8a44960a24636e4082a82
4 * This file is part of the mingw-w64 runtime package.
6 * The mingw-w64 runtime package and its code is distributed in the hope that it
7 * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR
8 * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to
9 * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Sponsored in part by the Defense Advanced Research Projects
27 * Agency (DARPA) and Air Force Research Laboratory, Air Force
28 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
31 * Copyright (c) 2000 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Dieter Baron and Thomas Klausner.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
61 /* All the headers include this file. */
75 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
78 int opterr
= 1; /* if error message should be printed */
79 int optind
= 1; /* index into parent argv vector */
80 int optopt
= '?'; /* character checked for validity */
81 #undef optreset /* see getopt.h */
82 #define optreset __mingw_optreset
83 int optreset
; /* reset getopt */
84 char *optarg
; /* argument associated with option */
87 //extern int optind; /* index of first non-option in argv */
88 //extern int optopt; /* single option character, as parsed */
89 //extern int opterr; /* flag to enable built-in diagnostics... */
90 // /* (user may set to zero, to suppress) */
92 //extern char *optarg; /* pointer to argument of current option */
94 #define PRINT_ERROR ((opterr) && (*options != ':'))
96 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
97 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
98 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
101 #define BADCH (int)'?'
102 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
103 #define INORDER (int)1
106 #define __progname __argv[0]
108 extern char __declspec(dllimport
) *__progname
;
112 static char EMSG
[] = "";
117 static int getopt_internal(int, char * const *, const char *,
118 const struct option
*, int *, int);
119 static int parse_long_options(char * const *, const char *,
120 const struct option
*, int *, int);
121 static int gcd(int, int);
122 static void permute_args(int, int, int, char * const *);
124 static char *place
= EMSG
; /* option letter processing */
126 /* XXX: set optreset to 1 rather than these two */
127 static int nonopt_start
= -1; /* first non option argument (for permute) */
128 static int nonopt_end
= -1; /* first option after non options (for permute) */
131 static const char recargchar
[] = "option requires an argument -- %c";
132 static const char recargstring
[] = "option requires an argument -- %s";
133 static const char ambig
[] = "ambiguous option -- %.*s";
134 static const char noarg
[] = "option doesn't take an argument -- %.*s";
135 static const char illoptchar
[] = "unknown option -- %c";
136 static const char illoptstring
[] = "unknown option -- %s";
139 _vwarnx(const char *fmt
,va_list ap
)
141 (void)fprintf(stderr
,"%s: ",__progname
);
143 (void)vfprintf(stderr
,fmt
,ap
);
144 (void)fprintf(stderr
,"\n");
148 warnx(const char *fmt
,...)
157 * Compute the greatest common divisor of a and b.
175 * Exchange the block from nonopt_start to nonopt_end with the block
176 * from nonopt_end to opt_end (keeping the same order of arguments
180 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
183 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
187 * compute lengths of blocks and number and size of cycles
189 nnonopts
= panonopt_end
- panonopt_start
;
190 nopts
= opt_end
- panonopt_end
;
191 ncycle
= gcd(nnonopts
, nopts
);
192 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
194 for (i
= 0; i
< ncycle
; i
++) {
195 cstart
= panonopt_end
+i
;
197 for (j
= 0; j
< cyclelen
; j
++) {
198 if (pos
>= panonopt_end
)
203 /* LINTED const cast */
204 ((char **) nargv
)[pos
] = nargv
[cstart
];
205 /* LINTED const cast */
206 ((char **)nargv
)[cstart
] = swap
;
211 #ifdef REPLACE_GETOPT
214 * Parse argc/argv argument vector.
216 * [eventually this will replace the BSD getopt]
219 getopt(int nargc
, char * const *nargv
, const char *options
)
223 * We don't pass FLAG_PERMUTE to getopt_internal() since
224 * the BSD getopt(3) (unlike GNU) has never done this.
226 * Furthermore, since many privileged programs call getopt()
227 * before dropping privileges it makes sense to keep things
228 * as simple (and bug-free) as possible.
230 return (getopt_internal(nargc
, nargv
, options
, NULL
, NULL
, 0));
232 #endif /* REPLACE_GETOPT */
234 //extern int getopt(int nargc, char * const *nargv, const char *options);
238 * BSD adds the non-standard `optreset' feature, for reinitialisation
239 * of `getopt' parsing. We support this feature, for applications which
240 * proclaim their BSD heritage, before including this header; however,
241 * to maintain portability, developers are advised to avoid it.
243 # define optreset __mingw_optreset
250 * POSIX requires the `getopt' API to be specified in `unistd.h';
251 * thus, `unistd.h' includes this header. However, we do not want
252 * to expose the `getopt_long' or `getopt_long_only' APIs, when
253 * included in this manner. Thus, close the standard __GETOPT_H__
254 * declarations block, and open an additional __GETOPT_LONG_H__
255 * specific block, only when *not* __UNISTD_H_SOURCED__, in which
256 * to declare the extended API.
258 #endif /* !defined(__GETOPT_H__) */
260 #if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
261 #define __GETOPT_LONG_H__
267 struct option
/* specification for a long form option... */
269 const char *name
; /* option name, without leading hyphens */
270 int has_arg
; /* does it take an argument? */
271 int *flag
; /* where to save its status, or NULL */
272 int val
; /* its associated status value */
275 enum /* permitted values for its `has_arg' field... */
277 no_argument
= 0, /* option never takes an argument */
278 required_argument
, /* option always requires an argument */
279 optional_argument
/* option may take an argument */
283 * parse_long_options --
284 * Parse long options in argc/argv argument vector.
285 * Returns -1 if short_too is set and the option does not match long_options.
288 parse_long_options(char * const *nargv
, const char *options
,
289 const struct option
*long_options
, int *idx
, int short_too
)
291 char *current_argv
, *has_equal
;
292 size_t current_argv_len
;
293 int i
, ambiguous
, match
;
295 #define IDENTICAL_INTERPRETATION(_x, _y) \
296 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
297 long_options[(_x)].flag == long_options[(_y)].flag && \
298 long_options[(_x)].val == long_options[(_y)].val)
300 current_argv
= place
;
306 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
307 /* argument found (--option=arg) */
308 current_argv_len
= has_equal
- current_argv
;
311 current_argv_len
= strlen(current_argv
);
313 for (i
= 0; long_options
[i
].name
; i
++) {
314 /* find matching long option */
315 if (strncmp(current_argv
, long_options
[i
].name
,
319 if (strlen(long_options
[i
].name
) == current_argv_len
) {
326 * If this is a known short option, don't allow
327 * a partial match of a single character.
329 if (short_too
&& current_argv_len
== 1)
332 if (match
== -1) /* partial match */
334 else if (!IDENTICAL_INTERPRETATION(i
, match
))
338 /* ambiguous abbreviation */
340 warnx(ambig
, (int)current_argv_len
,
345 if (match
!= -1) { /* option found */
346 if (long_options
[match
].has_arg
== no_argument
349 warnx(noarg
, (int)current_argv_len
,
352 * XXX: GNU sets optopt to val regardless of flag
354 if (long_options
[match
].flag
== NULL
)
355 optopt
= long_options
[match
].val
;
360 if (long_options
[match
].has_arg
== required_argument
||
361 long_options
[match
].has_arg
== optional_argument
) {
364 else if (long_options
[match
].has_arg
==
367 * optional argument doesn't use next nargv
369 optarg
= nargv
[optind
++];
372 if ((long_options
[match
].has_arg
== required_argument
)
373 && (optarg
== NULL
)) {
375 * Missing argument; leading ':' indicates no error
376 * should be generated.
382 * XXX: GNU sets optopt to val regardless of flag
384 if (long_options
[match
].flag
== NULL
)
385 optopt
= long_options
[match
].val
;
391 } else { /* unknown option */
397 warnx(illoptstring
, current_argv
);
403 if (long_options
[match
].flag
) {
404 *long_options
[match
].flag
= long_options
[match
].val
;
407 return (long_options
[match
].val
);
408 #undef IDENTICAL_INTERPRETATION
413 * Parse argc/argv argument vector. Called by user level routines.
416 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
417 const struct option
*long_options
, int *idx
, int flags
)
419 char *oli
; /* option letter list index */
420 int optchar
, short_too
;
421 static int posixly_correct
= -1;
427 * XXX Some GNU programs (like cvs) set optind to 0 instead of
428 * XXX using optreset. Work around this braindamage.
431 optind
= optreset
= 1;
434 * Disable GNU extensions if POSIXLY_CORRECT is set or options
435 * string begins with a '+'.
437 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
438 * optreset != 0 for GNU compatibility.
440 if (posixly_correct
== -1 || optreset
!= 0)
441 posixly_correct
= (getenv("POSIXLY_CORRECT") != NULL
);
443 flags
|= FLAG_ALLARGS
;
444 else if (posixly_correct
|| *options
== '+')
445 flags
&= ~FLAG_PERMUTE
;
446 if (*options
== '+' || *options
== '-')
451 nonopt_start
= nonopt_end
= -1;
453 if (optreset
|| !*place
) { /* update scanning pointer */
455 if (optind
>= nargc
) { /* end of argument vector */
457 if (nonopt_end
!= -1) {
458 /* do permutation, if we have to */
459 permute_args(nonopt_start
, nonopt_end
,
461 optind
-= nonopt_end
- nonopt_start
;
463 else if (nonopt_start
!= -1) {
465 * If we skipped non-options, set optind
466 * to the first of them.
468 optind
= nonopt_start
;
470 nonopt_start
= nonopt_end
= -1;
473 if (*(place
= nargv
[optind
]) != '-' ||
474 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
475 place
= EMSG
; /* found non-option */
476 if (flags
& FLAG_ALLARGS
) {
479 * return non-option as argument to option 1
481 optarg
= nargv
[optind
++];
484 if (!(flags
& FLAG_PERMUTE
)) {
486 * If no permutation wanted, stop parsing
487 * at first non-option.
492 if (nonopt_start
== -1)
493 nonopt_start
= optind
;
494 else if (nonopt_end
!= -1) {
495 permute_args(nonopt_start
, nonopt_end
,
497 nonopt_start
= optind
-
498 (nonopt_end
- nonopt_start
);
502 /* process next argument */
505 if (nonopt_start
!= -1 && nonopt_end
== -1)
509 * If we have "-" do nothing, if "--" we are done.
511 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
515 * We found an option (--), so if we skipped
516 * non-options, we have to permute.
518 if (nonopt_end
!= -1) {
519 permute_args(nonopt_start
, nonopt_end
,
521 optind
-= nonopt_end
- nonopt_start
;
523 nonopt_start
= nonopt_end
= -1;
529 * Check long options if:
530 * 1) we were passed some
531 * 2) the arg is not just "-"
532 * 3) either the arg starts with -- we are getopt_long_only()
534 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
535 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
538 place
++; /* --foo long option */
539 else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
540 short_too
= 1; /* could be short option too */
542 optchar
= parse_long_options(nargv
, options
, long_options
,
550 if ((optchar
= (int)*place
++) == (int)':' ||
551 (optchar
== (int)'-' && *place
!= '\0') ||
552 (oli
= (char*)strchr(options
, optchar
)) == NULL
) {
554 * If the user specified "-" and '-' isn't listed in
555 * options, return -1 (non-option) as per POSIX.
556 * Otherwise, it is an unknown option character (or ':').
558 if (optchar
== (int)'-' && *place
== '\0')
563 warnx(illoptchar
, optchar
);
567 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
569 if (*place
) /* no space */
571 else if (++optind
>= nargc
) { /* no arg */
574 warnx(recargchar
, optchar
);
577 } else /* white space */
578 place
= nargv
[optind
];
579 optchar
= parse_long_options(nargv
, options
, long_options
,
584 if (*++oli
!= ':') { /* doesn't take argument */
587 } else { /* takes (optional) argument */
589 if (*place
) /* no white space */
591 else if (oli
[1] != ':') { /* arg not optional */
592 if (++optind
>= nargc
) { /* no arg */
595 warnx(recargchar
, optchar
);
599 optarg
= nargv
[optind
];
604 /* dump back option letter */
610 * Parse argc/argv argument vector.
613 getopt_long(int nargc
, char * const *nargv
, const char *options
,
614 const struct option
*long_options
, int *idx
)
617 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
622 * getopt_long_only --
623 * Parse argc/argv argument vector.
626 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
627 const struct option
*long_options
, int *idx
)
630 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
631 FLAG_PERMUTE
|FLAG_LONGONLY
));
634 //extern int getopt_long(int nargc, char * const *nargv, const char *options,
635 // const struct option *long_options, int *idx);
636 //extern int getopt_long_only(int nargc, char * const *nargv, const char *options,
637 // const struct option *long_options, int *idx);
639 * Previous MinGW implementation had...
641 #ifndef HAVE_DECL_GETOPT
643 * ...for the long form API only; keep this for compatibility.
645 # define HAVE_DECL_GETOPT 1
652 #endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */