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. More...
 
static int getopt (int argc, char *const argv[], const char *optstring)
 Parse command-line options. More...
 
static void reset_getopt (void)
 Reset getopt() internal state. More...
 

Variables

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

Detailed Description

Parse command-line options.

Definition in file getopt.h.

Enumeration Type Documentation

◆ getopt_argument_requirement

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.

14  {
15  /** Option does not take an argument */
16  no_argument = 0,
17  /** Option requires an argument */
19  /** Option may have an argument */
21 };
Option does not take an argument.
Definition: getopt.h:16
Option requires an argument.
Definition: getopt.h:18
Option may have an argument.
Definition: getopt.h:20

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ getopt_long()

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.

230  {
231  const char *opttext = argv[optind];
232  const struct option *longopt;
233  int shortopt;
235  int option;
236 
237  /* Check for end of argv array */
238  if ( optind >= argc )
239  return -1;
240 
241  /* Check for end of options */
242  if ( *(opttext++) != '-' )
243  return -1;
244 
245  /* Check for long options */
246  if ( *(opttext++) == '-' ) {
247  /* "--" indicates end of options */
248  if ( *opttext == '\0' ) {
249  optind++;
250  return -1;
251  }
252  for ( longopt = longopts ; longopt->name ; longopt++ ) {
253  if ( ! match_long_option ( argc, argv, opttext,
254  longopt, &option ) )
255  continue;
256  if ( longindex )
257  *longindex = ( longopt - longopts );
258  return option;
259  }
260  optopt = '?';
261  printf ( "Unrecognised option \"--%s\"\n", opttext );
262  return '?';
263  }
264 
265  /* Check for short options */
266  if ( nextchar < 1 )
267  nextchar = 1;
268  opttext = ( argv[optind] + nextchar );
269  while ( ( shortopt = *(optstring++) ) ) {
271  while ( *optstring == ':' ) {
272  has_arg++;
273  optstring++;
274  }
275  if ( match_short_option ( argc, argv, opttext, shortopt,
276  has_arg, &option ) ) {
277  return option;
278  }
279  }
280  optopt = *opttext;
281  printf ( "Unrecognised option \"-%c\"\n", optopt );
282  return '?';
283 }
int nextchar
Current option character index.
Definition: getopt.c:58
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int optopt
Unrecognised option.
Definition: getopt.c:66
int optind
Current option index.
Definition: getopt.c:51
A long option, as used for getopt_long()
Definition: getopt.h:24
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.
Definition: getopt.c:170
getopt_argument_requirement
Definition: getopt.h:14
const char * name
Long name of this option.
Definition: getopt.h:26
Option does not take an argument.
Definition: getopt.h:16
int has_arg
Option takes an argument.
Definition: getopt.h:32
static int match_long_option(int argc, char *const argv[], const char *opttext, const struct option *longopt, int *option)
Match long option.
Definition: getopt.c:106

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

◆ getopt()

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

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.

71  {
72  static const struct option no_options[] = {
73  { NULL, 0, NULL, 0 }
74  };
75  return getopt_long ( argc, argv, optstring, no_options, NULL );
76 }
A long option, as used for getopt_long()
Definition: getopt.h:24
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Parse command-line options.
Definition: getopt.c:229
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References getopt_long(), and NULL.

◆ reset_getopt()

static void reset_getopt ( void  )
inlinestatic

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.

89  {
90  optind = 1;
91  nextchar = 0;
92 }
int optind
Current option index.
Definition: getopt.c:51
int nextchar
Current option character index.
Definition: getopt.c:58

References nextchar, and optind.

Referenced by execv(), and linux_args_parse().

Variable Documentation

◆ optarg

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

◆ optind

int optind

◆ nextchar

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

◆ optopt

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