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