iPXE
Data Structures | Enumerations | Functions | Variables
getopt.h File Reference

Parse command-line options. More...

#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  option
 A long option, as used for getopt_long() More...

Enumerations

enum  getopt_argument_requirement { no_argument = 0, required_argument = 1, optional_argument = 2 }

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int getopt_long (int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
 Parse command-line options.
static int getopt (int argc, char *const argv[], const char *optstring)
 Parse command-line options.
static void reset_getopt (void)
 Reset getopt() internal state.

Variables

char * optarg
 Option argument.
int optind
 Current option index.
int nextchar
 Current option character index.
int optopt
 Unrecognised option.

Detailed Description

Parse command-line options.

Definition in file getopt.h.


Enumeration Type Documentation

Enumerator:
no_argument 

Option does not take an argument.

required_argument 

Option requires an argument.

optional_argument 

Option may have an argument.

Definition at line 14 of file getopt.h.

                                 {
        /** Option does not take an argument */
        no_argument = 0,
        /** Option requires an argument */
        required_argument = 1,
        /** Option may have an argument */
        optional_argument = 2,
};

Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int getopt_long ( int  argc,
char *const  argv[],
const char *  optstring,
const struct option longopts,
int *  longindex 
)

Parse command-line options.

Parameters:
argcArgument count
argvArgument list
optstringOption specification string
longoptsLong option specification table
Return values:
longindexIndex of long option (or NULL)
optionOption found, or -1 for no more options

Note that the caller must arrange for reset_getopt() to be called before each set of calls to getopt_long(). In Etherboot, this is done automatically by execv().

Definition at line 229 of file getopt.c.

References option::has_arg, match_long_option(), match_short_option(), option::name, nextchar, no_argument, optind, optopt, and printf().

Referenced by getopt(), linux_args_parse(), and reparse_options().

                                                                  {
        const char *opttext = argv[optind];
        const struct option *longopt;
        int shortopt;
        enum getopt_argument_requirement has_arg;
        int option;

        /* Check for end of argv array */
        if ( optind >= argc )
                return -1;

        /* Check for end of options */
        if ( *(opttext++) != '-' )
                return -1;

        /* Check for long options */
        if ( *(opttext++) == '-' ) {
                /* "--" indicates end of options */
                if ( *opttext == '\0' ) {
                        optind++;
                        return -1;
                }
                for ( longopt = longopts ; longopt->name ; longopt++ ) {
                        if ( ! match_long_option ( argc, argv, opttext,
                                                   longopt, &option ) )
                                continue;
                        if ( longindex )
                                *longindex = ( longopt - longopts );
                        return option;
                }
                optopt = '?';
                printf ( "Unrecognised option \"--%s\"\n", opttext );
                return '?';
        }

        /* Check for short options */
        if ( nextchar < 1 )
                nextchar = 1;
        opttext = ( argv[optind] + nextchar );
        while ( ( shortopt = *(optstring++) ) ) {
                has_arg = no_argument;
                while ( *optstring == ':' ) {
                        has_arg++;
                        optstring++;
                }
                if ( match_short_option ( argc, argv, opttext, shortopt,
                                          has_arg, &option ) ) {
                        return option;
                }
        }
        optopt = *opttext;
        printf ( "Unrecognised option \"-%c\"\n", optopt );
        return '?';
}
static int getopt ( int  argc,
char *const  argv[],
const char *  optstring 
) [inline, static]

Parse command-line options.

Parameters:
argvArgument count
argvArgument list
optstringOption specification string
Return values:
optionOption found, or -1 for no more options

See getopt_long() for full details.

Definition at line 70 of file getopt.h.

References getopt_long(), and NULL.

                                                   {
        static const struct option no_options[] = {
                { NULL, 0, NULL, 0 }
        };
        return getopt_long ( argc, argv, optstring, no_options, NULL );
}
static void reset_getopt ( void  ) [inline, static]

Reset getopt() internal state.

Due to a limitation of the POSIX getopt() API, it is necessary to add a call to reset_getopt() before each set of calls to getopt() or getopt_long(). This arises because POSIX assumes that each process will parse command line arguments no more than once; this assumption is not valid within Etherboot. We work around the limitation by arranging for execv() to call reset_getopt() before executing the command.

Definition at line 89 of file getopt.h.

References nextchar, and optind.

Referenced by execv(), and linux_args_parse().

                                         {
        optind = 1;
        nextchar = 0;
}

Variable Documentation

char* optarg

Option argument.

This will point to the argument for the most recently returned option, if applicable.

Definition at line 43 of file getopt.c.

Referenced by linux_args_parse(), match_long_option(), match_short_option(), and reparse_options().

int optind
int nextchar

Current option character index.

This is an index into the current element of argv[].

Definition at line 58 of file getopt.c.

Referenced by getopt_long(), match_short_option(), and reset_getopt().

int optopt

Unrecognised option.

When an unrecognised option is encountered, the actual option character is stored in optopt.

Definition at line 66 of file getopt.c.

Referenced by getopt_long().