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