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