Use setvbuf() for log writing
[fio.git] / parse.c
... / ...
CommitLineData
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>
11#include <stdlib.h>
12#include <math.h>
13#include <float.h>
14
15#include "parse.h"
16#include "debug.h"
17#include "options.h"
18#include "minmax.h"
19#include "lib/ieee754.h"
20
21static struct fio_option *__fio_options;
22
23static int vp_cmp(const void *p1, const void *p2)
24{
25 const struct value_pair *vp1 = p1;
26 const struct value_pair *vp2 = p2;
27
28 return strlen(vp2->ival) - strlen(vp1->ival);
29}
30
31static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
32{
33 const struct value_pair *vp;
34 int entries;
35
36 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
37
38 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
39 vp = &o->posval[entries];
40 if (!vp->ival || vp->ival[0] == '\0')
41 break;
42
43 memcpy(&vpmap[entries], vp, sizeof(*vp));
44 }
45
46 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
47}
48
49static void show_option_range(struct fio_option *o,
50 int (*logger)(const char *format, ...))
51{
52 if (o->type == FIO_OPT_FLOAT_LIST) {
53 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
54 return;
55
56 logger("%20s: min=%f", "range", o->minfp);
57 if (o->maxfp != DBL_MAX)
58 logger(", max=%f", o->maxfp);
59 logger("\n");
60 } else if (!o->posval[0].ival) {
61 if (!o->minval && !o->maxval)
62 return;
63
64 logger("%20s: min=%d", "range", o->minval);
65 if (o->maxval)
66 logger(", max=%d", o->maxval);
67 logger("\n");
68 }
69}
70
71static void show_option_values(struct fio_option *o)
72{
73 int i;
74
75 for (i = 0; i < PARSE_MAX_VP; i++) {
76 const struct value_pair *vp = &o->posval[i];
77
78 if (!vp->ival)
79 continue;
80
81 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
82 if (vp->help)
83 log_info(" %s", vp->help);
84 log_info("\n");
85 }
86
87 if (i)
88 log_info("\n");
89}
90
91static void show_option_help(struct fio_option *o, int is_err)
92{
93 const char *typehelp[] = {
94 "invalid",
95 "string (opt=bla)",
96 "string (opt=bla)",
97 "string with possible k/m/g postfix (opt=4k)",
98 "string with time postfix (opt=10s)",
99 "string (opt=bla)",
100 "string with dual range (opt=1k-4k,4k-8k)",
101 "integer value (opt=100)",
102 "boolean value (opt=1)",
103 "list of floating point values separated by ':' (opt=5.9:7.8)",
104 "no argument (opt)",
105 "deprecated",
106 };
107 int (*logger)(const char *format, ...);
108
109 if (is_err)
110 logger = log_err;
111 else
112 logger = log_info;
113
114 if (o->alias)
115 logger("%20s: %s\n", "alias", o->alias);
116
117 logger("%20s: %s\n", "type", typehelp[o->type]);
118 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
119 if (o->prof_name)
120 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
121 show_option_range(o, logger);
122 show_option_values(o);
123}
124
125static unsigned long long get_mult_time(const char *str, int len,
126 int is_seconds)
127{
128 const char *p = str;
129 char *c;
130 unsigned long long mult = 1;
131
132 /*
133 * Go forward until we hit a non-digit, or +/- sign
134 */
135 while ((p - str) <= len) {
136 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
137 break;
138 p++;
139 }
140
141 if (!isalpha((int) *p)) {
142 if (is_seconds)
143 return 1000000UL;
144 else
145 return 1;
146 }
147
148 c = strdup(p);
149 for (int i = 0; i < strlen(c); i++)
150 c[i] = tolower(c[i]);
151
152 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
153 mult = 1;
154 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
155 mult = 1000;
156 else if (!strcmp("s", c))
157 mult = 1000000;
158 else if (!strcmp("m", c))
159 mult = 60 * 1000000UL;
160 else if (!strcmp("h", c))
161 mult = 60 * 60 * 1000000UL;
162 else if (!strcmp("d", c))
163 mult = 24 * 60 * 60 * 1000000UL;
164
165 free(c);
166 return mult;
167}
168
169static int is_separator(char c)
170{
171 switch (c) {
172 case ':':
173 case '-':
174 case ',':
175 case '/':
176 return 1;
177 default:
178 return 0;
179 }
180}
181
182static unsigned long long __get_mult_bytes(const char *p, void *data,
183 int *percent)
184{
185 unsigned int kb_base = fio_get_kb_base(data);
186 unsigned long long ret = 1;
187 unsigned int i, pow = 0, mult = kb_base;
188 char *c;
189
190 if (!p)
191 return 1;
192
193 c = strdup(p);
194
195 for (i = 0; i < strlen(c); i++) {
196 c[i] = tolower(c[i]);
197 if (is_separator(c[i])) {
198 c[i] = '\0';
199 break;
200 }
201 }
202
203 if (!strncmp("pib", c, 3)) {
204 pow = 5;
205 mult = 1000;
206 } else if (!strncmp("tib", c, 3)) {
207 pow = 4;
208 mult = 1000;
209 } else if (!strncmp("gib", c, 3)) {
210 pow = 3;
211 mult = 1000;
212 } else if (!strncmp("mib", c, 3)) {
213 pow = 2;
214 mult = 1000;
215 } else if (!strncmp("kib", c, 3)) {
216 pow = 1;
217 mult = 1000;
218 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2))
219 pow = 5;
220 else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2))
221 pow = 4;
222 else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2))
223 pow = 3;
224 else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2))
225 pow = 2;
226 else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2))
227 pow = 1;
228 else if (!strncmp("%", c, 1)) {
229 *percent = 1;
230 free(c);
231 return ret;
232 }
233
234 while (pow--)
235 ret *= (unsigned long long) mult;
236
237 free(c);
238 return ret;
239}
240
241static unsigned long long get_mult_bytes(const char *str, int len, void *data,
242 int *percent)
243{
244 const char *p = str;
245 int digit_seen = 0;
246
247 if (len < 2)
248 return __get_mult_bytes(str, data, percent);
249
250 /*
251 * Go forward until we hit a non-digit, or +/- sign
252 */
253 while ((p - str) <= len) {
254 if (!isdigit((int) *p) &&
255 (((*p != '+') && (*p != '-')) || digit_seen))
256 break;
257 digit_seen |= isdigit((int) *p);
258 p++;
259 }
260
261 if (!isalpha((int) *p) && (*p != '%'))
262 p = NULL;
263
264 return __get_mult_bytes(p, data, percent);
265}
266
267/*
268 * Convert string into a floating number. Return 1 for success and 0 otherwise.
269 */
270int str_to_float(const char *str, double *val)
271{
272 return (1 == sscanf(str, "%lf", val));
273}
274
275/*
276 * convert string into decimal value, noting any size suffix
277 */
278int str_to_decimal(const char *str, long long *val, int kilo, void *data,
279 int is_seconds)
280{
281 int len, base;
282
283 len = strlen(str);
284 if (!len)
285 return 1;
286
287 if (strstr(str, "0x") || strstr(str, "0X"))
288 base = 16;
289 else
290 base = 10;
291
292 *val = strtoll(str, NULL, base);
293 if (*val == LONG_MAX && errno == ERANGE)
294 return 1;
295
296 if (kilo) {
297 unsigned long long mult;
298 int perc = 0;
299
300 mult = get_mult_bytes(str, len, data, &perc);
301 if (perc)
302 *val = -1ULL - *val;
303 else
304 *val *= mult;
305 } else
306 *val *= get_mult_time(str, len, is_seconds);
307
308 return 0;
309}
310
311int check_str_bytes(const char *p, long long *val, void *data)
312{
313 return str_to_decimal(p, val, 1, data, 0);
314}
315
316int check_str_time(const char *p, long long *val, int is_seconds)
317{
318 return str_to_decimal(p, val, 0, NULL, is_seconds);
319}
320
321void strip_blank_front(char **p)
322{
323 char *s = *p;
324
325 if (!strlen(s))
326 return;
327 while (isspace((int) *s))
328 s++;
329
330 *p = s;
331}
332
333void strip_blank_end(char *p)
334{
335 char *start = p, *s;
336
337 if (!strlen(p))
338 return;
339
340 s = strchr(p, ';');
341 if (s)
342 *s = '\0';
343 s = strchr(p, '#');
344 if (s)
345 *s = '\0';
346 if (s)
347 p = s;
348
349 s = p + strlen(p);
350 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
351 s--;
352
353 *(s + 1) = '\0';
354}
355
356static int check_range_bytes(const char *str, long *val, void *data)
357{
358 long long __val;
359
360 if (!str_to_decimal(str, &__val, 1, data, 0)) {
361 *val = __val;
362 return 0;
363 }
364
365 return 1;
366}
367
368static int check_int(const char *p, int *val)
369{
370 if (!strlen(p))
371 return 1;
372 if (strstr(p, "0x") || strstr(p, "0X")) {
373 if (sscanf(p, "%x", val) == 1)
374 return 0;
375 } else {
376 if (sscanf(p, "%u", val) == 1)
377 return 0;
378 }
379
380 return 1;
381}
382
383static int opt_len(const char *str)
384{
385 char *postfix;
386
387 postfix = strchr(str, ':');
388 if (!postfix)
389 return strlen(str);
390
391 return (int)(postfix - str);
392}
393
394static int str_match_len(const struct value_pair *vp, const char *str)
395{
396 return max(strlen(vp->ival), opt_len(str));
397}
398
399#define val_store(ptr, val, off, or, data, o) \
400 do { \
401 ptr = td_var((data), (o), (off)); \
402 if ((or)) \
403 *ptr |= (val); \
404 else \
405 *ptr = (val); \
406 } while (0)
407
408static int __handle_option(struct fio_option *o, const char *ptr, void *data,
409 int first, int more, int curr)
410{
411 int il=0, *ilp;
412 fio_fp64_t *flp;
413 long long ull, *ullp;
414 long ul1, ul2;
415 double uf;
416 char **cp = NULL;
417 int ret = 0, is_time = 0;
418 const struct value_pair *vp;
419 struct value_pair posval[PARSE_MAX_VP];
420 int i, all_skipped = 1;
421
422 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
423 o->type, ptr);
424
425 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
426 log_err("Option %s requires an argument\n", o->name);
427 return 1;
428 }
429
430 switch (o->type) {
431 case FIO_OPT_STR:
432 case FIO_OPT_STR_MULTI: {
433 fio_opt_str_fn *fn = o->cb;
434
435 posval_sort(o, posval);
436
437 ret = 1;
438 for (i = 0; i < PARSE_MAX_VP; i++) {
439 vp = &posval[i];
440 if (!vp->ival || vp->ival[0] == '\0')
441 continue;
442 all_skipped = 0;
443 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
444 ret = 0;
445 if (o->off1)
446 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
447 continue;
448 }
449 }
450
451 if (ret && !all_skipped)
452 show_option_values(o);
453 else if (fn)
454 ret = fn(data, ptr);
455 break;
456 }
457 case FIO_OPT_STR_VAL_TIME:
458 is_time = 1;
459 case FIO_OPT_INT:
460 case FIO_OPT_STR_VAL: {
461 fio_opt_str_val_fn *fn = o->cb;
462 char tmp[128], *p;
463
464 strncpy(tmp, ptr, sizeof(tmp) - 1);
465 p = strchr(tmp, ',');
466 if (p)
467 *p = '\0';
468
469 if (is_time)
470 ret = check_str_time(tmp, &ull, o->is_seconds);
471 else
472 ret = check_str_bytes(tmp, &ull, data);
473
474 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
475
476 if (ret)
477 break;
478
479 if (o->maxval && ull > o->maxval) {
480 log_err("max value out of range: %llu"
481 " (%u max)\n", ull, o->maxval);
482 return 1;
483 }
484 if (o->minval && ull < o->minval) {
485 log_err("min value out of range: %llu"
486 " (%u min)\n", ull, o->minval);
487 return 1;
488 }
489 if (o->posval[0].ival) {
490 posval_sort(o, posval);
491
492 ret = 1;
493 for (i = 0; i < PARSE_MAX_VP; i++) {
494 vp = &posval[i];
495 if (!vp->ival || vp->ival[0] == '\0')
496 continue;
497 if (vp->oval == ull) {
498 ret = 0;
499 break;
500 }
501 }
502 if (ret) {
503 log_err("fio: value %llu not allowed:\n", ull);
504 show_option_values(o);
505 return 1;
506 }
507 }
508
509 if (fn)
510 ret = fn(data, &ull);
511 else {
512 if (o->type == FIO_OPT_INT) {
513 if (first)
514 val_store(ilp, ull, o->off1, 0, data, o);
515 if (curr == 1) {
516 if (o->off2)
517 val_store(ilp, ull, o->off2, 0, data, o);
518 }
519 if (curr == 2) {
520 if (o->off3)
521 val_store(ilp, ull, o->off3, 0, data, o);
522 }
523 if (!more) {
524 if (curr < 1) {
525 if (o->off2)
526 val_store(ilp, ull, o->off2, 0, data, o);
527 }
528 if (curr < 2) {
529 if (o->off3)
530 val_store(ilp, ull, o->off3, 0, data, o);
531 }
532 }
533 } else {
534 if (first)
535 val_store(ullp, ull, o->off1, 0, data, o);
536 if (!more) {
537 if (o->off2)
538 val_store(ullp, ull, o->off2, 0, data, o);
539 }
540 }
541 }
542 break;
543 }
544 case FIO_OPT_FLOAT_LIST: {
545 char *cp2;
546
547 if (first) {
548 /*
549 ** Initialize precision to 0 and zero out list
550 ** in case specified list is shorter than default
551 */
552 if (o->off2) {
553 ul2 = 0;
554 ilp = td_var(data, o, o->off2);
555 *ilp = ul2;
556 }
557
558 flp = td_var(data, o, o->off1);
559 for(i = 0; i < o->maxlen; i++)
560 flp[i].u.f = 0.0;
561 }
562 if (curr >= o->maxlen) {
563 log_err("the list exceeding max length %d\n",
564 o->maxlen);
565 return 1;
566 }
567 if (!str_to_float(ptr, &uf)) {
568 log_err("not a floating point value: %s\n", ptr);
569 return 1;
570 }
571 if (uf > o->maxfp) {
572 log_err("value out of range: %f"
573 " (range max: %f)\n", uf, o->maxfp);
574 return 1;
575 }
576 if (uf < o->minfp) {
577 log_err("value out of range: %f"
578 " (range min: %f)\n", uf, o->minfp);
579 return 1;
580 }
581
582 flp = td_var(data, o, o->off1);
583 flp[curr].u.f = uf;
584
585 dprint(FD_PARSE, " out=%f\n", uf);
586
587 /*
588 ** Calculate precision for output by counting
589 ** number of digits after period. Find first
590 ** period in entire remaining list each time
591 */
592 cp2 = strchr(ptr, '.');
593 if (cp2 != NULL) {
594 int len = 0;
595
596 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
597 len++;
598
599 if (o->off2) {
600 ilp = td_var(data, o, o->off2);
601 if (len > *ilp)
602 *ilp = len;
603 }
604 }
605
606 break;
607 }
608 case FIO_OPT_STR_STORE: {
609 fio_opt_str_fn *fn = o->cb;
610
611 if (o->off1) {
612 cp = td_var(data, o, o->off1);
613 *cp = strdup(ptr);
614 }
615
616 if (fn)
617 ret = fn(data, ptr);
618 else if (o->posval[0].ival) {
619 posval_sort(o, posval);
620
621 ret = 1;
622 for (i = 0; i < PARSE_MAX_VP; i++) {
623 vp = &posval[i];
624 if (!vp->ival || vp->ival[0] == '\0')
625 continue;
626 all_skipped = 0;
627 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
628 char *rest;
629
630 ret = 0;
631 if (vp->cb)
632 fn = vp->cb;
633 rest = strstr(*cp ?: ptr, ":");
634 if (rest) {
635 if (*cp)
636 *rest = '\0';
637 ptr = rest + 1;
638 } else
639 ptr = NULL;
640 break;
641 }
642 }
643 }
644
645 if (!all_skipped) {
646 if (ret && !*cp)
647 show_option_values(o);
648 else if (ret && *cp)
649 ret = 0;
650 else if (fn && ptr)
651 ret = fn(data, ptr);
652 }
653
654 break;
655 }
656 case FIO_OPT_RANGE: {
657 char tmp[128];
658 char *p1, *p2;
659
660 strncpy(tmp, ptr, sizeof(tmp) - 1);
661
662 /* Handle bsrange with separate read,write values: */
663 p1 = strchr(tmp, ',');
664 if (p1)
665 *p1 = '\0';
666
667 p1 = strchr(tmp, '-');
668 if (!p1) {
669 p1 = strchr(tmp, ':');
670 if (!p1) {
671 ret = 1;
672 break;
673 }
674 }
675
676 p2 = p1 + 1;
677 *p1 = '\0';
678 p1 = tmp;
679
680 ret = 1;
681 if (!check_range_bytes(p1, &ul1, data) &&
682 !check_range_bytes(p2, &ul2, data)) {
683 ret = 0;
684 if (ul1 > ul2) {
685 unsigned long foo = ul1;
686
687 ul1 = ul2;
688 ul2 = foo;
689 }
690
691 if (first) {
692 val_store(ilp, ul1, o->off1, 0, data, o);
693 val_store(ilp, ul2, o->off2, 0, data, o);
694 }
695 if (curr == 1) {
696 if (o->off3 && o->off4) {
697 val_store(ilp, ul1, o->off3, 0, data, o);
698 val_store(ilp, ul2, o->off4, 0, data, o);
699 }
700 }
701 if (curr == 2) {
702 if (o->off5 && o->off6) {
703 val_store(ilp, ul1, o->off5, 0, data, o);
704 val_store(ilp, ul2, o->off6, 0, data, o);
705 }
706 }
707 if (!more) {
708 if (curr < 1) {
709 if (o->off3 && o->off4) {
710 val_store(ilp, ul1, o->off3, 0, data, o);
711 val_store(ilp, ul2, o->off4, 0, data, o);
712 }
713 }
714 if (curr < 2) {
715 if (o->off5 && o->off6) {
716 val_store(ilp, ul1, o->off5, 0, data, o);
717 val_store(ilp, ul2, o->off6, 0, data, o);
718 }
719 }
720 }
721 }
722
723 break;
724 }
725 case FIO_OPT_BOOL:
726 case FIO_OPT_STR_SET: {
727 fio_opt_int_fn *fn = o->cb;
728
729 if (ptr)
730 ret = check_int(ptr, &il);
731 else if (o->type == FIO_OPT_BOOL)
732 ret = 1;
733 else
734 il = 1;
735
736 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
737
738 if (ret)
739 break;
740
741 if (o->maxval && il > (int) o->maxval) {
742 log_err("max value out of range: %d (%d max)\n",
743 il, o->maxval);
744 return 1;
745 }
746 if (o->minval && il < o->minval) {
747 log_err("min value out of range: %d (%d min)\n",
748 il, o->minval);
749 return 1;
750 }
751
752 if (o->neg)
753 il = !il;
754
755 if (fn)
756 ret = fn(data, &il);
757 else {
758 if (first)
759 val_store(ilp, il, o->off1, 0, data, o);
760 if (!more) {
761 if (o->off2)
762 val_store(ilp, il, o->off2, 0, data, o);
763 }
764 }
765 break;
766 }
767 case FIO_OPT_DEPRECATED:
768 log_info("Option %s is deprecated\n", o->name);
769 ret = 1;
770 break;
771 default:
772 log_err("Bad option type %u\n", o->type);
773 ret = 1;
774 }
775
776 if (ret)
777 return ret;
778
779 if (o->verify) {
780 ret = o->verify(o, data);
781 if (ret) {
782 log_err("Correct format for offending option\n");
783 log_err("%20s: %s\n", o->name, o->help);
784 show_option_help(o, 1);
785 }
786 }
787
788 return ret;
789}
790
791static int handle_option(struct fio_option *o, const char *__ptr, void *data)
792{
793 char *o_ptr, *ptr, *ptr2;
794 int ret, done;
795
796 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
797
798 o_ptr = ptr = NULL;
799 if (__ptr)
800 o_ptr = ptr = strdup(__ptr);
801
802 /*
803 * See if we have another set of parameters, hidden after a comma.
804 * Do this before parsing this round, to check if we should
805 * copy set 1 options to set 2.
806 */
807 done = 0;
808 ret = 1;
809 do {
810 int __ret;
811
812 ptr2 = NULL;
813 if (ptr &&
814 (o->type != FIO_OPT_STR_STORE) &&
815 (o->type != FIO_OPT_STR) &&
816 (o->type != FIO_OPT_FLOAT_LIST)) {
817 ptr2 = strchr(ptr, ',');
818 if (ptr2 && *(ptr2 + 1) == '\0')
819 *ptr2 = '\0';
820 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
821 if (!ptr2)
822 ptr2 = strchr(ptr, ':');
823 if (!ptr2)
824 ptr2 = strchr(ptr, '-');
825 }
826 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
827 ptr2 = strchr(ptr, ':');
828 }
829
830 /*
831 * Don't return early if parsing the first option fails - if
832 * we are doing multiple arguments, we can allow the first one
833 * being empty.
834 */
835 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
836 if (ret)
837 ret = __ret;
838
839 if (!ptr2)
840 break;
841
842 ptr = ptr2 + 1;
843 done++;
844 } while (1);
845
846 if (o_ptr)
847 free(o_ptr);
848 return ret;
849}
850
851static struct fio_option *get_option(char *opt,
852 struct fio_option *options, char **post)
853{
854 struct fio_option *o;
855 char *ret;
856
857 ret = strchr(opt, '=');
858 if (ret) {
859 *post = ret;
860 *ret = '\0';
861 ret = opt;
862 (*post)++;
863 strip_blank_end(ret);
864 o = find_option(options, ret);
865 } else {
866 o = find_option(options, opt);
867 *post = NULL;
868 }
869
870 return o;
871}
872
873static int opt_cmp(const void *p1, const void *p2)
874{
875 struct fio_option *o;
876 char *s, *foo;
877 int prio1, prio2;
878
879 prio1 = prio2 = 0;
880
881 if (*(char **)p1) {
882 s = strdup(*((char **) p1));
883 o = get_option(s, __fio_options, &foo);
884 if (o)
885 prio1 = o->prio;
886 free(s);
887 }
888 if (*(char **)p2) {
889 s = strdup(*((char **) p2));
890 o = get_option(s, __fio_options, &foo);
891 if (o)
892 prio2 = o->prio;
893 free(s);
894 }
895
896 return prio2 - prio1;
897}
898
899void sort_options(char **opts, struct fio_option *options, int num_opts)
900{
901 __fio_options = options;
902 qsort(opts, num_opts, sizeof(char *), opt_cmp);
903 __fio_options = NULL;
904}
905
906int parse_cmd_option(const char *opt, const char *val,
907 struct fio_option *options, void *data)
908{
909 struct fio_option *o;
910
911 o = find_option(options, opt);
912 if (!o) {
913 log_err("Bad option <%s>\n", opt);
914 return 1;
915 }
916
917 if (!handle_option(o, val, data))
918 return 0;
919
920 log_err("fio: failed parsing %s=%s\n", opt, val);
921 return 1;
922}
923
924int parse_option(char *opt, const char *input,
925 struct fio_option *options, struct fio_option **o, void *data,
926 int dump_cmdline)
927{
928 char *post;
929
930 if (!opt) {
931 log_err("fio: failed parsing %s\n", input);
932 *o = NULL;
933 return 1;
934 }
935
936 *o = get_option(opt, options, &post);
937 if (!*o) {
938 if (post) {
939 int len = strlen(opt);
940 if (opt + len + 1 != post)
941 memmove(opt + len + 1, post, strlen(post));
942 opt[len] = '=';
943 }
944 return 1;
945 }
946
947 if (handle_option(*o, post, data)) {
948 log_err("fio: failed parsing %s\n", input);
949 return 1;
950 }
951
952 if (dump_cmdline) {
953 const char *delim;
954
955 if (!strcmp("description", (*o)->name))
956 delim = "\"";
957 else
958 delim = "";
959
960 log_info("--%s%s", (*o)->name, post ? "" : " ");
961 if (post)
962 log_info("=%s%s%s ", delim, post, delim);
963 }
964
965 return 0;
966}
967
968/*
969 * Option match, levenshtein distance. Handy for not quite remembering what
970 * the option name is.
971 */
972static int string_distance(const char *s1, const char *s2)
973{
974 unsigned int s1_len = strlen(s1);
975 unsigned int s2_len = strlen(s2);
976 unsigned int *p, *q, *r;
977 unsigned int i, j;
978
979 p = malloc(sizeof(unsigned int) * (s2_len + 1));
980 q = malloc(sizeof(unsigned int) * (s2_len + 1));
981
982 p[0] = 0;
983 for (i = 1; i <= s2_len; i++)
984 p[i] = p[i - 1] + 1;
985
986 for (i = 1; i <= s1_len; i++) {
987 q[0] = p[0] + 1;
988 for (j = 1; j <= s2_len; j++) {
989 unsigned int sub = p[j - 1];
990
991 if (s1[i - 1] != s2[j - 1])
992 sub++;
993
994 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
995 }
996 r = p;
997 p = q;
998 q = r;
999 }
1000
1001 i = p[s2_len];
1002 free(p);
1003 free(q);
1004 return i;
1005}
1006
1007static struct fio_option *find_child(struct fio_option *options,
1008 struct fio_option *o)
1009{
1010 struct fio_option *__o;
1011
1012 for (__o = options + 1; __o->name; __o++)
1013 if (__o->parent && !strcmp(__o->parent, o->name))
1014 return __o;
1015
1016 return NULL;
1017}
1018
1019static void __print_option(struct fio_option *o, struct fio_option *org,
1020 int level)
1021{
1022 char name[256], *p;
1023 int depth;
1024
1025 if (!o)
1026 return;
1027 if (!org)
1028 org = o;
1029
1030 p = name;
1031 depth = level;
1032 while (depth--)
1033 p += sprintf(p, "%s", " ");
1034
1035 sprintf(p, "%s", o->name);
1036
1037 log_info("%-24s: %s\n", name, o->help);
1038}
1039
1040static void print_option(struct fio_option *o)
1041{
1042 struct fio_option *parent;
1043 struct fio_option *__o;
1044 unsigned int printed;
1045 unsigned int level;
1046
1047 __print_option(o, NULL, 0);
1048 parent = o;
1049 level = 0;
1050 do {
1051 level++;
1052 printed = 0;
1053
1054 while ((__o = find_child(o, parent)) != NULL) {
1055 __print_option(__o, o, level);
1056 o = __o;
1057 printed++;
1058 }
1059
1060 parent = o;
1061 } while (printed);
1062}
1063
1064int show_cmd_help(struct fio_option *options, const char *name)
1065{
1066 struct fio_option *o, *closest;
1067 unsigned int best_dist = -1U;
1068 int found = 0;
1069 int show_all = 0;
1070
1071 if (!name || !strcmp(name, "all"))
1072 show_all = 1;
1073
1074 closest = NULL;
1075 best_dist = -1;
1076 for (o = &options[0]; o->name; o++) {
1077 int match = 0;
1078
1079 if (o->type == FIO_OPT_DEPRECATED)
1080 continue;
1081 if (!exec_profile && o->prof_name)
1082 continue;
1083 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1084 continue;
1085
1086 if (name) {
1087 if (!strcmp(name, o->name) ||
1088 (o->alias && !strcmp(name, o->alias)))
1089 match = 1;
1090 else {
1091 unsigned int dist;
1092
1093 dist = string_distance(name, o->name);
1094 if (dist < best_dist) {
1095 best_dist = dist;
1096 closest = o;
1097 }
1098 }
1099 }
1100
1101 if (show_all || match) {
1102 found = 1;
1103 if (match)
1104 log_info("%20s: %s\n", o->name, o->help);
1105 if (show_all) {
1106 if (!o->parent)
1107 print_option(o);
1108 continue;
1109 }
1110 }
1111
1112 if (!match)
1113 continue;
1114
1115 show_option_help(o, 0);
1116 }
1117
1118 if (found)
1119 return 0;
1120
1121 log_err("No such command: %s", name);
1122
1123 /*
1124 * Only print an appropriately close option, one where the edit
1125 * distance isn't too big. Otherwise we get crazy matches.
1126 */
1127 if (closest && best_dist < 3) {
1128 log_info(" - showing closest match\n");
1129 log_info("%20s: %s\n", closest->name, closest->help);
1130 show_option_help(closest, 0);
1131 } else
1132 log_info("\n");
1133
1134 return 1;
1135}
1136
1137/*
1138 * Handle parsing of default parameters.
1139 */
1140void fill_default_options(void *data, struct fio_option *options)
1141{
1142 struct fio_option *o;
1143
1144 dprint(FD_PARSE, "filling default options\n");
1145
1146 for (o = &options[0]; o->name; o++)
1147 if (o->def)
1148 handle_option(o, o->def, data);
1149}
1150
1151void option_init(struct fio_option *o)
1152{
1153 if (o->type == FIO_OPT_DEPRECATED)
1154 return;
1155 if (o->type == FIO_OPT_BOOL) {
1156 o->minval = 0;
1157 o->maxval = 1;
1158 }
1159 if (o->type == FIO_OPT_INT) {
1160 if (!o->maxval)
1161 o->maxval = UINT_MAX;
1162 }
1163 if (o->type == FIO_OPT_FLOAT_LIST) {
1164 o->minfp = DBL_MIN;
1165 o->maxfp = DBL_MAX;
1166 }
1167 if (o->type == FIO_OPT_STR_SET && o->def) {
1168 log_err("Option %s: string set option with"
1169 " default will always be true\n", o->name);
1170 }
1171 if (!o->cb && !o->off1)
1172 log_err("Option %s: neither cb nor offset given\n", o->name);
1173 if (!o->category) {
1174 log_info("Option %s: no category defined. Setting to misc\n", o->name);
1175 o->category = FIO_OPT_C_GENERAL;
1176 o->group = FIO_OPT_G_INVALID;
1177 }
1178 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1179 o->type == FIO_OPT_STR_MULTI)
1180 return;
1181 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
1182 log_err("Option %s: both cb and offset given\n", o->name);
1183}
1184
1185/*
1186 * Sanitize the options structure. For now it just sets min/max for bool
1187 * values and whether both callback and offsets are given.
1188 */
1189void options_init(struct fio_option *options)
1190{
1191 struct fio_option *o;
1192
1193 dprint(FD_PARSE, "init options\n");
1194
1195 for (o = &options[0]; o->name; o++) {
1196 option_init(o);
1197 if (o->inverse)
1198 o->inv_opt = find_option(options, o->inverse);
1199 }
1200}
1201
1202void options_free(struct fio_option *options, void *data)
1203{
1204 struct fio_option *o;
1205 char **ptr;
1206
1207 dprint(FD_PARSE, "free options\n");
1208
1209 for (o = &options[0]; o->name; o++) {
1210 if (o->type != FIO_OPT_STR_STORE || !o->off1)
1211 continue;
1212
1213 ptr = td_var(data, o, o->off1);
1214 if (*ptr) {
1215 free(*ptr);
1216 *ptr = NULL;
1217 }
1218 }
1219}