source: libcfa/src/parseargs.cfa @ 419c434

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 419c434 was 419c434, checked in by Thierry Delisle <tdelisle@…>, 4 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
RevLine 
[7f389a5c]1#include "parseargs.hfa"
2
[e699eb6]3#include <stdint.h>
[7f389a5c]4#include <string.h>
[e699eb6]5#include <errno.h>
[3f1d9b5]6#include <unistd.h>
[7f389a5c]7extern "C" {
8        #include <getopt.h>
[3f1d9b5]9        #include <sys/ioctl.h>
[7f389a5c]10
11        struct FILE;
12        extern FILE * stderr;
13        extern FILE * stdout;
14
[3f1d9b5]15        extern int fileno(FILE *stream);
16
[7f389a5c]17        extern int fprintf ( FILE * stream, const char * format, ... );
[53e4562]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);
[7f6e9eb]21        extern                 double strtod  (const char* str, char** endptr);
[7f389a5c]22}
23
[e699eb6]24#include "common.hfa"
25#include "limits.hfa"
[3f1d9b5]26
[7874d77]27extern int cfa_args_argc;
28extern char ** cfa_args_argv;
29extern char ** cfa_args_envp;
30
[419c434]31static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out)  __attribute__ ((noreturn));
[3f1d9b5]32
[7874d77]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
[419c434]37//-----------------------------------------------------------------------------
38// getopt_long wrapping
[7f389a5c]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                }
[e699eb6]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];
[7f389a5c]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 '?':
[419c434]96                                usage(argv[0], options, opt_count, usage, out);
[7f389a5c]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);
[419c434]105                                                usage(argv[0], options, opt_count, usage, out);
[7f389a5c]106                                        }
107                                }
108                                abort("Internal parse arg error\n");
109                }
110
111        }
[419c434]112}
[7f389a5c]113
[419c434]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;
[3f1d9b5]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
[419c434]157        fprintf(out, "Usage:\n  %s %s\n", cmd, help);
[7f389a5c]158
159        for(i; opt_count) {
[3f1d9b5]160                printopt(out, width, max_width, options[i].short_name, options[i].long_name, options[i].help);
[7f389a5c]161        }
162        fprintf(out, "  -%c, --%-*s   %s\n", 'h', width, "help", "print this help message");
163        exit(out == stdout ? 0 : 1);
164}
165
[419c434]166//-----------------------------------------------------------------------------
167// Typed argument parsing
[7f389a5c]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
[7f6e9eb]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
[53e4562]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;
[3f1d9b5]210        if(r > (unsigned)MAX) return false;
[53e4562]211
212        value = r;
213        return true;
214}
215
[56e8cb3]216bool parse(const char * arg, unsigned long & value) {
[53e4562]217        char * end;
218        unsigned long long int r = strtoull(arg, &end, 10);
219        if(*end != '\0') return false;
[56e8cb3]220        if(r > (unsigned long)MAX) return false;
[53e4562]221
222        value = r;
223        return true;
224}
225
[56e8cb3]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
[7f6e9eb]236bool parse(const char * arg, double & value) {
[7f389a5c]237        char * end;
[7f6e9eb]238        double r = strtod(arg, &end);
[7f389a5c]239        if(*end != '\0') return false;
240
241        value = r;
242        return true;
[56e8cb3]243}
Note: See TracBrowser for help on using the repository browser.