getopt.c File Reference

#include <stdio.h>
#include <string.h>
#include "getopt.h"

Defines

#define _(msgid)   (msgid)
#define NONOPTION_P

Enumerations

enum  { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }

Functions

char * getenv ()
int _getopt_internal (int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
int getopt (int argc, char *const *argv, const char *optstring)

Variables

char * optarg = NULL
int optind = 0
int opterr = 1
int optopt = '?'

Define Documentation

#define _ ( msgid   )     (msgid)

This is for other GNU distributions with internationalized messages. When compiling libc, the _ macro is predefined.

#define NONOPTION_P

Value:

(argv[optind][0] != '-' || argv[optind][1] == '\0'            \
                     || (optind < nonoption_flags_len                         \
                         && nonoption_flags[optind] == '1'))


Enumeration Type Documentation

anonymous enum

Describe how to deal with options that follow non-option ARGV-elements.

If the caller did not specify anything, the default is REQUIRE_ORDER if the environment variable POSIXLY_CORRECT is defined, PERMUTE otherwise.

REQUIRE_ORDER means don't recognize them as options; stop option processing when the first non-option is seen. This is what Unix does. This mode of operation is selected by either setting the environment variable POSIXLY_CORRECT, or using `+' as the first character of the list of option characters.

PERMUTE is the default. We permute the contents of ARGV as we scan, so that eventually all the non-options are at the end. This allows options to be given in any order, even with programs that were not written to expect this.

RETURN_IN_ORDER is an option available to programs that were written to expect options and other ARGV-elements in any order and that care about the ordering of the two. We describe each non-option ARGV-element as if it were the argument of an option with character code 1. Using `-' as the first character of the list of option characters selects this mode of operation.

The special argument `--' forces an end of option-scanning regardless of the value of `ordering'. In the case of RETURN_IN_ORDER, only `--' can cause `getopt' to return EOF with `optind' != ARGC.

Enumerator:
REQUIRE_ORDER 
PERMUTE 
RETURN_IN_ORDER 


Function Documentation

int _getopt_internal ( int  argc,
char *const *  argv,
const char *  optstring,
const struct option longopts,
int *  longind,
int  long_only 
)

Scan elements of ARGV (whose length is ARGC) for option characters given in OPTSTRING.

If an element of ARGV starts with '-', and is not exactly "-" or "--", then it is an option element. The characters of this element (aside from the initial '-') are option characters. If `getopt' is called repeatedly, it returns successively each of the option characters from each of the option elements.

If `getopt' finds another option character, it returns that character, updating `optind' and `nextchar' so that the next call to `getopt' can resume the scan with the following option character or ARGV-element.

If there are no more option characters, `getopt' returns `EOF'. Then `optind' is the index in ARGV of the first ARGV-element that is not an option. (The ARGV-elements have been permuted so that those that are not options now come last.)

OPTSTRING is a string containing the legitimate option characters. If an option character is seen that is not listed in OPTSTRING, return '?' after printing an error message. If you set `opterr' to zero, the error message is suppressed but we still return '?'.

If a char in OPTSTRING is followed by a colon, that means it wants an arg, so the following text in the same ARGV-element, or the text of the following ARGV-element, is returned in `optarg'. Two colons mean an option that wants an optional arg; if there is text in the current ARGV-element, it is returned in `optarg', otherwise `optarg' is set to zero.

If OPTSTRING starts with `-' or `+', it requests different methods of handling the non-option ARGV-elements. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.

Long-named options begin with `--' instead of `-'. Their names may be abbreviated as long as the abbreviation is unique or is an exact match for some defined option. If they have an argument, it follows the option name in the same ARGV-element, separated from the option name by a `=', or else the in next ARGV-element. When `getopt' finds a long-named option, it returns 0 if that option's `flag' field is nonzero, the value of the option's `val' field if the `flag' field is zero.

The elements of ARGV aren't really const, because we permute them. But we pretend they're const in the prototype to be compatible with other systems.

LONGOPTS is a vector of `struct option' terminated by an element containing a name which is zero.

LONGIND returns the index in LONGOPT of the long-named option found. It is only valid when a long-named option has been found by the most recent call.

If LONG_ONLY is nonzero, '-' as well as '--' can introduce long-named options.

Don't scan ARGV[0], the program name.

Test whether ARGV[optind] points to a non-option argument. Either it does not have option syntax, or there is an environment flag from the shell indicating it is not an option.

Advance to the next ARGV-element.

Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been moved back by the user (who may also have changed the arguments).

If we have just processed some options following some non-options, exchange them so that the options come first.

Skip any additional non-options and extend the range of non-options previously skipped.

The special ARGV-element `--' means premature end of options. Skip it like a null option, then exchange with previous non-options as if it were an option, then skip everything else like a non-option.

If we have done all the ARGV-elements, stop the scan and back over any non-options that we skipped and permuted.

Set the next-arg-index to point at the non-options that we previously skipped, so the caller will digest them.

If we have come to a non-option and did not permute it, either stop the scan or describe it to the caller and pass it by.

We have found another option-ARGV-element. Skip the initial punctuation.

Decode the current option-ARGV-element.

Check whether the ARGV-element is a long option.

If long_only and the ARGV-element has the form "-f", where f is a valid short option, don't consider it an abbreviated form of a long option that starts with f. Otherwise there would be no way to give the -f short option.

On the other hand, if there's a long option "fubar" and the ARGV-element is "-fu", do consider that an abbreviation of the long option, just like "--fu", and not "-f" with arg "u".

This distinction seems to be the most useful approach.

Do nothing.

Test all long options for either exact match or abbreviated matches.

Exact match found.

First nonexact match found.

Second or later nonexact match found.

Don't test has_arg with >, because some C compilers don't allow it to be used on enums.

--option

+option or -option

Can't find it as a long option. If this is not getopt_long_only, or the option starts with '--' or is not a valid short option, then it's an error. Otherwise interpret it as a short option.

--option

+option or -option

Look at and handle the next short option-character.

Increment `optind' when we start to process its last character.

1003.2 specifies the format of this message.

This is an option that accepts an argument optionally.

This is an option that requires an argument.

If we end this ARGV-element by taking the rest as an arg, we must advance to the next element now.

1003.2 specifies the format of this message.

We already incremented `optind' once; increment it again when taking next ARGV-elt as argument.

char* getenv (  ) 

Avoid depending on library functions or files whose names are inconsistent.

int getopt ( int  ,
char *const *  ,
const char *   
)

argument associated with option


Variable Documentation

char* optarg = NULL

For communication from `getopt' to the caller. When `getopt' finds an option that takes an argument, the argument value is returned here. Also, when `ordering' is RETURN_IN_ORDER, each non-option ARGV-element is returned here.

int opterr = 1

Callers store zero here to inhibit the error message for unrecognized options.

int optind = 0

XXX 1003.2 says this must be 1 before any call.

int optopt = '?'

Set to an option character which was unrecognized. This must be initialized on some systems to avoid linking in the system's own getopt implementation.


Generated on Tue Sep 4 19:58:56 2007 by  doxygen 1.5.1