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