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