source: benchmark/io/http/parseargs.cfa@ ffa48a8

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 ffa48a8 was 53e4562, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added options to list files instead of running the server

  • Property mode set to 100644
File size: 3.6 KB
Line 
1#include "parseargs.hfa"
2
3// #include <stdio.h>
4// #include <stdlib.h>
5#include <string.h>
6extern "C" {
7 #include <getopt.h>
8
9 struct FILE;
10 extern FILE * stderr;
11 extern FILE * stdout;
12
13 extern int fprintf ( FILE * stream, const char * format, ... );
14
15 extern long long int strtoll (const char* str, char** endptr, int base);
16 extern unsigned long long int strtoull(const char* str, char** endptr, int base);
17}
18
19void parse_args(
20 int argc,
21 char * argv[],
22 cfa_option options[],
23 size_t opt_count,
24 const char * usage,
25 char ** & left
26) {
27 struct option optarr[opt_count + 2];
28 int width = 0;
29 {
30 int idx = 0;
31 for(i; opt_count) {
32 if(options[i].long_name) {
33 optarr[idx].name = options[i].long_name;
34 optarr[idx].flag = 0p;
35 optarr[idx].val = options[i].short_name;
36 if( ((intptr_t)options[i].parse) == ((intptr_t)parse_settrue)
37 || ((intptr_t)options[i].parse) == ((intptr_t)parse_setfalse) ) {
38 optarr[idx].has_arg = no_argument;
39 } else {
40 optarr[idx].has_arg = required_argument;
41 }
42 idx++;
43
44 int w = strlen(options[i].long_name);
45 if(w > width) width = w;
46 }
47 }
48 optarr[idx+0].[name, has_arg, flag, val] = ["help", no_argument, 0p, 'h'];
49 optarr[idx+1].[name, has_arg, flag, val] = [0p, no_argument, 0p, 0];
50 }
51
52 char optstring[opt_count * 3] = { '\0' };
53 {
54 int idx = 0;
55 for(i; opt_count) {
56 optstring[idx] = options[i].short_name;
57 idx++;
58 if( ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue)
59 && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) {
60 optstring[idx] = ':';
61 idx++;
62 }
63 }
64 optstring[idx+0] = 'h';
65 optstring[idx+1] = '\0';
66 }
67
68 FILE * out = stderr;
69 NEXT_ARG:
70 for() {
71 int idx = 0;
72 int opt = getopt_long(argc, argv, optstring, optarr, &idx);
73 switch(opt) {
74 case -1:
75 if(&left != 0p) left = argv + optind;
76 return;
77 case 'h':
78 out = stdout;
79 case '?':
80 goto USAGE;
81 default:
82 for(i; opt_count) {
83 if(opt == options[i].short_name) {
84 const char * arg = optarg ? optarg : "";
85 bool success = options[i].parse( arg, options[i].variable );
86 if(success) continue NEXT_ARG;
87
88 fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt);
89 goto USAGE;
90 }
91 }
92 abort("Internal parse arg error\n");
93 }
94
95 }
96
97 USAGE:
98 fprintf(out, "Usage:\n %s %s\n", argv[0], usage);
99
100 for(i; opt_count) {
101 fprintf(out, " -%c, --%-*s %s\n", options[i].short_name, width, options[i].long_name, options[i].help);
102 }
103 fprintf(out, " -%c, --%-*s %s\n", 'h', width, "help", "print this help message");
104 exit(out == stdout ? 0 : 1);
105}
106
107bool parse_yesno(const char * arg, bool & value ) {
108 if(strcmp(arg, "yes") == 0) {
109 value = true;
110 return true;
111 }
112
113 if(strcmp(arg, "no") == 0) {
114 value = false;
115 return true;
116 }
117
118 return false;
119}
120
121bool parse_settrue (const char *, bool & value ) {
122 value = true;
123 return true;
124}
125
126bool parse_setfalse(const char *, bool & value ) {
127 value = false;
128 return true;
129}
130
131bool parse(const char * arg, const char * & value ) {
132 value = arg;
133 return true;
134}
135
136bool parse(const char * arg, unsigned & value) {
137 char * end;
138 unsigned long long int r = strtoull(arg, &end, 10);
139 if(*end != '\0') return false;
140#warning not checking max
141
142 value = r;
143 return true;
144}
145
146bool parse(const char * arg, size_t & value) {
147 char * end;
148 unsigned long long int r = strtoull(arg, &end, 10);
149 if(*end != '\0') return false;
150#warning not checking max
151
152 value = r;
153 return true;
154}
155
156bool parse(const char * arg, int & value) {
157 char * end;
158 int r = strtoll(arg, &end, 10);
159 if(*end != '\0') return false;
160
161 value = r;
162 return true;
163}
Note: See TracBrowser for help on using the repository browser.