6 * Implementation of the `getopt', `getopt_long' and `getopt_long_only'
7 * APIs, for inclusion in the MinGW runtime library.
9 * This file is part of the MinGW32 package set.
11 * Contributed by Keith Marshall <keithmarshall@users.sourceforge.net>
14 * THIS SOFTWARE IS NOT COPYRIGHTED
16 * This source code is offered for use in the public domain. You may
17 * use, modify or distribute it freely.
19 * This code is distributed in the hope that it will be useful but
20 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
21 * DISCLAIMED. This includes but is not limited to warranties of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 /* Identify how to get the calling program name, for use in messages...
39 * CYGWIN uses this DLL reference...
41 # define PROGNAME __progname
42 extern char __declspec(dllimport) *__progname;
45 * ...while elsewhere, we simply use the first argument passed.
47 # define PROGNAME *argv
50 /* Initialise the public variables. */
52 int optind = 1; /* index for first non-option arg */
53 int opterr = 1; /* enable built-in error messages */
55 char *optarg = NULL; /* pointer to current option argument */
57 #define CHAR char /* argument type selector */
59 #define getopt_switchar '-' /* option prefix character in argv */
60 #define getopt_pluschar '+' /* prefix for POSIX mode in optstring */
61 #define getopt_takes_argument ':' /* marker for optarg in optstring */
62 #define getopt_arg_assign '=' /* longopt argument field separator */
63 #define getopt_unknown '?' /* return code for unmatched option */
64 #define getopt_ordered 1 /* return code for ordered non-option */
66 #define getopt_all_done -1 /* return code to indicate completion */
69 { /* All `getopt' API functions are implemented via calls to the
70 * common static function `getopt_parse()'; these `mode' selectors
71 * determine the behaviour of `getopt_parse()', to deliver the
72 * appropriate result in each case.
74 getopt_mode_standard = 0, /* getopt() */
75 getopt_mode_long, /* getopt_long() */
76 getopt_mode_long_only /* getopt_long_only() */
80 { /* When attempting to match a command line argument to a long form option,
81 * these indicate the status of the match.
83 getopt_no_match = 0, /* no successful match */
84 getopt_abbreviated_match, /* argument is an abbreviation for an option */
85 getopt_exact_match /* argument matches the full option name */
88 int optopt = getopt_unknown; /* return value for option being evaluated */
90 /* Some BSD applications expect to be able to reinitialise `getopt' parsing
91 * by setting a global variable called `optreset'. We provide an obfuscated
92 * API, which allows applications to emulate this brain damage; however, any
93 * use of this is non-portable, and is strongly discouraged.
95 #define optreset __mingw_optreset
99 int getopt_missing_arg( const CHAR *optstring )
101 /* Helper function to determine the appropriate return value,
102 * for the case where a required option argument is missing.
104 if( (*optstring == getopt_pluschar) || (*optstring == getopt_switchar) )
106 return (*optstring == getopt_takes_argument)
107 ? getopt_takes_argument
111 /* `complain' macro facilitates the generation of simple built-in
112 * error messages, displayed on various fault conditions, provided
113 * `opterr' is non-zero.
115 #define complain( MSG, ARG ) if( opterr ) \
116 fprintf( stderr, "%s: "MSG"\n", PROGNAME, ARG )
119 int getopt_argerror( int mode, char *fmt, CHAR *prog, struct option *opt, int retval )
121 /* Helper function, to generate more complex built-in error
122 * messages, for invalid arguments to long form options ...
126 /* ... but, displayed only if `opterr' is non-zero.
129 if( mode != getopt_mode_long )
131 * only display one hyphen, for implicit long form options,
132 * improperly resolved by `getopt_long_only()'.
136 * always preface the program name ...
138 fprintf( stderr, "%s: ", prog );
140 * to the appropriate, option specific message.
142 fprintf( stderr, fmt, flag, opt->name );
144 /* Whether displaying the message, or not, always set `optopt'
145 * to identify the faulty option ...
149 * and return the `invalid option' indicator.
154 /* `getopt_conventions' establish behavioural options, to control
155 * the operation of `getopt_parse()', e.g. to select between POSIX
156 * and GNU style argument parsing behaviour.
158 #define getopt_set_conventions 0x1000
159 #define getopt_posixly_correct 0x0010
162 int getopt_conventions( int flags )
164 static int conventions = 0;
166 if( (conventions == 0) && ((flags & getopt_set_conventions) == 0) )
168 /* default conventions have not yet been established;
169 * initialise them now!
171 conventions = getopt_set_conventions;
172 if( (flags == getopt_pluschar) || (getenv( "POSIXLY_CORRECT" ) != NULL) )
173 conventions |= getopt_posixly_correct;
176 else if( flags & getopt_set_conventions )
178 * default conventions may have already been established,
179 * but this is a specific request to augment them.
181 conventions |= flags;
183 /* in any event, return the currently established conventions.
189 int is_switchar( CHAR flag )
191 /* A simple helper function, used to identify the switch character
192 * introducing an optional command line argument.
194 return flag == getopt_switchar;
198 const CHAR *getopt_match( CHAR lookup, const CHAR *opt_string )
200 /* Helper function, used to identify short form options.
202 if( (*opt_string == getopt_pluschar) || (*opt_string == getopt_switchar) )
204 if( *opt_string == getopt_takes_argument )
206 do if( lookup == *opt_string ) return opt_string;
207 while( *++opt_string );
212 int getopt_match_long( const CHAR *nextchar, const CHAR *optname )
214 /* Helper function, used to identify potential matches for
218 while( (matchchar = *nextchar++) && (matchchar == *optname) )
220 * skip over initial substring which DOES match.
226 /* did NOT match the entire argument to an initial substring
227 * of a defined option name ...
229 if( matchchar != getopt_arg_assign )
231 * ... and didn't stop at an `=' internal field separator,
232 * so this is NOT a possible match.
234 return getopt_no_match;
236 /* DID stop at an `=' internal field separator,
237 * so this IS a possible match, and what follows is an
238 * argument to the possibly matched option.
240 optarg = (char *)(nextchar);
244 * if we DIDN'T match the ENTIRE text of the option name,
245 * then it's a possible abbreviated match ...
247 ? getopt_abbreviated_match
249 * but if we DID match the entire option name,
250 * then it's a DEFINITE EXACT match.
252 : getopt_exact_match;
256 int getopt_resolved( int mode, int argc, CHAR *const *argv, int *argind,
257 struct option *opt, int index, int *retindex, const CHAR *optstring )
259 /* Helper function to establish appropriate return conditions,
260 * on resolution of a long form option.
262 if( retindex != NULL )
265 /* On return, `optind' should normally refer to the argument, if any,
266 * which follows the current one; it is convenient to set this, before
267 * checking for the presence of any `optarg'.
269 optind = *argind + 1;
271 if( optarg && (opt[index].has_arg == no_argument) )
273 * it is an error for the user to specify an option specific argument
274 * with an option which doesn't expect one!
276 return getopt_argerror( mode, "option `%s%s' doesn't accept an argument\n",
277 PROGNAME, opt + index, getopt_unknown );
279 else if( (optarg == NULL) && (opt[index].has_arg == required_argument) )
281 /* similarly, it is an error if no argument is specified
282 * with an option which requires one ...
286 * ... except that the requirement may be satisfied from
287 * the following command line argument, if any ...
289 optarg = argv[*argind = optind++];
292 /* so fail this case, only if no such argument exists!
294 return getopt_argerror( mode, "option `%s%s' requires an argument\n",
295 PROGNAME, opt + index, getopt_missing_arg( optstring ) );
298 /* when the caller has provided a return buffer ...
300 if( opt[index].flag != NULL )
302 /* ... then we place the proper return value there,
303 * and return a status code of zero ...
305 *(opt[index].flag) = opt[index].val;
308 /* ... otherwise, the return value becomes the status code.
310 return opt[index].val;
314 int getopt_verify( const CHAR *nextchar, const CHAR *optstring )
316 /* Helper function, called by getopt_parse() when invoked
317 * by getopt_long_only(), to verify when an unmatched or an
318 * ambiguously matched long form option string is valid as
319 * a short form option specification.
321 if( ! (nextchar && *nextchar && optstring && *optstring) )
323 * There are no characters to be matched, or there are no
324 * valid short form option characters to which they can be
325 * matched, so this can never be valid.
331 /* For each command line character in turn ...
334 if( (test = getopt_match( *nextchar++, optstring )) == NULL )
336 * ... there is no short form option to match the current
337 * candidate, so the entire argument fails.
341 if( test[1] == getopt_takes_argument )
343 * The current candidate is valid, and it matches an option
344 * which takes an argument, so this command line argument is
345 * a valid short form option specification; accept it.
349 /* If we get to here, then every character in the command line
350 * argument was valid as a short form option; accept it.
356 #define getopt_std_args int argc, CHAR *const argv[], const CHAR *optstring
357 int getopt_parse( int mode, getopt_std_args, ... )
359 /* Common core implementation for ALL `getopt' functions.
361 static int argind = 0;
362 static int optbase = 0;
363 static const CHAR *nextchar = NULL;
364 static int optmark = 0;
366 if( (optreset |= (optind < 1)) || (optind < optbase) )
368 /* POSIX does not prescribe any definitive mechanism for restarting
369 * a `getopt' scan, but some applications may require such capability.
370 * We will support it, by allowing the caller to adjust the value of
371 * `optind' downwards, (nominally setting it to zero). Since POSIX
372 * wants `optind' to have an initial value of one, but we want all
373 * of our internal place holders to be initialised to zero, when we
374 * are called for the first time, we will handle such a reset by
375 * adjusting all of the internal place holders to one less than
376 * the adjusted `optind' value, (but never to less than zero).
380 /* User has explicitly requested reinitialisation...
381 * We need to reset `optind' to it's normal initial value of 1,
382 * to avoid a potential infinitely recursive loop; by doing this
383 * up front, we also ensure that the remaining place holders
384 * will be correctly reinitialised to no less than zero.
388 /* We also need to clear the `optreset' request...
393 /* Now, we may safely reinitialise the internal place holders, to
394 * one less than `optind', without fear of making them negative.
396 optmark = optbase = argind = optind - 1;
400 /* From a POSIX perspective, the following is `undefined behaviour';
401 * we implement it thus, for compatibility with GNU and BSD getopt.
403 else if( optind > (argind + 1) )
405 /* Some applications expect to be able to manipulate `optind',
406 * causing `getopt' to skip over one or more elements of `argv';
407 * POSIX doesn't require us to support this brain-damaged concept;
408 * (indeed, POSIX defines no particular behaviour, in the event of
409 * such usage, so it must be considered a bug for an application
410 * to rely on any particular outcome); nonetheless, Mac-OS-X and
411 * BSD actually provide *documented* support for this capability,
412 * so we ensure that our internal place holders keep track of
413 * external `optind' increments; (`argind' must lag by one).
417 /* When `optind' is misused, in this fashion, we also abandon any
418 * residual text in the argument we had been parsing; this is done
419 * without any further processing of such abandoned text, assuming
420 * that the caller is equipped to handle it appropriately.
425 if( nextchar && *nextchar )
427 /* we are parsing a standard, or short format, option argument ...
430 if( (optchar = getopt_match( optopt = *nextchar++, optstring )) != NULL )
432 /* we have identified it as valid ...
434 if( optchar[1] == getopt_takes_argument )
436 /* and determined that it requires an associated argument ...
438 if( ! *(optarg = (char *)(nextchar)) )
440 /* the argument is NOT attached ...
442 if( optchar[2] == getopt_takes_argument )
444 * but this GNU extension marks it as optional,
445 * so we don't provide one on this occasion.
449 /* otherwise this option takes a mandatory argument,
450 * so, provided there is one available ...
452 else if( (argc - argind) > 1 )
454 * we take the following command line argument,
455 * as the appropriate option argument.
457 optarg = argv[++argind];
459 /* but if no further argument is available,
460 * then there is nothing we can do, except for
461 * issuing the requisite diagnostic message.
465 complain( "option requires an argument -- %c", optopt );
466 return getopt_missing_arg( optstring );
474 optind = (nextchar && *nextchar) ? argind : argind + 1;
477 /* if we didn't find a valid match for the specified option character,
478 * then we fall through to here, so take appropriate diagnostic action.
480 if( mode == getopt_mode_long_only )
482 complain( "unrecognised option `-%s'", --nextchar );
487 complain( "invalid option -- %c", optopt );
488 optind = (nextchar && *nextchar) ? argind : argind + 1;
489 return getopt_unknown;
492 if( optmark > optbase )
494 /* This can happen, in GNU parsing mode ONLY, when we have
495 * skipped over non-option arguments, and found a subsequent
496 * option argument; in this case we permute the arguments.
500 * `optspan' specifies the number of contiguous arguments
501 * which are spanned by the current option, and so must be
502 * moved together during permutation.
504 int optspan = argind - optmark + 1;
506 * we use `this_arg' to store these temporarily.
508 CHAR *this_arg[optspan];
510 * we cannot manipulate `argv' directly, since the `getopt'
511 * API prototypes it as `read-only'; this cast to `arglist'
512 * allows us to work around that restriction.
514 CHAR **arglist = (char **)(argv);
516 /* save temporary copies of the arguments which are associated
517 * with the current option ...
519 for( index = 0; index < optspan; ++index )
520 this_arg[index] = arglist[optmark + index];
522 /* move all preceding non-option arguments to the right,
523 * overwriting these saved arguments, while making space
524 * to replace them in their permuted location.
526 for( --optmark; optmark >= optbase; --optmark )
527 arglist[optmark + optspan] = arglist[optmark];
529 /* restore the temporarily saved option arguments to
530 * their permuted location.
532 for( index = 0; index < optspan; ++index )
533 arglist[optbase + index] = this_arg[index];
535 /* adjust `optbase', to account for the relocated option.
541 /* no permutation occurred ...
542 * simply adjust `optbase' for all options parsed so far.
544 optbase = argind + 1;
546 /* enter main parsing loop ...
548 while( argc > ++argind )
550 /* inspect each argument in turn, identifying possible options ...
552 if( is_switchar( *(nextchar = argv[optmark = argind]) ) && *++nextchar )
554 /* we've found a candidate option argument ... */
556 if( is_switchar( *nextchar ) )
558 /* it's a double hyphen argument ... */
560 const CHAR *refchar = nextchar;
563 /* and it looks like a long format option ...
564 * `getopt_long' mode must be active to accept it as such,
565 * `getopt_long_only' also qualifies, but we must downgrade
566 * it to force explicit handling as a long format option.
568 if( mode >= getopt_mode_long )
571 mode = getopt_mode_long;
576 /* this is an explicit `--' end of options marker, so wrap up now!
578 if( optmark > optbase )
580 /* permuting the argument list as necessary ...
581 * (note use of `this_arg' and `arglist', as above).
583 CHAR *this_arg = argv[optmark];
584 CHAR **arglist = (CHAR **)(argv);
586 /* move all preceding non-option arguments to the right ...
588 do arglist[optmark] = arglist[optmark - 1];
589 while( optmark-- > optbase );
591 /* reinstate the `--' marker, in its permuted location.
593 arglist[optbase] = this_arg;
595 /* ... before finally bumping `optbase' past the `--' marker,
596 * and returning the `all done' completion indicator.
599 return getopt_all_done;
602 else if( mode < getopt_mode_long_only )
604 /* it's not an explicit long option, and `getopt_long_only' isn't active,
605 * so we must explicitly try to match it as a short option.
607 mode = getopt_mode_standard;
610 if( mode >= getopt_mode_long )
612 /* the current argument is a long form option, (either explicitly,
613 * introduced by a double hyphen, or implicitly because we were called
614 * by `getopt_long_only'); this is where we parse it.
619 /* we need to fetch the `extra' function arguments, which are
620 * specified for the `getopt_long' APIs.
623 va_start( refptr, optstring );
624 struct option *longopts = va_arg( refptr, struct option * );
625 int *optindex = va_arg( refptr, int * );
628 /* ensuring that `optarg' does not inherit any junk, from parsing
629 * preceding arguments ...
632 for( lookup = 0; longopts && longopts[lookup].name; ++lookup )
634 /* scan the list of defined long form options ...
636 switch( getopt_match_long( nextchar, longopts[lookup].name ) )
638 /* looking for possible matches for the current argument.
640 case getopt_exact_match:
642 * when an exact match is found,
643 * return it immediately, setting `nextchar' to NULL,
644 * to ensure we don't mistakenly try to match any
645 * subsequent characters as short form options.
648 return getopt_resolved( mode, argc, argv, &argind,
649 longopts, lookup, optindex, optstring );
651 case getopt_abbreviated_match:
653 * but, for a partial (initial substring) match ...
657 /* if this is not the first, then we have an ambiguity ...
659 if( (mode == getopt_mode_long_only)
661 * However, in the case of getopt_long_only(), if
662 * the entire ambiguously matched string represents
663 * a valid short option specification, then we may
664 * proceed to interpret it as such.
666 && getopt_verify( nextchar, optstring ) )
667 return getopt_parse( mode, argc, argv, optstring );
669 /* If we get to here, then the ambiguously matched
670 * partial long option isn't valid for short option
671 * evaluation; reset parser context to resume with
672 * the following command line argument, diagnose
673 * ambiguity, and bail out.
678 complain( "option `%s' is ambiguous", argv[argind] );
679 return getopt_unknown;
681 /* otherwise just note that we've found a possible match ...
688 /* if we get to here, then we found exactly one partial match,
689 * so return it, as for an exact match.
692 return getopt_resolved( mode, argc, argv, &argind,
693 longopts, matched, optindex, optstring );
695 /* if here, then we had what SHOULD have been a long form option,
696 * but it is unmatched ...
698 if( (mode < getopt_mode_long_only)
700 * ... although paradoxically, `mode == getopt_mode_long_only'
701 * allows us to still try to match it as a short form option.
703 || (getopt_verify( nextchar, optstring ) == 0) )
705 /* When it cannot be matched, reset the parsing context to
706 * resume from the next argument, diagnose the failed match,
712 complain( "unrecognised option `%s'", argv[argind] );
713 return getopt_unknown;
716 /* fall through to handle standard short form options...
717 * when the option argument format is neither explictly identified
718 * as long, nor implicitly matched as such, and the argument isn't
719 * just a bare hyphen, (which isn't an option), then we make one
720 * recursive call to explicitly interpret it as short format.
723 return getopt_parse( mode, argc, argv, optstring );
725 /* if we get to here, then we've parsed a non-option argument ...
726 * in GNU compatibility mode, we step over it, so we can permute
727 * any subsequent option arguments, but ...
729 if( *optstring == getopt_switchar )
731 /* if `optstring' begins with a `-' character, this special
732 * GNU specific behaviour requires us to return the non-option
733 * arguments in strict order, as pseudo-arguments to a special
734 * option, with return value defined as `getopt_ordered'.
738 optarg = argv[argind];
739 return getopt_ordered;
741 if( getopt_conventions( *optstring ) & getopt_posixly_correct )
744 * for POSIXLY_CORRECT behaviour, or if `optstring' begins with
745 * a `+' character, then we break out of the parsing loop, so that
746 * the scan ends at the current argument, with no permutation.
750 /* fall through when all arguments have been evaluated,
753 return getopt_all_done;
756 /* All three public API entry points are trivially defined,
757 * in terms of the internal `getopt_parse' function.
759 int getopt( getopt_std_args )
761 return getopt_parse( getopt_mode_standard, argc, argv, optstring );
764 int getopt_long( getopt_std_args, const struct option *opts, int *index )
766 return getopt_parse( getopt_mode_long, argc, argv, optstring, opts, index );
769 int getopt_long_only( getopt_std_args, const struct option *opts, int *index )
771 return getopt_parse( getopt_mode_long_only, argc, argv, optstring, opts, index );
776 * These Microsnot style uglified aliases are provided for compatibility
777 * with the previous MinGW implementation of the getopt API.
779 __weak_alias( getopt, _getopt )
780 __weak_alias( getopt_long, _getopt_long )
781 __weak_alias( getopt_long_only, _getopt_long_only )
784 /* $RCSfile$Revision: 1.9 $: end of file */