Add errfile definition for new e1000.c
[gpxe.git] / src / include / getopt.h
blob2505223edb881ea3971319ac8a746faf371d6aa8
1 #ifndef _GETOPT_H
2 #define _GETOPT_H
4 /** @file
6 * Parse command-line options
8 */
10 #include <stddef.h>
12 enum getopt_argument_requirement {
13 /** Option does not take an argument */
14 no_argument = 0,
15 /** Option requires an argument */
16 required_argument = 1,
17 /** Option may have an argument */
18 optional_argument = 2,
21 /** A long option, as used for getopt_long() */
22 struct option {
23 /** Long name of this option */
24 const char *name;
25 /** Option takes an argument
27 * Must be one of @c no_argument, @c required_argument, or @c
28 * optional_argument.
30 int has_arg;
31 /** Location into which to store @c val, or NULL.
33 * See the description for @c val for more details.
35 int *flag;
36 /** Value to return
38 * If @c flag is NULL, then this is the value that will be
39 * returned by getopt_long() when this option is found, and
40 * should therefore be set to the equivalent short option
41 * character.
43 * If @c flag is non-NULL, then this value will be written to
44 * the location pointed to by @flag, and getopt_long() will
45 * return 0.
47 int val;
50 extern char *optarg;
51 extern int optind;
52 extern int nextchar;
53 extern int optopt;
55 extern int getopt_long ( int argc, char * const argv[], const char *optstring,
56 const struct option *longopts, int *longindex );
58 /**
59 * Parse command-line options
61 * @v argv Argument count
62 * @v argv Argument list
63 * @v optstring Option specification string
64 * @ret option Option found, or -1 for no more options
66 * See getopt_long() for full details.
68 static inline int getopt ( int argc, char * const argv[],
69 const char *optstring ) {
70 static const struct option no_options[] = {
71 { NULL, 0, NULL, 0 }
73 return getopt_long ( argc, argv, optstring, no_options, NULL );
76 /**
77 * Reset getopt() internal state
79 * Due to a limitation of the POSIX getopt() API, it is necessary to
80 * add a call to reset_getopt() before each set of calls to getopt()
81 * or getopt_long(). This arises because POSIX assumes that each
82 * process will parse command line arguments no more than once; this
83 * assumption is not valid within Etherboot. We work around the
84 * limitation by arranging for execv() to call reset_getopt() before
85 * executing the command.
87 static inline void reset_getopt ( void ) {
88 optind = 1;
89 nextchar = 0;
92 #endif /* _GETOPT_H */