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