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