1 /* opt.h - decls for command-line option parsing 2 * 3 **************************************************************** 4 * Copyright (C) 1998, 2000 Thomas Lord 5 * 6 * See the file "COPYING" for further information about 7 * the copyright and warranty status of this work. 8 */ 9 10 11 #ifndef INCLUDE__CMD__OPT_H 12 #define INCLUDE__CMD__OPT_H 13 14 15 #include "hackerlab/machine/types.h" 16 #include "hackerlab/mem/alloc-limits.h" 17 18 19 /************************************************************************ 20 *(h1 "Option Tables") 21 * 22 * 23 * 24 */ 25 26 27 /*(c opt_desc :category type) 28 insert*/ 29 struct opt_desc 30 { 31 int opt_value; 32 t_uchar * char_name; 33 t_uchar * long_name; 34 int requires_arg; 35 t_uchar * desc; 36 }; 37 /*end-insert 38 */ 39 40 /*(c opt_parsed :category type) 41 insert*/ 42 struct opt_parsed 43 { 44 int opt_value; 45 t_uchar * opt_string; 46 t_uchar * arg_string; 47 struct opt_desc * desc; 48 }; 49 /*end-insert 50 */ 51 52 #if 0 53 /*(text) 54 * 55 * An array of `struct opt_desc' is used to describe the options 56 * accepted by a program. 57 * 58 * The easiest way to create this array is by defining a macro `OPTS' 59 * of two arguments: `OP', which is used to define a program option 60 * and `OP2', which is used to add additional lines of documentation 61 * to an option. 62 * 63 * Both `OP' and `OP2' are used as macros which accept 5 arguments: 64 * 65 * name an enum name for the option 66 * char_name 0 or a string beginning with a one-character 67 * name for the option. 68 * long_name 0 or a string beginning with a long name for 69 * the option 70 * arg 1 if the option requires an argument, 0 71 * otherwise 72 * desc A documentation string for the option 73 * 74 * Once you have defined `OPTS' it is easy to create a table of 75 * `struct opt_desc', as in the following example: 76 * 77 insert*/ 78 79 #define OPTS(OP, OP2) \ 80 OP (opt_help_msg, /* An enum name for the option */ \ 81 "h", /* A short name for the option */ \ 82 "help", /* A long name for the option */ \ 83 0, /* The option takes no arguments */ \ 84 "Display a help message and exit.") /* help message */ \ 85 \ 86 OP (opt_version, "V", "version", 0, \ 87 "Display a release identifier string and exit.") \ 88 \ 89 /* The next option illustrates how to handle a multi-line */ \ 90 /* help message: */ \ 91 \ 92 OP (opt_output_file, "o file", "output-file=file", 1, \ 93 "Write output to FILE.") \ 94 OP2 (opt_output_file, 0, 0, 1, \ 95 "The file must not already exist.") \ 96 OP (...) ... 97 98 /* Note that the short and long names for an option are optional 99 * (may be 0) but if both are omitted, then there is no way to 100 * specify the option on a command line. 101 */ 102 103 enum options 104 { 105 OPTS (OPT_ENUM, OPT_IGN) 106 }; 107 108 struct opt_desc opts[] = 109 { 110 OPTS (OPT_DESC, OPT_DESC) 111 {-1, 0, 0, 0, 0} 112 }; 113 /*end-insert 114 */ 115 #endif 116 117 #define OPT_ENUM(name,char_name,long_name,arg,desc) name, 118 #define OPT_DESC(name,char_name,long_name,arg,desc) {name,char_name,long_name,arg,desc}, 119 #define OPT_IGN(name,char_name,long_name,arg,desc) 120 121 122 123 124 /* See `opt' in `opt.c'. 125 */ 126 enum opt_return_values 127 { 128 opt_dash = -1, /* "-" */ 129 opt_double_dash = -2, /* "--" */ 130 opt_unknown = -3, /* unrecognized option */ 131 opt_bogus_argument = -4, /* --foo=bar but foo does not take an argument */ 132 opt_none = -5, /* next argument is not an option */ 133 opt_allocation_failure = -6 /* from op */ 134 }; 135 136 137 138 /* automatically generated __STDC__ prototypes */ 139 extern int opt_low (t_uchar ** opt_string, 140 t_uchar ** opt_arg, 141 struct opt_desc ** desc, 142 struct opt_desc * opts, 143 int * argcp, 144 char ** argv, 145 t_uchar * opt_string_space); 146 extern int opt (alloc_limits limits, 147 struct opt_parsed ** parsed_p, 148 struct opt_desc * opts, 149 int * argcp, 150 char ** argv); 151 extern int opt_standard (alloc_limits limits, 152 struct opt_parsed ** parsed_p, 153 struct opt_desc * opts, 154 int * argc, 155 char * argv[], 156 t_uchar * program_name, 157 t_uchar * usage, 158 t_uchar * version_string, 159 t_uchar * long_help, 160 int help_option, 161 int long_help_option, 162 int version_option); 163 extern void opt_usage (int fd, 164 char * argv0, 165 t_uchar * program_name, 166 t_uchar * usage, 167 int suggest_help); 168 extern void opt_help (int fd, struct opt_desc * opts); 169 extern void opt_shift_char_option (int * argcp, char ** argv); 170 extern void opt_shift (int * argcp, char ** argv); 171 extern int main (int argc, char * argv[]); 172 #endif /* INCLUDE__CMD__OPT_H */ 173