00001 #ifndef _GETOPT_H 00002 #define _GETOPT_H 00003 00004 /** @file 00005 * 00006 * Parse command-line options 00007 * 00008 */ 00009 00010 FILE_LICENCE ( GPL2_OR_LATER ); 00011 00012 #include <stddef.h> 00013 00014 enum getopt_argument_requirement { 00015 /** Option does not take an argument */ 00016 no_argument = 0, 00017 /** Option requires an argument */ 00018 required_argument = 1, 00019 /** Option may have an argument */ 00020 optional_argument = 2, 00021 }; 00022 00023 /** A long option, as used for getopt_long() */ 00024 struct option { 00025 /** Long name of this option */ 00026 const char *name; 00027 /** Option takes an argument 00028 * 00029 * Must be one of @c no_argument, @c required_argument, or @c 00030 * optional_argument. 00031 */ 00032 int has_arg; 00033 /** Location into which to store @c val, or NULL. 00034 * 00035 * See the description for @c val for more details. 00036 */ 00037 int *flag; 00038 /** Value to return 00039 * 00040 * If @c flag is NULL, then this is the value that will be 00041 * returned by getopt_long() when this option is found, and 00042 * should therefore be set to the equivalent short option 00043 * character. 00044 * 00045 * If @c flag is non-NULL, then this value will be written to 00046 * the location pointed to by @flag, and getopt_long() will 00047 * return 0. 00048 */ 00049 int val; 00050 }; 00051 00052 extern char *optarg; 00053 extern int optind; 00054 extern int nextchar; 00055 extern int optopt; 00056 00057 extern int getopt_long ( int argc, char * const argv[], const char *optstring, 00058 const struct option *longopts, int *longindex ); 00059 00060 /** 00061 * Parse command-line options 00062 * 00063 * @v argv Argument count 00064 * @v argv Argument list 00065 * @v optstring Option specification string 00066 * @ret option Option found, or -1 for no more options 00067 * 00068 * See getopt_long() for full details. 00069 */ 00070 static inline int getopt ( int argc, char * const argv[], 00071 const char *optstring ) { 00072 static const struct option no_options[] = { 00073 { NULL, 0, NULL, 0 } 00074 }; 00075 return getopt_long ( argc, argv, optstring, no_options, NULL ); 00076 } 00077 00078 /** 00079 * Reset getopt() internal state 00080 * 00081 * Due to a limitation of the POSIX getopt() API, it is necessary to 00082 * add a call to reset_getopt() before each set of calls to getopt() 00083 * or getopt_long(). This arises because POSIX assumes that each 00084 * process will parse command line arguments no more than once; this 00085 * assumption is not valid within Etherboot. We work around the 00086 * limitation by arranging for execv() to call reset_getopt() before 00087 * executing the command. 00088 */ 00089 static inline void reset_getopt ( void ) { 00090 optind = 1; 00091 nextchar = 0; 00092 } 00093 00094 #endif /* _GETOPT_H */
1.5.7.1