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