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 | */ |
63f29372 | 51 | static int str_to_decimal(const char *str, long long *val, int kilo) |
cb2c86fd | 52 | { |
cb2c86fd JA |
53 | int len; |
54 | ||
cb2c86fd | 55 | len = strlen(str); |
f90eff5a JA |
56 | if (!len) |
57 | return 1; | |
cb2c86fd | 58 | |
cda866ca JA |
59 | *val = strtol(str, NULL, 10); |
60 | if (*val == LONG_MAX && errno == ERANGE) | |
cb2c86fd JA |
61 | return 1; |
62 | ||
63 | if (kilo) | |
64 | *val *= get_mult_bytes(str[len - 1]); | |
65 | else | |
66 | *val *= get_mult_time(str[len - 1]); | |
787f7e95 | 67 | |
cb2c86fd JA |
68 | return 0; |
69 | } | |
70 | ||
63f29372 | 71 | static int check_str_bytes(const char *p, long long *val) |
cb2c86fd | 72 | { |
cb2c86fd JA |
73 | return str_to_decimal(p, val, 1); |
74 | } | |
75 | ||
63f29372 | 76 | static int check_str_time(const char *p, long long *val) |
cb2c86fd | 77 | { |
cb2c86fd JA |
78 | return str_to_decimal(p, val, 0); |
79 | } | |
80 | ||
81 | void strip_blank_front(char **p) | |
82 | { | |
83 | char *s = *p; | |
84 | ||
85 | while (isspace(*s)) | |
86 | s++; | |
87 | } | |
88 | ||
89 | void strip_blank_end(char *p) | |
90 | { | |
91 | char *s = p + strlen(p) - 1; | |
92 | ||
93 | while (isspace(*s) || iscntrl(*s)) | |
94 | s--; | |
95 | ||
96 | *(s + 1) = '\0'; | |
97 | } | |
98 | ||
63f29372 | 99 | static int check_range_bytes(const char *str, long *val) |
cb2c86fd JA |
100 | { |
101 | char suffix; | |
102 | ||
787f7e95 JA |
103 | if (!strlen(str)) |
104 | return 1; | |
105 | ||
cb2c86fd JA |
106 | if (sscanf(str, "%lu%c", val, &suffix) == 2) { |
107 | *val *= get_mult_bytes(suffix); | |
108 | return 0; | |
109 | } | |
110 | ||
111 | if (sscanf(str, "%lu", val) == 1) | |
112 | return 0; | |
113 | ||
114 | return 1; | |
115 | } | |
116 | ||
63f29372 | 117 | static int check_int(const char *p, int *val) |
cb2c86fd | 118 | { |
787f7e95 JA |
119 | if (!strlen(p)) |
120 | return 1; | |
e1f36503 JA |
121 | if (sscanf(p, "%u", val) == 1) |
122 | return 0; | |
cb2c86fd | 123 | |
e1f36503 JA |
124 | return 1; |
125 | } | |
cb2c86fd | 126 | |
e1f36503 JA |
127 | static struct fio_option *find_option(struct fio_option *options, |
128 | const char *opt) | |
129 | { | |
4945ba12 | 130 | struct fio_option *o; |
cb2c86fd | 131 | |
4945ba12 | 132 | for (o = &options[0]; o->name; o++) { |
e1f36503 JA |
133 | if (!strcmp(o->name, opt)) |
134 | return o; | |
03b74b3e JA |
135 | else if (o->alias && !strcmp(o->alias, opt)) |
136 | return o; | |
33963c6c | 137 | } |
cb2c86fd | 138 | |
e1f36503 | 139 | return NULL; |
cb2c86fd JA |
140 | } |
141 | ||
17abbe89 JA |
142 | #define val_store(ptr, val, off, data) \ |
143 | do { \ | |
144 | ptr = td_var((data), (off)); \ | |
145 | *ptr = (val); \ | |
146 | } while (0) | |
147 | ||
f90eff5a | 148 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
787f7e95 | 149 | int first, int more) |
cb2c86fd | 150 | { |
63f29372 JA |
151 | int il, *ilp; |
152 | long long ull, *ullp; | |
153 | long ul1, ul2; | |
b4692828 | 154 | char **cp; |
e1f36503 | 155 | int ret = 0, is_time = 0; |
cb2c86fd | 156 | |
08e26e35 JA |
157 | if (!ptr && o->type != FIO_OPT_STR_SET) { |
158 | fprintf(stderr, "Option %s requires an argument\n", o->name); | |
159 | return 1; | |
160 | } | |
161 | ||
e1f36503 JA |
162 | switch (o->type) { |
163 | case FIO_OPT_STR: { | |
164 | fio_opt_str_fn *fn = o->cb; | |
cb2c86fd | 165 | |
e1f36503 JA |
166 | ret = fn(data, ptr); |
167 | break; | |
168 | } | |
169 | case FIO_OPT_STR_VAL_TIME: | |
170 | is_time = 1; | |
75e6f36f JA |
171 | case FIO_OPT_STR_VAL: |
172 | case FIO_OPT_STR_VAL_INT: { | |
e1f36503 JA |
173 | fio_opt_str_val_fn *fn = o->cb; |
174 | ||
175 | if (is_time) | |
176 | ret = check_str_time(ptr, &ull); | |
177 | else | |
178 | ret = check_str_bytes(ptr, &ull); | |
179 | ||
180 | if (ret) | |
181 | break; | |
182 | ||
db8e0165 JA |
183 | if (o->maxval && ull > o->maxval) { |
184 | fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval); | |
185 | return 1; | |
186 | } | |
187 | if (o->minval && ull < o->minval) { | |
188 | fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval); | |
189 | return 1; | |
190 | } | |
e1f36503 JA |
191 | |
192 | if (fn) | |
193 | ret = fn(data, &ull); | |
194 | else { | |
75e6f36f | 195 | if (o->type == FIO_OPT_STR_VAL_INT) { |
17abbe89 JA |
196 | if (first) |
197 | val_store(ilp, ull, o->off1, data); | |
787f7e95 | 198 | if (!more && o->off2) |
17abbe89 | 199 | val_store(ilp, ull, o->off2, data); |
75e6f36f | 200 | } else { |
17abbe89 JA |
201 | if (first) |
202 | val_store(ullp, ull, o->off1, data); | |
787f7e95 | 203 | if (!more && o->off2) |
17abbe89 | 204 | val_store(ullp, ull, o->off2, data); |
75e6f36f | 205 | } |
e1f36503 JA |
206 | } |
207 | break; | |
208 | } | |
209 | case FIO_OPT_STR_STORE: | |
210 | cp = td_var(data, o->off1); | |
211 | *cp = strdup(ptr); | |
212 | break; | |
213 | case FIO_OPT_RANGE: { | |
b765a372 | 214 | char tmp[128]; |
e1f36503 JA |
215 | char *p1, *p2; |
216 | ||
0bbab0e7 | 217 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 JA |
218 | |
219 | p1 = strchr(tmp, '-'); | |
e1f36503 JA |
220 | if (!p1) { |
221 | ret = 1; | |
222 | break; | |
223 | } | |
224 | ||
225 | p2 = p1 + 1; | |
226 | *p1 = '\0'; | |
b765a372 | 227 | p1 = tmp; |
e1f36503 JA |
228 | |
229 | ret = 1; | |
230 | if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) { | |
231 | ret = 0; | |
e1f36503 | 232 | if (ul1 > ul2) { |
f90eff5a JA |
233 | unsigned long foo = ul1; |
234 | ||
235 | ul1 = ul2; | |
236 | ul2 = foo; | |
237 | } | |
238 | ||
239 | if (first) { | |
17abbe89 JA |
240 | val_store(ilp, ul1, o->off1, data); |
241 | val_store(ilp, ul2, o->off2, data); | |
242 | } | |
787f7e95 | 243 | if (!more && o->off3 && o->off4) { |
17abbe89 JA |
244 | val_store(ilp, ul1, o->off3, data); |
245 | val_store(ilp, ul2, o->off4, data); | |
e1f36503 | 246 | } |
17abbe89 JA |
247 | } |
248 | ||
e1f36503 JA |
249 | break; |
250 | } | |
13335ddb JA |
251 | case FIO_OPT_INT: |
252 | case FIO_OPT_BOOL: { | |
e1f36503 JA |
253 | fio_opt_int_fn *fn = o->cb; |
254 | ||
255 | ret = check_int(ptr, &il); | |
256 | if (ret) | |
257 | break; | |
258 | ||
db8e0165 JA |
259 | if (o->maxval && il > (int) o->maxval) { |
260 | fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval); | |
261 | return 1; | |
262 | } | |
263 | if (o->minval && il < o->minval) { | |
264 | fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval); | |
265 | return 1; | |
266 | } | |
e1f36503 | 267 | |
76a43db4 JA |
268 | if (o->neg) |
269 | il = !il; | |
270 | ||
e1f36503 JA |
271 | if (fn) |
272 | ret = fn(data, &il); | |
273 | else { | |
17abbe89 JA |
274 | if (first) |
275 | val_store(ilp, il, o->off1, data); | |
787f7e95 | 276 | if (!more && o->off2) |
17abbe89 | 277 | val_store(ilp, il, o->off2, data); |
e1f36503 JA |
278 | } |
279 | break; | |
280 | } | |
281 | case FIO_OPT_STR_SET: { | |
282 | fio_opt_str_set_fn *fn = o->cb; | |
283 | ||
284 | if (fn) | |
285 | ret = fn(data); | |
286 | else { | |
17abbe89 JA |
287 | if (first) |
288 | val_store(ilp, 1, o->off1, data); | |
787f7e95 | 289 | if (!more && o->off2) |
17abbe89 | 290 | val_store(ilp, 1, o->off2, data); |
e1f36503 JA |
291 | } |
292 | break; | |
293 | } | |
294 | default: | |
1e97cce9 | 295 | fprintf(stderr, "Bad option type %u\n", o->type); |
e1f36503 JA |
296 | ret = 1; |
297 | } | |
cb2c86fd | 298 | |
e1f36503 | 299 | return ret; |
cb2c86fd JA |
300 | } |
301 | ||
f90eff5a JA |
302 | static int handle_option(struct fio_option *o, const char *ptr, void *data) |
303 | { | |
92b586f8 | 304 | const char *ptr2 = NULL; |
787f7e95 | 305 | int r1, r2; |
f90eff5a JA |
306 | |
307 | /* | |
787f7e95 JA |
308 | * See if we have a second set of parameters, hidden after a comma. |
309 | * Do this before parsing the first round, to check if we should | |
310 | * copy set 1 options to set 2. | |
f90eff5a | 311 | */ |
92b586f8 JA |
312 | if (ptr) |
313 | ptr2 = strchr(ptr, ','); | |
787f7e95 JA |
314 | |
315 | /* | |
316 | * Don't return early if parsing the first option fails - if | |
317 | * we are doing multiple arguments, we can allow the first one | |
318 | * being empty. | |
319 | */ | |
320 | r1 = __handle_option(o, ptr, data, 1, !!ptr2); | |
321 | ||
f90eff5a | 322 | if (!ptr2) |
787f7e95 | 323 | return r1; |
f90eff5a JA |
324 | |
325 | ptr2++; | |
787f7e95 JA |
326 | r2 = __handle_option(o, ptr2, data, 0, 0); |
327 | ||
328 | return r1 && r2; | |
f90eff5a JA |
329 | } |
330 | ||
b4692828 JA |
331 | int parse_cmd_option(const char *opt, const char *val, |
332 | struct fio_option *options, void *data) | |
333 | { | |
334 | struct fio_option *o; | |
335 | ||
336 | o = find_option(options, opt); | |
337 | if (!o) { | |
338 | fprintf(stderr, "Bad option %s\n", opt); | |
339 | return 1; | |
340 | } | |
341 | ||
b1508cf9 JA |
342 | if (!handle_option(o, val, data)) |
343 | return 0; | |
344 | ||
345 | fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val); | |
346 | return 1; | |
b4692828 JA |
347 | } |
348 | ||
e1f36503 | 349 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 350 | { |
b4692828 | 351 | struct fio_option *o; |
e1f36503 JA |
352 | char *pre, *post; |
353 | char tmp[64]; | |
354 | ||
0bbab0e7 | 355 | strncpy(tmp, opt, sizeof(tmp) - 1); |
e1f36503 JA |
356 | |
357 | pre = strchr(tmp, '='); | |
358 | if (pre) { | |
359 | post = pre; | |
360 | *pre = '\0'; | |
361 | pre = tmp; | |
362 | post++; | |
363 | o = find_option(options, pre); | |
364 | } else { | |
365 | o = find_option(options, tmp); | |
366 | post = NULL; | |
367 | } | |
cb2c86fd | 368 | |
e1f36503 JA |
369 | if (!o) { |
370 | fprintf(stderr, "Bad option %s\n", tmp); | |
371 | return 1; | |
372 | } | |
373 | ||
b1508cf9 JA |
374 | if (!handle_option(o, post, data)) |
375 | return 0; | |
376 | ||
377 | fprintf(stderr, "fio: failed parsing %s\n", opt); | |
378 | return 1; | |
e1f36503 | 379 | } |
fd28ca49 | 380 | |
15f7918f JA |
381 | static void show_option_range(struct fio_option *o) |
382 | { | |
383 | if (!o->minval && !o->maxval) | |
384 | return; | |
385 | ||
386 | printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval); | |
387 | } | |
388 | ||
389 | static void show_option_values(struct fio_option *o) | |
390 | { | |
391 | const char *msg; | |
392 | int i = 0; | |
393 | ||
394 | if (!o->posval) | |
395 | return; | |
396 | ||
397 | do { | |
398 | msg = o->posval[i]; | |
399 | if (!msg) | |
400 | break; | |
401 | ||
402 | if (!i) | |
403 | printf("%16s: ", "valid values"); | |
404 | ||
405 | printf("%s,", msg); | |
406 | i++; | |
407 | } while (1); | |
408 | ||
409 | if (i) | |
410 | printf("\n"); | |
411 | } | |
412 | ||
fd28ca49 JA |
413 | int show_cmd_help(struct fio_option *options, const char *name) |
414 | { | |
415 | int show_all = !strcmp(name, "all"); | |
fd28ca49 JA |
416 | const char *typehelp[] = { |
417 | "string (opt=bla)", | |
418 | "string with possible k/m/g postfix (opt=4k)", | |
419 | "string with range and postfix (opt=1k-4k)", | |
420 | "string with time postfix (opt=10s)", | |
421 | "string (opt=bla)", | |
422 | "string with dual range (opt=1k-4k,4k-8k)", | |
423 | "integer value (opt=100)", | |
13335ddb | 424 | "boolean value (opt=1)", |
fd28ca49 JA |
425 | "no argument (opt)", |
426 | }; | |
4945ba12 | 427 | struct fio_option *o; |
29fc6afe | 428 | int found = 0; |
fd28ca49 | 429 | |
4945ba12 | 430 | for (o = &options[0]; o->name; o++) { |
fd28ca49 JA |
431 | int match = !strcmp(name, o->name); |
432 | ||
433 | if (show_all || match) { | |
29fc6afe | 434 | found = 1; |
facba0e5 | 435 | printf("%16s: %s\n", o->name, o->help); |
4945ba12 JA |
436 | if (show_all) |
437 | continue; | |
fd28ca49 JA |
438 | } |
439 | ||
4945ba12 JA |
440 | printf("%16s: %s\n", "type", typehelp[o->type]); |
441 | printf("%16s: %s\n", "default", o->def ? o->def : "no default"); | |
442 | show_option_range(o); | |
443 | show_option_values(o); | |
fd28ca49 JA |
444 | } |
445 | ||
29fc6afe JA |
446 | if (found) |
447 | return 0; | |
fd28ca49 | 448 | |
29fc6afe JA |
449 | printf("No such command: %s\n", name); |
450 | return 1; | |
fd28ca49 | 451 | } |
ee738499 | 452 | |
13335ddb JA |
453 | /* |
454 | * Handle parsing of default parameters. | |
455 | */ | |
ee738499 JA |
456 | void fill_default_options(void *data, struct fio_option *options) |
457 | { | |
4945ba12 | 458 | struct fio_option *o; |
ee738499 | 459 | |
4945ba12 | 460 | for (o = &options[0]; o->name; o++) |
ee738499 JA |
461 | if (o->def) |
462 | handle_option(o, o->def, data); | |
ee738499 | 463 | } |
13335ddb JA |
464 | |
465 | /* | |
466 | * Sanitize the options structure. For now it just sets min/max for bool | |
5b0a8880 | 467 | * values and whether both callback and offsets are given. |
13335ddb JA |
468 | */ |
469 | void options_init(struct fio_option *options) | |
470 | { | |
4945ba12 | 471 | struct fio_option *o; |
13335ddb | 472 | |
4945ba12 | 473 | for (o = &options[0]; o->name; o++) { |
13335ddb JA |
474 | if (o->type == FIO_OPT_BOOL) { |
475 | o->minval = 0; | |
476 | o->maxval = 1; | |
477 | } | |
5b0a8880 JA |
478 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) |
479 | fprintf(stderr, "Option %s: both cb and offset given\n", o->name); | |
13335ddb JA |
480 | } |
481 | } |