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