Commit | Line | Data |
---|---|---|
cb2c86fd JA |
1 | /* |
2 | * This file contains the ini and command liner parser main. | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <ctype.h> | |
8 | #include <string.h> | |
9 | #include <errno.h> | |
10 | #include <limits.h> | |
11 | ||
12 | #include "parse.h" | |
13 | ||
14 | static unsigned long get_mult_time(char c) | |
15 | { | |
16 | switch (c) { | |
17 | case 'm': | |
18 | case 'M': | |
19 | return 60; | |
20 | case 'h': | |
21 | case 'H': | |
22 | return 60 * 60; | |
23 | case 'd': | |
24 | case 'D': | |
25 | return 24 * 60 * 60; | |
26 | default: | |
27 | return 1; | |
28 | } | |
29 | } | |
30 | ||
31 | static unsigned long get_mult_bytes(char c) | |
32 | { | |
33 | switch (c) { | |
34 | case 'k': | |
35 | case 'K': | |
36 | return 1024; | |
37 | case 'm': | |
38 | case 'M': | |
39 | return 1024 * 1024; | |
40 | case 'g': | |
41 | case 'G': | |
42 | return 1024 * 1024 * 1024; | |
43 | default: | |
44 | return 1; | |
45 | } | |
46 | } | |
47 | ||
48 | /* | |
e1f36503 | 49 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 50 | */ |
b4692828 | 51 | static int str_to_decimal(const char *str, unsigned long long *val, int kilo) |
cb2c86fd | 52 | { |
cb2c86fd JA |
53 | int len; |
54 | ||
cb2c86fd JA |
55 | len = strlen(str); |
56 | ||
57 | *val = strtoul(str, NULL, 10); | |
58 | if (*val == ULONG_MAX && errno == ERANGE) | |
59 | return 1; | |
60 | ||
61 | if (kilo) | |
62 | *val *= get_mult_bytes(str[len - 1]); | |
63 | else | |
64 | *val *= get_mult_time(str[len - 1]); | |
65 | return 0; | |
66 | } | |
67 | ||
b4692828 | 68 | static int check_str_bytes(const char *p, unsigned long long *val) |
cb2c86fd | 69 | { |
cb2c86fd JA |
70 | return str_to_decimal(p, val, 1); |
71 | } | |
72 | ||
b4692828 | 73 | static int check_str_time(const char *p, unsigned long long *val) |
cb2c86fd | 74 | { |
cb2c86fd JA |
75 | return str_to_decimal(p, val, 0); |
76 | } | |
77 | ||
78 | void strip_blank_front(char **p) | |
79 | { | |
80 | char *s = *p; | |
81 | ||
82 | while (isspace(*s)) | |
83 | s++; | |
84 | } | |
85 | ||
86 | void strip_blank_end(char *p) | |
87 | { | |
88 | char *s = p + strlen(p) - 1; | |
89 | ||
90 | while (isspace(*s) || iscntrl(*s)) | |
91 | s--; | |
92 | ||
93 | *(s + 1) = '\0'; | |
94 | } | |
95 | ||
b765a372 | 96 | static int check_range_bytes(const char *str, unsigned long *val) |
cb2c86fd JA |
97 | { |
98 | char suffix; | |
99 | ||
100 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { | |
101 | *val *= get_mult_bytes(suffix); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | if (sscanf(str, "%lu", val) == 1) | |
106 | return 0; | |
107 | ||
108 | return 1; | |
109 | } | |
110 | ||
b4692828 | 111 | static int check_int(const char *p, unsigned int *val) |
cb2c86fd | 112 | { |
e1f36503 JA |
113 | if (sscanf(p, "%u", val) == 1) |
114 | return 0; | |
cb2c86fd | 115 | |
e1f36503 JA |
116 | return 1; |
117 | } | |
cb2c86fd | 118 | |
e1f36503 JA |
119 | static struct fio_option *find_option(struct fio_option *options, |
120 | const char *opt) | |
121 | { | |
33963c6c | 122 | struct fio_option *o = &options[0]; |
cb2c86fd | 123 | |
33963c6c | 124 | while (o->name) { |
e1f36503 JA |
125 | if (!strcmp(o->name, opt)) |
126 | return o; | |
cb2c86fd | 127 | |
33963c6c JA |
128 | o++; |
129 | } | |
cb2c86fd | 130 | |
e1f36503 | 131 | return NULL; |
cb2c86fd JA |
132 | } |
133 | ||
b4692828 | 134 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
cb2c86fd | 135 | { |
75e6f36f | 136 | unsigned int il, *ilp1, *ilp2; |
e1f36503 | 137 | unsigned long long ull, *ullp; |
75e6f36f | 138 | unsigned long ul1, ul2; |
b4692828 | 139 | char **cp; |
e1f36503 | 140 | int ret = 0, is_time = 0; |
cb2c86fd | 141 | |
e1f36503 JA |
142 | switch (o->type) { |
143 | case FIO_OPT_STR: { | |
144 | fio_opt_str_fn *fn = o->cb; | |
cb2c86fd | 145 | |
e1f36503 JA |
146 | ret = fn(data, ptr); |
147 | break; | |
148 | } | |
149 | case FIO_OPT_STR_VAL_TIME: | |
150 | is_time = 1; | |
75e6f36f JA |
151 | case FIO_OPT_STR_VAL: |
152 | case FIO_OPT_STR_VAL_INT: { | |
e1f36503 JA |
153 | fio_opt_str_val_fn *fn = o->cb; |
154 | ||
155 | if (is_time) | |
156 | ret = check_str_time(ptr, &ull); | |
157 | else | |
158 | ret = check_str_bytes(ptr, &ull); | |
159 | ||
160 | if (ret) | |
161 | break; | |
162 | ||
163 | if (o->max_val && ull > o->max_val) | |
164 | ull = o->max_val; | |
165 | ||
166 | if (fn) | |
167 | ret = fn(data, &ull); | |
168 | else { | |
75e6f36f JA |
169 | if (o->type == FIO_OPT_STR_VAL_INT) { |
170 | ilp1 = td_var(data, o->off1); | |
171 | *ilp1 = ull; | |
172 | } else { | |
173 | ullp = td_var(data, o->off1); | |
174 | *ullp = ull; | |
175 | } | |
e1f36503 JA |
176 | } |
177 | break; | |
178 | } | |
179 | case FIO_OPT_STR_STORE: | |
180 | cp = td_var(data, o->off1); | |
181 | *cp = strdup(ptr); | |
182 | break; | |
183 | case FIO_OPT_RANGE: { | |
b765a372 | 184 | char tmp[128]; |
e1f36503 JA |
185 | char *p1, *p2; |
186 | ||
0bbab0e7 | 187 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 JA |
188 | |
189 | p1 = strchr(tmp, '-'); | |
e1f36503 JA |
190 | if (!p1) { |
191 | ret = 1; | |
192 | break; | |
193 | } | |
194 | ||
195 | p2 = p1 + 1; | |
196 | *p1 = '\0'; | |
b765a372 | 197 | p1 = tmp; |
e1f36503 JA |
198 | |
199 | ret = 1; | |
200 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { | |
201 | ret = 0; | |
75e6f36f JA |
202 | ilp1 = td_var(data, o->off1); |
203 | ilp2 = td_var(data, o->off2); | |
e1f36503 | 204 | if (ul1 > ul2) { |
75e6f36f JA |
205 | *ilp1 = ul2; |
206 | *ilp2 = ul1; | |
e1f36503 | 207 | } else { |
75e6f36f JA |
208 | *ilp2 = ul2; |
209 | *ilp1 = ul1; | |
e1f36503 JA |
210 | } |
211 | } | |
212 | ||
213 | break; | |
214 | } | |
215 | case FIO_OPT_INT: { | |
216 | fio_opt_int_fn *fn = o->cb; | |
217 | ||
218 | ret = check_int(ptr, &il); | |
219 | if (ret) | |
220 | break; | |
221 | ||
222 | if (o->max_val && il > o->max_val) | |
223 | il = o->max_val; | |
224 | ||
225 | if (fn) | |
226 | ret = fn(data, &il); | |
227 | else { | |
75e6f36f JA |
228 | ilp1 = td_var(data, o->off1); |
229 | *ilp1 = il; | |
e1f36503 JA |
230 | } |
231 | break; | |
232 | } | |
233 | case FIO_OPT_STR_SET: { | |
234 | fio_opt_str_set_fn *fn = o->cb; | |
235 | ||
236 | if (fn) | |
237 | ret = fn(data); | |
238 | else { | |
75e6f36f JA |
239 | ilp1 = td_var(data, o->off1); |
240 | *ilp1 = 1; | |
e1f36503 JA |
241 | } |
242 | break; | |
243 | } | |
244 | default: | |
245 | fprintf(stderr, "Bad option type %d\n", o->type); | |
246 | ret = 1; | |
247 | } | |
cb2c86fd | 248 | |
e1f36503 | 249 | return ret; |
cb2c86fd JA |
250 | } |
251 | ||
b4692828 JA |
252 | int parse_cmd_option(const char *opt, const char *val, |
253 | struct fio_option *options, void *data) | |
254 | { | |
255 | struct fio_option *o; | |
256 | ||
257 | o = find_option(options, opt); | |
258 | if (!o) { | |
259 | fprintf(stderr, "Bad option %s\n", opt); | |
260 | return 1; | |
261 | } | |
262 | ||
b1508cf9 JA |
263 | if (!handle_option(o, val, data)) |
264 | return 0; | |
265 | ||
266 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); | |
267 | return 1; | |
b4692828 JA |
268 | } |
269 | ||
e1f36503 | 270 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 271 | { |
b4692828 | 272 | struct fio_option *o; |
e1f36503 JA |
273 | char *pre, *post; |
274 | char tmp[64]; | |
275 | ||
0bbab0e7 | 276 | strncpy(tmp, opt, sizeof(tmp) - 1); |
e1f36503 JA |
277 | |
278 | pre = strchr(tmp, '='); | |
279 | if (pre) { | |
280 | post = pre; | |
281 | *pre = '\0'; | |
282 | pre = tmp; | |
283 | post++; | |
284 | o = find_option(options, pre); | |
285 | } else { | |
286 | o = find_option(options, tmp); | |
287 | post = NULL; | |
288 | } | |
cb2c86fd | 289 | |
e1f36503 JA |
290 | if (!o) { |
291 | fprintf(stderr, "Bad option %s\n", tmp); | |
292 | return 1; | |
293 | } | |
294 | ||
b1508cf9 JA |
295 | if (!handle_option(o, post, data)) |
296 | return 0; | |
297 | ||
298 | fprintf(stderr, "fio: failed parsing %s\n", opt); | |
299 | return 1; | |
e1f36503 | 300 | } |