[7f389a5c] | 1 | #include "parseargs.hfa" |
---|
| 2 | |
---|
| 3 | // #include <stdio.h> |
---|
| 4 | // #include <stdlib.h> |
---|
[3f1d9b5] | 5 | #include <errno.h> |
---|
[7f389a5c] | 6 | #include <string.h> |
---|
[3f1d9b5] | 7 | #include <unistd.h> |
---|
[7f389a5c] | 8 | extern "C" { |
---|
| 9 | #include <getopt.h> |
---|
[3f1d9b5] | 10 | #include <sys/ioctl.h> |
---|
[7f389a5c] | 11 | |
---|
| 12 | struct FILE; |
---|
| 13 | extern FILE * stderr; |
---|
| 14 | extern FILE * stdout; |
---|
| 15 | |
---|
[3f1d9b5] | 16 | extern int fileno(FILE *stream); |
---|
| 17 | |
---|
[7f389a5c] | 18 | extern int fprintf ( FILE * stream, const char * format, ... ); |
---|
[53e4562] | 19 | |
---|
| 20 | extern long long int strtoll (const char* str, char** endptr, int base); |
---|
| 21 | extern unsigned long long int strtoull(const char* str, char** endptr, int base); |
---|
[7f389a5c] | 22 | } |
---|
| 23 | |
---|
[3f1d9b5] | 24 | #include <common.hfa> |
---|
| 25 | #include <limits.hfa> |
---|
| 26 | |
---|
| 27 | void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { |
---|
| 28 | int hwidth = max - (11 + width); |
---|
| 29 | if(hwidth <= 0) hwidth = max; |
---|
| 30 | |
---|
| 31 | fprintf(out, " -%c, --%-*s %.*s\n", sn, width, ln, hwidth, help); |
---|
| 32 | for() { |
---|
| 33 | help += min(strlen(help), hwidth); |
---|
| 34 | if('\0' == *help) break; |
---|
| 35 | fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
[7f389a5c] | 39 | void parse_args( |
---|
| 40 | int argc, |
---|
| 41 | char * argv[], |
---|
| 42 | cfa_option options[], |
---|
| 43 | size_t opt_count, |
---|
| 44 | const char * usage, |
---|
| 45 | char ** & left |
---|
| 46 | ) { |
---|
| 47 | struct option optarr[opt_count + 2]; |
---|
| 48 | int width = 0; |
---|
[3f1d9b5] | 49 | int max_width = 1_000_000; |
---|
[7f389a5c] | 50 | { |
---|
| 51 | int idx = 0; |
---|
| 52 | for(i; opt_count) { |
---|
| 53 | if(options[i].long_name) { |
---|
| 54 | optarr[idx].name = options[i].long_name; |
---|
| 55 | optarr[idx].flag = 0p; |
---|
| 56 | optarr[idx].val = options[i].short_name; |
---|
| 57 | if( ((intptr_t)options[i].parse) == ((intptr_t)parse_settrue) |
---|
| 58 | || ((intptr_t)options[i].parse) == ((intptr_t)parse_setfalse) ) { |
---|
| 59 | optarr[idx].has_arg = no_argument; |
---|
| 60 | } else { |
---|
| 61 | optarr[idx].has_arg = required_argument; |
---|
| 62 | } |
---|
| 63 | idx++; |
---|
| 64 | |
---|
| 65 | int w = strlen(options[i].long_name); |
---|
| 66 | if(w > width) width = w; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | optarr[idx+0].[name, has_arg, flag, val] = ["help", no_argument, 0p, 'h']; |
---|
| 70 | optarr[idx+1].[name, has_arg, flag, val] = [0p, no_argument, 0p, 0]; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | char optstring[opt_count * 3] = { '\0' }; |
---|
| 74 | { |
---|
| 75 | int idx = 0; |
---|
| 76 | for(i; opt_count) { |
---|
| 77 | optstring[idx] = options[i].short_name; |
---|
| 78 | idx++; |
---|
| 79 | if( ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue) |
---|
| 80 | && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) { |
---|
| 81 | optstring[idx] = ':'; |
---|
| 82 | idx++; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | optstring[idx+0] = 'h'; |
---|
| 86 | optstring[idx+1] = '\0'; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | FILE * out = stderr; |
---|
| 90 | NEXT_ARG: |
---|
| 91 | for() { |
---|
| 92 | int idx = 0; |
---|
| 93 | int opt = getopt_long(argc, argv, optstring, optarr, &idx); |
---|
| 94 | switch(opt) { |
---|
| 95 | case -1: |
---|
| 96 | if(&left != 0p) left = argv + optind; |
---|
| 97 | return; |
---|
| 98 | case 'h': |
---|
| 99 | out = stdout; |
---|
| 100 | case '?': |
---|
| 101 | goto USAGE; |
---|
| 102 | default: |
---|
| 103 | for(i; opt_count) { |
---|
| 104 | if(opt == options[i].short_name) { |
---|
| 105 | const char * arg = optarg ? optarg : ""; |
---|
| 106 | bool success = options[i].parse( arg, options[i].variable ); |
---|
| 107 | if(success) continue NEXT_ARG; |
---|
| 108 | |
---|
| 109 | fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); |
---|
| 110 | goto USAGE; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | abort("Internal parse arg error\n"); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
[3f1d9b5] | 118 | USAGE:; |
---|
| 119 | int outfd = fileno(out); |
---|
| 120 | if(isatty(outfd)) { |
---|
| 121 | struct winsize size; |
---|
| 122 | int ret = ioctl(outfd, TIOCGWINSZ, &size); |
---|
| 123 | if(ret < 0) abort( "ioctl error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 124 | max_width = size.ws_col; |
---|
| 125 | } |
---|
| 126 | |
---|
[53e4562] | 127 | fprintf(out, "Usage:\n %s %s\n", argv[0], usage); |
---|
[7f389a5c] | 128 | |
---|
| 129 | for(i; opt_count) { |
---|
[3f1d9b5] | 130 | printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help); |
---|
[7f389a5c] | 131 | } |
---|
| 132 | fprintf(out, " -%c, --%-*s %s\n", 'h', width, "help", "print this help message"); |
---|
| 133 | exit(out == stdout ? 0 : 1); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | bool parse_yesno(const char * arg, bool & value ) { |
---|
| 137 | if(strcmp(arg, "yes") == 0) { |
---|
| 138 | value = true; |
---|
| 139 | return true; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | if(strcmp(arg, "no") == 0) { |
---|
| 143 | value = false; |
---|
| 144 | return true; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return false; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | bool parse_settrue (const char *, bool & value ) { |
---|
| 151 | value = true; |
---|
| 152 | return true; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | bool parse_setfalse(const char *, bool & value ) { |
---|
| 156 | value = false; |
---|
| 157 | return true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | bool parse(const char * arg, const char * & value ) { |
---|
| 161 | value = arg; |
---|
| 162 | return true; |
---|
| 163 | } |
---|
| 164 | |
---|
[53e4562] | 165 | bool parse(const char * arg, unsigned & value) { |
---|
| 166 | char * end; |
---|
| 167 | unsigned long long int r = strtoull(arg, &end, 10); |
---|
| 168 | if(*end != '\0') return false; |
---|
[3f1d9b5] | 169 | if(r > (unsigned)MAX) return false; |
---|
[53e4562] | 170 | |
---|
| 171 | value = r; |
---|
| 172 | return true; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | bool parse(const char * arg, size_t & value) { |
---|
| 176 | char * end; |
---|
| 177 | unsigned long long int r = strtoull(arg, &end, 10); |
---|
| 178 | if(*end != '\0') return false; |
---|
[3f1d9b5] | 179 | if(r > (size_t)MAX) return false; |
---|
[53e4562] | 180 | |
---|
| 181 | value = r; |
---|
| 182 | return true; |
---|
| 183 | } |
---|
| 184 | |
---|
[7f389a5c] | 185 | bool parse(const char * arg, int & value) { |
---|
| 186 | char * end; |
---|
| 187 | int r = strtoll(arg, &end, 10); |
---|
| 188 | if(*end != '\0') return false; |
---|
| 189 | |
---|
| 190 | value = r; |
---|
| 191 | return true; |
---|
| 192 | } |
---|