Improve mixed random append option
[fio.git] / parse.c
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
12 #include "parse.h"
13
14 static int vp_cmp(const void *p1, const void *p2)
15 {
16         const struct value_pair *vp1 = p1;
17         const struct value_pair *vp2 = p2;
18
19         return strlen(vp2->ival) - strlen(vp1->ival);
20 }
21
22 static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
23 {
24         const struct value_pair *vp;
25         int entries;
26
27         memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
28
29         for (entries = 0; entries < PARSE_MAX_VP; entries++) {
30                 vp = &o->posval[entries];
31                 if (!vp->ival || vp->ival[0] == '\0')
32                         break;
33
34                 memcpy(&vpmap[entries], vp, sizeof(*vp));
35         }
36
37         qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
38 }
39
40 static void show_option_range(struct fio_option *o)
41 {
42         if (!o->minval && !o->maxval)
43                 return;
44
45         printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
46 }
47
48 static void show_option_values(struct fio_option *o)
49 {
50         int i = 0;
51
52         do {
53                 const struct value_pair *vp = &o->posval[i];
54
55                 if (!vp->ival)
56                         break;
57
58                 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
59                 if (vp->help)
60                         printf(" %s", vp->help);
61                 printf("\n");
62                 i++;
63         } while (i < PARSE_MAX_VP);
64
65         if (i)
66                 printf("\n");
67 }
68
69 static unsigned long get_mult_time(char c)
70 {
71         switch (c) {
72                 case 'm':
73                 case 'M':
74                         return 60;
75                 case 'h':
76                 case 'H':
77                         return 60 * 60;
78                 case 'd':
79                 case 'D':
80                         return 24 * 60 * 60;
81                 default:
82                         return 1;
83         }
84 }
85
86 static unsigned long get_mult_bytes(char c)
87 {
88         switch (c) {
89                 case 'k':
90                 case 'K':
91                         return 1024;
92                 case 'm':
93                 case 'M':
94                         return 1024 * 1024;
95                 case 'g':
96                 case 'G':
97                         return 1024 * 1024 * 1024;
98                 case 'e':
99                 case 'E':
100                         return 1024 * 1024 * 1024 * 1024UL;
101                 default:
102                         return 1;
103         }
104 }
105
106 /*
107  * convert string into decimal value, noting any size suffix
108  */
109 static int str_to_decimal(const char *str, long long *val, int kilo)
110 {
111         int len;
112
113         len = strlen(str);
114         if (!len)
115                 return 1;
116
117         *val = strtoll(str, NULL, 10);
118         if (*val == LONG_MAX && errno == ERANGE)
119                 return 1;
120
121         if (kilo)
122                 *val *= get_mult_bytes(str[len - 1]);
123         else
124                 *val *= get_mult_time(str[len - 1]);
125
126         return 0;
127 }
128
129 static int check_str_bytes(const char *p, long long *val)
130 {
131         return str_to_decimal(p, val, 1);
132 }
133
134 static int check_str_time(const char *p, long long *val)
135 {
136         return str_to_decimal(p, val, 0);
137 }
138
139 void strip_blank_front(char **p)
140 {
141         char *s = *p;
142
143         while (isspace(*s))
144                 s++;
145 }
146
147 void strip_blank_end(char *p)
148 {
149         char *s = p + strlen(p) - 1;
150
151         while (isspace(*s) || iscntrl(*s))
152                 s--;
153
154         *(s + 1) = '\0';
155 }
156
157 static int check_range_bytes(const char *str, long *val)
158 {
159         char suffix;
160
161         if (!strlen(str))
162                 return 1;
163
164         if (sscanf(str, "%lu%c", val, &suffix) == 2) {
165                 *val *= get_mult_bytes(suffix);
166                 return 0;
167         }
168
169         if (sscanf(str, "%lu", val) == 1)
170                 return 0;
171
172         return 1;
173 }
174
175 static int check_int(const char *p, int *val)
176 {
177         if (!strlen(p))
178                 return 1;
179         if (sscanf(p, "%u", val) == 1)
180                 return 0;
181
182         return 1;
183 }
184
185 static struct fio_option *find_option(struct fio_option *options,
186                                       const char *opt)
187 {
188         struct fio_option *o;
189
190         for (o = &options[0]; o->name; o++) {
191                 if (!strcmp(o->name, opt))
192                         return o;
193                 else if (o->alias && !strcmp(o->alias, opt))
194                         return o;
195         }
196
197         return NULL;
198 }
199
200 #define val_store(ptr, val, off, data)                  \
201         do {                                            \
202                 ptr = td_var((data), (off));            \
203                 *ptr = (val);                           \
204         } while (0)
205
206 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
207                            int first, int more)
208 {
209         int il, *ilp;
210         long long ull, *ullp;
211         long ul1, ul2;
212         char **cp;
213         int ret = 0, is_time = 0;
214
215         if (!ptr && o->type != FIO_OPT_STR_SET) {
216                 fprintf(stderr, "Option %s requires an argument\n", o->name);
217                 return 1;
218         }
219
220         switch (o->type) {
221         case FIO_OPT_STR: {
222                 fio_opt_str_fn *fn = o->cb;
223                 const struct value_pair *vp;
224                 struct value_pair posval[PARSE_MAX_VP];
225                 int i;
226
227                 posval_sort(o, posval);
228
229                 for (i = 0; i < PARSE_MAX_VP; i++) {
230                         vp = &posval[i];
231                         if (!vp->ival || vp->ival[0] == '\0')
232                                 break;
233                         ret = 1;
234                         if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
235                                 ret = 0;
236                                 if (!o->off1)
237                                         break;
238                                 val_store(ilp, vp->oval, o->off1, data);
239                                 break;
240                         }
241                 }
242
243                 if (ret)
244                         show_option_values(o);
245                 else if (fn)
246                         ret = fn(data, ptr);
247                 break;
248         }
249         case FIO_OPT_STR_VAL_TIME:
250                 is_time = 1;
251         case FIO_OPT_STR_VAL:
252         case FIO_OPT_STR_VAL_INT: {
253                 fio_opt_str_val_fn *fn = o->cb;
254
255                 if (is_time)
256                         ret = check_str_time(ptr, &ull);
257                 else
258                         ret = check_str_bytes(ptr, &ull);
259
260                 if (ret)
261                         break;
262
263                 if (o->maxval && ull > o->maxval) {
264                         fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
265                         return 1;
266                 }
267                 if (o->minval && ull < o->minval) {
268                         fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
269                         return 1;
270                 }
271
272                 if (fn)
273                         ret = fn(data, &ull);
274                 else {
275                         if (o->type == FIO_OPT_STR_VAL_INT) {
276                                 if (first)
277                                         val_store(ilp, ull, o->off1, data);
278                                 if (!more && o->off2)
279                                         val_store(ilp, ull, o->off2, data);
280                         } else {
281                                 if (first)
282                                         val_store(ullp, ull, o->off1, data);
283                                 if (!more && o->off2)
284                                         val_store(ullp, ull, o->off2, data);
285                         }
286                 }
287                 break;
288         }
289         case FIO_OPT_STR_STORE: {
290                 fio_opt_str_fn *fn = o->cb;
291
292                 cp = td_var(data, o->off1);
293                 *cp = strdup(ptr);
294                 if (fn) {
295                         ret = fn(data, ptr);
296                         if (ret) {
297                                 free(*cp);
298                                 *cp = NULL;
299                         }
300                 }
301                 break;
302         }
303         case FIO_OPT_RANGE: {
304                 char tmp[128];
305                 char *p1, *p2;
306
307                 strncpy(tmp, ptr, sizeof(tmp) - 1);
308
309                 p1 = strchr(tmp, '-');
310                 if (!p1) {
311                         p1 = strchr(tmp, ':');
312                         if (!p1) {
313                                 ret = 1;
314                                 break;
315                         }
316                 }
317
318                 p2 = p1 + 1;
319                 *p1 = '\0';
320                 p1 = tmp;
321
322                 ret = 1;
323                 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
324                         ret = 0;
325                         if (ul1 > ul2) {
326                                 unsigned long foo = ul1;
327
328                                 ul1 = ul2;
329                                 ul2 = foo;
330                         }
331
332                         if (first) {
333                                 val_store(ilp, ul1, o->off1, data);
334                                 val_store(ilp, ul2, o->off2, data);
335                         }
336                         if (!more && o->off3 && o->off4) {
337                                 val_store(ilp, ul1, o->off3, data);
338                                 val_store(ilp, ul2, o->off4, data);
339                         }
340                 }
341
342                 break;
343         }
344         case FIO_OPT_INT:
345         case FIO_OPT_BOOL: {
346                 fio_opt_int_fn *fn = o->cb;
347
348                 ret = check_int(ptr, &il);
349                 if (ret)
350                         break;
351
352                 if (o->maxval && il > (int) o->maxval) {
353                         fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
354                         return 1;
355                 }
356                 if (o->minval && il < o->minval) {
357                         fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
358                         return 1;
359                 }
360
361                 if (o->neg)
362                         il = !il;
363
364                 if (fn)
365                         ret = fn(data, &il);
366                 else {
367                         if (first)
368                                 val_store(ilp, il, o->off1, data);
369                         if (!more && o->off2)
370                                 val_store(ilp, il, o->off2, data);
371                 }
372                 break;
373         }
374         case FIO_OPT_STR_SET: {
375                 fio_opt_str_set_fn *fn = o->cb;
376
377                 if (fn)
378                         ret = fn(data);
379                 else {
380                         if (first)
381                                 val_store(ilp, 1, o->off1, data);
382                         if (!more && o->off2)
383                                 val_store(ilp, 1, o->off2, data);
384                 }
385                 break;
386         }
387         default:
388                 fprintf(stderr, "Bad option type %u\n", o->type);
389                 ret = 1;
390         }
391
392         return ret;
393 }
394
395 static int handle_option(struct fio_option *o, const char *ptr, void *data)
396 {
397         const char *ptr2 = NULL;
398         int r1, r2;
399
400         /*
401          * See if we have a second set of parameters, hidden after a comma.
402          * Do this before parsing the first round, to check if we should
403          * copy set 1 options to set 2.
404          */
405         if (ptr &&
406             (o->type != FIO_OPT_STR_STORE) &&
407             (o->type != FIO_OPT_STR)) {
408                 ptr2 = strchr(ptr, ',');
409                 if (!ptr2)
410                         ptr2 = strchr(ptr, ':');
411                 if (!ptr2)
412                         ptr2 = strchr(ptr, '-');
413         }
414
415         /*
416          * Don't return early if parsing the first option fails - if
417          * we are doing multiple arguments, we can allow the first one
418          * being empty.
419          */
420         r1 = __handle_option(o, ptr, data, 1, !!ptr2);
421
422         if (!ptr2)
423                 return r1;
424
425         ptr2++;
426         r2 = __handle_option(o, ptr2, data, 0, 0);
427
428         return r1 && r2;
429 }
430
431 int parse_cmd_option(const char *opt, const char *val,
432                      struct fio_option *options, void *data)
433 {
434         struct fio_option *o;
435
436         o = find_option(options, opt);
437         if (!o) {
438                 fprintf(stderr, "Bad option %s\n", opt);
439                 return 1;
440         }
441
442         if (!handle_option(o, val, data))
443                 return 0;
444
445         fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
446         return 1;
447 }
448
449 int parse_option(const char *opt, struct fio_option *options, void *data)
450 {
451         struct fio_option *o;
452         char *pre, *post;
453         char tmp[64];
454
455         strncpy(tmp, opt, sizeof(tmp) - 1);
456
457         pre = strchr(tmp, '=');
458         if (pre) {
459                 post = pre;
460                 *pre = '\0';
461                 pre = tmp;
462                 post++;
463                 o = find_option(options, pre);
464         } else {
465                 o = find_option(options, tmp);
466                 post = NULL;
467         }
468
469         if (!o) {
470                 fprintf(stderr, "Bad option %s\n", tmp);
471                 return 1;
472         }
473
474         if (!handle_option(o, post, data))
475                 return 0;
476
477         fprintf(stderr, "fio: failed parsing %s\n", opt);
478         return 1;
479 }
480
481 /*
482  * Option match, levenshtein distance. Handy for not quite remembering what
483  * the option name is.
484  */
485 static int string_distance(const char *s1, const char *s2)
486 {
487         unsigned int s1_len = strlen(s1);
488         unsigned int s2_len = strlen(s2);
489         unsigned int *p, *q, *r;
490         unsigned int i, j;
491
492         p = malloc(sizeof(unsigned int) * (s2_len + 1));
493         q = malloc(sizeof(unsigned int) * (s2_len + 1));
494
495         p[0] = 0;
496         for (i = 1; i <= s2_len; i++)
497                 p[i] = p[i - 1] + 1;
498
499         for (i = 1; i <= s1_len; i++) {
500                 q[0] = p[0] + 1;
501                 for (j = 1; j <= s2_len; j++) {
502                         unsigned int sub = p[j - 1];
503
504                         if (s1[i - 1] != s2[j - 1])
505                                 sub++;
506
507                         q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
508                 }
509                 r = p;
510                 p = q;
511                 q = r;
512         }
513
514         i = p[s2_len];
515         free(p);
516         free(q);
517         return i;
518 }
519
520 static void show_option_help(struct fio_option *o)
521 {
522         const char *typehelp[] = {
523                 "string (opt=bla)",
524                 "string with possible k/m/g postfix (opt=4k)",
525                 "string with range and postfix (opt=1k-4k)",
526                 "string with time postfix (opt=10s)",
527                 "string (opt=bla)",
528                 "string with dual range (opt=1k-4k,4k-8k)",
529                 "integer value (opt=100)",
530                 "boolean value (opt=1)",
531                 "no argument (opt)",
532         };
533
534         if (o->alias)
535                 printf("%20s: %s\n", "alias", o->alias);
536
537         printf("%20s: %s\n", "type", typehelp[o->type]);
538         printf("%20s: %s\n", "default", o->def ? o->def : "no default");
539         show_option_range(o);
540         show_option_values(o);
541 }
542
543 int show_cmd_help(struct fio_option *options, const char *name)
544 {
545         struct fio_option *o, *closest;
546         unsigned int best_dist;
547         int found = 0;
548         int show_all = 0;
549
550         if (!name || !strcmp(name, "all"))
551                 show_all = 1;
552
553         closest = NULL;
554         best_dist = -1;
555         for (o = &options[0]; o->name; o++) {
556                 int match = 0;
557
558                 if (name) {
559                         if (!strcmp(name, o->name) ||
560                             (o->alias && !strcmp(name, o->alias)))
561                                 match = 1;
562                         else {
563                                 unsigned int dist;
564
565                                 dist = string_distance(name, o->name);
566                                 if (dist < best_dist) {
567                                         best_dist = dist;
568                                         closest = o;
569                                 }
570                         }
571                 }
572
573                 if (show_all || match) {
574                         found = 1;
575                         if (match)
576                                 printf("%20s: %s\n", o->name, o->help);
577                         if (show_all) {
578                                 printf("%-20s: %s\n", o->name, o->help);
579                                 continue;
580                         }
581                 }
582
583                 if (!match)
584                         continue;
585
586                 show_option_help(o);
587         }
588
589         if (found)
590                 return 0;
591
592         printf("No such command: %s", name);
593         if (closest) {
594                 printf(" - showing closest match\n");
595                 printf("%20s: %s\n", closest->name, closest->help);
596                 show_option_help(closest);
597         } else
598                 printf("\n");
599
600         return 1;
601 }
602
603 /*
604  * Handle parsing of default parameters.
605  */
606 void fill_default_options(void *data, struct fio_option *options)
607 {
608         struct fio_option *o;
609
610         for (o = &options[0]; o->name; o++)
611                 if (o->def)
612                         handle_option(o, o->def, data);
613 }
614
615 /*
616  * Sanitize the options structure. For now it just sets min/max for bool
617  * values and whether both callback and offsets are given.
618  */
619 void options_init(struct fio_option *options)
620 {
621         struct fio_option *o;
622
623         for (o = &options[0]; o->name; o++) {
624                 if (o->type == FIO_OPT_BOOL) {
625                         o->minval = 0;
626                         o->maxval = 1;
627                 }
628                 if (o->type == FIO_OPT_STR_SET && o->def)
629                         fprintf(stderr, "Option %s: string set option with default will always be true\n", o->name);
630                 if (!o->cb && !o->off1)
631                         fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
632                 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
633                         continue;
634                 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
635                         fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
636         }
637 }