source: libcfa/src/parseargs.cfa@ 762fbc1

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 762fbc1 was 419c434, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added support for printing the usage as parseargs would,
without the option --help or an error in parsing args.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1#include "parseargs.hfa"
2
3#include <stdint.h>
4#include <string.h>
5#include <errno.h>
6#include <unistd.h>
7extern "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
27extern int cfa_args_argc;
28extern char ** cfa_args_argv;
29extern char ** cfa_args_envp;
30
31static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out) __attribute__ ((noreturn));
32
33void 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
39void 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
116static 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
128void 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
132void 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
136static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * help, FILE * out) __attribute__((noreturn)) {
137 int width = 0;
138 {
139 int idx = 0;
140 for(i; opt_count) {
141 if(options[i].long_name) {
142 int w = strlen(options[i].long_name);
143 if(w > width) width = w;
144 }
145 }
146 }
147
148 int max_width = 1_000_000;
149 int outfd = fileno(out);
150 if(isatty(outfd)) {
151 struct winsize size;
152 int ret = ioctl(outfd, TIOCGWINSZ, &size);
153 if(ret < 0) abort( "ioctl error: (%d) %s\n", (int)errno, strerror(errno) );
154 max_width = size.ws_col;
155 }
156
157 fprintf(out, "Usage:\n %s %s\n", cmd, help);
158
159 for(i; opt_count) {
160 printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help);
161 }
162 fprintf(out, " -%c, --%-*s %s\n", 'h', width, "help", "print this help message");
163 exit(out == stdout ? 0 : 1);
164}
165
166//-----------------------------------------------------------------------------
167// Typed argument parsing
168bool parse_yesno(const char * arg, bool & value ) {
169 if(strcmp(arg, "yes") == 0) {
170 value = true;
171 return true;
172 }
173
174 if(strcmp(arg, "no") == 0) {
175 value = false;
176 return true;
177 }
178
179 return false;
180}
181
182bool parse_settrue (const char *, bool & value ) {
183 value = true;
184 return true;
185}
186
187bool parse_setfalse(const char *, bool & value ) {
188 value = false;
189 return true;
190}
191
192bool parse(const char * arg, const char * & value ) {
193 value = arg;
194 return true;
195}
196
197bool parse(const char * arg, int & value) {
198 char * end;
199 int r = strtoll(arg, &end, 10);
200 if(*end != '\0') return false;
201
202 value = r;
203 return true;
204}
205
206bool parse(const char * arg, unsigned & value) {
207 char * end;
208 unsigned long long int r = strtoull(arg, &end, 10);
209 if(*end != '\0') return false;
210 if(r > (unsigned)MAX) return false;
211
212 value = r;
213 return true;
214}
215
216bool parse(const char * arg, unsigned long & value) {
217 char * end;
218 unsigned long long int r = strtoull(arg, &end, 10);
219 if(*end != '\0') return false;
220 if(r > (unsigned long)MAX) return false;
221
222 value = r;
223 return true;
224}
225
226bool parse(const char * arg, unsigned long long & value) {
227 char * end;
228 unsigned long long int r = strtoull(arg, &end, 10);
229 if(*end != '\0') return false;
230 if(r > (unsigned long long)MAX) return false;
231
232 value = r;
233 return true;
234}
235
236bool parse(const char * arg, double & value) {
237 char * end;
238 double r = strtod(arg, &end);
239 if(*end != '\0') return false;
240
241 value = r;
242 return true;
243}
Note: See TracBrowser for help on using the repository browser.