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)
 
 FILE_SECBOOT (PERMITTED)
 
static const char * get_argv_argument (int argc, char *const argv[])
 Get option argument from argv[] array. More...
 
static int match_long_option (int argc, char *const argv[], const char *opttext, const struct option *longopt, int *option)
 Match long option. More...
 
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. More...
 
int getopt_long (int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
 Parse command-line options. 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.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ get_argv_argument()

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

78  {
79  char *arg;
80 
81  /* Don't overrun argv[] */
82  if ( optind >= argc )
83  return NULL;
84  arg = argv[optind];
85 
86  /* If next argv element is an option, then it's not usable as
87  * an argument.
88  */
89  if ( *arg == '-' )
90  return NULL;
91 
92  /** Consume this argv element, and return it */
93  optind++;
94  return arg;
95 }
int optind
Current option index.
Definition: getopt.c:52
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References NULL, and optind.

Referenced by match_long_option(), and match_short_option().

◆ match_long_option()

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

109  {
110  size_t optlen;
111  const char *argument = NULL;
112 
113  /* Compare option name */
114  optlen = strlen ( longopt->name );
115  if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
116  return 0;
117 
118  /* Check for inline argument */
119  if ( opttext[optlen] == '=' ) {
120  argument = &opttext[ optlen + 1 ];
121  } else if ( opttext[optlen] ) {
122  /* Long option with trailing garbage - no match */
123  return 0;
124  }
125 
126  /* Consume this argv element */
127  optind++;
128 
129  /* If we want an argument but don't have one yet, try to grab
130  * the next argv element
131  */
132  if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
133  argument = get_argv_argument ( argc, argv );
134 
135  /* If we need an argument but don't have one, sulk */
136  if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
137  printf ( "Option \"%s\" requires an argument\n",
138  longopt->name );
139  *option = ':';
140  return 1;
141  }
142 
143  /* If we have an argument where we shouldn't have one, sulk */
144  if ( ( longopt->has_arg == no_argument ) && argument ) {
145  printf ( "Option \"%s\" takes no argument\n", longopt->name );
146  *option = ':';
147  return 1;
148  }
149 
150  /* Store values and return success */
151  optarg = ( char * ) argument;
152  if ( longopt->flag ) {
153  *(longopt->flag) = longopt->val;
154  *option = 0;
155  } else {
156  *option = longopt->val;
157  }
158  return 1;
159 }
static const char * get_argv_argument(int argc, char *const argv[])
Get option argument from argv[] array.
Definition: getopt.c:78
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:465
int optind
Current option index.
Definition: getopt.c:52
int val
Value to return.
Definition: getopt.h:50
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Definition: string.c:187
A long option, as used for getopt_long()
Definition: getopt.h:25
const char * name
Long name of this option.
Definition: getopt.h:27
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
Option does not take an argument.
Definition: getopt.h:17
int * flag
Location into which to store val, or NULL.
Definition: getopt.h:38
Option requires an argument.
Definition: getopt.h:19
char * optarg
Option argument.
Definition: getopt.c:44
int has_arg
Option takes an argument.
Definition: getopt.h:33
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

◆ match_short_option()

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

174  {
175  const char *argument = NULL;
176 
177  /* Compare option character */
178  if ( *opttext != shortopt )
179  return 0;
180 
181  /* Consume option character */
182  opttext++;
183  nextchar++;
184  if ( *opttext ) {
185  if ( has_arg != no_argument ) {
186  /* Consume remainder of element as inline argument */
187  argument = opttext;
188  optind++;
189  nextchar = 0;
190  }
191  } else {
192  /* Reached end of argv element */
193  optind++;
194  nextchar = 0;
195  }
196 
197  /* If we want an argument but don't have one yet, try to grab
198  * the next argv element
199  */
200  if ( ( has_arg != no_argument ) && ( ! argument ) )
201  argument = get_argv_argument ( argc, argv );
202 
203  /* If we need an argument but don't have one, sulk */
204  if ( ( has_arg == required_argument ) && ( ! argument ) ) {
205  printf ( "Option \"%c\" requires an argument\n", shortopt );
206  *option = ':';
207  return 1;
208  }
209 
210  /* Store values and return success */
211  optarg = ( char * ) argument;
212  *option = shortopt;
213  return 1;
214 }
static const char * get_argv_argument(int argc, char *const argv[])
Get option argument from argv[] array.
Definition: getopt.c:78
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 optind
Current option index.
Definition: getopt.c:52
A long option, as used for getopt_long()
Definition: getopt.h:25
Option does not take an argument.
Definition: getopt.h:17
Option requires an argument.
Definition: getopt.h:19
char * optarg
Option argument.
Definition: getopt.c:44
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

Referenced by getopt_long().

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

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