iPXE
Functions | Variables
getopt.c File Reference

Parse command-line options. More...

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

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static const char * get_argv_argument (int argc, char *const argv[])
 Get option argument from argv[] array.
static int match_long_option (int argc, char *const argv[], const char *opttext, const struct option *longopt, int *option)
 Match long option.
static int match_short_option (int argc, char *const argv[], const char *opttext, int shortopt, enum getopt_argument_requirement has_arg, int *option)
 Match short option.
int getopt_long (int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
 Parse command-line options.

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.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static const char* get_argv_argument ( int  argc,
char *const  argv[] 
) [static]

Get option argument from argv[] array.

Parameters:
argcArgument count
argvArgument list
Return values:
argumentOption argument, or NULL

Grab the next element of argv[], if it exists and is not an option.

Consume this argv element, and return it

Definition at line 77 of file getopt.c.

References NULL, and optind.

Referenced by match_long_option(), and match_short_option().

                                                                        {
        char *arg;

        /* Don't overrun argv[] */
        if ( optind >= argc )
                return NULL;
        arg = argv[optind];

        /* If next argv element is an option, then it's not usable as
         * an argument.
         */
        if ( *arg == '-' )
                return NULL;

        /** Consume this argv element, and return it */
        optind++;
        return arg;
}
static int match_long_option ( int  argc,
char *const  argv[],
const char *  opttext,
const struct option longopt,
int *  option 
) [static]

Match long option.

Parameters:
argcArgument count
argvArgument list
opttextOption text within current argv[] element
longoptLong option specification
Return values:
optionOption to return from getopt()
matchedFound a match for this long option

Definition at line 106 of file getopt.c.

References option::flag, get_argv_argument(), option::has_arg, option::name, no_argument, NULL, optarg, optind, printf(), required_argument, strlen(), strncmp(), and option::val.

Referenced by getopt_long().

                                                                           {
        size_t optlen;
        const char *argument = NULL;

        /* Compare option name */
        optlen = strlen ( longopt->name );
        if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
                return 0;

        /* Check for inline argument */
        if ( opttext[optlen] == '=' ) {
                argument = &opttext[ optlen + 1 ];
        } else if ( opttext[optlen] ) {
                /* Long option with trailing garbage - no match */
                return 0;
        }

        /* Consume this argv element */
        optind++;

        /* If we want an argument but don't have one yet, try to grab
         * the next argv element
         */
        if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
                argument = get_argv_argument ( argc, argv );

        /* If we need an argument but don't have one, sulk */
        if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
                printf ( "Option \"%s\" requires an argument\n",
                         longopt->name );
                *option = ':';
                return 1;
        }

        /* If we have an argument where we shouldn't have one, sulk */
        if ( ( longopt->has_arg == no_argument ) && argument ) {
                printf ( "Option \"%s\" takes no argument\n", longopt->name );
                *option = ':';
                return 1;
        }

        /* Store values and return success */
        optarg = ( char * ) argument;
        if ( longopt->flag ) {
                *(longopt->flag) = longopt->val;
                *option = 0;
        } else {
                *option = longopt->val;
        }
        return 1;
}
static int match_short_option ( int  argc,
char *const  argv[],
const char *  opttext,
int  shortopt,
enum getopt_argument_requirement  has_arg,
int *  option 
) [static]

Match short option.

Parameters:
argcArgument count
argvArgument list
opttextOption text within current argv[] element
shortoptOption character from option specification
Return values:
optionOption to return from getopt()
matchedFound a match for this short option

Definition at line 170 of file getopt.c.

References get_argv_argument(), nextchar, no_argument, NULL, optarg, optind, printf(), and required_argument.

Referenced by getopt_long().

                                              {
        const char *argument = NULL;

        /* Compare option character */
        if ( *opttext != shortopt )
                return 0;

        /* Consume option character */
        opttext++;
        nextchar++;
        if ( *opttext ) {
                if ( has_arg != no_argument ) {
                        /* Consume remainder of element as inline argument */
                        argument = opttext;
                        optind++;
                        nextchar = 0;
                }
        } else {
                /* Reached end of argv element */
                optind++;
                nextchar = 0;
        }

        /* If we want an argument but don't have one yet, try to grab
         * the next argv element
         */
        if ( ( has_arg != no_argument ) && ( ! argument ) )
                argument = get_argv_argument ( argc, argv );

        /* If we need an argument but don't have one, sulk */
        if ( ( has_arg == required_argument ) && ( ! argument ) ) {
                printf ( "Option \"%c\" requires an argument\n", shortopt );
                *option = ':';
                return 1;
        }

        /* Store values and return success */
        optarg = ( char * ) argument;
        *option = shortopt;
        return 1;
}
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 '?';
}

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().