Accept '-' as siint delimter as well.
[fio.git] / parse.c
CommitLineData
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
b1ec1da6
JA
14static void show_option_range(struct fio_option *o)
15{
16 if (!o->minval && !o->maxval)
17 return;
18
ec1aee01 19 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
b1ec1da6
JA
20}
21
22static void show_option_values(struct fio_option *o)
23{
b1ec1da6
JA
24 int i = 0;
25
26 do {
7837213b 27 const struct value_pair *vp = &o->posval[i];
b1ec1da6 28
7837213b
JA
29 if (!vp->ival)
30 break;
b1ec1da6 31
7837213b
JA
32 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
33 if (vp->help)
34 printf(" %s", vp->help);
35 printf("\n");
b1ec1da6
JA
36 i++;
37 } while (i < PARSE_MAX_VP);
38
39 if (i)
40 printf("\n");
41}
42
cb2c86fd
JA
43static unsigned long get_mult_time(char c)
44{
45 switch (c) {
46 case 'm':
47 case 'M':
48 return 60;
49 case 'h':
50 case 'H':
51 return 60 * 60;
52 case 'd':
53 case 'D':
54 return 24 * 60 * 60;
55 default:
56 return 1;
57 }
58}
59
60static unsigned long get_mult_bytes(char c)
61{
62 switch (c) {
63 case 'k':
64 case 'K':
65 return 1024;
66 case 'm':
67 case 'M':
68 return 1024 * 1024;
69 case 'g':
70 case 'G':
71 return 1024 * 1024 * 1024;
f3502ba2
JA
72 case 'e':
73 case 'E':
74 return 1024 * 1024 * 1024 * 1024UL;
cb2c86fd
JA
75 default:
76 return 1;
77 }
78}
79
80/*
e1f36503 81 * convert string into decimal value, noting any size suffix
cb2c86fd 82 */
63f29372 83static int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 84{
cb2c86fd
JA
85 int len;
86
cb2c86fd 87 len = strlen(str);
f90eff5a
JA
88 if (!len)
89 return 1;
cb2c86fd 90
675de85a 91 *val = strtoll(str, NULL, 10);
cda866ca 92 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
93 return 1;
94
95 if (kilo)
96 *val *= get_mult_bytes(str[len - 1]);
97 else
98 *val *= get_mult_time(str[len - 1]);
787f7e95 99
cb2c86fd
JA
100 return 0;
101}
102
63f29372 103static int check_str_bytes(const char *p, long long *val)
cb2c86fd 104{
cb2c86fd
JA
105 return str_to_decimal(p, val, 1);
106}
107
63f29372 108static int check_str_time(const char *p, long long *val)
cb2c86fd 109{
cb2c86fd
JA
110 return str_to_decimal(p, val, 0);
111}
112
113void strip_blank_front(char **p)
114{
115 char *s = *p;
116
117 while (isspace(*s))
118 s++;
119}
120
121void strip_blank_end(char *p)
122{
123 char *s = p + strlen(p) - 1;
124
125 while (isspace(*s) || iscntrl(*s))
126 s--;
127
128 *(s + 1) = '\0';
129}
130
63f29372 131static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
132{
133 char suffix;
134
787f7e95
JA
135 if (!strlen(str))
136 return 1;
137
cb2c86fd
JA
138 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
139 *val *= get_mult_bytes(suffix);
140 return 0;
141 }
142
143 if (sscanf(str, "%lu", val) == 1)
144 return 0;
145
146 return 1;
147}
148
63f29372 149static int check_int(const char *p, int *val)
cb2c86fd 150{
787f7e95
JA
151 if (!strlen(p))
152 return 1;
e1f36503
JA
153 if (sscanf(p, "%u", val) == 1)
154 return 0;
cb2c86fd 155
e1f36503
JA
156 return 1;
157}
cb2c86fd 158
e1f36503
JA
159static struct fio_option *find_option(struct fio_option *options,
160 const char *opt)
161{
4945ba12 162 struct fio_option *o;
cb2c86fd 163
4945ba12 164 for (o = &options[0]; o->name; o++) {
e1f36503
JA
165 if (!strcmp(o->name, opt))
166 return o;
03b74b3e
JA
167 else if (o->alias && !strcmp(o->alias, opt))
168 return o;
33963c6c 169 }
cb2c86fd 170
e1f36503 171 return NULL;
cb2c86fd
JA
172}
173
17abbe89
JA
174#define val_store(ptr, val, off, data) \
175 do { \
176 ptr = td_var((data), (off)); \
177 *ptr = (val); \
178 } while (0)
179
f90eff5a 180static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 181 int first, int more)
cb2c86fd 182{
63f29372
JA
183 int il, *ilp;
184 long long ull, *ullp;
185 long ul1, ul2;
b4692828 186 char **cp;
e1f36503 187 int ret = 0, is_time = 0;
cb2c86fd 188
08e26e35
JA
189 if (!ptr && o->type != FIO_OPT_STR_SET) {
190 fprintf(stderr, "Option %s requires an argument\n", o->name);
191 return 1;
192 }
193
e1f36503
JA
194 switch (o->type) {
195 case FIO_OPT_STR: {
196 fio_opt_str_fn *fn = o->cb;
b1ec1da6
JA
197 const struct value_pair *vp;
198 int i;
199
b1ec1da6
JA
200 for (i = 0; i < PARSE_MAX_VP; i++) {
201 vp = &o->posval[i];
202 if (!vp->ival || vp->ival[0] == '\0')
203 break;
6612a27b 204 ret = 1;
b1ec1da6
JA
205 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
206 ret = 0;
207 if (!o->off1)
208 break;
209 val_store(ilp, vp->oval, o->off1, data);
210 break;
211 }
212 }
cb2c86fd 213
b1ec1da6
JA
214 if (ret)
215 show_option_values(o);
216 else if (fn)
217 ret = fn(data, ptr);
e1f36503
JA
218 break;
219 }
220 case FIO_OPT_STR_VAL_TIME:
221 is_time = 1;
75e6f36f
JA
222 case FIO_OPT_STR_VAL:
223 case FIO_OPT_STR_VAL_INT: {
e1f36503
JA
224 fio_opt_str_val_fn *fn = o->cb;
225
226 if (is_time)
227 ret = check_str_time(ptr, &ull);
228 else
229 ret = check_str_bytes(ptr, &ull);
230
231 if (ret)
232 break;
233
db8e0165
JA
234 if (o->maxval && ull > o->maxval) {
235 fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
236 return 1;
237 }
238 if (o->minval && ull < o->minval) {
239 fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
240 return 1;
241 }
e1f36503
JA
242
243 if (fn)
244 ret = fn(data, &ull);
245 else {
75e6f36f 246 if (o->type == FIO_OPT_STR_VAL_INT) {
17abbe89
JA
247 if (first)
248 val_store(ilp, ull, o->off1, data);
787f7e95 249 if (!more && o->off2)
17abbe89 250 val_store(ilp, ull, o->off2, data);
75e6f36f 251 } else {
17abbe89
JA
252 if (first)
253 val_store(ullp, ull, o->off1, data);
787f7e95 254 if (!more && o->off2)
17abbe89 255 val_store(ullp, ull, o->off2, data);
75e6f36f 256 }
e1f36503
JA
257 }
258 break;
259 }
af52b345
JA
260 case FIO_OPT_STR_STORE: {
261 fio_opt_str_fn *fn = o->cb;
262
e1f36503
JA
263 cp = td_var(data, o->off1);
264 *cp = strdup(ptr);
af52b345
JA
265 if (fn) {
266 ret = fn(data, ptr);
267 if (ret) {
268 free(*cp);
269 *cp = NULL;
270 }
271 }
e1f36503 272 break;
af52b345 273 }
e1f36503 274 case FIO_OPT_RANGE: {
b765a372 275 char tmp[128];
e1f36503
JA
276 char *p1, *p2;
277
0bbab0e7 278 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
279
280 p1 = strchr(tmp, '-');
e1f36503 281 if (!p1) {
0c9baf91
JA
282 p1 = strchr(tmp, ':');
283 if (!p1) {
284 ret = 1;
285 break;
286 }
e1f36503
JA
287 }
288
289 p2 = p1 + 1;
290 *p1 = '\0';
b765a372 291 p1 = tmp;
e1f36503
JA
292
293 ret = 1;
294 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
295 ret = 0;
e1f36503 296 if (ul1 > ul2) {
f90eff5a
JA
297 unsigned long foo = ul1;
298
299 ul1 = ul2;
300 ul2 = foo;
301 }
302
303 if (first) {
17abbe89
JA
304 val_store(ilp, ul1, o->off1, data);
305 val_store(ilp, ul2, o->off2, data);
306 }
787f7e95 307 if (!more && o->off3 && o->off4) {
17abbe89
JA
308 val_store(ilp, ul1, o->off3, data);
309 val_store(ilp, ul2, o->off4, data);
e1f36503 310 }
17abbe89
JA
311 }
312
e1f36503
JA
313 break;
314 }
13335ddb
JA
315 case FIO_OPT_INT:
316 case FIO_OPT_BOOL: {
e1f36503
JA
317 fio_opt_int_fn *fn = o->cb;
318
319 ret = check_int(ptr, &il);
320 if (ret)
321 break;
322
db8e0165
JA
323 if (o->maxval && il > (int) o->maxval) {
324 fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
325 return 1;
326 }
327 if (o->minval && il < o->minval) {
328 fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
329 return 1;
330 }
e1f36503 331
76a43db4
JA
332 if (o->neg)
333 il = !il;
334
e1f36503
JA
335 if (fn)
336 ret = fn(data, &il);
337 else {
17abbe89
JA
338 if (first)
339 val_store(ilp, il, o->off1, data);
787f7e95 340 if (!more && o->off2)
17abbe89 341 val_store(ilp, il, o->off2, data);
e1f36503
JA
342 }
343 break;
344 }
345 case FIO_OPT_STR_SET: {
346 fio_opt_str_set_fn *fn = o->cb;
347
348 if (fn)
349 ret = fn(data);
350 else {
17abbe89
JA
351 if (first)
352 val_store(ilp, 1, o->off1, data);
787f7e95 353 if (!more && o->off2)
17abbe89 354 val_store(ilp, 1, o->off2, data);
e1f36503
JA
355 }
356 break;
357 }
358 default:
1e97cce9 359 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
360 ret = 1;
361 }
cb2c86fd 362
e1f36503 363 return ret;
cb2c86fd
JA
364}
365
f90eff5a
JA
366static int handle_option(struct fio_option *o, const char *ptr, void *data)
367{
92b586f8 368 const char *ptr2 = NULL;
787f7e95 369 int r1, r2;
f90eff5a
JA
370
371 /*
787f7e95
JA
372 * See if we have a second set of parameters, hidden after a comma.
373 * Do this before parsing the first round, to check if we should
374 * copy set 1 options to set 2.
f90eff5a 375 */
ad231bc4
JB
376 if (ptr &&
377 (o->type != FIO_OPT_STR_STORE) &&
378 (o->type != FIO_OPT_STR)) {
92b586f8 379 ptr2 = strchr(ptr, ',');
0c9baf91
JA
380 if (!ptr2)
381 ptr2 = strchr(ptr, ':');
b486f76a
JA
382 if (!ptr2)
383 ptr2 = strchr(ptr, '-');
0c9baf91 384 }
787f7e95
JA
385
386 /*
387 * Don't return early if parsing the first option fails - if
388 * we are doing multiple arguments, we can allow the first one
389 * being empty.
390 */
391 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
392
f90eff5a 393 if (!ptr2)
787f7e95 394 return r1;
f90eff5a
JA
395
396 ptr2++;
787f7e95
JA
397 r2 = __handle_option(o, ptr2, data, 0, 0);
398
399 return r1 && r2;
f90eff5a
JA
400}
401
b4692828
JA
402int parse_cmd_option(const char *opt, const char *val,
403 struct fio_option *options, void *data)
404{
405 struct fio_option *o;
406
407 o = find_option(options, opt);
408 if (!o) {
409 fprintf(stderr, "Bad option %s\n", opt);
410 return 1;
411 }
412
b1508cf9
JA
413 if (!handle_option(o, val, data))
414 return 0;
415
416 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
417 return 1;
b4692828
JA
418}
419
e1f36503 420int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 421{
b4692828 422 struct fio_option *o;
e1f36503
JA
423 char *pre, *post;
424 char tmp[64];
425
0bbab0e7 426 strncpy(tmp, opt, sizeof(tmp) - 1);
e1f36503
JA
427
428 pre = strchr(tmp, '=');
429 if (pre) {
430 post = pre;
431 *pre = '\0';
432 pre = tmp;
433 post++;
434 o = find_option(options, pre);
435 } else {
436 o = find_option(options, tmp);
437 post = NULL;
438 }
cb2c86fd 439
e1f36503
JA
440 if (!o) {
441 fprintf(stderr, "Bad option %s\n", tmp);
442 return 1;
443 }
444
b1508cf9
JA
445 if (!handle_option(o, post, data))
446 return 0;
447
448 fprintf(stderr, "fio: failed parsing %s\n", opt);
449 return 1;
e1f36503 450}
fd28ca49 451
0e9f7fac
JA
452/*
453 * Option match, levenshtein distance. Handy for not quite remembering what
454 * the option name is.
455 */
456static int string_distance(const char *s1, const char *s2)
457{
458 unsigned int s1_len = strlen(s1);
459 unsigned int s2_len = strlen(s2);
460 unsigned int *p, *q, *r;
461 unsigned int i, j;
462
463 p = malloc(sizeof(unsigned int) * (s2_len + 1));
464 q = malloc(sizeof(unsigned int) * (s2_len + 1));
465
466 p[0] = 0;
467 for (i = 1; i <= s2_len; i++)
468 p[i] = p[i - 1] + 1;
469
470 for (i = 1; i <= s1_len; i++) {
471 q[0] = p[0] + 1;
472 for (j = 1; j <= s2_len; j++) {
473 unsigned int sub = p[j - 1];
474
475 if (s1[i - 1] != s2[j - 1])
476 sub++;
477
478 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
479 }
480 r = p;
481 p = q;
482 q = r;
483 }
484
485 i = p[s2_len];
486 free(p);
487 free(q);
488 return i;
489}
490
491static void show_option_help(struct fio_option *o)
fd28ca49 492{
fd28ca49
JA
493 const char *typehelp[] = {
494 "string (opt=bla)",
495 "string with possible k/m/g postfix (opt=4k)",
496 "string with range and postfix (opt=1k-4k)",
497 "string with time postfix (opt=10s)",
498 "string (opt=bla)",
499 "string with dual range (opt=1k-4k,4k-8k)",
500 "integer value (opt=100)",
13335ddb 501 "boolean value (opt=1)",
fd28ca49
JA
502 "no argument (opt)",
503 };
0e9f7fac
JA
504
505 printf("%20s: %s\n", "type", typehelp[o->type]);
506 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
507 show_option_range(o);
508 show_option_values(o);
509}
510
511int show_cmd_help(struct fio_option *options, const char *name)
512{
513 struct fio_option *o, *closest;
514 unsigned int best_dist;
29fc6afe 515 int found = 0;
320beefe
JA
516 int show_all = 0;
517
518 if (!name || !strcmp(name, "all"))
519 show_all = 1;
fd28ca49 520
0e9f7fac
JA
521 closest = NULL;
522 best_dist = -1;
4945ba12 523 for (o = &options[0]; o->name; o++) {
320beefe
JA
524 int match = 0;
525
0e9f7fac
JA
526 if (name) {
527 if (!strcmp(name, o->name))
528 match = 1;
529 else {
530 unsigned int dist;
531
532 dist = string_distance(name, o->name);
533 if (dist < best_dist) {
534 best_dist = dist;
535 closest = o;
536 }
537 }
538 }
fd28ca49
JA
539
540 if (show_all || match) {
29fc6afe 541 found = 1;
c167dedc
JA
542 if (match)
543 printf("%20s: %s\n", o->name, o->help);
544 if (show_all) {
545 printf("%-20s: %s\n", o->name, o->help);
4945ba12 546 continue;
c167dedc 547 }
fd28ca49
JA
548 }
549
70df2f19
JA
550 if (!match)
551 continue;
552
0e9f7fac 553 show_option_help(o);
fd28ca49
JA
554 }
555
29fc6afe
JA
556 if (found)
557 return 0;
fd28ca49 558
0e9f7fac
JA
559 printf("No such command: %s", name);
560 if (closest) {
561 printf(" - showing closest match\n");
562 printf("%20s: %s\n", closest->name, closest->help);
563 show_option_help(closest);
564 } else
565 printf("\n");
566
29fc6afe 567 return 1;
fd28ca49 568}
ee738499 569
13335ddb
JA
570/*
571 * Handle parsing of default parameters.
572 */
ee738499
JA
573void fill_default_options(void *data, struct fio_option *options)
574{
4945ba12 575 struct fio_option *o;
ee738499 576
4945ba12 577 for (o = &options[0]; o->name; o++)
ee738499
JA
578 if (o->def)
579 handle_option(o, o->def, data);
ee738499 580}
13335ddb
JA
581
582/*
583 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 584 * values and whether both callback and offsets are given.
13335ddb
JA
585 */
586void options_init(struct fio_option *options)
587{
4945ba12 588 struct fio_option *o;
13335ddb 589
4945ba12 590 for (o = &options[0]; o->name; o++) {
13335ddb
JA
591 if (o->type == FIO_OPT_BOOL) {
592 o->minval = 0;
593 o->maxval = 1;
594 }
b1ec1da6
JA
595 if (!o->cb && !o->off1)
596 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
af52b345 597 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
b1ec1da6 598 continue;
5b0a8880
JA
599 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
600 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
13335ddb
JA
601 }
602}