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 #ifndef WIN32_LEAN_AND_MEAN
62 # define WIN32_LEAN_AND_MEAN
82 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
85 int opterr
= 1; /* if error message should be printed */
86 int optind
= 1; /* index into parent argv vector */
87 int optopt
= '?'; /* character checked for validity */
88 #undef optreset /* see getopt.h */
89 #define optreset __mingw_optreset
90 int optreset
; /* reset getopt */
91 char *optarg
; /* argument associated with option */
94 //extern int optind; /* index of first non-option in argv */
95 //extern int optopt; /* single option character, as parsed */
96 //extern int opterr; /* flag to enable built-in diagnostics... */
97 // /* (user may set to zero, to suppress) */
99 //extern char *optarg; /* pointer to argument of current option */
101 #define PRINT_ERROR ((opterr) && (*options != ':'))
103 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
104 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
105 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
108 #define BADCH (int)'?'
109 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
110 #define INORDER (int)1
113 #define __progname __argv[0]
115 extern char __declspec(dllimport
) *__progname
;
119 static char EMSG
[] = "";
124 static int getopt_internal(int, char * const *, const char *,
125 const struct option
*, int *, int);
126 static int parse_long_options(char * const *, const char *,
127 const struct option
*, int *, int);
128 static int gcd(int, int);
129 static void permute_args(int, int, int, char * const *);
131 static char *place
= EMSG
; /* option letter processing */
133 /* XXX: set optreset to 1 rather than these two */
134 static int nonopt_start
= -1; /* first non option argument (for permute) */
135 static int nonopt_end
= -1; /* first option after non options (for permute) */
138 static const char recargchar
[] = "option requires an argument -- %c";
139 static const char recargstring
[] = "option requires an argument -- %s";
140 static const char ambig
[] = "ambiguous option -- %.*s";
141 static const char noarg
[] = "option doesn't take an argument -- %.*s";
142 static const char illoptchar
[] = "unknown option -- %c";
143 static const char illoptstring
[] = "unknown option -- %s";
146 _vwarnx(const char *fmt
,va_list ap
)
148 (void)fprintf(stderr
,"%s: ",__progname
);
150 (void)vfprintf(stderr
,fmt
,ap
);
151 (void)fprintf(stderr
,"\n");
155 warnx(const char *fmt
,...)
164 * Compute the greatest common divisor of a and b.
182 * Exchange the block from nonopt_start to nonopt_end with the block
183 * from nonopt_end to opt_end (keeping the same order of arguments
187 permute_args(int panonopt_start
, int panonopt_end
, int opt_end
,
190 int cstart
, cyclelen
, i
, j
, ncycle
, nnonopts
, nopts
, pos
;
194 * compute lengths of blocks and number and size of cycles
196 nnonopts
= panonopt_end
- panonopt_start
;
197 nopts
= opt_end
- panonopt_end
;
198 ncycle
= gcd(nnonopts
, nopts
);
199 cyclelen
= (opt_end
- panonopt_start
) / ncycle
;
201 for (i
= 0; i
< ncycle
; i
++) {
202 cstart
= panonopt_end
+i
;
204 for (j
= 0; j
< cyclelen
; j
++) {
205 if (pos
>= panonopt_end
)
210 /* LINTED const cast */
211 ((char **) nargv
)[pos
] = nargv
[cstart
];
212 /* LINTED const cast */
213 ((char **)nargv
)[cstart
] = swap
;
218 #ifdef REPLACE_GETOPT
221 * Parse argc/argv argument vector.
223 * [eventually this will replace the BSD getopt]
226 getopt(int nargc
, char * const *nargv
, const char *options
)
230 * We don't pass FLAG_PERMUTE to getopt_internal() since
231 * the BSD getopt(3) (unlike GNU) has never done this.
233 * Furthermore, since many privileged programs call getopt()
234 * before dropping privileges it makes sense to keep things
235 * as simple (and bug-free) as possible.
237 return (getopt_internal(nargc
, nargv
, options
, NULL
, NULL
, 0));
239 #endif /* REPLACE_GETOPT */
241 //extern int getopt(int nargc, char * const *nargv, const char *options);
245 * BSD adds the non-standard `optreset' feature, for reinitialisation
246 * of `getopt' parsing. We support this feature, for applications which
247 * proclaim their BSD heritage, before including this header; however,
248 * to maintain portability, developers are advised to avoid it.
250 # define optreset __mingw_optreset
257 * POSIX requires the `getopt' API to be specified in `unistd.h';
258 * thus, `unistd.h' includes this header. However, we do not want
259 * to expose the `getopt_long' or `getopt_long_only' APIs, when
260 * included in this manner. Thus, close the standard __GETOPT_H__
261 * declarations block, and open an additional __GETOPT_LONG_H__
262 * specific block, only when *not* __UNISTD_H_SOURCED__, in which
263 * to declare the extended API.
265 #endif /* !defined(__GETOPT_H__) */
267 #if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
268 #define __GETOPT_LONG_H__
274 struct option
/* specification for a long form option... */
276 const char *name
; /* option name, without leading hyphens */
277 int has_arg
; /* does it take an argument? */
278 int *flag
; /* where to save its status, or NULL */
279 int val
; /* its associated status value */
282 enum /* permitted values for its `has_arg' field... */
284 no_argument
= 0, /* option never takes an argument */
285 required_argument
, /* option always requires an argument */
286 optional_argument
/* option may take an argument */
290 * parse_long_options --
291 * Parse long options in argc/argv argument vector.
292 * Returns -1 if short_too is set and the option does not match long_options.
295 parse_long_options(char * const *nargv
, const char *options
,
296 const struct option
*long_options
, int *idx
, int short_too
)
298 char *current_argv
, *has_equal
;
299 size_t current_argv_len
;
300 int i
, ambiguous
, match
;
302 #define IDENTICAL_INTERPRETATION(_x, _y) \
303 (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \
304 long_options[(_x)].flag == long_options[(_y)].flag && \
305 long_options[(_x)].val == long_options[(_y)].val)
307 current_argv
= place
;
313 if ((has_equal
= strchr(current_argv
, '=')) != NULL
) {
314 /* argument found (--option=arg) */
315 current_argv_len
= has_equal
- current_argv
;
318 current_argv_len
= strlen(current_argv
);
320 for (i
= 0; long_options
[i
].name
; i
++) {
321 /* find matching long option */
322 if (strncmp(current_argv
, long_options
[i
].name
,
326 if (strlen(long_options
[i
].name
) == current_argv_len
) {
333 * If this is a known short option, don't allow
334 * a partial match of a single character.
336 if (short_too
&& current_argv_len
== 1)
339 if (match
== -1) /* partial match */
341 else if (!IDENTICAL_INTERPRETATION(i
, match
))
345 /* ambiguous abbreviation */
347 warnx(ambig
, (int)current_argv_len
,
352 if (match
!= -1) { /* option found */
353 if (long_options
[match
].has_arg
== no_argument
356 warnx(noarg
, (int)current_argv_len
,
359 * XXX: GNU sets optopt to val regardless of flag
361 if (long_options
[match
].flag
== NULL
)
362 optopt
= long_options
[match
].val
;
367 if (long_options
[match
].has_arg
== required_argument
||
368 long_options
[match
].has_arg
== optional_argument
) {
371 else if (long_options
[match
].has_arg
==
374 * optional argument doesn't use next nargv
376 optarg
= nargv
[optind
++];
379 if ((long_options
[match
].has_arg
== required_argument
)
380 && (optarg
== NULL
)) {
382 * Missing argument; leading ':' indicates no error
383 * should be generated.
389 * XXX: GNU sets optopt to val regardless of flag
391 if (long_options
[match
].flag
== NULL
)
392 optopt
= long_options
[match
].val
;
398 } else { /* unknown option */
404 warnx(illoptstring
, current_argv
);
410 if (long_options
[match
].flag
) {
411 *long_options
[match
].flag
= long_options
[match
].val
;
414 return (long_options
[match
].val
);
415 #undef IDENTICAL_INTERPRETATION
420 * Parse argc/argv argument vector. Called by user level routines.
423 getopt_internal(int nargc
, char * const *nargv
, const char *options
,
424 const struct option
*long_options
, int *idx
, int flags
)
426 char *oli
; /* option letter list index */
427 int optchar
, short_too
;
428 static int posixly_correct
= -1;
434 * XXX Some GNU programs (like cvs) set optind to 0 instead of
435 * XXX using optreset. Work around this braindamage.
438 optind
= optreset
= 1;
441 * Disable GNU extensions if POSIXLY_CORRECT is set or options
442 * string begins with a '+'.
444 * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or
445 * optreset != 0 for GNU compatibility.
447 if (posixly_correct
== -1 || optreset
!= 0)
448 posixly_correct
= (getenv("POSIXLY_CORRECT") != NULL
);
450 flags
|= FLAG_ALLARGS
;
451 else if (posixly_correct
|| *options
== '+')
452 flags
&= ~FLAG_PERMUTE
;
453 if (*options
== '+' || *options
== '-')
458 nonopt_start
= nonopt_end
= -1;
460 if (optreset
|| !*place
) { /* update scanning pointer */
462 if (optind
>= nargc
) { /* end of argument vector */
464 if (nonopt_end
!= -1) {
465 /* do permutation, if we have to */
466 permute_args(nonopt_start
, nonopt_end
,
468 optind
-= nonopt_end
- nonopt_start
;
470 else if (nonopt_start
!= -1) {
472 * If we skipped non-options, set optind
473 * to the first of them.
475 optind
= nonopt_start
;
477 nonopt_start
= nonopt_end
= -1;
480 if (*(place
= nargv
[optind
]) != '-' ||
481 (place
[1] == '\0' && strchr(options
, '-') == NULL
)) {
482 place
= EMSG
; /* found non-option */
483 if (flags
& FLAG_ALLARGS
) {
486 * return non-option as argument to option 1
488 optarg
= nargv
[optind
++];
491 if (!(flags
& FLAG_PERMUTE
)) {
493 * If no permutation wanted, stop parsing
494 * at first non-option.
499 if (nonopt_start
== -1)
500 nonopt_start
= optind
;
501 else if (nonopt_end
!= -1) {
502 permute_args(nonopt_start
, nonopt_end
,
504 nonopt_start
= optind
-
505 (nonopt_end
- nonopt_start
);
509 /* process next argument */
512 if (nonopt_start
!= -1 && nonopt_end
== -1)
516 * If we have "-" do nothing, if "--" we are done.
518 if (place
[1] != '\0' && *++place
== '-' && place
[1] == '\0') {
522 * We found an option (--), so if we skipped
523 * non-options, we have to permute.
525 if (nonopt_end
!= -1) {
526 permute_args(nonopt_start
, nonopt_end
,
528 optind
-= nonopt_end
- nonopt_start
;
530 nonopt_start
= nonopt_end
= -1;
536 * Check long options if:
537 * 1) we were passed some
538 * 2) the arg is not just "-"
539 * 3) either the arg starts with -- we are getopt_long_only()
541 if (long_options
!= NULL
&& place
!= nargv
[optind
] &&
542 (*place
== '-' || (flags
& FLAG_LONGONLY
))) {
545 place
++; /* --foo long option */
546 else if (*place
!= ':' && strchr(options
, *place
) != NULL
)
547 short_too
= 1; /* could be short option too */
549 optchar
= parse_long_options(nargv
, options
, long_options
,
557 if ((optchar
= (int)*place
++) == (int)':' ||
558 (optchar
== (int)'-' && *place
!= '\0') ||
559 (oli
= (char*)strchr(options
, optchar
)) == NULL
) {
561 * If the user specified "-" and '-' isn't listed in
562 * options, return -1 (non-option) as per POSIX.
563 * Otherwise, it is an unknown option character (or ':').
565 if (optchar
== (int)'-' && *place
== '\0')
570 warnx(illoptchar
, optchar
);
574 if (long_options
!= NULL
&& optchar
== 'W' && oli
[1] == ';') {
576 if (*place
) /* no space */
578 else if (++optind
>= nargc
) { /* no arg */
581 warnx(recargchar
, optchar
);
584 } else /* white space */
585 place
= nargv
[optind
];
586 optchar
= parse_long_options(nargv
, options
, long_options
,
591 if (*++oli
!= ':') { /* doesn't take argument */
594 } else { /* takes (optional) argument */
596 if (*place
) /* no white space */
598 else if (oli
[1] != ':') { /* arg not optional */
599 if (++optind
>= nargc
) { /* no arg */
602 warnx(recargchar
, optchar
);
606 optarg
= nargv
[optind
];
611 /* dump back option letter */
617 * Parse argc/argv argument vector.
620 getopt_long(int nargc
, char * const *nargv
, const char *options
,
621 const struct option
*long_options
, int *idx
)
624 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
629 * getopt_long_only --
630 * Parse argc/argv argument vector.
633 getopt_long_only(int nargc
, char * const *nargv
, const char *options
,
634 const struct option
*long_options
, int *idx
)
637 return (getopt_internal(nargc
, nargv
, options
, long_options
, idx
,
638 FLAG_PERMUTE
|FLAG_LONGONLY
));
641 //extern int getopt_long(int nargc, char * const *nargv, const char *options,
642 // const struct option *long_options, int *idx);
643 //extern int getopt_long_only(int nargc, char * const *nargv, const char *options,
644 // const struct option *long_options, int *idx);
646 * Previous MinGW implementation had...
648 #ifndef HAVE_DECL_GETOPT
650 * ...for the long form API only; keep this for compatibility.
652 # define HAVE_DECL_GETOPT 1
659 #endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */