getopt.c revision 2
2N/A NOTE: getopt is part of the C library, so if you don't know what 2N/A "Keep this file name-space clean" means, talk to drepper@gnu.org 2N/A Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2010 Free Software 2N/A This file is part of the GNU C Library. 2N/A This program is free software: you can redistribute it and/or modify 2N/A it under the terms of the GNU General Public License as published by 2N/A the Free Software Foundation; either version 3 of the License, or 2N/A (at your option) any later version. 2N/A This program is distributed in the hope that it will be useful, 2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of 2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2N/A GNU General Public License for more details. 2N/A You should have received a copy of the GNU General Public License 2N/A/* This version of `getopt' appears to the caller like standard Unix `getopt' 2N/A but it behaves differently for the user, since it allows the user 2N/A to intersperse the options with the other arguments. 2N/A As `getopt_long' works, it permutes the elements of ARGV so that, 2N/A when it is done, all the options precede everything else. Thus 2N/A all application programs are extended to handle flexible argument order. 2N/A Using `getopt' or setting the environment variable POSIXLY_CORRECT 2N/A disables permutation. 2N/A Then the behavior is completely standard. 2N/A GNU application programs can use a third alternative mode in which 2N/A they can distinguish the relative order of options and other arguments. */ 2N/A/* For communication from `getopt' to the caller. 2N/A When `getopt' finds an option that takes an argument, 2N/A the argument value is returned here. 2N/A Also, when `ordering' is RETURN_IN_ORDER, 2N/A each non-option ARGV-element is returned here. */ 2N/A/* Index in ARGV of the next element to be scanned. 2N/A This is used for communication to and from the caller 2N/A and for communication between successive calls to `getopt'. 2N/A On entry to `getopt', zero means this is the first call; initialize. 2N/A When `getopt' returns -1, this is the index of the first of the 2N/A non-option elements that the caller should itself scan. 2N/A Otherwise, `optind' communicates from one call to the next 2N/A how much of ARGV has been scanned so far. */ 2N/A/* 1003.2 says this must be 1 before any call. */ 2N/A/* Callers store zero here to inhibit the error message 2N/A for unrecognized options. */ 2N/A/* Set to an option character which was unrecognized. 2N/A This must be initialized on some systems to avoid linking in the 2N/A system's own getopt implementation. */ 2N/A/* Keep a global copy of all internal members of getopt_data. */ 2N/A/* Stored original parameters. 2N/A XXX This is no good solution. We should rather copy the args so 2N/A that we can compare them later. But we must not use malloc(3). */ 2N/A/* Bash 2.0 gives us an environment variable containing flags 2N/A indicating ARGV elements that should not be considered arguments. */ 2N/A/* Exchange two adjacent subsequences of ARGV. 2N/A One subsequence is elements [first_nonopt,last_nonopt) 2N/A which contains all the non-options that have been skipped so far. 2N/A The other is elements [last_nonopt,optind), which contains all 2N/A the options processed since those non-options were skipped. 2N/A `first_nonopt' and `last_nonopt' are relocated so that they describe 2N/A the new indices of the non-options in ARGV after they are moved. */ 2N/A /* Exchange the shorter segment with the far end of the longer segment. 2N/A That puts the shorter segment into the right place. 2N/A It leaves the longer segment in the right place overall, 2N/A but it consists of two parts that need to be swapped next. */ 2N/A /* First make sure the handling of the `__getopt_nonoption_flags' 2N/A string can work normally. Our top argument must be in the range 2N/A /* We must extend the array. The user plays games with us and 2N/A presents new arguments. */ 2N/A /* Bottom segment is the short one. */ 2N/A /* Swap it with the top part of the top segment. */ 2N/A /* Exclude the moved bottom segment from further swapping. */ 2N/A /* Top segment is the short one. */ 2N/A /* Swap it with the bottom part of the bottom segment. */ 2N/A /* Exclude the moved top segment from further swapping. */ 2N/A /* Update records for the slots the non-options now occupy. */ 2N/A/* Initialize the internal data when the first call is made. */ 2N/A /* Start processing options with ARGV-element 1 (since ARGV-element 0 2N/A is the program name); the sequence of previously skipped 2N/A non-option ARGV-elements is empty. */ 2N/A /* Determine how to handle the ordering of options and nonoptions. */ 2N/A/* Scan elements of ARGV (whose length is ARGC) for option characters 2N/A If an element of ARGV starts with '-', and is not exactly "-" or "--", 2N/A then it is an option element. The characters of this element 2N/A (aside from the initial '-') are option characters. If `getopt' 2N/A is called repeatedly, it returns successively each of the option characters 2N/A from each of the option elements. 2N/A If `getopt' finds another option character, it returns that character, 2N/A updating `optind' and `nextchar' so that the next call to `getopt' can 2N/A resume the scan with the following option character or ARGV-element. 2N/A If there are no more option characters, `getopt' returns -1. 2N/A Then `optind' is the index in ARGV of the first ARGV-element 2N/A that is not an option. (The ARGV-elements have been permuted 2N/A so that those that are not options now come last.) 2N/A OPTSTRING is a string containing the legitimate option characters. 2N/A If an option character is seen that is not listed in OPTSTRING, 2N/A return '?' after printing an error message. If you set `opterr' to 2N/A zero, the error message is suppressed but we still return '?'. 2N/A If a char in OPTSTRING is followed by a colon, that means it wants an arg, 2N/A so the following text in the same ARGV-element, or the text of the following 2N/A ARGV-element, is returned in `optarg'. Two colons mean an option that 2N/A wants an optional arg; if there is text in the current ARGV-element, 2N/A it is returned in `optarg', otherwise `optarg' is set to zero. 2N/A If OPTSTRING starts with `-' or `+', it requests different methods of 2N/A handling the non-option ARGV-elements. 2N/A See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. 2N/A Long-named options begin with `--' instead of `-'. 2N/A Their names may be abbreviated as long as the abbreviation is unique 2N/A or is an exact match for some defined option. If they have an 2N/A argument, it follows the option name in the same ARGV-element, separated 2N/A from the option name by a `=', or else the in next ARGV-element. 2N/A When `getopt' finds a long-named option, it returns 0 if that option's 2N/A `flag' field is nonzero, the value of the option's `val' field 2N/A if the `flag' field is zero. 2N/A The elements of ARGV aren't really const, because we permute them. 2N/A But we pretend they're const in the prototype to be compatible 2N/A LONGOPTS is a vector of `struct option' terminated by an 2N/A element containing a name which is zero. 2N/A LONGIND returns the index in LONGOPT of the long-named option found. 2N/A It is only valid when a long-named option has been found by the most 2N/A If LONG_ONLY is nonzero, '-' as well as '--' can introduce 2N/A long-named options. */ 2N/A d->
optind =
1;
/* Don't scan ARGV[0], the program name. */ 2N/A /* Test whether ARGV[optind] points to a non-option argument. 2N/A Either it does not have option syntax, or there is an environment flag 2N/A from the shell indicating it is not an option. The later information 2N/A is only used when the used in the GNU libc. */ 2N/A /* Advance to the next ARGV-element. */ 2N/A /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 2N/A moved back by the user (who may also have changed the arguments). */ 2N/A /* If we have just processed some options following some non-options, 2N/A exchange them so that the options come first. */ 2N/A /* Skip any additional non-options 2N/A and extend the range of non-options previously skipped. */ 2N/A /* The special ARGV-element `--' means premature end of options. 2N/A Skip it like a null option, 2N/A then exchange with previous non-options as if it were an option, 2N/A then skip everything else like a non-option. */ 2N/A /* If we have done all the ARGV-elements, stop the scan 2N/A and back over any non-options that we skipped and permuted. */ 2N/A /* Set the next-arg-index to point at the non-options 2N/A that we previously skipped, so the caller will digest them. */ 2N/A /* If we have come to a non-option and did not permute it, 2N/A either stop the scan or describe it to the caller and pass it by. */ 2N/A /* We have found another option-ARGV-element. 2N/A Skip the initial punctuation. */ 2N/A /* Decode the current option-ARGV-element. */ 2N/A /* Check whether the ARGV-element is a long option. 2N/A If long_only and the ARGV-element has the form "-f", where f is 2N/A a valid short option, don't consider it an abbreviated form of 2N/A a long option that starts with f. Otherwise there would be no 2N/A way to give the -f short option. 2N/A On the other hand, if there's a long option "fubar" and 2N/A the ARGV-element is "-fu", do consider that an abbreviation of 2N/A the long option, just like "--fu", and not "-f" with arg "u". 2N/A This distinction seems to be the most useful approach. */ 2N/A /* Test all long options for either exact match 2N/A or abbreviated matches. */ 2N/A /* Exact match found. */ 2N/A /* First nonexact match found. */ 2N/A /* Second or later nonexact match found. */ 2N/A /* Don't test has_arg with >, because some C compilers don't 2N/A allow it to be used on enums. */ 2N/A%s: option '--%s' doesn't allow an argument\n"),
2N/A%s: option '--%s' doesn't allow an argument\n"),
2N/A /* +option or -option */ 2N/A%s: option '%c%s' doesn't allow an argument\n"),
2N/A%s: option '%c%s' doesn't allow an argument\n"),
2N/A%s: option '--%s' requires an argument\n"),
2N/A _(
"%s: option '--%s' requires an argument\n"),
2N/A /* Can't find it as a long option. If this is not getopt_long_only, 2N/A or the option starts with '--' or is not a valid short 2N/A option, then it's an error. 2N/A Otherwise interpret it as a short option. */ 2N/A /* +option or -option */ 2N/A /* Look at and handle the next short option-character. */ 2N/A /* Increment `optind' when we start to process its last character. */ 2N/A /* Convenience. Treat POSIX -W foo same as long option --foo */ 2N/A /* This is an option that requires an argument. */ 2N/A /* If we end this ARGV-element by taking the rest as an arg, 2N/A we must advance to the next element now. */ 2N/A _(
"%s: option requires an argument -- '%c'\n"),
2N/A _(
"%s: option requires an argument -- '%c'\n"),
2N/A /* We already incremented `d->optind' once; 2N/A increment it again when taking next ARGV-elt as argument. */ 2N/A /* optarg is now the argument, see if it's in the 2N/A table of longopts. */ 2N/A /* Test all long options for either exact match 2N/A or abbreviated matches. */ 2N/A /* Exact match found. */ 2N/A /* First nonexact match found. */ 2N/A /* Second or later nonexact match found. */ 2N/A /* Don't test has_arg with >, because some C compilers don't 2N/A allow it to be used on enums. */ 2N/A%s: option '-W %s' doesn't allow an argument\n"),
2N/A%s: option '-W %s' doesn't allow an argument\n"),
2N/A%s: option '-W %s' requires an argument\n"),
2N/A%s: option '-W %s' requires an argument\n"),
2N/A return 'W';
/* Let the application handle it. */ 2N/A /* This is an option that accepts an argument optionally. */ 2N/A /* This is an option that requires an argument. */ 2N/A /* If we end this ARGV-element by taking the rest as an arg, 2N/A we must advance to the next element now. */ 2N/A%s: option requires an argument -- '%c'\n"),
2N/A _(
"%s: option requires an argument -- '%c'\n"),
2N/A /* We already incremented `optind' once; 2N/A increment it again when taking next ARGV-elt as argument. */ 2N/A/* glibc gets a LSB-compliant getopt. 2N/A Standalone applications get a POSIX-compliant getopt. */ 2N/A/* Compile with -DTEST to make an executable for use in testing 2N/A the above definition of `getopt'. */ 2N/A printf (
"digits occur in two different argv-elements.\n");
2N/A printf (
"?? getopt returned character code 0%o ??\n", c);