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)
 
 FILE_SECBOOT (PERMITTED)
 
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 15 of file getopt.h.

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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ 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 230 of file getopt.c.

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

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 71 of file getopt.h.

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

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 90 of file getopt.h.

90  {
91  optind = 1;
92  nextchar = 0;
93 }
int optind
Current option index.
Definition: getopt.c:52
int nextchar
Current option character index.
Definition: getopt.c:59

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 44 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 59 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 67 of file getopt.c.

Referenced by getopt_long().