| 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 | 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 |
|
|---|
| 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;
|
|---|
| 49 | int max_width = 1_000_000;
|
|---|
| 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, 0, 'h'];
|
|---|
| 70 | optarr[idx+1].[name, has_arg, flag, val] = [0, no_argument, 0, 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 |
|
|---|
| 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 |
|
|---|
| 127 | fprintf(out, "Usage:\n %s %s\n", argv[0], usage);
|
|---|
| 128 |
|
|---|
| 129 | for(i; opt_count) {
|
|---|
| 130 | printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help);
|
|---|
| 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 |
|
|---|
| 165 | bool parse(const char * arg, int & value) {
|
|---|
| 166 | char * end;
|
|---|
| 167 | int r = strtoll(arg, &end, 10);
|
|---|
| 168 | if(*end != '\0') return false;
|
|---|
| 169 |
|
|---|
| 170 | value = r;
|
|---|
| 171 | return true;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | bool parse(const char * arg, unsigned & value) {
|
|---|
| 175 | char * end;
|
|---|
| 176 | unsigned long long int r = strtoull(arg, &end, 10);
|
|---|
| 177 | if(*end != '\0') return false;
|
|---|
| 178 | if(r > (unsigned)MAX) return false;
|
|---|
| 179 |
|
|---|
| 180 | value = r;
|
|---|
| 181 | return true;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | bool parse(const char * arg, size_t & value) {
|
|---|
| 185 | char * end;
|
|---|
| 186 | unsigned long long int r = strtoull(arg, &end, 10);
|
|---|
| 187 | if(*end != '\0') return false;
|
|---|
| 188 | if(r > (size_t)MAX) return false;
|
|---|
| 189 |
|
|---|
| 190 | value = r;
|
|---|
| 191 | return true;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | bool parse(const char * arg, double & value) {
|
|---|
| 195 | char * end;
|
|---|
| 196 | double r = strtod(arg, &end);
|
|---|
| 197 | if(*end != '\0') return false;
|
|---|
| 198 |
|
|---|
| 199 | value = r;
|
|---|
| 200 | return true;
|
|---|
| 201 | }
|
|---|