Get rid of KiB vs KB distinction
[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>
cb2c86fd
JA
12
13#include "parse.h"
a3d741fa 14#include "debug.h"
cb2c86fd 15
3b8b7135
JA
16static struct fio_option *fio_options;
17
f085737f
JA
18static int vp_cmp(const void *p1, const void *p2)
19{
20 const struct value_pair *vp1 = p1;
21 const struct value_pair *vp2 = p2;
22
23 return strlen(vp2->ival) - strlen(vp1->ival);
24}
25
26static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
27{
28 const struct value_pair *vp;
29 int entries;
30
31 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
32
33 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
34 vp = &o->posval[entries];
35 if (!vp->ival || vp->ival[0] == '\0')
36 break;
37
38 memcpy(&vpmap[entries], vp, sizeof(*vp));
39 }
40
41 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
42}
43
d447a8c2 44static void show_option_range(struct fio_option *o, FILE *out)
b1ec1da6
JA
45{
46 if (!o->minval && !o->maxval)
47 return;
48
d447a8c2 49 fprintf(out, "%20s: min=%d", "range", o->minval);
3980127d 50 if (o->maxval)
d447a8c2
JA
51 fprintf(out, ", max=%d", o->maxval);
52 fprintf(out, "\n");
b1ec1da6
JA
53}
54
55static void show_option_values(struct fio_option *o)
56{
b1ec1da6
JA
57 int i = 0;
58
59 do {
7837213b 60 const struct value_pair *vp = &o->posval[i];
b1ec1da6 61
7837213b
JA
62 if (!vp->ival)
63 break;
b1ec1da6 64
7837213b
JA
65 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
66 if (vp->help)
67 printf(" %s", vp->help);
68 printf("\n");
b1ec1da6
JA
69 i++;
70 } while (i < PARSE_MAX_VP);
71
72 if (i)
73 printf("\n");
74}
75
d447a8c2
JA
76static void show_option_help(struct fio_option *o, FILE *out)
77{
78 const char *typehelp[] = {
79 "string (opt=bla)",
80 "string with possible k/m/g postfix (opt=4k)",
81 "string with time postfix (opt=10s)",
82 "string (opt=bla)",
83 "string with dual range (opt=1k-4k,4k-8k)",
84 "integer value (opt=100)",
85 "boolean value (opt=1)",
86 "no argument (opt)",
87 };
88
89 if (o->alias)
90 fprintf(out, "%20s: %s\n", "alias", o->alias);
91
92 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
93 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
94 show_option_range(o, stdout);
95 show_option_values(o);
96}
97
cb2c86fd
JA
98static unsigned long get_mult_time(char c)
99{
100 switch (c) {
5ec10eaa
JA
101 case 'm':
102 case 'M':
103 return 60;
104 case 'h':
105 case 'H':
106 return 60 * 60;
107 case 'd':
108 case 'D':
109 return 24 * 60 * 60;
110 default:
111 return 1;
cb2c86fd
JA
112 }
113}
114
115static unsigned long get_mult_bytes(char c)
116{
117 switch (c) {
5ec10eaa
JA
118 case 'k':
119 case 'K':
120 return 1024;
121 case 'm':
122 case 'M':
123 return 1024 * 1024;
124 case 'g':
125 case 'G':
126 return 1024 * 1024 * 1024;
127 case 'e':
128 case 'E':
129 return 1024 * 1024 * 1024 * 1024UL;
130 default:
131 return 1;
cb2c86fd
JA
132 }
133}
134
135/*
e1f36503 136 * convert string into decimal value, noting any size suffix
cb2c86fd 137 */
564ca972 138int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 139{
b347f9da 140 int len, base;
cb2c86fd 141
cb2c86fd 142 len = strlen(str);
f90eff5a
JA
143 if (!len)
144 return 1;
cb2c86fd 145
b347f9da
JA
146 if (strstr(str, "0x") || strstr(str, "0X"))
147 base = 16;
148 else
149 base = 10;
150
151 *val = strtoll(str, NULL, base);
cda866ca 152 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
153 return 1;
154
155 if (kilo)
156 *val *= get_mult_bytes(str[len - 1]);
157 else
158 *val *= get_mult_time(str[len - 1]);
787f7e95 159
cb2c86fd
JA
160 return 0;
161}
162
63f29372 163static int check_str_bytes(const char *p, long long *val)
cb2c86fd 164{
cb2c86fd
JA
165 return str_to_decimal(p, val, 1);
166}
167
63f29372 168static int check_str_time(const char *p, long long *val)
cb2c86fd 169{
cb2c86fd
JA
170 return str_to_decimal(p, val, 0);
171}
172
173void strip_blank_front(char **p)
174{
175 char *s = *p;
176
177 while (isspace(*s))
178 s++;
4d651dad
JA
179
180 *p = s;
cb2c86fd
JA
181}
182
183void strip_blank_end(char *p)
184{
853ee7fc 185 char *start = p, *s;
523bfadb
JA
186
187 s = strchr(p, ';');
188 if (s)
189 *s = '\0';
190 s = strchr(p, '#');
191 if (s)
192 *s = '\0';
193 if (s)
194 p = s;
195
7f7e6e59 196 s = p + strlen(p);
853ee7fc 197 while ((isspace(*s) || iscntrl(*s)) && (s > start))
cb2c86fd
JA
198 s--;
199
200 *(s + 1) = '\0';
201}
202
63f29372 203static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
204{
205 char suffix;
206
787f7e95
JA
207 if (!strlen(str))
208 return 1;
209
cb2c86fd
JA
210 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
211 *val *= get_mult_bytes(suffix);
212 return 0;
213 }
214
215 if (sscanf(str, "%lu", val) == 1)
216 return 0;
217
218 return 1;
219}
220
63f29372 221static int check_int(const char *p, int *val)
cb2c86fd 222{
787f7e95
JA
223 if (!strlen(p))
224 return 1;
d78ee463 225 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
226 if (sscanf(p, "%x", val) == 1)
227 return 0;
228 } else {
229 if (sscanf(p, "%u", val) == 1)
230 return 0;
231 }
cb2c86fd 232
e1f36503
JA
233 return 1;
234}
cb2c86fd 235
e1f36503
JA
236static struct fio_option *find_option(struct fio_option *options,
237 const char *opt)
238{
4945ba12 239 struct fio_option *o;
cb2c86fd 240
4945ba12 241 for (o = &options[0]; o->name; o++) {
e1f36503
JA
242 if (!strcmp(o->name, opt))
243 return o;
03b74b3e
JA
244 else if (o->alias && !strcmp(o->alias, opt))
245 return o;
33963c6c 246 }
cb2c86fd 247
e1f36503 248 return NULL;
cb2c86fd
JA
249}
250
17abbe89
JA
251#define val_store(ptr, val, off, data) \
252 do { \
253 ptr = td_var((data), (off)); \
254 *ptr = (val); \
255 } while (0)
256
f90eff5a 257static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 258 int first, int more)
cb2c86fd 259{
63f29372
JA
260 int il, *ilp;
261 long long ull, *ullp;
262 long ul1, ul2;
b4692828 263 char **cp;
e1f36503 264 int ret = 0, is_time = 0;
cb2c86fd 265
a3d741fa
JA
266 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
267 o->type, ptr);
268
e3cedca7 269 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
08e26e35
JA
270 fprintf(stderr, "Option %s requires an argument\n", o->name);
271 return 1;
272 }
273
e1f36503
JA
274 switch (o->type) {
275 case FIO_OPT_STR: {
276 fio_opt_str_fn *fn = o->cb;
b1ec1da6 277 const struct value_pair *vp;
f085737f 278 struct value_pair posval[PARSE_MAX_VP];
b1ec1da6
JA
279 int i;
280
f085737f
JA
281 posval_sort(o, posval);
282
b1ec1da6 283 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 284 vp = &posval[i];
b1ec1da6
JA
285 if (!vp->ival || vp->ival[0] == '\0')
286 break;
6612a27b 287 ret = 1;
b1ec1da6
JA
288 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
289 ret = 0;
290 if (!o->off1)
291 break;
292 val_store(ilp, vp->oval, o->off1, data);
293 break;
294 }
295 }
cb2c86fd 296
b1ec1da6
JA
297 if (ret)
298 show_option_values(o);
299 else if (fn)
300 ret = fn(data, ptr);
e1f36503
JA
301 break;
302 }
303 case FIO_OPT_STR_VAL_TIME:
304 is_time = 1;
f7fa2653 305 case FIO_OPT_INT:
e01b22b8 306 case FIO_OPT_STR_VAL: {
e1f36503
JA
307 fio_opt_str_val_fn *fn = o->cb;
308
309 if (is_time)
310 ret = check_str_time(ptr, &ull);
311 else
312 ret = check_str_bytes(ptr, &ull);
313
314 if (ret)
315 break;
316
db8e0165 317 if (o->maxval && ull > o->maxval) {
5ec10eaa
JA
318 fprintf(stderr, "max value out of range: %lld"
319 " (%d max)\n", ull, o->maxval);
db8e0165
JA
320 return 1;
321 }
322 if (o->minval && ull < o->minval) {
5ec10eaa
JA
323 fprintf(stderr, "min value out of range: %lld"
324 " (%d min)\n", ull, o->minval);
db8e0165
JA
325 return 1;
326 }
e1f36503
JA
327
328 if (fn)
329 ret = fn(data, &ull);
330 else {
e01b22b8 331 if (o->type == FIO_OPT_INT) {
17abbe89
JA
332 if (first)
333 val_store(ilp, ull, o->off1, data);
787f7e95 334 if (!more && o->off2)
17abbe89 335 val_store(ilp, ull, o->off2, data);
75e6f36f 336 } else {
17abbe89
JA
337 if (first)
338 val_store(ullp, ull, o->off1, data);
787f7e95 339 if (!more && o->off2)
17abbe89 340 val_store(ullp, ull, o->off2, data);
75e6f36f 341 }
e1f36503
JA
342 }
343 break;
344 }
af52b345
JA
345 case FIO_OPT_STR_STORE: {
346 fio_opt_str_fn *fn = o->cb;
347
e1f36503
JA
348 cp = td_var(data, o->off1);
349 *cp = strdup(ptr);
af52b345
JA
350 if (fn) {
351 ret = fn(data, ptr);
352 if (ret) {
353 free(*cp);
354 *cp = NULL;
355 }
356 }
e1f36503 357 break;
af52b345 358 }
e1f36503 359 case FIO_OPT_RANGE: {
b765a372 360 char tmp[128];
e1f36503
JA
361 char *p1, *p2;
362
0bbab0e7 363 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
364
365 p1 = strchr(tmp, '-');
e1f36503 366 if (!p1) {
0c9baf91
JA
367 p1 = strchr(tmp, ':');
368 if (!p1) {
369 ret = 1;
370 break;
371 }
e1f36503
JA
372 }
373
374 p2 = p1 + 1;
375 *p1 = '\0';
b765a372 376 p1 = tmp;
e1f36503
JA
377
378 ret = 1;
5ec10eaa
JA
379 if (!check_range_bytes(p1, &ul1) &&
380 !check_range_bytes(p2, &ul2)) {
e1f36503 381 ret = 0;
e1f36503 382 if (ul1 > ul2) {
f90eff5a
JA
383 unsigned long foo = ul1;
384
385 ul1 = ul2;
386 ul2 = foo;
387 }
388
389 if (first) {
17abbe89
JA
390 val_store(ilp, ul1, o->off1, data);
391 val_store(ilp, ul2, o->off2, data);
392 }
97e8cd44 393 if (o->off3 && o->off4) {
17abbe89
JA
394 val_store(ilp, ul1, o->off3, data);
395 val_store(ilp, ul2, o->off4, data);
e1f36503 396 }
17abbe89
JA
397 }
398
e1f36503
JA
399 break;
400 }
13335ddb 401 case FIO_OPT_BOOL: {
e1f36503
JA
402 fio_opt_int_fn *fn = o->cb;
403
404 ret = check_int(ptr, &il);
405 if (ret)
406 break;
407
db8e0165 408 if (o->maxval && il > (int) o->maxval) {
5ec10eaa
JA
409 fprintf(stderr, "max value out of range: %d (%d max)\n",
410 il, o->maxval);
db8e0165
JA
411 return 1;
412 }
413 if (o->minval && il < o->minval) {
5ec10eaa
JA
414 fprintf(stderr, "min value out of range: %d (%d min)\n",
415 il, o->minval);
db8e0165
JA
416 return 1;
417 }
e1f36503 418
76a43db4
JA
419 if (o->neg)
420 il = !il;
421
e1f36503
JA
422 if (fn)
423 ret = fn(data, &il);
424 else {
17abbe89
JA
425 if (first)
426 val_store(ilp, il, o->off1, data);
787f7e95 427 if (!more && o->off2)
17abbe89 428 val_store(ilp, il, o->off2, data);
e1f36503
JA
429 }
430 break;
431 }
432 case FIO_OPT_STR_SET: {
433 fio_opt_str_set_fn *fn = o->cb;
434
435 if (fn)
436 ret = fn(data);
437 else {
17abbe89
JA
438 if (first)
439 val_store(ilp, 1, o->off1, data);
787f7e95 440 if (!more && o->off2)
17abbe89 441 val_store(ilp, 1, o->off2, data);
e1f36503
JA
442 }
443 break;
444 }
15ca150e
JA
445 case FIO_OPT_DEPRECATED:
446 fprintf(stdout, "Option %s is deprecated\n", o->name);
447 break;
e1f36503 448 default:
1e97cce9 449 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
450 ret = 1;
451 }
cb2c86fd 452
70a4c0c8
JA
453 if (ret)
454 return ret;
455
d447a8c2 456 if (o->verify) {
70a4c0c8 457 ret = o->verify(o, data);
d447a8c2
JA
458 if (ret) {
459 fprintf(stderr,"Correct format for offending option\n");
460 fprintf(stderr, "%20s: %s\n", o->name, o->help);
461 show_option_help(o, stderr);
462 }
463 }
70a4c0c8 464
e1f36503 465 return ret;
cb2c86fd
JA
466}
467
7c8f1a5c 468static int handle_option(struct fio_option *o, const char *__ptr, void *data)
f90eff5a 469{
7c8f1a5c 470 char *ptr, *ptr2 = NULL;
787f7e95 471 int r1, r2;
f90eff5a 472
7c8f1a5c
JA
473 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
474
475 ptr = NULL;
476 if (__ptr)
477 ptr = strdup(__ptr);
a3d741fa 478
f90eff5a 479 /*
787f7e95
JA
480 * See if we have a second set of parameters, hidden after a comma.
481 * Do this before parsing the first round, to check if we should
482 * copy set 1 options to set 2.
f90eff5a 483 */
ad231bc4
JB
484 if (ptr &&
485 (o->type != FIO_OPT_STR_STORE) &&
486 (o->type != FIO_OPT_STR)) {
92b586f8 487 ptr2 = strchr(ptr, ',');
7c8f1a5c
JA
488 if (ptr2 && *(ptr2 + 1) == '\0')
489 *ptr2 = '\0';
0c9baf91
JA
490 if (!ptr2)
491 ptr2 = strchr(ptr, ':');
b486f76a
JA
492 if (!ptr2)
493 ptr2 = strchr(ptr, '-');
0c9baf91 494 }
787f7e95
JA
495
496 /*
497 * Don't return early if parsing the first option fails - if
498 * we are doing multiple arguments, we can allow the first one
499 * being empty.
500 */
501 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
502
7c8f1a5c
JA
503 if (!ptr2) {
504 if (ptr)
505 free(ptr);
787f7e95 506 return r1;
7c8f1a5c 507 }
f90eff5a
JA
508
509 ptr2++;
787f7e95
JA
510 r2 = __handle_option(o, ptr2, data, 0, 0);
511
7c8f1a5c
JA
512 if (ptr)
513 free(ptr);
787f7e95 514 return r1 && r2;
f90eff5a
JA
515}
516
3b8b7135
JA
517static struct fio_option *get_option(const char *opt,
518 struct fio_option *options, char **post)
519{
520 struct fio_option *o;
521 char *ret;
522
523 ret = strchr(opt, '=');
524 if (ret) {
525 *post = ret;
526 *ret = '\0';
527 ret = (char *) opt;
528 (*post)++;
43c129b4 529 strip_blank_end(ret);
3b8b7135
JA
530 o = find_option(options, ret);
531 } else {
532 o = find_option(options, opt);
533 *post = NULL;
534 }
535
536 return o;
537}
538
539static int opt_cmp(const void *p1, const void *p2)
540{
541 struct fio_option *o1, *o2;
542 char *s1, *s2, *foo;
8cdabc1d 543 int prio1, prio2;
3b8b7135
JA
544
545 s1 = strdup(*((char **) p1));
546 s2 = strdup(*((char **) p2));
547
548 o1 = get_option(s1, fio_options, &foo);
549 o2 = get_option(s2, fio_options, &foo);
8cdabc1d
JA
550
551 prio1 = prio2 = 0;
552 if (o1)
553 prio1 = o1->prio;
554 if (o2)
555 prio2 = o2->prio;
3b8b7135
JA
556
557 free(s1);
558 free(s2);
8cdabc1d 559 return prio2 - prio1;
3b8b7135
JA
560}
561
562void sort_options(char **opts, struct fio_option *options, int num_opts)
563{
564 fio_options = options;
565 qsort(opts, num_opts, sizeof(char *), opt_cmp);
566 fio_options = NULL;
567}
568
b4692828
JA
569int parse_cmd_option(const char *opt, const char *val,
570 struct fio_option *options, void *data)
571{
572 struct fio_option *o;
573
574 o = find_option(options, opt);
575 if (!o) {
43c129b4 576 fprintf(stderr, "Bad option <%s>\n", opt);
b4692828
JA
577 return 1;
578 }
579
b1508cf9
JA
580 if (!handle_option(o, val, data))
581 return 0;
582
583 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
584 return 1;
b4692828
JA
585}
586
88b5a391
AC
587/*
588 * Return a copy of the input string with substrings of the form ${VARNAME}
589 * substituted with the value of the environment variable VARNAME. The
590 * substitution always occurs, even if VARNAME is empty or the corresponding
591 * environment variable undefined.
592 */
593static char *option_dup_subs(const char *opt)
594{
595 char out[OPT_LEN_MAX+1];
596 char in[OPT_LEN_MAX+1];
597 char *outptr = out;
598 char *inptr = in;
599 char *ch1, *ch2, *env;
600 ssize_t nchr = OPT_LEN_MAX;
601 size_t envlen;
602
603 in[OPT_LEN_MAX] = '\0';
604 strncpy(in, opt, OPT_LEN_MAX);
605
606 while (*inptr && nchr > 0) {
607 if (inptr[0] == '$' && inptr[1] == '{') {
608 ch2 = strchr(inptr, '}');
609 if (ch2 && inptr+1 < ch2) {
610 ch1 = inptr+2;
611 inptr = ch2+1;
612 *ch2 = '\0';
613
614 env = getenv(ch1);
615 if (env) {
616 envlen = strlen(env);
617 if (envlen <= nchr) {
618 memcpy(outptr, env, envlen);
619 outptr += envlen;
620 nchr -= envlen;
621 }
622 }
623
624 continue;
625 }
626 }
627
628 *outptr++ = *inptr++;
629 --nchr;
630 }
631
632 *outptr = '\0';
633 return strdup(out);
634}
635
e1f36503 636int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 637{
b4692828 638 struct fio_option *o;
3b8b7135 639 char *post, *tmp;
e1f36503 640
88b5a391 641 tmp = option_dup_subs(opt);
e1f36503 642
3b8b7135 643 o = get_option(tmp, options, &post);
e1f36503 644 if (!o) {
43c129b4 645 fprintf(stderr, "Bad option <%s>\n", tmp);
0401bca6 646 free(tmp);
e1f36503
JA
647 return 1;
648 }
649
0401bca6
JA
650 if (!handle_option(o, post, data)) {
651 free(tmp);
b1508cf9 652 return 0;
0401bca6 653 }
b1508cf9
JA
654
655 fprintf(stderr, "fio: failed parsing %s\n", opt);
0401bca6 656 free(tmp);
b1508cf9 657 return 1;
e1f36503 658}
fd28ca49 659
0e9f7fac
JA
660/*
661 * Option match, levenshtein distance. Handy for not quite remembering what
662 * the option name is.
663 */
664static int string_distance(const char *s1, const char *s2)
665{
666 unsigned int s1_len = strlen(s1);
667 unsigned int s2_len = strlen(s2);
668 unsigned int *p, *q, *r;
669 unsigned int i, j;
670
671 p = malloc(sizeof(unsigned int) * (s2_len + 1));
672 q = malloc(sizeof(unsigned int) * (s2_len + 1));
673
674 p[0] = 0;
675 for (i = 1; i <= s2_len; i++)
676 p[i] = p[i - 1] + 1;
677
678 for (i = 1; i <= s1_len; i++) {
679 q[0] = p[0] + 1;
680 for (j = 1; j <= s2_len; j++) {
681 unsigned int sub = p[j - 1];
682
683 if (s1[i - 1] != s2[j - 1])
684 sub++;
685
686 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
687 }
688 r = p;
689 p = q;
690 q = r;
691 }
692
693 i = p[s2_len];
694 free(p);
695 free(q);
696 return i;
697}
698
afdf9352
JA
699static struct fio_option *find_child(struct fio_option *options,
700 struct fio_option *o)
701{
702 struct fio_option *__o;
703
fdf28744
JA
704 for (__o = options + 1; __o->name; __o++)
705 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
706 return __o;
707
708 return NULL;
709}
710
323d9113
JA
711static void __print_option(struct fio_option *o, struct fio_option *org,
712 int level)
afdf9352
JA
713{
714 char name[256], *p;
323d9113 715 int depth;
afdf9352
JA
716
717 if (!o)
718 return;
ef9aff52
JA
719 if (!org)
720 org = o;
5ec10eaa 721
afdf9352 722 p = name;
323d9113
JA
723 depth = level;
724 while (depth--)
725 p += sprintf(p, "%s", " ");
afdf9352
JA
726
727 sprintf(p, "%s", o->name);
728
729 printf("%-24s: %s\n", name, o->help);
323d9113
JA
730}
731
732static void print_option(struct fio_option *o)
733{
734 struct fio_option *parent;
735 struct fio_option *__o;
736 unsigned int printed;
737 unsigned int level;
738
739 __print_option(o, NULL, 0);
740 parent = o;
741 level = 0;
742 do {
743 level++;
744 printed = 0;
745
746 while ((__o = find_child(o, parent)) != NULL) {
747 __print_option(__o, o, level);
748 o = __o;
749 printed++;
750 }
751
752 parent = o;
753 } while (printed);
afdf9352
JA
754}
755
0e9f7fac
JA
756int show_cmd_help(struct fio_option *options, const char *name)
757{
758 struct fio_option *o, *closest;
759 unsigned int best_dist;
29fc6afe 760 int found = 0;
320beefe
JA
761 int show_all = 0;
762
763 if (!name || !strcmp(name, "all"))
764 show_all = 1;
fd28ca49 765
0e9f7fac
JA
766 closest = NULL;
767 best_dist = -1;
4945ba12 768 for (o = &options[0]; o->name; o++) {
320beefe
JA
769 int match = 0;
770
15ca150e
JA
771 if (o->type == FIO_OPT_DEPRECATED)
772 continue;
773
0e9f7fac 774 if (name) {
7f9348f8
JA
775 if (!strcmp(name, o->name) ||
776 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
777 match = 1;
778 else {
779 unsigned int dist;
780
781 dist = string_distance(name, o->name);
782 if (dist < best_dist) {
783 best_dist = dist;
784 closest = o;
785 }
786 }
787 }
fd28ca49
JA
788
789 if (show_all || match) {
29fc6afe 790 found = 1;
c167dedc 791 if (match)
afdf9352 792 printf("%24s: %s\n", o->name, o->help);
c167dedc 793 if (show_all) {
afdf9352 794 if (!o->parent)
323d9113 795 print_option(o);
4945ba12 796 continue;
c167dedc 797 }
fd28ca49
JA
798 }
799
70df2f19
JA
800 if (!match)
801 continue;
802
d447a8c2 803 show_option_help(o, stdout);
fd28ca49
JA
804 }
805
29fc6afe
JA
806 if (found)
807 return 0;
fd28ca49 808
0e9f7fac
JA
809 printf("No such command: %s", name);
810 if (closest) {
811 printf(" - showing closest match\n");
812 printf("%20s: %s\n", closest->name, closest->help);
d447a8c2 813 show_option_help(closest, stdout);
0e9f7fac
JA
814 } else
815 printf("\n");
816
29fc6afe 817 return 1;
fd28ca49 818}
ee738499 819
13335ddb
JA
820/*
821 * Handle parsing of default parameters.
822 */
ee738499
JA
823void fill_default_options(void *data, struct fio_option *options)
824{
4945ba12 825 struct fio_option *o;
ee738499 826
a3d741fa
JA
827 dprint(FD_PARSE, "filling default options\n");
828
4945ba12 829 for (o = &options[0]; o->name; o++)
ee738499
JA
830 if (o->def)
831 handle_option(o, o->def, data);
ee738499 832}
13335ddb
JA
833
834/*
835 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 836 * values and whether both callback and offsets are given.
13335ddb
JA
837 */
838void options_init(struct fio_option *options)
839{
4945ba12 840 struct fio_option *o;
13335ddb 841
a3d741fa
JA
842 dprint(FD_PARSE, "init options\n");
843
4945ba12 844 for (o = &options[0]; o->name; o++) {
15ca150e
JA
845 if (o->type == FIO_OPT_DEPRECATED)
846 continue;
13335ddb
JA
847 if (o->type == FIO_OPT_BOOL) {
848 o->minval = 0;
849 o->maxval = 1;
850 }
5ec10eaa
JA
851 if (o->type == FIO_OPT_STR_SET && o->def) {
852 fprintf(stderr, "Option %s: string set option with"
853 " default will always be true\n",
854 o->name);
855 }
856 if (!o->cb && !o->off1) {
857 fprintf(stderr, "Option %s: neither cb nor offset"
858 " given\n", o->name);
859 }
af52b345 860 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
b1ec1da6 861 continue;
5ec10eaa
JA
862 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
863 fprintf(stderr, "Option %s: both cb and offset given\n",
864 o->name);
865 }
13335ddb
JA
866 }
867}