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