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