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