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