[7f389a5c] | 1 | #include "parseargs.hfa" |
---|
| 2 | |
---|
[e699eb6] | 3 | #include <stdint.h> |
---|
[7f389a5c] | 4 | #include <string.h> |
---|
[e699eb6] | 5 | #include <errno.h> |
---|
[3f1d9b5] | 6 | #include <unistd.h> |
---|
[7f389a5c] | 7 | extern "C" { |
---|
| 8 | #include <getopt.h> |
---|
[3f1d9b5] | 9 | #include <sys/ioctl.h> |
---|
[7f389a5c] | 10 | |
---|
| 11 | struct FILE; |
---|
| 12 | extern FILE * stderr; |
---|
| 13 | extern FILE * stdout; |
---|
| 14 | |
---|
[3f1d9b5] | 15 | extern int fileno(FILE *stream); |
---|
| 16 | |
---|
[7f389a5c] | 17 | extern int fprintf ( FILE * stream, const char * format, ... ); |
---|
[53e4562] | 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); |
---|
[7f6e9eb] | 21 | extern double strtod (const char* str, char** endptr); |
---|
[7f389a5c] | 22 | } |
---|
| 23 | |
---|
[e699eb6] | 24 | #include "common.hfa" |
---|
| 25 | #include "limits.hfa" |
---|
[3f1d9b5] | 26 | |
---|
[433d352] | 27 | extern int cfa_args_argc __attribute__((weak)); |
---|
| 28 | extern char ** cfa_args_argv __attribute__((weak)); |
---|
| 29 | extern char ** cfa_args_envp __attribute__((weak)); |
---|
[7874d77] | 30 | |
---|
[419c434] | 31 | static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out) __attribute__ ((noreturn)); |
---|
[3f1d9b5] | 32 | |
---|
[7874d77] | 33 | void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) { |
---|
[433d352] | 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 | } |
---|
[7874d77] | 41 | } |
---|
| 42 | |
---|
[419c434] | 43 | //----------------------------------------------------------------------------- |
---|
| 44 | // getopt_long wrapping |
---|
[7f389a5c] | 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 | } |
---|
[e699eb6] | 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]; |
---|
[7f389a5c] | 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 '?': |
---|
[419c434] | 102 | usage(argv[0], options, opt_count, usage, out); |
---|
[7f389a5c] | 103 | default: |
---|
| 104 | for(i; opt_count) { |
---|
| 105 | if(opt == options[i].short_name) { |
---|
| 106 | const char * arg = optarg ? optarg : ""; |
---|
| 107 | bool success = options[i].parse( arg, options[i].variable ); |
---|
| 108 | if(success) continue NEXT_ARG; |
---|
| 109 | |
---|
| 110 | fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); |
---|
[419c434] | 111 | usage(argv[0], options, opt_count, usage, out); |
---|
[7f389a5c] | 112 | } |
---|
| 113 | } |
---|
| 114 | abort("Internal parse arg error\n"); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | } |
---|
[419c434] | 118 | } |
---|
[7f389a5c] | 119 | |
---|
[419c434] | 120 | //----------------------------------------------------------------------------- |
---|
| 121 | // Print usage |
---|
| 122 | static void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { |
---|
| 123 | int hwidth = max - (11 + width); |
---|
| 124 | if(hwidth <= 0) hwidth = max; |
---|
| 125 | |
---|
| 126 | fprintf(out, " -%c, --%-*s %.*s\n", sn, width, ln, hwidth, help); |
---|
| 127 | for() { |
---|
| 128 | help += min(strlen(help), hwidth); |
---|
| 129 | if('\0' == *help) break; |
---|
| 130 | fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void print_args_usage(cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)) { |
---|
| 135 | usage(cfa_args_argv[0], options, opt_count, usage, error ? stderr : stdout); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void print_args_usage(int , char * argv[], cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)) { |
---|
| 139 | usage(argv[0], options, opt_count, usage, error ? stderr : stdout); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * help, FILE * out) __attribute__((noreturn)) { |
---|
| 143 | int width = 0; |
---|
| 144 | { |
---|
| 145 | for(i; opt_count) { |
---|
| 146 | if(options[i].long_name) { |
---|
| 147 | int w = strlen(options[i].long_name); |
---|
| 148 | if(w > width) width = w; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | int max_width = 1_000_000; |
---|
[3f1d9b5] | 154 | int outfd = fileno(out); |
---|
| 155 | if(isatty(outfd)) { |
---|
| 156 | struct winsize size; |
---|
| 157 | int ret = ioctl(outfd, TIOCGWINSZ, &size); |
---|
| 158 | if(ret < 0) abort( "ioctl error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 159 | max_width = size.ws_col; |
---|
| 160 | } |
---|
| 161 | |
---|
[419c434] | 162 | fprintf(out, "Usage:\n %s %s\n", cmd, help); |
---|
[7f389a5c] | 163 | |
---|
| 164 | for(i; opt_count) { |
---|
[3f1d9b5] | 165 | printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help); |
---|
[7f389a5c] | 166 | } |
---|
| 167 | fprintf(out, " -%c, --%-*s %s\n", 'h', width, "help", "print this help message"); |
---|
| 168 | exit(out == stdout ? 0 : 1); |
---|
| 169 | } |
---|
| 170 | |
---|
[419c434] | 171 | //----------------------------------------------------------------------------- |
---|
| 172 | // Typed argument parsing |
---|
[7f389a5c] | 173 | bool parse_yesno(const char * arg, bool & value ) { |
---|
| 174 | if(strcmp(arg, "yes") == 0) { |
---|
| 175 | value = true; |
---|
| 176 | return true; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | if(strcmp(arg, "no") == 0) { |
---|
| 180 | value = false; |
---|
| 181 | return true; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | return false; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | bool parse_settrue (const char *, bool & value ) { |
---|
| 188 | value = true; |
---|
| 189 | return true; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | bool parse_setfalse(const char *, bool & value ) { |
---|
| 193 | value = false; |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | bool parse(const char * arg, const char * & value ) { |
---|
| 198 | value = arg; |
---|
| 199 | return true; |
---|
| 200 | } |
---|
| 201 | |
---|
[7f6e9eb] | 202 | bool parse(const char * arg, int & value) { |
---|
| 203 | char * end; |
---|
| 204 | int r = strtoll(arg, &end, 10); |
---|
| 205 | if(*end != '\0') return false; |
---|
| 206 | |
---|
| 207 | value = r; |
---|
| 208 | return true; |
---|
| 209 | } |
---|
| 210 | |
---|
[53e4562] | 211 | bool parse(const char * arg, unsigned & value) { |
---|
| 212 | char * end; |
---|
| 213 | unsigned long long int r = strtoull(arg, &end, 10); |
---|
| 214 | if(*end != '\0') return false; |
---|
[3f1d9b5] | 215 | if(r > (unsigned)MAX) return false; |
---|
[53e4562] | 216 | |
---|
| 217 | value = r; |
---|
| 218 | return true; |
---|
| 219 | } |
---|
| 220 | |
---|
[56e8cb3] | 221 | bool parse(const char * arg, unsigned long & value) { |
---|
[53e4562] | 222 | char * end; |
---|
| 223 | unsigned long long int r = strtoull(arg, &end, 10); |
---|
| 224 | if(*end != '\0') return false; |
---|
[56e8cb3] | 225 | if(r > (unsigned long)MAX) return false; |
---|
[53e4562] | 226 | |
---|
| 227 | value = r; |
---|
| 228 | return true; |
---|
| 229 | } |
---|
| 230 | |
---|
[56e8cb3] | 231 | bool parse(const char * arg, unsigned long long & value) { |
---|
| 232 | char * end; |
---|
| 233 | unsigned long long int r = strtoull(arg, &end, 10); |
---|
| 234 | if(*end != '\0') return false; |
---|
| 235 | if(r > (unsigned long long)MAX) return false; |
---|
| 236 | |
---|
| 237 | value = r; |
---|
| 238 | return true; |
---|
| 239 | } |
---|
| 240 | |
---|
[7f6e9eb] | 241 | bool parse(const char * arg, double & value) { |
---|
[7f389a5c] | 242 | char * end; |
---|
[7f6e9eb] | 243 | double r = strtod(arg, &end); |
---|
[7f389a5c] | 244 | if(*end != '\0') return false; |
---|
| 245 | |
---|
| 246 | value = r; |
---|
| 247 | return true; |
---|
[56e8cb3] | 248 | } |
---|