Allow 0x (or 0X) prefix for any int value to indicate hex base
[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_STR_VAL:
281 case FIO_OPT_STR_VAL_INT: {
282 fio_opt_str_val_fn *fn = o->cb;
283
284 if (is_time)
285 ret = check_str_time(ptr, &ull);
286 else
287 ret = check_str_bytes(ptr, &ull);
288
289 if (ret)
290 break;
291
292 if (o->maxval && ull > o->maxval) {
293 fprintf(stderr, "max value out of range: %lld"
294 " (%d max)\n", ull, o->maxval);
295 return 1;
296 }
297 if (o->minval && ull < o->minval) {
298 fprintf(stderr, "min value out of range: %lld"
299 " (%d min)\n", ull, o->minval);
300 return 1;
301 }
302
303 if (fn)
304 ret = fn(data, &ull);
305 else {
306 if (o->type == FIO_OPT_STR_VAL_INT) {
307 if (first)
308 val_store(ilp, ull, o->off1, data);
309 if (!more && o->off2)
310 val_store(ilp, ull, o->off2, data);
311 } else {
312 if (first)
313 val_store(ullp, ull, o->off1, data);
314 if (!more && o->off2)
315 val_store(ullp, ull, o->off2, data);
316 }
317 }
318 break;
319 }
320 case FIO_OPT_STR_STORE: {
321 fio_opt_str_fn *fn = o->cb;
322
323 cp = td_var(data, o->off1);
324 *cp = strdup(ptr);
325 if (fn) {
326 ret = fn(data, ptr);
327 if (ret) {
328 free(*cp);
329 *cp = NULL;
330 }
331 }
332 break;
333 }
334 case FIO_OPT_RANGE: {
335 char tmp[128];
336 char *p1, *p2;
337
338 strncpy(tmp, ptr, sizeof(tmp) - 1);
339
340 p1 = strchr(tmp, '-');
341 if (!p1) {
342 p1 = strchr(tmp, ':');
343 if (!p1) {
344 ret = 1;
345 break;
346 }
347 }
348
349 p2 = p1 + 1;
350 *p1 = '\0';
351 p1 = tmp;
352
353 ret = 1;
354 if (!check_range_bytes(p1, &ul1) &&
355 !check_range_bytes(p2, &ul2)) {
356 ret = 0;
357 if (ul1 > ul2) {
358 unsigned long foo = ul1;
359
360 ul1 = ul2;
361 ul2 = foo;
362 }
363
364 if (first) {
365 val_store(ilp, ul1, o->off1, data);
366 val_store(ilp, ul2, o->off2, data);
367 }
368 if (o->off3 && o->off4) {
369 val_store(ilp, ul1, o->off3, data);
370 val_store(ilp, ul2, o->off4, data);
371 }
372 }
373
374 break;
375 }
376 case FIO_OPT_INT:
377 case FIO_OPT_BOOL: {
378 fio_opt_int_fn *fn = o->cb;
379
380 ret = check_int(ptr, &il);
381 if (ret)
382 break;
383
384 if (o->maxval && il > (int) o->maxval) {
385 fprintf(stderr, "max value out of range: %d (%d max)\n",
386 il, o->maxval);
387 return 1;
388 }
389 if (o->minval && il < o->minval) {
390 fprintf(stderr, "min value out of range: %d (%d min)\n",
391 il, o->minval);
392 return 1;
393 }
394
395 if (o->neg)
396 il = !il;
397
398 if (fn)
399 ret = fn(data, &il);
400 else {
401 if (first)
402 val_store(ilp, il, o->off1, data);
403 if (!more && o->off2)
404 val_store(ilp, il, o->off2, data);
405 }
406 break;
407 }
408 case FIO_OPT_STR_SET: {
409 fio_opt_str_set_fn *fn = o->cb;
410
411 if (fn)
412 ret = fn(data);
413 else {
414 if (first)
415 val_store(ilp, 1, o->off1, data);
416 if (!more && o->off2)
417 val_store(ilp, 1, o->off2, data);
418 }
419 break;
420 }
421 case FIO_OPT_DEPRECATED:
422 fprintf(stdout, "Option %s is deprecated\n", o->name);
423 break;
424 default:
425 fprintf(stderr, "Bad option type %u\n", o->type);
426 ret = 1;
427 }
428
429 return ret;
430}
431
432static int handle_option(struct fio_option *o, const char *ptr, void *data)
433{
434 const char *ptr2 = NULL;
435 int r1, r2;
436
437 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
438
439 /*
440 * See if we have a second set of parameters, hidden after a comma.
441 * Do this before parsing the first round, to check if we should
442 * copy set 1 options to set 2.
443 */
444 if (ptr &&
445 (o->type != FIO_OPT_STR_STORE) &&
446 (o->type != FIO_OPT_STR)) {
447 ptr2 = strchr(ptr, ',');
448 if (!ptr2)
449 ptr2 = strchr(ptr, ':');
450 if (!ptr2)
451 ptr2 = strchr(ptr, '-');
452 }
453
454 /*
455 * Don't return early if parsing the first option fails - if
456 * we are doing multiple arguments, we can allow the first one
457 * being empty.
458 */
459 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
460
461 if (!ptr2)
462 return r1;
463
464 ptr2++;
465 r2 = __handle_option(o, ptr2, data, 0, 0);
466
467 return r1 && r2;
468}
469
470static struct fio_option *get_option(const char *opt,
471 struct fio_option *options, char **post)
472{
473 struct fio_option *o;
474 char *ret;
475
476 ret = strchr(opt, '=');
477 if (ret) {
478 *post = ret;
479 *ret = '\0';
480 ret = (char *) opt;
481 (*post)++;
482 strip_blank_end(ret);
483 o = find_option(options, ret);
484 } else {
485 o = find_option(options, opt);
486 *post = NULL;
487 }
488
489 return o;
490}
491
492static int opt_cmp(const void *p1, const void *p2)
493{
494 struct fio_option *o1, *o2;
495 char *s1, *s2, *foo;
496 int prio1, prio2;
497
498 s1 = strdup(*((char **) p1));
499 s2 = strdup(*((char **) p2));
500
501 o1 = get_option(s1, fio_options, &foo);
502 o2 = get_option(s2, fio_options, &foo);
503
504 prio1 = prio2 = 0;
505 if (o1)
506 prio1 = o1->prio;
507 if (o2)
508 prio2 = o2->prio;
509
510 free(s1);
511 free(s2);
512 return prio2 - prio1;
513}
514
515void sort_options(char **opts, struct fio_option *options, int num_opts)
516{
517 fio_options = options;
518 qsort(opts, num_opts, sizeof(char *), opt_cmp);
519 fio_options = NULL;
520}
521
522int parse_cmd_option(const char *opt, const char *val,
523 struct fio_option *options, void *data)
524{
525 struct fio_option *o;
526
527 o = find_option(options, opt);
528 if (!o) {
529 fprintf(stderr, "Bad option <%s>\n", opt);
530 return 1;
531 }
532
533 if (!handle_option(o, val, data))
534 return 0;
535
536 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
537 return 1;
538}
539
540/*
541 * Return a copy of the input string with substrings of the form ${VARNAME}
542 * substituted with the value of the environment variable VARNAME. The
543 * substitution always occurs, even if VARNAME is empty or the corresponding
544 * environment variable undefined.
545 */
546static char *option_dup_subs(const char *opt)
547{
548 char out[OPT_LEN_MAX+1];
549 char in[OPT_LEN_MAX+1];
550 char *outptr = out;
551 char *inptr = in;
552 char *ch1, *ch2, *env;
553 ssize_t nchr = OPT_LEN_MAX;
554 size_t envlen;
555
556 in[OPT_LEN_MAX] = '\0';
557 strncpy(in, opt, OPT_LEN_MAX);
558
559 while (*inptr && nchr > 0) {
560 if (inptr[0] == '$' && inptr[1] == '{') {
561 ch2 = strchr(inptr, '}');
562 if (ch2 && inptr+1 < ch2) {
563 ch1 = inptr+2;
564 inptr = ch2+1;
565 *ch2 = '\0';
566
567 env = getenv(ch1);
568 if (env) {
569 envlen = strlen(env);
570 if (envlen <= nchr) {
571 memcpy(outptr, env, envlen);
572 outptr += envlen;
573 nchr -= envlen;
574 }
575 }
576
577 continue;
578 }
579 }
580
581 *outptr++ = *inptr++;
582 --nchr;
583 }
584
585 *outptr = '\0';
586 return strdup(out);
587}
588
589int parse_option(const char *opt, struct fio_option *options, void *data)
590{
591 struct fio_option *o;
592 char *post, *tmp;
593
594 tmp = option_dup_subs(opt);
595
596 o = get_option(tmp, options, &post);
597 if (!o) {
598 fprintf(stderr, "Bad option <%s>\n", tmp);
599 free(tmp);
600 return 1;
601 }
602
603 if (!handle_option(o, post, data)) {
604 free(tmp);
605 return 0;
606 }
607
608 fprintf(stderr, "fio: failed parsing %s\n", opt);
609 free(tmp);
610 return 1;
611}
612
613/*
614 * Option match, levenshtein distance. Handy for not quite remembering what
615 * the option name is.
616 */
617static int string_distance(const char *s1, const char *s2)
618{
619 unsigned int s1_len = strlen(s1);
620 unsigned int s2_len = strlen(s2);
621 unsigned int *p, *q, *r;
622 unsigned int i, j;
623
624 p = malloc(sizeof(unsigned int) * (s2_len + 1));
625 q = malloc(sizeof(unsigned int) * (s2_len + 1));
626
627 p[0] = 0;
628 for (i = 1; i <= s2_len; i++)
629 p[i] = p[i - 1] + 1;
630
631 for (i = 1; i <= s1_len; i++) {
632 q[0] = p[0] + 1;
633 for (j = 1; j <= s2_len; j++) {
634 unsigned int sub = p[j - 1];
635
636 if (s1[i - 1] != s2[j - 1])
637 sub++;
638
639 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
640 }
641 r = p;
642 p = q;
643 q = r;
644 }
645
646 i = p[s2_len];
647 free(p);
648 free(q);
649 return i;
650}
651
652static void show_option_help(struct fio_option *o)
653{
654 const char *typehelp[] = {
655 "string (opt=bla)",
656 "string with possible k/m/g postfix (opt=4k)",
657 "string with range and postfix (opt=1k-4k)",
658 "string with time postfix (opt=10s)",
659 "string (opt=bla)",
660 "string with dual range (opt=1k-4k,4k-8k)",
661 "integer value (opt=100)",
662 "boolean value (opt=1)",
663 "no argument (opt)",
664 };
665
666 if (o->alias)
667 printf("%20s: %s\n", "alias", o->alias);
668
669 printf("%20s: %s\n", "type", typehelp[o->type]);
670 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
671 show_option_range(o);
672 show_option_values(o);
673}
674
675static struct fio_option *find_child(struct fio_option *options,
676 struct fio_option *o)
677{
678 struct fio_option *__o;
679
680 for (__o = options + 1; __o->name; __o++)
681 if (__o->parent && !strcmp(__o->parent, o->name))
682 return __o;
683
684 return NULL;
685}
686
687static void __print_option(struct fio_option *o, struct fio_option *org,
688 int level)
689{
690 char name[256], *p;
691 int depth;
692
693 if (!o)
694 return;
695 if (!org)
696 org = o;
697
698 p = name;
699 depth = level;
700 while (depth--)
701 p += sprintf(p, "%s", " ");
702
703 sprintf(p, "%s", o->name);
704
705 printf("%-24s: %s\n", name, o->help);
706}
707
708static void print_option(struct fio_option *o)
709{
710 struct fio_option *parent;
711 struct fio_option *__o;
712 unsigned int printed;
713 unsigned int level;
714
715 __print_option(o, NULL, 0);
716 parent = o;
717 level = 0;
718 do {
719 level++;
720 printed = 0;
721
722 while ((__o = find_child(o, parent)) != NULL) {
723 __print_option(__o, o, level);
724 o = __o;
725 printed++;
726 }
727
728 parent = o;
729 } while (printed);
730}
731
732int show_cmd_help(struct fio_option *options, const char *name)
733{
734 struct fio_option *o, *closest;
735 unsigned int best_dist;
736 int found = 0;
737 int show_all = 0;
738
739 if (!name || !strcmp(name, "all"))
740 show_all = 1;
741
742 closest = NULL;
743 best_dist = -1;
744 for (o = &options[0]; o->name; o++) {
745 int match = 0;
746
747 if (o->type == FIO_OPT_DEPRECATED)
748 continue;
749
750 if (name) {
751 if (!strcmp(name, o->name) ||
752 (o->alias && !strcmp(name, o->alias)))
753 match = 1;
754 else {
755 unsigned int dist;
756
757 dist = string_distance(name, o->name);
758 if (dist < best_dist) {
759 best_dist = dist;
760 closest = o;
761 }
762 }
763 }
764
765 if (show_all || match) {
766 found = 1;
767 if (match)
768 printf("%24s: %s\n", o->name, o->help);
769 if (show_all) {
770 if (!o->parent)
771 print_option(o);
772 continue;
773 }
774 }
775
776 if (!match)
777 continue;
778
779 show_option_help(o);
780 }
781
782 if (found)
783 return 0;
784
785 printf("No such command: %s", name);
786 if (closest) {
787 printf(" - showing closest match\n");
788 printf("%20s: %s\n", closest->name, closest->help);
789 show_option_help(closest);
790 } else
791 printf("\n");
792
793 return 1;
794}
795
796/*
797 * Handle parsing of default parameters.
798 */
799void fill_default_options(void *data, struct fio_option *options)
800{
801 struct fio_option *o;
802
803 dprint(FD_PARSE, "filling default options\n");
804
805 for (o = &options[0]; o->name; o++)
806 if (o->def)
807 handle_option(o, o->def, data);
808}
809
810/*
811 * Sanitize the options structure. For now it just sets min/max for bool
812 * values and whether both callback and offsets are given.
813 */
814void options_init(struct fio_option *options)
815{
816 struct fio_option *o;
817
818 dprint(FD_PARSE, "init options\n");
819
820 for (o = &options[0]; o->name; o++) {
821 if (o->type == FIO_OPT_DEPRECATED)
822 continue;
823 if (o->type == FIO_OPT_BOOL) {
824 o->minval = 0;
825 o->maxval = 1;
826 }
827 if (o->type == FIO_OPT_STR_SET && o->def) {
828 fprintf(stderr, "Option %s: string set option with"
829 " default will always be true\n",
830 o->name);
831 }
832 if (!o->cb && !o->off1) {
833 fprintf(stderr, "Option %s: neither cb nor offset"
834 " given\n", o->name);
835 }
836 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
837 continue;
838 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
839 fprintf(stderr, "Option %s: both cb and offset given\n",
840 o->name);
841 }
842 }
843}