| 1 | #include "parseargs.hfa" | 
|---|
| 2 |  | 
|---|
| 3 | #include <stdint.h> | 
|---|
| 4 | #include <string.h> | 
|---|
| 5 | #include <errno.h> | 
|---|
| 6 | #include <unistd.h> | 
|---|
| 7 | extern "C" { | 
|---|
| 8 | #include <getopt.h> | 
|---|
| 9 | #include <sys/ioctl.h> | 
|---|
| 10 |  | 
|---|
| 11 | struct FILE; | 
|---|
| 12 | extern FILE * stderr; | 
|---|
| 13 | extern FILE * stdout; | 
|---|
| 14 |  | 
|---|
| 15 | extern int fileno(FILE *stream); | 
|---|
| 16 |  | 
|---|
| 17 | extern int fprintf ( FILE * stream, const char * format, ... ); | 
|---|
| 18 |  | 
|---|
| 19 | extern          long long int strtoll (const char* str, char** endptr, int base); | 
|---|
| 20 | extern unsigned long long int strtoull(const char* str, char** endptr, int base); | 
|---|
| 21 | extern                 double strtod  (const char* str, char** endptr); | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | #include "common.hfa" | 
|---|
| 25 | #include "limits.hfa" | 
|---|
| 26 |  | 
|---|
| 27 | extern int cfa_args_argc __attribute__((weak)); | 
|---|
| 28 | extern char ** cfa_args_argv __attribute__((weak)); | 
|---|
| 29 | extern char ** cfa_args_envp __attribute__((weak)); | 
|---|
| 30 |  | 
|---|
| 31 | static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out)  __attribute__ ((noreturn)); | 
|---|
| 32 |  | 
|---|
| 33 | void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) { | 
|---|
| 34 | if( 0p != &cfa_args_argc ) { | 
|---|
| 35 | parse_args(cfa_args_argc, cfa_args_argv, options, opt_count, usage, left ); | 
|---|
| 36 | } | 
|---|
| 37 | else { | 
|---|
| 38 | char * temp = ""; | 
|---|
| 39 | parse_args(0, &temp, options, opt_count, usage, left ); | 
|---|
| 40 | } | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | //----------------------------------------------------------------------------- | 
|---|
| 44 | // getopt_long wrapping | 
|---|
| 45 | void parse_args( | 
|---|
| 46 | int argc, | 
|---|
| 47 | char * argv[], | 
|---|
| 48 | cfa_option options[], | 
|---|
| 49 | size_t opt_count, | 
|---|
| 50 | const char * usage, | 
|---|
| 51 | char ** & left | 
|---|
| 52 | ) { | 
|---|
| 53 | struct option optarr[opt_count + 2]; | 
|---|
| 54 | { | 
|---|
| 55 | int idx = 0; | 
|---|
| 56 | for(i; opt_count) { | 
|---|
| 57 | if(options[i].long_name) { | 
|---|
| 58 | optarr[idx].name = options[i].long_name; | 
|---|
| 59 | optarr[idx].flag = 0p; | 
|---|
| 60 | optarr[idx].val  = options[i].short_name; | 
|---|
| 61 | if(    ((intptr_t)options[i].parse) == ((intptr_t)parse_settrue) | 
|---|
| 62 | || ((intptr_t)options[i].parse) == ((intptr_t)parse_setfalse) ) { | 
|---|
| 63 | optarr[idx].has_arg = no_argument; | 
|---|
| 64 | } else { | 
|---|
| 65 | optarr[idx].has_arg = required_argument; | 
|---|
| 66 | } | 
|---|
| 67 | idx++; | 
|---|
| 68 | } | 
|---|
| 69 | } | 
|---|
| 70 | optarr[idx+0].[name, has_arg, flag, val] = ["help", no_argument, 0, 'h']; | 
|---|
| 71 | optarr[idx+1].[name, has_arg, flag, val] = [0, no_argument, 0, 0]; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | char optstring[opt_count * 3] = { '\0' }; | 
|---|
| 75 | { | 
|---|
| 76 | int idx = 0; | 
|---|
| 77 | for(i; opt_count) { | 
|---|
| 78 | optstring[idx] = options[i].short_name; | 
|---|
| 79 | idx++; | 
|---|
| 80 | if(    ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue) | 
|---|
| 81 | && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) { | 
|---|
| 82 | optstring[idx] = ':'; | 
|---|
| 83 | idx++; | 
|---|
| 84 | } | 
|---|
| 85 | } | 
|---|
| 86 | optstring[idx+0] = 'h'; | 
|---|
| 87 | optstring[idx+1] = '\0'; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | FILE * out = stderr; | 
|---|
| 91 | NEXT_ARG: | 
|---|
| 92 | for() { | 
|---|
| 93 | int idx = 0; | 
|---|
| 94 | int opt = getopt_long(argc, argv, optstring, optarr, &idx); | 
|---|
| 95 | switch(opt) { | 
|---|
| 96 | case -1: | 
|---|
| 97 | if(&left != 0p) left = argv + optind; | 
|---|
| 98 | return; | 
|---|
| 99 | case 'h': | 
|---|
| 100 | out = stdout; | 
|---|
| 101 | case '?': | 
|---|
| 102 | usage(argv[0], options, opt_count, usage, out); | 
|---|
| 103 | default: | 
|---|
| 104 | for(i; opt_count) { | 
|---|
| 105 | if(opt == options[i].short_name) { | 
|---|
| 106 | const char * arg = optarg ? optarg : ""; | 
|---|
| 107 | if( arg[0] == '=' ) { arg++; } | 
|---|
| 108 | bool success = options[i].parse( arg, options[i].variable ); | 
|---|
| 109 | if(success) continue NEXT_ARG; | 
|---|
| 110 |  | 
|---|
| 111 | fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); | 
|---|
| 112 | usage(argv[0], options, opt_count, usage, out); | 
|---|
| 113 | } | 
|---|
| 114 | } | 
|---|
| 115 | abort("Internal parse arg error\n"); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | //----------------------------------------------------------------------------- | 
|---|
| 122 | // Print usage | 
|---|
| 123 | static void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { | 
|---|
| 124 | int hwidth = max - (11 + width); | 
|---|
| 125 | if(hwidth <= 0) hwidth = max; | 
|---|
| 126 |  | 
|---|
| 127 | fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help); | 
|---|
| 128 | for() { | 
|---|
| 129 | help += min(strlen(help), hwidth); | 
|---|
| 130 | if('\0' == *help) break; | 
|---|
| 131 | fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void print_args_usage(cfa_option options[], size_t opt_count, const char * usage, bool error)  __attribute__ ((noreturn)) { | 
|---|
| 136 | usage(cfa_args_argv[0], options, opt_count, usage, error ? stderr : stdout); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void print_args_usage(int , char * argv[], cfa_option options[], size_t opt_count, const char * usage, bool error)  __attribute__ ((noreturn)) { | 
|---|
| 140 | usage(argv[0], options, opt_count, usage, error ? stderr : stdout); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * help, FILE * out) __attribute__((noreturn)) { | 
|---|
| 144 | int width = 0; | 
|---|
| 145 | { | 
|---|
| 146 | for(i; opt_count) { | 
|---|
| 147 | if(options[i].long_name) { | 
|---|
| 148 | int w = strlen(options[i].long_name); | 
|---|
| 149 | if(w > width) width = w; | 
|---|
| 150 | } | 
|---|
| 151 | } | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | int max_width = 1_000_000; | 
|---|
| 155 | int outfd = fileno(out); | 
|---|
| 156 | if(isatty(outfd)) { | 
|---|
| 157 | struct winsize size; | 
|---|
| 158 | int ret = ioctl(outfd, TIOCGWINSZ, &size); | 
|---|
| 159 | if(ret < 0) abort( "ioctl error: (%d) %s\n", (int)errno, strerror(errno) ); | 
|---|
| 160 | max_width = size.ws_col; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | fprintf(out, "Usage:\n  %s %s\n", cmd, help); | 
|---|
| 164 |  | 
|---|
| 165 | for(i; opt_count) { | 
|---|
| 166 | printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help); | 
|---|
| 167 | } | 
|---|
| 168 | fprintf(out, "  -%c, --%-*s   %s\n", 'h', width, "help", "print this help message"); | 
|---|
| 169 | exit(out == stdout ? 0 : 1); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | //----------------------------------------------------------------------------- | 
|---|
| 173 | // Typed argument parsing | 
|---|
| 174 | bool parse_yesno(const char * arg, bool & value ) { | 
|---|
| 175 | if(strcmp(arg, "yes") == 0) { | 
|---|
| 176 | value = true; | 
|---|
| 177 | return true; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | if(strcmp(arg, "no") == 0) { | 
|---|
| 181 | value = false; | 
|---|
| 182 | return true; | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | return false; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | bool parse_truefalse(const char * arg, bool & value) { | 
|---|
| 189 | if(strcmp(arg, "true") == 0) { | 
|---|
| 190 | value = true; | 
|---|
| 191 | return true; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | if(strcmp(arg, "false") == 0) { | 
|---|
| 195 | value = false; | 
|---|
| 196 | return true; | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | return false; | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | bool parse_settrue (const char *, bool & value ) { | 
|---|
| 203 | value = true; | 
|---|
| 204 | return true; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | bool parse_setfalse(const char *, bool & value )  { | 
|---|
| 208 | value = false; | 
|---|
| 209 | return true; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | bool parse(const char * arg, const char * & value ) { | 
|---|
| 213 | value = arg; | 
|---|
| 214 | return true; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | bool parse(const char * arg, int & value) { | 
|---|
| 218 | char * end; | 
|---|
| 219 | int r = strtoll(arg, &end, 10); | 
|---|
| 220 | if(*end != '\0') return false; | 
|---|
| 221 |  | 
|---|
| 222 | value = r; | 
|---|
| 223 | return true; | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | bool parse(const char * arg, unsigned & value) { | 
|---|
| 227 | char * end; | 
|---|
| 228 | unsigned long long int r = strtoull(arg, &end, 10); | 
|---|
| 229 | if(*end != '\0') return false; | 
|---|
| 230 | if(r > (unsigned)MAX) return false; | 
|---|
| 231 |  | 
|---|
| 232 | value = r; | 
|---|
| 233 | return true; | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | bool parse(const char * arg, unsigned long & value) { | 
|---|
| 237 | char * end; | 
|---|
| 238 | unsigned long long int r = strtoull(arg, &end, 10); | 
|---|
| 239 | if(*end != '\0') return false; | 
|---|
| 240 | if(r > (unsigned long)MAX) return false; | 
|---|
| 241 |  | 
|---|
| 242 | value = r; | 
|---|
| 243 | return true; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 | bool parse(const char * arg, unsigned long long & value) { | 
|---|
| 247 | char * end; | 
|---|
| 248 | unsigned long long int r = strtoull(arg, &end, 10); | 
|---|
| 249 | if(*end != '\0') return false; | 
|---|
| 250 | if(r > (unsigned long long)MAX) return false; | 
|---|
| 251 |  | 
|---|
| 252 | value = r; | 
|---|
| 253 | return true; | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | bool parse(const char * arg, double & value) { | 
|---|
| 257 | char * end; | 
|---|
| 258 | double r = strtod(arg, &end); | 
|---|
| 259 | if(*end != '\0') return false; | 
|---|
| 260 |  | 
|---|
| 261 | value = r; | 
|---|
| 262 | return true; | 
|---|
| 263 | } | 
|---|