Add check for OPT_LEN_MAX being too small
[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 if (strlen(in) + 1 > OPT_LEN_MAX) {
624 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
625 return NULL;
626 }
627
628 in[OPT_LEN_MAX] = '\0';
629 strncpy(in, opt, OPT_LEN_MAX);
630
631 while (*inptr && nchr > 0) {
632 if (inptr[0] == '$' && inptr[1] == '{') {
633 ch2 = strchr(inptr, '}');
634 if (ch2 && inptr+1 < ch2) {
635 ch1 = inptr+2;
636 inptr = ch2+1;
637 *ch2 = '\0';
638
639 env = getenv(ch1);
640 if (env) {
641 envlen = strlen(env);
642 if (envlen <= nchr) {
643 memcpy(outptr, env, envlen);
644 outptr += envlen;
645 nchr -= envlen;
646 }
647 }
648
649 continue;
650 }
651 }
652
653 *outptr++ = *inptr++;
654 --nchr;
655 }
656
657 *outptr = '\0';
658 return strdup(out);
659}
660
661int parse_option(const char *opt, struct fio_option *options, void *data)
662{
663 struct fio_option *o;
664 char *post, *tmp;
665
666 tmp = option_dup_subs(opt);
667 if (!tmp)
668 return 1;
669
670 o = get_option(tmp, options, &post);
671 if (!o) {
672 fprintf(stderr, "Bad option <%s>\n", tmp);
673 free(tmp);
674 return 1;
675 }
676
677 if (!handle_option(o, post, data)) {
678 free(tmp);
679 return 0;
680 }
681
682 fprintf(stderr, "fio: failed parsing %s\n", opt);
683 free(tmp);
684 return 1;
685}
686
687/*
688 * Option match, levenshtein distance. Handy for not quite remembering what
689 * the option name is.
690 */
691static int string_distance(const char *s1, const char *s2)
692{
693 unsigned int s1_len = strlen(s1);
694 unsigned int s2_len = strlen(s2);
695 unsigned int *p, *q, *r;
696 unsigned int i, j;
697
698 p = malloc(sizeof(unsigned int) * (s2_len + 1));
699 q = malloc(sizeof(unsigned int) * (s2_len + 1));
700
701 p[0] = 0;
702 for (i = 1; i <= s2_len; i++)
703 p[i] = p[i - 1] + 1;
704
705 for (i = 1; i <= s1_len; i++) {
706 q[0] = p[0] + 1;
707 for (j = 1; j <= s2_len; j++) {
708 unsigned int sub = p[j - 1];
709
710 if (s1[i - 1] != s2[j - 1])
711 sub++;
712
713 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
714 }
715 r = p;
716 p = q;
717 q = r;
718 }
719
720 i = p[s2_len];
721 free(p);
722 free(q);
723 return i;
724}
725
726static struct fio_option *find_child(struct fio_option *options,
727 struct fio_option *o)
728{
729 struct fio_option *__o;
730
731 for (__o = options + 1; __o->name; __o++)
732 if (__o->parent && !strcmp(__o->parent, o->name))
733 return __o;
734
735 return NULL;
736}
737
738static void __print_option(struct fio_option *o, struct fio_option *org,
739 int level)
740{
741 char name[256], *p;
742 int depth;
743
744 if (!o)
745 return;
746 if (!org)
747 org = o;
748
749 p = name;
750 depth = level;
751 while (depth--)
752 p += sprintf(p, "%s", " ");
753
754 sprintf(p, "%s", o->name);
755
756 printf("%-24s: %s\n", name, o->help);
757}
758
759static void print_option(struct fio_option *o)
760{
761 struct fio_option *parent;
762 struct fio_option *__o;
763 unsigned int printed;
764 unsigned int level;
765
766 __print_option(o, NULL, 0);
767 parent = o;
768 level = 0;
769 do {
770 level++;
771 printed = 0;
772
773 while ((__o = find_child(o, parent)) != NULL) {
774 __print_option(__o, o, level);
775 o = __o;
776 printed++;
777 }
778
779 parent = o;
780 } while (printed);
781}
782
783int show_cmd_help(struct fio_option *options, const char *name)
784{
785 struct fio_option *o, *closest;
786 unsigned int best_dist;
787 int found = 0;
788 int show_all = 0;
789
790 if (!name || !strcmp(name, "all"))
791 show_all = 1;
792
793 closest = NULL;
794 best_dist = -1;
795 for (o = &options[0]; o->name; o++) {
796 int match = 0;
797
798 if (o->type == FIO_OPT_DEPRECATED)
799 continue;
800
801 if (name) {
802 if (!strcmp(name, o->name) ||
803 (o->alias && !strcmp(name, o->alias)))
804 match = 1;
805 else {
806 unsigned int dist;
807
808 dist = string_distance(name, o->name);
809 if (dist < best_dist) {
810 best_dist = dist;
811 closest = o;
812 }
813 }
814 }
815
816 if (show_all || match) {
817 found = 1;
818 if (match)
819 printf("%24s: %s\n", o->name, o->help);
820 if (show_all) {
821 if (!o->parent)
822 print_option(o);
823 continue;
824 }
825 }
826
827 if (!match)
828 continue;
829
830 show_option_help(o, stdout);
831 }
832
833 if (found)
834 return 0;
835
836 printf("No such command: %s", name);
837 if (closest) {
838 printf(" - showing closest match\n");
839 printf("%20s: %s\n", closest->name, closest->help);
840 show_option_help(closest, stdout);
841 } else
842 printf("\n");
843
844 return 1;
845}
846
847/*
848 * Handle parsing of default parameters.
849 */
850void fill_default_options(void *data, struct fio_option *options)
851{
852 struct fio_option *o;
853
854 dprint(FD_PARSE, "filling default options\n");
855
856 for (o = &options[0]; o->name; o++)
857 if (o->def)
858 handle_option(o, o->def, data);
859}
860
861/*
862 * Sanitize the options structure. For now it just sets min/max for bool
863 * values and whether both callback and offsets are given.
864 */
865void options_init(struct fio_option *options)
866{
867 struct fio_option *o;
868
869 dprint(FD_PARSE, "init options\n");
870
871 for (o = &options[0]; o->name; o++) {
872 if (o->type == FIO_OPT_DEPRECATED)
873 continue;
874 if (o->type == FIO_OPT_BOOL) {
875 o->minval = 0;
876 o->maxval = 1;
877 }
878 if (o->type == FIO_OPT_STR_SET && o->def) {
879 fprintf(stderr, "Option %s: string set option with"
880 " default will always be true\n",
881 o->name);
882 }
883 if (!o->cb && !o->off1) {
884 fprintf(stderr, "Option %s: neither cb nor offset"
885 " given\n", o->name);
886 }
887 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
888 continue;
889 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
890 fprintf(stderr, "Option %s: both cb and offset given\n",
891 o->name);
892 }
893 }
894}