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