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