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

00014                                  {
00015         /** Option does not take an argument */
00016         no_argument = 0,
00017         /** Option requires an argument */
00018         required_argument = 1,
00019         /** Option may have an argument */
00020         optional_argument = 2,
00021 };


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

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

Parse command-line options.

Parameters:
argc Argument count
argv Argument list
optstring Option specification string
longopts Long option specification table
Return values:
longindex Index of long option (or NULL)
option Option 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 224 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 dhcp_exec(), gdbstub_exec(), getopt(), ifcommon_exec(), imgargs_exec(), imgexec_exec(), imgfetch_core_exec(), imgfree_exec(), imgload_exec(), imgstat_exec(), pxebs_exec(), route_exec(), and sanboot_exec().

00225                                                                   {
00226         const char *opttext = argv[optind];
00227         const struct option *longopt;
00228         int shortopt;
00229         enum getopt_argument_requirement has_arg;
00230         int option;
00231 
00232         /* Check for end of argv array */
00233         if ( optind >= argc )
00234                 return -1;
00235 
00236         /* Check for end of options */
00237         if ( *(opttext++) != '-' )
00238                 return -1;
00239 
00240         /* Check for long options */
00241         if ( *(opttext++) == '-' ) {
00242                 for ( longopt = longopts ; longopt->name ; longopt++ ) {
00243                         if ( ! match_long_option ( argc, argv, opttext,
00244                                                    longopt, &option ) )
00245                                 continue;
00246                         if ( longindex )
00247                                 *longindex = ( longopt - longopts );
00248                         return option;
00249                 }
00250                 optopt = '?';
00251                 printf ( "Unrecognised option \"--%s\"\n", opttext );
00252                 return '?';
00253         }
00254 
00255         /* Check for short options */
00256         if ( nextchar < 1 )
00257                 nextchar = 1;
00258         opttext = ( argv[optind] + nextchar );
00259         while ( ( shortopt = *(optstring++) ) ) {
00260                 has_arg = no_argument;
00261                 while ( *optstring == ':' ) {
00262                         has_arg++;
00263                         optstring++;
00264                 }
00265                 if ( match_short_option ( argc, argv, opttext, shortopt,
00266                                           has_arg, &option ) ) {
00267                         return option;
00268                 }
00269         }
00270         optopt = *opttext;
00271         printf ( "Unrecognised option \"-%c\"\n", optopt );
00272         return '?';
00273 }

static int getopt ( int  argc,
char *const   argv[],
const char *  optstring 
) [inline, static]

Parse command-line options.

Parameters:
argv Argument count
argv Argument list
optstring Option specification string
Return values:
option Option 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.

00071                                                    {
00072         static const struct option no_options[] = {
00073                 { NULL, 0, NULL, 0 }
00074         };
00075         return getopt_long ( argc, argv, optstring, no_options, NULL );
00076 }

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

00089                                          {
00090         optind = 1;
00091         nextchar = 0;
00092 }


Variable Documentation

char* optarg

Option argument.

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

Definition at line 38 of file getopt.c.

Referenced by imgfetch_core_exec(), match_long_option(), and match_short_option().

int optind

Current option index.

This is an index into the argv[] array. When getopt() returns -1, optind is the index to the first element that is not an option.

Definition at line 41 of file exec.c.

Referenced by dhcp_exec(), gdbstub_exec(), get_argv_argument(), getopt_long(), ifcommon_exec(), imgargs_exec(), imgexec_exec(), imgfetch_core_exec(), imgfree_exec(), imgload_exec(), imgstat_exec(), match_long_option(), match_short_option(), pxebs_exec(), reset_getopt(), route_exec(), and sanboot_exec().

int nextchar

Current option character index.

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

Definition at line 42 of file exec.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 61 of file getopt.c.

Referenced by getopt_long().


Generated on Tue Apr 6 20:01:48 2010 for gPXE by  doxygen 1.5.7.1