iPXE
getopt.h
Go to the documentation of this file.
1 #ifndef _GETOPT_H
2 #define _GETOPT_H
3 
4 /** @file
5  *
6  * Parse command-line options
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stddef.h>
13 
15  /** Option does not take an argument */
17  /** Option requires an argument */
19  /** Option may have an argument */
21 };
22 
23 /** A long option, as used for getopt_long() */
24 struct option {
25  /** Long name of this option */
26  const char *name;
27  /** Option takes an argument
28  *
29  * Must be one of @c no_argument, @c required_argument, or @c
30  * optional_argument.
31  */
32  int has_arg;
33  /** Location into which to store @c val, or NULL.
34  *
35  * See the description for @c val for more details.
36  */
37  int *flag;
38  /** Value to return
39  *
40  * If @c flag is NULL, then this is the value that will be
41  * returned by getopt_long() when this option is found, and
42  * should therefore be set to the equivalent short option
43  * character.
44  *
45  * If @c flag is non-NULL, then this value will be written to
46  * the location pointed to by @flag, and getopt_long() will
47  * return 0.
48  */
49  int val;
50 };
51 
52 extern char *optarg;
53 extern int optind;
54 extern int nextchar;
55 extern int optopt;
56 
57 extern int getopt_long ( int argc, char * const argv[], const char *optstring,
58  const struct option *longopts, int *longindex );
59 
60 /**
61  * Parse command-line options
62  *
63  * @v argv Argument count
64  * @v argv Argument list
65  * @v optstring Option specification string
66  * @ret option Option found, or -1 for no more options
67  *
68  * See getopt_long() for full details.
69  */
70 static inline int getopt ( int argc, char * const argv[],
71  const char *optstring ) {
72  static const struct option no_options[] = {
73  { NULL, 0, NULL, 0 }
74  };
75  return getopt_long ( argc, argv, optstring, no_options, NULL );
76 }
77 
78 /**
79  * Reset getopt() internal state
80  *
81  * Due to a limitation of the POSIX getopt() API, it is necessary to
82  * add a call to reset_getopt() before each set of calls to getopt()
83  * or getopt_long(). This arises because POSIX assumes that each
84  * process will parse command line arguments no more than once; this
85  * assumption is not valid within Etherboot. We work around the
86  * limitation by arranging for execv() to call reset_getopt() before
87  * executing the command.
88  */
89 static inline void reset_getopt ( void ) {
90  optind = 1;
91  nextchar = 0;
92 }
93 
94 #endif /* _GETOPT_H */
int val
Value to return.
Definition: getopt.h:49
A long option, as used for getopt_long()
Definition: getopt.h:24
int optopt
Unrecognised option.
Definition: getopt.c:66
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
char * optarg
Option argument.
Definition: getopt.c:43
static void reset_getopt(void)
Reset getopt() internal state.
Definition: getopt.h:89
int optind
Current option index.
Definition: getopt.c:51
int * flag
Location into which to store val, or NULL.
Definition: getopt.h:37
static int getopt(int argc, char *const argv[], const char *optstring)
Parse command-line options.
Definition: getopt.h:70
Option requires an argument.
Definition: getopt.h:18
int nextchar
Current option character index.
Definition: getopt.c:58
Option may have an argument.
Definition: getopt.h:20
int has_arg
Option takes an argument.
Definition: getopt.h:32
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
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)