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

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

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

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

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

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

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

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

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