do not call fprintf from yyerror in expression parser
[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>
88b5a391 11#include <stdlib.h>
83349190 12#include <math.h>
ab9461ea 13#include <float.h>
cb2c86fd
JA
14
15#include "parse.h"
a3d741fa 16#include "debug.h"
9f988e2e 17#include "options.h"
e25839d4 18#include "minmax.h"
fd112d34 19#include "lib/ieee754.h"
cb2c86fd 20
b470a02c
SC
21#ifdef CONFIG_ARITHMETIC
22#include "y.tab.h"
23#endif
24
9af4a244 25static struct fio_option *__fio_options;
3b8b7135 26
f085737f
JA
27static int vp_cmp(const void *p1, const void *p2)
28{
29 const struct value_pair *vp1 = p1;
30 const struct value_pair *vp2 = p2;
31
32 return strlen(vp2->ival) - strlen(vp1->ival);
33}
34
ce952ab6 35static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
f085737f
JA
36{
37 const struct value_pair *vp;
38 int entries;
39
40 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
41
42 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
43 vp = &o->posval[entries];
44 if (!vp->ival || vp->ival[0] == '\0')
45 break;
46
47 memcpy(&vpmap[entries], vp, sizeof(*vp));
48 }
49
50 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
51}
52
06b0be6e
JA
53static void show_option_range(struct fio_option *o,
54 int (*logger)(const char *format, ...))
b1ec1da6 55{
3c037bcf 56 if (o->type == FIO_OPT_FLOAT_LIST) {
ab9461ea 57 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
83349190
YH
58 return;
59
06b0be6e 60 logger("%20s: min=%f", "range", o->minfp);
ab9461ea 61 if (o->maxfp != DBL_MAX)
06b0be6e
JA
62 logger(", max=%f", o->maxfp);
63 logger("\n");
3c037bcf 64 } else if (!o->posval[0].ival) {
83349190
YH
65 if (!o->minval && !o->maxval)
66 return;
b1ec1da6 67
06b0be6e 68 logger("%20s: min=%d", "range", o->minval);
83349190 69 if (o->maxval)
06b0be6e
JA
70 logger(", max=%d", o->maxval);
71 logger("\n");
83349190 72 }
b1ec1da6
JA
73}
74
75static void show_option_values(struct fio_option *o)
76{
a3073f4a 77 int i;
b1ec1da6 78
a3073f4a 79 for (i = 0; i < PARSE_MAX_VP; i++) {
7837213b 80 const struct value_pair *vp = &o->posval[i];
b1ec1da6 81
7837213b 82 if (!vp->ival)
a3073f4a 83 continue;
b1ec1da6 84
06b0be6e 85 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
7837213b 86 if (vp->help)
06b0be6e
JA
87 log_info(" %s", vp->help);
88 log_info("\n");
a3073f4a 89 }
b1ec1da6
JA
90
91 if (i)
06b0be6e 92 log_info("\n");
b1ec1da6
JA
93}
94
06b0be6e 95static void show_option_help(struct fio_option *o, int is_err)
d447a8c2
JA
96{
97 const char *typehelp[] = {
07b3232d 98 "invalid",
d447a8c2 99 "string (opt=bla)",
ae3fb6fb 100 "string (opt=bla)",
d447a8c2
JA
101 "string with possible k/m/g postfix (opt=4k)",
102 "string with time postfix (opt=10s)",
103 "string (opt=bla)",
104 "string with dual range (opt=1k-4k,4k-8k)",
105 "integer value (opt=100)",
106 "boolean value (opt=1)",
83349190 107 "list of floating point values separated by ':' (opt=5.9:7.8)",
d447a8c2 108 "no argument (opt)",
07b3232d 109 "deprecated",
d447a8c2 110 };
06b0be6e
JA
111 int (*logger)(const char *format, ...);
112
113 if (is_err)
114 logger = log_err;
115 else
116 logger = log_info;
d447a8c2
JA
117
118 if (o->alias)
06b0be6e 119 logger("%20s: %s\n", "alias", o->alias);
d447a8c2 120
06b0be6e
JA
121 logger("%20s: %s\n", "type", typehelp[o->type]);
122 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
07b3232d 123 if (o->prof_name)
06b0be6e
JA
124 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
125 show_option_range(o, logger);
d447a8c2
JA
126 show_option_values(o);
127}
128
0de5b26f
JA
129static unsigned long long get_mult_time(const char *str, int len,
130 int is_seconds)
cb2c86fd 131{
74454ce4
CE
132 const char *p = str;
133 char *c;
e4668264 134 unsigned long long mult = 1;
74454ce4
CE
135
136 /*
137 * Go forward until we hit a non-digit, or +/- sign
138 */
139 while ((p - str) <= len) {
140 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
141 break;
142 p++;
cb2c86fd 143 }
74454ce4 144
0de5b26f
JA
145 if (!isalpha((int) *p)) {
146 if (is_seconds)
147 return 1000000UL;
148 else
149 return 1;
150 }
74454ce4
CE
151
152 c = strdup(p);
153 for (int i = 0; i < strlen(c); i++)
154 c[i] = tolower(c[i]);
155
e4668264 156 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
74454ce4 157 mult = 1;
e4668264 158 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
74454ce4 159 mult = 1000;
e4668264
JA
160 else if (!strcmp("s", c))
161 mult = 1000000;
74454ce4 162 else if (!strcmp("m", c))
e4668264 163 mult = 60 * 1000000UL;
74454ce4 164 else if (!strcmp("h", c))
e4668264 165 mult = 60 * 60 * 1000000UL;
74454ce4 166 else if (!strcmp("d", c))
e4668264 167 mult = 24 * 60 * 60 * 1000000UL;
74454ce4
CE
168
169 free(c);
170 return mult;
cb2c86fd
JA
171}
172
a03fb65f
JA
173static int is_separator(char c)
174{
175 switch (c) {
176 case ':':
177 case '-':
178 case ',':
179 case '/':
180 return 1;
181 default:
182 return 0;
183 }
184}
185
7bb59102
JA
186static unsigned long long __get_mult_bytes(const char *p, void *data,
187 int *percent)
cb2c86fd 188{
d6978a32 189 unsigned int kb_base = fio_get_kb_base(data);
a639f0bb 190 unsigned long long ret = 1;
57fc29fa
JA
191 unsigned int i, pow = 0, mult = kb_base;
192 char *c;
a639f0bb 193
57fc29fa
JA
194 if (!p)
195 return 1;
a639f0bb 196
57fc29fa
JA
197 c = strdup(p);
198
a03fb65f 199 for (i = 0; i < strlen(c); i++) {
57fc29fa 200 c[i] = tolower(c[i]);
a03fb65f
JA
201 if (is_separator(c[i])) {
202 c[i] = '\0';
203 break;
204 }
205 }
57fc29fa 206
a04f158d 207 if (!strncmp("pib", c, 3)) {
57fc29fa
JA
208 pow = 5;
209 mult = 1000;
a04f158d 210 } else if (!strncmp("tib", c, 3)) {
57fc29fa
JA
211 pow = 4;
212 mult = 1000;
a04f158d 213 } else if (!strncmp("gib", c, 3)) {
57fc29fa
JA
214 pow = 3;
215 mult = 1000;
a04f158d 216 } else if (!strncmp("mib", c, 3)) {
57fc29fa
JA
217 pow = 2;
218 mult = 1000;
a04f158d 219 } else if (!strncmp("kib", c, 3)) {
57fc29fa
JA
220 pow = 1;
221 mult = 1000;
a04f158d 222 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
57fc29fa 223 pow = 5;
a04f158d 224 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
57fc29fa 225 pow = 4;
a04f158d 226 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
57fc29fa 227 pow = 3;
a04f158d 228 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
57fc29fa 229 pow = 2;
a04f158d 230 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
57fc29fa 231 pow = 1;
a04f158d 232 else if (!strncmp("%", c, 1)) {
7bb59102 233 *percent = 1;
e721c57f 234 free(c);
7bb59102
JA
235 return ret;
236 }
57fc29fa
JA
237
238 while (pow--)
239 ret *= (unsigned long long) mult;
240
241 free(c);
a639f0bb 242 return ret;
cb2c86fd
JA
243}
244
7bb59102 245static unsigned long long get_mult_bytes(const char *str, int len, void *data,
6925dd35 246 int *percent)
57fc29fa 247{
55ed9636 248 const char *p = str;
ba4ddd69 249 int digit_seen = 0;
57fc29fa 250
1d1c187b 251 if (len < 2)
7bb59102 252 return __get_mult_bytes(str, data, percent);
1d1c187b 253
d0c814ec
SL
254 /*
255 * Go forward until we hit a non-digit, or +/- sign
256 */
55ed9636 257 while ((p - str) <= len) {
ba4ddd69
JA
258 if (!isdigit((int) *p) &&
259 (((*p != '+') && (*p != '-')) || digit_seen))
55ed9636 260 break;
3c703d13 261 digit_seen |= isdigit((int) *p);
55ed9636
JA
262 p++;
263 }
264
7bb59102 265 if (!isalpha((int) *p) && (*p != '%'))
57fc29fa
JA
266 p = NULL;
267
7bb59102 268 return __get_mult_bytes(p, data, percent);
57fc29fa
JA
269}
270
b470a02c
SC
271extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
272 double *dval);
273
c05ff644
SC
274#ifdef CONFIG_ARITHMETIC
275/*
276 * These two verification functions are just to gain confidence that
277 * the arithmetic processing code is always getting the same answer as the
278 * original number parsing code. Once sufficiently sure that the arithmetic
279 * code is always getting the right answers, these can be removed.
280 */
281static void verify_exp_parser_float(const char *str)
282{
283 long long ival;
284 double dval, tmpval;
285
286 if (sscanf(str, "%lf", &tmpval) != 1)
287 return;
288
289 if (evaluate_arithmetic_expression(str, &ival, &dval) != 0) {
290 log_info("Arithmetic failed on '%s'\n", str);
291 return;
292 }
293 if (dval != tmpval) {
294 log_info("Arithmetic failed on: '%s' got %lf, expected %lf\n",
295 str, dval, tmpval);
296 }
297}
298
299static void verify_exp_parser_decimal(const char *str, long long val, int kilo, int is_seconds)
300{
301 int rc;
302 long long ival;
303 double dval;
304
305 rc = evaluate_arithmetic_expression(str, &ival, &dval);
306 if (!rc) {
307 if (ival != val)
308 log_info("Arithmetic failed on '%s', expected %lld, got %lld\n",
309 str, val, ival);
310 } else {
311 log_info("Arithmetic failed on '%s'\n", str);
312 }
313}
314#endif
315
83349190
YH
316/*
317 * Convert string into a floating number. Return 1 for success and 0 otherwise.
318 */
e25839d4 319int str_to_float(const char *str, double *val)
83349190 320{
b470a02c
SC
321#ifdef CONFIG_ARITHMETIC
322 int rc;
323 long long ival;
324 double dval;
325
326 if (str[0] == '(') {
327 rc = evaluate_arithmetic_expression(str, &ival, &dval);
328 if (!rc) {
329 *val = dval;
330 return 1;
331 }
c05ff644
SC
332 } else {
333 verify_exp_parser_float(str);
b470a02c
SC
334 }
335#endif
336 return 1 == sscanf(str, "%lf", val);
83349190
YH
337}
338
cb2c86fd 339/*
e1f36503 340 * convert string into decimal value, noting any size suffix
cb2c86fd 341 */
0de5b26f
JA
342int str_to_decimal(const char *str, long long *val, int kilo, void *data,
343 int is_seconds)
cb2c86fd 344{
b347f9da 345 int len, base;
b470a02c
SC
346 int rc = 1;
347#ifdef CONFIG_ARITHMETIC
348 long long ival;
349 double dval;
350#endif
cb2c86fd 351
cb2c86fd 352 len = strlen(str);
f90eff5a
JA
353 if (!len)
354 return 1;
cb2c86fd 355
b470a02c
SC
356#ifdef CONFIG_ARITHMETIC
357 if (str[0] == '(')
358 rc = evaluate_arithmetic_expression(str, &ival, &dval);
18722a18
SC
359 if (str[0] == '(' && !rc) {
360 if (!kilo && is_seconds)
361 *val = ival / 1000000LL;
362 else
363 *val = ival;
364 }
b470a02c 365#endif
b347f9da 366
b470a02c
SC
367 if (rc == 1) {
368 if (strstr(str, "0x") || strstr(str, "0X"))
369 base = 16;
370 else
371 base = 10;
372
373 *val = strtoll(str, NULL, base);
374 if (*val == LONG_MAX && errno == ERANGE)
375 return 1;
376 }
cb2c86fd 377
7bb59102
JA
378 if (kilo) {
379 unsigned long long mult;
380 int perc = 0;
381
6925dd35 382 mult = get_mult_bytes(str, len, data, &perc);
7bb59102
JA
383 if (perc)
384 *val = -1ULL - *val;
385 else
386 *val *= mult;
387 } else
0de5b26f 388 *val *= get_mult_time(str, len, is_seconds);
c05ff644
SC
389#ifdef CONFIG_ARITHMETIC
390 verify_exp_parser_decimal(str, *val, kilo, is_seconds);
391#endif
cb2c86fd
JA
392 return 0;
393}
394
9af4a244 395int check_str_bytes(const char *p, long long *val, void *data)
cb2c86fd 396{
0de5b26f 397 return str_to_decimal(p, val, 1, data, 0);
cb2c86fd
JA
398}
399
0de5b26f 400int check_str_time(const char *p, long long *val, int is_seconds)
cb2c86fd 401{
0de5b26f 402 return str_to_decimal(p, val, 0, NULL, is_seconds);
cb2c86fd
JA
403}
404
405void strip_blank_front(char **p)
406{
407 char *s = *p;
408
4c8e9640
JA
409 if (!strlen(s))
410 return;
76cd9378 411 while (isspace((int) *s))
cb2c86fd 412 s++;
4d651dad
JA
413
414 *p = s;
cb2c86fd
JA
415}
416
417void strip_blank_end(char *p)
418{
853ee7fc 419 char *start = p, *s;
523bfadb 420
4c8e9640
JA
421 if (!strlen(p))
422 return;
423
523bfadb
JA
424 s = strchr(p, ';');
425 if (s)
426 *s = '\0';
427 s = strchr(p, '#');
428 if (s)
429 *s = '\0';
430 if (s)
431 p = s;
432
7f7e6e59 433 s = p + strlen(p);
76cd9378 434 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
cb2c86fd
JA
435 s--;
436
437 *(s + 1) = '\0';
438}
439
d6978a32 440static int check_range_bytes(const char *str, long *val, void *data)
cb2c86fd 441{
57fc29fa 442 long long __val;
cb2c86fd 443
0de5b26f 444 if (!str_to_decimal(str, &__val, 1, data, 0)) {
57fc29fa 445 *val = __val;
cb2c86fd
JA
446 return 0;
447 }
448
cb2c86fd
JA
449 return 1;
450}
451
63f29372 452static int check_int(const char *p, int *val)
cb2c86fd 453{
787f7e95
JA
454 if (!strlen(p))
455 return 1;
d78ee463 456 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
457 if (sscanf(p, "%x", val) == 1)
458 return 0;
459 } else {
460 if (sscanf(p, "%u", val) == 1)
461 return 0;
462 }
cb2c86fd 463
e1f36503
JA
464 return 1;
465}
cb2c86fd 466
e6f735f0 467static size_t opt_len(const char *str)
808def70
JA
468{
469 char *postfix;
470
471 postfix = strchr(str, ':');
472 if (!postfix)
473 return strlen(str);
474
475 return (int)(postfix - str);
476}
477
119cd939
JA
478static int str_match_len(const struct value_pair *vp, const char *str)
479{
480 return max(strlen(vp->ival), opt_len(str));
481}
482
f0fdbcaf
JA
483#define val_store(ptr, val, off, or, data, o) \
484 do { \
485 ptr = td_var((data), (o), (off)); \
486 if ((or)) \
487 *ptr |= (val); \
488 else \
489 *ptr = (val); \
17abbe89
JA
490 } while (0)
491
f90eff5a 492static int __handle_option(struct fio_option *o, const char *ptr, void *data,
83349190 493 int first, int more, int curr)
cb2c86fd 494{
2fdbefdd 495 int il=0, *ilp;
fd112d34 496 fio_fp64_t *flp;
63f29372
JA
497 long long ull, *ullp;
498 long ul1, ul2;
83349190 499 double uf;
bfe1d592 500 char **cp = NULL;
e42b01eb 501 int ret = 0, is_time = 0;
c44b1ff5
JA
502 const struct value_pair *vp;
503 struct value_pair posval[PARSE_MAX_VP];
504 int i, all_skipped = 1;
cb2c86fd 505
a3d741fa
JA
506 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
507 o->type, ptr);
508
e3cedca7 509 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
28d7c363 510 log_err("Option %s requires an argument\n", o->name);
08e26e35
JA
511 return 1;
512 }
513
e1f36503 514 switch (o->type) {
5f6ddf1e
JA
515 case FIO_OPT_STR:
516 case FIO_OPT_STR_MULTI: {
e1f36503 517 fio_opt_str_fn *fn = o->cb;
b1ec1da6 518
f085737f
JA
519 posval_sort(o, posval);
520
5f6ddf1e 521 ret = 1;
b1ec1da6 522 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 523 vp = &posval[i];
b1ec1da6 524 if (!vp->ival || vp->ival[0] == '\0')
a3073f4a 525 continue;
ae2ddba4 526 all_skipped = 0;
119cd939 527 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
b1ec1da6 528 ret = 0;
7b504edd 529 if (o->off1)
ebadc0ce 530 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
5f6ddf1e 531 continue;
b1ec1da6
JA
532 }
533 }
cb2c86fd 534
ae2ddba4 535 if (ret && !all_skipped)
b1ec1da6
JA
536 show_option_values(o);
537 else if (fn)
538 ret = fn(data, ptr);
e1f36503
JA
539 break;
540 }
e42b01eb
JA
541 case FIO_OPT_STR_VAL_TIME:
542 is_time = 1;
1a1137d9 543 case FIO_OPT_INT:
e42b01eb
JA
544 case FIO_OPT_STR_VAL: {
545 fio_opt_str_val_fn *fn = o->cb;
6eaf09d6
SL
546 char tmp[128], *p;
547
548 strncpy(tmp, ptr, sizeof(tmp) - 1);
549 p = strchr(tmp, ',');
550 if (p)
551 *p = '\0';
e42b01eb
JA
552
553 if (is_time)
0de5b26f 554 ret = check_str_time(tmp, &ull, o->is_seconds);
e42b01eb 555 else
6eaf09d6 556 ret = check_str_bytes(tmp, &ull, data);
e1f36503 557
ae3fcb5a
JA
558 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
559
e1f36503
JA
560 if (ret)
561 break;
562
db8e0165 563 if (o->maxval && ull > o->maxval) {
d4fc2f04
JA
564 log_err("max value out of range: %llu"
565 " (%u max)\n", ull, o->maxval);
db8e0165
JA
566 return 1;
567 }
568 if (o->minval && ull < o->minval) {
d4fc2f04
JA
569 log_err("min value out of range: %llu"
570 " (%u min)\n", ull, o->minval);
db8e0165
JA
571 return 1;
572 }
d926d535
JA
573 if (o->posval[0].ival) {
574 posval_sort(o, posval);
575
576 ret = 1;
577 for (i = 0; i < PARSE_MAX_VP; i++) {
578 vp = &posval[i];
579 if (!vp->ival || vp->ival[0] == '\0')
580 continue;
581 if (vp->oval == ull) {
582 ret = 0;
583 break;
584 }
585 }
586 if (ret) {
128e4caf
JA
587 log_err("fio: value %llu not allowed:\n", ull);
588 show_option_values(o);
d926d535
JA
589 return 1;
590 }
591 }
e1f36503
JA
592
593 if (fn)
594 ret = fn(data, &ull);
595 else {
e01b22b8 596 if (o->type == FIO_OPT_INT) {
7b504edd
JA
597 if (first)
598 val_store(ilp, ull, o->off1, 0, data, o);
6eaf09d6 599 if (curr == 1) {
7b504edd
JA
600 if (o->off2)
601 val_store(ilp, ull, o->off2, 0, data, o);
02c6aad5 602 }
6eaf09d6 603 if (curr == 2) {
7b504edd
JA
604 if (o->off3)
605 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
606 }
607 if (!more) {
608 if (curr < 1) {
7b504edd
JA
609 if (o->off2)
610 val_store(ilp, ull, o->off2, 0, data, o);
6eaf09d6
SL
611 }
612 if (curr < 2) {
7b504edd
JA
613 if (o->off3)
614 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
615 }
616 }
75e6f36f 617 } else {
7b504edd
JA
618 if (first)
619 val_store(ullp, ull, o->off1, 0, data, o);
02c6aad5 620 if (!more) {
7b504edd
JA
621 if (o->off2)
622 val_store(ullp, ull, o->off2, 0, data, o);
02c6aad5 623 }
75e6f36f 624 }
e1f36503
JA
625 }
626 break;
627 }
83349190 628 case FIO_OPT_FLOAT_LIST: {
eef02441
JA
629 char *cp2;
630
435d195a
VKF
631 if (first) {
632 /*
633 ** Initialize precision to 0 and zero out list
634 ** in case specified list is shorter than default
635 */
3e260a46
JA
636 if (o->off2) {
637 ul2 = 0;
f0fdbcaf 638 ilp = td_var(data, o, o->off2);
3e260a46
JA
639 *ilp = ul2;
640 }
435d195a 641
f0fdbcaf 642 flp = td_var(data, o, o->off1);
435d195a
VKF
643 for(i = 0; i < o->maxlen; i++)
644 flp[i].u.f = 0.0;
645 }
83349190 646 if (curr >= o->maxlen) {
28d7c363 647 log_err("the list exceeding max length %d\n",
83349190
YH
648 o->maxlen);
649 return 1;
650 }
3c3ed070 651 if (!str_to_float(ptr, &uf)) {
28d7c363 652 log_err("not a floating point value: %s\n", ptr);
83349190
YH
653 return 1;
654 }
26650695 655 if (uf > o->maxfp) {
28d7c363 656 log_err("value out of range: %f"
83349190
YH
657 " (range max: %f)\n", uf, o->maxfp);
658 return 1;
659 }
26650695 660 if (uf < o->minfp) {
28d7c363 661 log_err("value out of range: %f"
83349190
YH
662 " (range min: %f)\n", uf, o->minfp);
663 return 1;
664 }
665
f0fdbcaf 666 flp = td_var(data, o, o->off1);
fd112d34 667 flp[curr].u.f = uf;
83349190 668
ae3fcb5a
JA
669 dprint(FD_PARSE, " out=%f\n", uf);
670
435d195a
VKF
671 /*
672 ** Calculate precision for output by counting
673 ** number of digits after period. Find first
674 ** period in entire remaining list each time
675 */
676 cp2 = strchr(ptr, '.');
677 if (cp2 != NULL) {
eef02441 678 int len = 0;
435d195a
VKF
679
680 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
681 len++;
682
3e260a46 683 if (o->off2) {
f0fdbcaf 684 ilp = td_var(data, o, o->off2);
3e260a46
JA
685 if (len > *ilp)
686 *ilp = len;
687 }
435d195a
VKF
688 }
689
83349190
YH
690 break;
691 }
af52b345
JA
692 case FIO_OPT_STR_STORE: {
693 fio_opt_str_fn *fn = o->cb;
694
f75c69a1
JA
695 if (!strlen(ptr))
696 return 1;
697
7b504edd 698 if (o->off1) {
f0fdbcaf 699 cp = td_var(data, o, o->off1);
184b4098 700 *cp = strdup(ptr);
1c964ce5 701 }
02c6aad5 702
184b4098
SL
703 if (fn)
704 ret = fn(data, ptr);
705 else if (o->posval[0].ival) {
706 posval_sort(o, posval);
707
708 ret = 1;
709 for (i = 0; i < PARSE_MAX_VP; i++) {
710 vp = &posval[i];
ab50817f 711 if (!vp->ival || vp->ival[0] == '\0' || !cp)
184b4098
SL
712 continue;
713 all_skipped = 0;
119cd939 714 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
184b4098
SL
715 char *rest;
716
717 ret = 0;
718 if (vp->cb)
719 fn = vp->cb;
720 rest = strstr(*cp ?: ptr, ":");
721 if (rest) {
722 if (*cp)
723 *rest = '\0';
724 ptr = rest + 1;
725 } else
726 ptr = NULL;
727 break;
728 }
af52b345
JA
729 }
730 }
c44b1ff5 731
184b4098
SL
732 if (!all_skipped) {
733 if (ret && !*cp)
734 show_option_values(o);
735 else if (ret && *cp)
736 ret = 0;
737 else if (fn && ptr)
738 ret = fn(data, ptr);
739 }
c44b1ff5 740
e1f36503 741 break;
af52b345 742 }
e1f36503 743 case FIO_OPT_RANGE: {
b765a372 744 char tmp[128];
e1f36503
JA
745 char *p1, *p2;
746
0bbab0e7 747 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372 748
31d23f47
DE
749 /* Handle bsrange with separate read,write values: */
750 p1 = strchr(tmp, ',');
751 if (p1)
752 *p1 = '\0';
753
b765a372 754 p1 = strchr(tmp, '-');
e1f36503 755 if (!p1) {
0c9baf91
JA
756 p1 = strchr(tmp, ':');
757 if (!p1) {
758 ret = 1;
759 break;
760 }
e1f36503
JA
761 }
762
763 p2 = p1 + 1;
764 *p1 = '\0';
b765a372 765 p1 = tmp;
e1f36503
JA
766
767 ret = 1;
d6978a32
JA
768 if (!check_range_bytes(p1, &ul1, data) &&
769 !check_range_bytes(p2, &ul2, data)) {
e1f36503 770 ret = 0;
e1f36503 771 if (ul1 > ul2) {
f90eff5a
JA
772 unsigned long foo = ul1;
773
774 ul1 = ul2;
775 ul2 = foo;
776 }
777
778 if (first) {
7b504edd
JA
779 val_store(ilp, ul1, o->off1, 0, data, o);
780 val_store(ilp, ul2, o->off2, 0, data, o);
17abbe89 781 }
6eaf09d6 782 if (curr == 1) {
7b504edd
JA
783 if (o->off3 && o->off4) {
784 val_store(ilp, ul1, o->off3, 0, data, o);
785 val_store(ilp, ul2, o->off4, 0, data, o);
6eaf09d6
SL
786 }
787 }
788 if (curr == 2) {
7b504edd
JA
789 if (o->off5 && o->off6) {
790 val_store(ilp, ul1, o->off5, 0, data, o);
791 val_store(ilp, ul2, o->off6, 0, data, o);
6eaf09d6
SL
792 }
793 }
794 if (!more) {
795 if (curr < 1) {
7b504edd
JA
796 if (o->off3 && o->off4) {
797 val_store(ilp, ul1, o->off3, 0, data, o);
798 val_store(ilp, ul2, o->off4, 0, data, o);
6eaf09d6
SL
799 }
800 }
801 if (curr < 2) {
7b504edd
JA
802 if (o->off5 && o->off6) {
803 val_store(ilp, ul1, o->off5, 0, data, o);
804 val_store(ilp, ul2, o->off6, 0, data, o);
6eaf09d6
SL
805 }
806 }
e1f36503 807 }
17abbe89
JA
808 }
809
e1f36503
JA
810 break;
811 }
74ba1808
JA
812 case FIO_OPT_BOOL:
813 case FIO_OPT_STR_SET: {
e1f36503
JA
814 fio_opt_int_fn *fn = o->cb;
815
74ba1808
JA
816 if (ptr)
817 ret = check_int(ptr, &il);
818 else if (o->type == FIO_OPT_BOOL)
819 ret = 1;
820 else
821 il = 1;
822
ae3fcb5a
JA
823 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
824
e1f36503
JA
825 if (ret)
826 break;
827
db8e0165 828 if (o->maxval && il > (int) o->maxval) {
28d7c363 829 log_err("max value out of range: %d (%d max)\n",
5ec10eaa 830 il, o->maxval);
db8e0165
JA
831 return 1;
832 }
833 if (o->minval && il < o->minval) {
28d7c363 834 log_err("min value out of range: %d (%d min)\n",
5ec10eaa 835 il, o->minval);
db8e0165
JA
836 return 1;
837 }
e1f36503 838
76a43db4
JA
839 if (o->neg)
840 il = !il;
841
e1f36503
JA
842 if (fn)
843 ret = fn(data, &il);
844 else {
7b504edd
JA
845 if (first)
846 val_store(ilp, il, o->off1, 0, data, o);
02c6aad5 847 if (!more) {
7b504edd
JA
848 if (o->off2)
849 val_store(ilp, il, o->off2, 0, data, o);
02c6aad5 850 }
e1f36503
JA
851 }
852 break;
853 }
15ca150e 854 case FIO_OPT_DEPRECATED:
06b0be6e 855 log_info("Option %s is deprecated\n", o->name);
d9472271 856 ret = 1;
15ca150e 857 break;
e1f36503 858 default:
06b0be6e 859 log_err("Bad option type %u\n", o->type);
e1f36503
JA
860 ret = 1;
861 }
cb2c86fd 862
70a4c0c8
JA
863 if (ret)
864 return ret;
865
d447a8c2 866 if (o->verify) {
70a4c0c8 867 ret = o->verify(o, data);
d447a8c2 868 if (ret) {
28d7c363
JA
869 log_err("Correct format for offending option\n");
870 log_err("%20s: %s\n", o->name, o->help);
06b0be6e 871 show_option_help(o, 1);
d447a8c2
JA
872 }
873 }
70a4c0c8 874
e1f36503 875 return ret;
cb2c86fd
JA
876}
877
7c8f1a5c 878static int handle_option(struct fio_option *o, const char *__ptr, void *data)
f90eff5a 879{
b62bdf2c
JA
880 char *o_ptr, *ptr, *ptr2;
881 int ret, done;
f90eff5a 882
7c8f1a5c
JA
883 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
884
b62bdf2c 885 o_ptr = ptr = NULL;
7c8f1a5c 886 if (__ptr)
b62bdf2c 887 o_ptr = ptr = strdup(__ptr);
a3d741fa 888
f90eff5a 889 /*
b62bdf2c
JA
890 * See if we have another set of parameters, hidden after a comma.
891 * Do this before parsing this round, to check if we should
787f7e95 892 * copy set 1 options to set 2.
f90eff5a 893 */
b62bdf2c
JA
894 done = 0;
895 ret = 1;
896 do {
897 int __ret;
898
899 ptr2 = NULL;
900 if (ptr &&
901 (o->type != FIO_OPT_STR_STORE) &&
83349190
YH
902 (o->type != FIO_OPT_STR) &&
903 (o->type != FIO_OPT_FLOAT_LIST)) {
b62bdf2c
JA
904 ptr2 = strchr(ptr, ',');
905 if (ptr2 && *(ptr2 + 1) == '\0')
906 *ptr2 = '\0';
6eaf09d6 907 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
b62bdf2c
JA
908 if (!ptr2)
909 ptr2 = strchr(ptr, ':');
910 if (!ptr2)
911 ptr2 = strchr(ptr, '-');
912 }
83349190
YH
913 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
914 ptr2 = strchr(ptr, ':');
b62bdf2c 915 }
787f7e95 916
b62bdf2c
JA
917 /*
918 * Don't return early if parsing the first option fails - if
919 * we are doing multiple arguments, we can allow the first one
920 * being empty.
921 */
83349190 922 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
b62bdf2c
JA
923 if (ret)
924 ret = __ret;
787f7e95 925
b62bdf2c
JA
926 if (!ptr2)
927 break;
f90eff5a 928
b62bdf2c
JA
929 ptr = ptr2 + 1;
930 done++;
931 } while (1);
787f7e95 932
b62bdf2c
JA
933 if (o_ptr)
934 free(o_ptr);
935 return ret;
f90eff5a
JA
936}
937
de890a1e 938static struct fio_option *get_option(char *opt,
07b3232d 939 struct fio_option *options, char **post)
3b8b7135
JA
940{
941 struct fio_option *o;
942 char *ret;
943
944 ret = strchr(opt, '=');
945 if (ret) {
946 *post = ret;
947 *ret = '\0';
de890a1e 948 ret = opt;
3b8b7135 949 (*post)++;
43c129b4 950 strip_blank_end(ret);
07b3232d 951 o = find_option(options, ret);
3b8b7135 952 } else {
07b3232d 953 o = find_option(options, opt);
3b8b7135
JA
954 *post = NULL;
955 }
956
957 return o;
958}
959
960static int opt_cmp(const void *p1, const void *p2)
961{
de890a1e
SL
962 struct fio_option *o;
963 char *s, *foo;
8cdabc1d 964 int prio1, prio2;
3b8b7135 965
8cdabc1d 966 prio1 = prio2 = 0;
3b8b7135 967
de890a1e
SL
968 if (*(char **)p1) {
969 s = strdup(*((char **) p1));
9af4a244 970 o = get_option(s, __fio_options, &foo);
de890a1e
SL
971 if (o)
972 prio1 = o->prio;
973 free(s);
974 }
975 if (*(char **)p2) {
976 s = strdup(*((char **) p2));
9af4a244 977 o = get_option(s, __fio_options, &foo);
de890a1e
SL
978 if (o)
979 prio2 = o->prio;
980 free(s);
981 }
982
8cdabc1d 983 return prio2 - prio1;
3b8b7135
JA
984}
985
986void sort_options(char **opts, struct fio_option *options, int num_opts)
987{
9af4a244 988 __fio_options = options;
3b8b7135 989 qsort(opts, num_opts, sizeof(char *), opt_cmp);
9af4a244 990 __fio_options = NULL;
3b8b7135
JA
991}
992
b4692828 993int parse_cmd_option(const char *opt, const char *val,
07b3232d 994 struct fio_option *options, void *data)
b4692828
JA
995{
996 struct fio_option *o;
997
07b3232d 998 o = find_option(options, opt);
b4692828 999 if (!o) {
06b0be6e 1000 log_err("Bad option <%s>\n", opt);
b4692828
JA
1001 return 1;
1002 }
1003
b1508cf9
JA
1004 if (!handle_option(o, val, data))
1005 return 0;
1006
06b0be6e 1007 log_err("fio: failed parsing %s=%s\n", opt, val);
b1508cf9 1008 return 1;
b4692828
JA
1009}
1010
de890a1e 1011int parse_option(char *opt, const char *input,
292cc475
JA
1012 struct fio_option *options, struct fio_option **o, void *data,
1013 int dump_cmdline)
cb2c86fd 1014{
d0c814ec 1015 char *post;
e1f36503 1016
d0c814ec
SL
1017 if (!opt) {
1018 log_err("fio: failed parsing %s\n", input);
de890a1e 1019 *o = NULL;
38789b58 1020 return 1;
d0c814ec 1021 }
e1f36503 1022
de890a1e
SL
1023 *o = get_option(opt, options, &post);
1024 if (!*o) {
1025 if (post) {
1026 int len = strlen(opt);
1027 if (opt + len + 1 != post)
1028 memmove(opt + len + 1, post, strlen(post));
1029 opt[len] = '=';
1030 }
e1f36503
JA
1031 return 1;
1032 }
1033
292cc475
JA
1034 if (handle_option(*o, post, data)) {
1035 log_err("fio: failed parsing %s\n", input);
1036 return 1;
1037 }
b1508cf9 1038
292cc475
JA
1039 if (dump_cmdline) {
1040 const char *delim;
1041
1042 if (!strcmp("description", (*o)->name))
1043 delim = "\"";
1044 else
1045 delim = "";
1046
1047 log_info("--%s%s", (*o)->name, post ? "" : " ");
1048 if (post)
1049 log_info("=%s%s%s ", delim, post, delim);
1050 }
1051
1052 return 0;
e1f36503 1053}
fd28ca49 1054
0e9f7fac
JA
1055/*
1056 * Option match, levenshtein distance. Handy for not quite remembering what
1057 * the option name is.
1058 */
1059static int string_distance(const char *s1, const char *s2)
1060{
1061 unsigned int s1_len = strlen(s1);
1062 unsigned int s2_len = strlen(s2);
1063 unsigned int *p, *q, *r;
1064 unsigned int i, j;
1065
1066 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1067 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1068
1069 p[0] = 0;
1070 for (i = 1; i <= s2_len; i++)
1071 p[i] = p[i - 1] + 1;
1072
1073 for (i = 1; i <= s1_len; i++) {
1074 q[0] = p[0] + 1;
1075 for (j = 1; j <= s2_len; j++) {
1076 unsigned int sub = p[j - 1];
1077
1078 if (s1[i - 1] != s2[j - 1])
1079 sub++;
1080
1081 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
1082 }
1083 r = p;
1084 p = q;
1085 q = r;
1086 }
1087
1088 i = p[s2_len];
1089 free(p);
1090 free(q);
1091 return i;
1092}
1093
afdf9352
JA
1094static struct fio_option *find_child(struct fio_option *options,
1095 struct fio_option *o)
1096{
1097 struct fio_option *__o;
1098
fdf28744
JA
1099 for (__o = options + 1; __o->name; __o++)
1100 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
1101 return __o;
1102
1103 return NULL;
1104}
1105
323d9113
JA
1106static void __print_option(struct fio_option *o, struct fio_option *org,
1107 int level)
afdf9352
JA
1108{
1109 char name[256], *p;
323d9113 1110 int depth;
afdf9352
JA
1111
1112 if (!o)
1113 return;
ef9aff52
JA
1114 if (!org)
1115 org = o;
5ec10eaa 1116
afdf9352 1117 p = name;
323d9113
JA
1118 depth = level;
1119 while (depth--)
1120 p += sprintf(p, "%s", " ");
afdf9352
JA
1121
1122 sprintf(p, "%s", o->name);
1123
28d7c363 1124 log_info("%-24s: %s\n", name, o->help);
323d9113
JA
1125}
1126
1127static void print_option(struct fio_option *o)
1128{
1129 struct fio_option *parent;
1130 struct fio_option *__o;
1131 unsigned int printed;
1132 unsigned int level;
1133
1134 __print_option(o, NULL, 0);
1135 parent = o;
1136 level = 0;
1137 do {
1138 level++;
1139 printed = 0;
1140
1141 while ((__o = find_child(o, parent)) != NULL) {
1142 __print_option(__o, o, level);
1143 o = __o;
1144 printed++;
1145 }
1146
1147 parent = o;
1148 } while (printed);
afdf9352
JA
1149}
1150
07b3232d 1151int show_cmd_help(struct fio_option *options, const char *name)
0e9f7fac
JA
1152{
1153 struct fio_option *o, *closest;
d091d099 1154 unsigned int best_dist = -1U;
29fc6afe 1155 int found = 0;
320beefe
JA
1156 int show_all = 0;
1157
1158 if (!name || !strcmp(name, "all"))
1159 show_all = 1;
fd28ca49 1160
0e9f7fac
JA
1161 closest = NULL;
1162 best_dist = -1;
4945ba12 1163 for (o = &options[0]; o->name; o++) {
320beefe
JA
1164 int match = 0;
1165
15ca150e
JA
1166 if (o->type == FIO_OPT_DEPRECATED)
1167 continue;
07b3232d
JA
1168 if (!exec_profile && o->prof_name)
1169 continue;
4ac23d27
JA
1170 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1171 continue;
15ca150e 1172
0e9f7fac 1173 if (name) {
7f9348f8
JA
1174 if (!strcmp(name, o->name) ||
1175 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
1176 match = 1;
1177 else {
1178 unsigned int dist;
1179
1180 dist = string_distance(name, o->name);
1181 if (dist < best_dist) {
1182 best_dist = dist;
1183 closest = o;
1184 }
1185 }
1186 }
fd28ca49
JA
1187
1188 if (show_all || match) {
29fc6afe 1189 found = 1;
c167dedc 1190 if (match)
28d7c363 1191 log_info("%20s: %s\n", o->name, o->help);
c167dedc 1192 if (show_all) {
afdf9352 1193 if (!o->parent)
323d9113 1194 print_option(o);
4945ba12 1195 continue;
c167dedc 1196 }
fd28ca49
JA
1197 }
1198
70df2f19
JA
1199 if (!match)
1200 continue;
1201
06b0be6e 1202 show_option_help(o, 0);
fd28ca49
JA
1203 }
1204
29fc6afe
JA
1205 if (found)
1206 return 0;
fd28ca49 1207
28d7c363 1208 log_err("No such command: %s", name);
d091d099
JA
1209
1210 /*
1211 * Only print an appropriately close option, one where the edit
1212 * distance isn't too big. Otherwise we get crazy matches.
1213 */
1214 if (closest && best_dist < 3) {
28d7c363
JA
1215 log_info(" - showing closest match\n");
1216 log_info("%20s: %s\n", closest->name, closest->help);
06b0be6e 1217 show_option_help(closest, 0);
0e9f7fac 1218 } else
28d7c363 1219 log_info("\n");
0e9f7fac 1220
29fc6afe 1221 return 1;
fd28ca49 1222}
ee738499 1223
13335ddb
JA
1224/*
1225 * Handle parsing of default parameters.
1226 */
ee738499
JA
1227void fill_default_options(void *data, struct fio_option *options)
1228{
4945ba12 1229 struct fio_option *o;
ee738499 1230
a3d741fa
JA
1231 dprint(FD_PARSE, "filling default options\n");
1232
4945ba12 1233 for (o = &options[0]; o->name; o++)
ee738499
JA
1234 if (o->def)
1235 handle_option(o, o->def, data);
ee738499 1236}
13335ddb 1237
9f988e2e
JA
1238void option_init(struct fio_option *o)
1239{
1240 if (o->type == FIO_OPT_DEPRECATED)
1241 return;
1242 if (o->type == FIO_OPT_BOOL) {
1243 o->minval = 0;
1244 o->maxval = 1;
1245 }
d4fc2f04
JA
1246 if (o->type == FIO_OPT_INT) {
1247 if (!o->maxval)
1248 o->maxval = UINT_MAX;
1249 }
83349190 1250 if (o->type == FIO_OPT_FLOAT_LIST) {
ab9461ea
BC
1251 o->minfp = DBL_MIN;
1252 o->maxfp = DBL_MAX;
83349190 1253 }
c8931876 1254 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
06b0be6e 1255 log_err("Option %s: string set option with"
9f988e2e
JA
1256 " default will always be true\n", o->name);
1257 }
7b504edd 1258 if (!o->cb && !o->off1)
06b0be6e 1259 log_err("Option %s: neither cb nor offset given\n", o->name);
22754746 1260 if (!o->category) {
cca5b5bc 1261 log_info("Option %s: no category defined. Setting to misc\n", o->name);
22754746 1262 o->category = FIO_OPT_C_GENERAL;
ffd7821f 1263 o->group = FIO_OPT_G_INVALID;
22754746 1264 }
5f6ddf1e
JA
1265 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1266 o->type == FIO_OPT_STR_MULTI)
9f988e2e 1267 return;
7b504edd 1268 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
06b0be6e 1269 log_err("Option %s: both cb and offset given\n", o->name);
9f988e2e
JA
1270}
1271
13335ddb
JA
1272/*
1273 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 1274 * values and whether both callback and offsets are given.
13335ddb
JA
1275 */
1276void options_init(struct fio_option *options)
1277{
4945ba12 1278 struct fio_option *o;
13335ddb 1279
a3d741fa
JA
1280 dprint(FD_PARSE, "init options\n");
1281
90265353 1282 for (o = &options[0]; o->name; o++) {
9f988e2e 1283 option_init(o);
90265353
JA
1284 if (o->inverse)
1285 o->inv_opt = find_option(options, o->inverse);
1286 }
13335ddb 1287}
7e356b2d
JA
1288
1289void options_free(struct fio_option *options, void *data)
1290{
1291 struct fio_option *o;
1292 char **ptr;
1293
1294 dprint(FD_PARSE, "free options\n");
1295
1296 for (o = &options[0]; o->name; o++) {
de890a1e 1297 if (o->type != FIO_OPT_STR_STORE || !o->off1)
7e356b2d
JA
1298 continue;
1299
f0fdbcaf 1300 ptr = td_var(data, o, o->off1);
7e356b2d
JA
1301 if (*ptr) {
1302 free(*ptr);
1303 *ptr = NULL;
1304 }
1305 }
1306}