source: libcfa/src/parseargs.cfa @ 06573b2

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 06573b2 was 7efb322, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

fixed bad copy/paste

  • Property mode set to 100644
File size: 6.5 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 __attribute__((weak));
28extern char ** cfa_args_argv __attribute__((weak));
29extern char ** cfa_args_envp __attribute__((weak));
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        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        }
41}
42
43//-----------------------------------------------------------------------------
44// getopt_long wrapping
45void 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                }
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];
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 '?':
102                                usage(argv[0], options, opt_count, usage, out);
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);
111                                                usage(argv[0], options, opt_count, usage, out);
112                                        }
113                                }
114                                abort("Internal parse arg error\n");
115                }
116
117        }
118}
119
120//-----------------------------------------------------------------------------
121// Print usage
122static 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
134void 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
138void 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
142static 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;
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
162        fprintf(out, "Usage:\n  %s %s\n", cmd, help);
163
164        for(i; opt_count) {
165                printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help);
166        }
167        fprintf(out, "  -%c, --%-*s   %s\n", 'h', width, "help", "print this help message");
168        exit(out == stdout ? 0 : 1);
169}
170
171//-----------------------------------------------------------------------------
172// Typed argument parsing
173bool 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
187bool parse_truefalse(const char * arg, bool & value) {
188        if(strcmp(arg, "true") == 0) {
189                value = true;
190                return true;
191        }
192
193        if(strcmp(arg, "false") == 0) {
194                value = false;
195                return true;
196        }
197
198        return false;
199}
200
201bool parse_settrue (const char *, bool & value ) {
202        value = true;
203        return true;
204}
205
206bool parse_setfalse(const char *, bool & value )  {
207        value = false;
208        return true;
209}
210
211bool parse(const char * arg, const char * & value ) {
212        value = arg;
213        return true;
214}
215
216bool parse(const char * arg, int & value) {
217        char * end;
218        int r = strtoll(arg, &end, 10);
219        if(*end != '\0') return false;
220
221        value = r;
222        return true;
223}
224
225bool parse(const char * arg, unsigned & value) {
226        char * end;
227        unsigned long long int r = strtoull(arg, &end, 10);
228        if(*end != '\0') return false;
229        if(r > (unsigned)MAX) return false;
230
231        value = r;
232        return true;
233}
234
235bool parse(const char * arg, unsigned long & value) {
236        char * end;
237        unsigned long long int r = strtoull(arg, &end, 10);
238        if(*end != '\0') return false;
239        if(r > (unsigned long)MAX) return false;
240
241        value = r;
242        return true;
243}
244
245bool parse(const char * arg, unsigned long long & value) {
246        char * end;
247        unsigned long long int r = strtoull(arg, &end, 10);
248        if(*end != '\0') return false;
249        if(r > (unsigned long long)MAX) return false;
250
251        value = r;
252        return true;
253}
254
255bool parse(const char * arg, double & value) {
256        char * end;
257        double r = strtod(arg, &end);
258        if(*end != '\0') return false;
259
260        value = r;
261        return true;
262}
Note: See TracBrowser for help on using the repository browser.