81ffb741094b2ebec9b24e18dff062af2bceced3
[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 unsigned long get_mult_time(char c)
15 {
16         switch (c) {
17                 case 'm':
18                 case 'M':
19                         return 60;
20                 case 'h':
21                 case 'H':
22                         return 60 * 60;
23                 case 'd':
24                 case 'D':
25                         return 24 * 60 * 60;
26                 default:
27                         return 1;
28         }
29 }
30
31 static unsigned long get_mult_bytes(char c)
32 {
33         switch (c) {
34                 case 'k':
35                 case 'K':
36                         return 1024;
37                 case 'm':
38                 case 'M':
39                         return 1024 * 1024;
40                 case 'g':
41                 case 'G':
42                         return 1024 * 1024 * 1024;
43                 default:
44                         return 1;
45         }
46 }
47
48 /*
49  * convert string into decimal value, noting any size suffix
50  */
51 static int str_to_decimal(const char *str, long long *val, int kilo)
52 {
53         int len;
54
55         len = strlen(str);
56         if (!len)
57                 return 1;
58
59         *val = strtol(str, NULL, 10);
60         if (*val == LONG_MAX && errno == ERANGE)
61                 return 1;
62
63         if (kilo)
64                 *val *= get_mult_bytes(str[len - 1]);
65         else
66                 *val *= get_mult_time(str[len - 1]);
67
68         return 0;
69 }
70
71 static int check_str_bytes(const char *p, long long *val)
72 {
73         return str_to_decimal(p, val, 1);
74 }
75
76 static int check_str_time(const char *p, long long *val)
77 {
78         return str_to_decimal(p, val, 0);
79 }
80
81 void strip_blank_front(char **p)
82 {
83         char *s = *p;
84
85         while (isspace(*s))
86                 s++;
87 }
88
89 void strip_blank_end(char *p)
90 {
91         char *s = p + strlen(p) - 1;
92
93         while (isspace(*s) || iscntrl(*s))
94                 s--;
95
96         *(s + 1) = '\0';
97 }
98
99 static int check_range_bytes(const char *str, long *val)
100 {
101         char suffix;
102
103         if (!strlen(str))
104                 return 1;
105
106         if (sscanf(str, "%lu%c", val, &suffix) == 2) {
107                 *val *= get_mult_bytes(suffix);
108                 return 0;
109         }
110
111         if (sscanf(str, "%lu", val) == 1)
112                 return 0;
113
114         return 1;
115 }
116
117 static int check_int(const char *p, int *val)
118 {
119         if (!strlen(p))
120                 return 1;
121         if (sscanf(p, "%u", val) == 1)
122                 return 0;
123
124         return 1;
125 }
126
127 static struct fio_option *find_option(struct fio_option *options,
128                                       const char *opt)
129 {
130         struct fio_option *o = &options[0];
131
132         while (o->name) {
133                 if (!strcmp(o->name, opt))
134                         return o;
135                 else if (o->alias && !strcmp(o->alias, opt))
136                         return o;
137
138                 o++;
139         }
140
141         return NULL;
142 }
143
144 #define val_store(ptr, val, off, data)                  \
145         do {                                            \
146                 ptr = td_var((data), (off));            \
147                 *ptr = (val);                           \
148         } while (0)
149
150 static int __handle_option(struct fio_option *o, const char *ptr, void *data,
151                            int first, int more)
152 {
153         int il, *ilp;
154         long long ull, *ullp;
155         long ul1, ul2;
156         char **cp;
157         int ret = 0, is_time = 0;
158
159         if (!ptr && o->type != FIO_OPT_STR_SET) {
160                 fprintf(stderr, "Option %s requires an argument\n", o->name);
161                 return 1;
162         }
163
164         switch (o->type) {
165         case FIO_OPT_STR: {
166                 fio_opt_str_fn *fn = o->cb;
167
168                 ret = fn(data, ptr);
169                 break;
170         }
171         case FIO_OPT_STR_VAL_TIME:
172                 is_time = 1;
173         case FIO_OPT_STR_VAL:
174         case FIO_OPT_STR_VAL_INT: {
175                 fio_opt_str_val_fn *fn = o->cb;
176
177                 if (is_time)
178                         ret = check_str_time(ptr, &ull);
179                 else
180                         ret = check_str_bytes(ptr, &ull);
181
182                 if (ret)
183                         break;
184
185                 if (o->maxval && ull > o->maxval) {
186                         fprintf(stderr, "max value out of range: %lld (%d max)\n", ull, o->maxval);
187                         return 1;
188                 }
189                 if (o->minval && ull < o->minval) {
190                         fprintf(stderr, "min value out of range: %lld (%d min)\n", ull, o->minval);
191                         return 1;
192                 }
193
194                 if (fn)
195                         ret = fn(data, &ull);
196                 else {
197                         if (o->type == FIO_OPT_STR_VAL_INT) {
198                                 if (first)
199                                         val_store(ilp, ull, o->off1, data);
200                                 if (!more && o->off2)
201                                         val_store(ilp, ull, o->off2, data);
202                         } else {
203                                 if (first)
204                                         val_store(ullp, ull, o->off1, data);
205                                 if (!more && o->off2)
206                                         val_store(ullp, ull, o->off2, data);
207                         }
208                 }
209                 break;
210         }
211         case FIO_OPT_STR_STORE:
212                 cp = td_var(data, o->off1);
213                 *cp = strdup(ptr);
214                 break;
215         case FIO_OPT_RANGE: {
216                 char tmp[128];
217                 char *p1, *p2;
218
219                 strncpy(tmp, ptr, sizeof(tmp) - 1);
220
221                 p1 = strchr(tmp, '-');
222                 if (!p1) {
223                         ret = 1;
224                         break;
225                 }
226
227                 p2 = p1 + 1;
228                 *p1 = '\0';
229                 p1 = tmp;
230
231                 ret = 1;
232                 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
233                         ret = 0;
234                         if (ul1 > ul2) {
235                                 unsigned long foo = ul1;
236
237                                 ul1 = ul2;
238                                 ul2 = foo;
239                         }
240
241                         if (first) {
242                                 val_store(ilp, ul1, o->off1, data);
243                                 val_store(ilp, ul2, o->off2, data);
244                         }
245                         if (!more && o->off3 && o->off4) {
246                                 val_store(ilp, ul1, o->off3, data);
247                                 val_store(ilp, ul2, o->off4, data);
248                         }
249                 }
250
251                 break;
252         }
253         case FIO_OPT_INT:
254         case FIO_OPT_BOOL: {
255                 fio_opt_int_fn *fn = o->cb;
256
257                 ret = check_int(ptr, &il);
258                 if (ret)
259                         break;
260
261                 if (o->maxval && il > (int) o->maxval) {
262                         fprintf(stderr, "max value out of range: %d (%d max)\n", il, o->maxval);
263                         return 1;
264                 }
265                 if (o->minval && il < o->minval) {
266                         fprintf(stderr, "min value out of range: %d (%d min)\n", il, o->minval);
267                         return 1;
268                 }
269
270                 if (o->neg)
271                         il = !il;
272
273                 if (fn)
274                         ret = fn(data, &il);
275                 else {
276                         if (first)
277                                 val_store(ilp, il, o->off1, data);
278                         if (!more && o->off2)
279                                 val_store(ilp, il, o->off2, data);
280                 }
281                 break;
282         }
283         case FIO_OPT_STR_SET: {
284                 fio_opt_str_set_fn *fn = o->cb;
285
286                 if (fn)
287                         ret = fn(data);
288                 else {
289                         if (first)
290                                 val_store(ilp, 1, o->off1, data);
291                         if (!more && o->off2)
292                                 val_store(ilp, 1, o->off2, data);
293                 }
294                 break;
295         }
296         default:
297                 fprintf(stderr, "Bad option type %u\n", o->type);
298                 ret = 1;
299         }
300
301         return ret;
302 }
303
304 static int handle_option(struct fio_option *o, const char *ptr, void *data)
305 {
306         const char *ptr2 = NULL;
307         int r1, r2;
308
309         /*
310          * See if we have a second set of parameters, hidden after a comma.
311          * Do this before parsing the first round, to check if we should
312          * copy set 1 options to set 2.
313          */
314         if (ptr)
315                 ptr2 = strchr(ptr, ',');
316
317         /*
318          * Don't return early if parsing the first option fails - if
319          * we are doing multiple arguments, we can allow the first one
320          * being empty.
321          */
322         r1 = __handle_option(o, ptr, data, 1, !!ptr2);
323
324         if (!ptr2)
325                 return r1;
326
327         ptr2++;
328         r2 = __handle_option(o, ptr2, data, 0, 0);
329
330         return r1 && r2;
331 }
332
333 int parse_cmd_option(const char *opt, const char *val,
334                      struct fio_option *options, void *data)
335 {
336         struct fio_option *o;
337
338         o = find_option(options, opt);
339         if (!o) {
340                 fprintf(stderr, "Bad option %s\n", opt);
341                 return 1;
342         }
343
344         if (!handle_option(o, val, data))
345                 return 0;
346
347         fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
348         return 1;
349 }
350
351 int parse_option(const char *opt, struct fio_option *options, void *data)
352 {
353         struct fio_option *o;
354         char *pre, *post;
355         char tmp[64];
356
357         strncpy(tmp, opt, sizeof(tmp) - 1);
358
359         pre = strchr(tmp, '=');
360         if (pre) {
361                 post = pre;
362                 *pre = '\0';
363                 pre = tmp;
364                 post++;
365                 o = find_option(options, pre);
366         } else {
367                 o = find_option(options, tmp);
368                 post = NULL;
369         }
370
371         if (!o) {
372                 fprintf(stderr, "Bad option %s\n", tmp);
373                 return 1;
374         }
375
376         if (!handle_option(o, post, data))
377                 return 0;
378
379         fprintf(stderr, "fio: failed parsing %s\n", opt);
380         return 1;
381 }
382
383 static void show_option_range(struct fio_option *o)
384 {
385         if (!o->minval && !o->maxval)
386                 return;
387
388         printf("%16s: min=%d, max=%d\n", "range", o->minval, o->maxval);
389 }
390
391 static void show_option_values(struct fio_option *o)
392 {
393         const char *msg;
394         int i = 0;
395
396         if (!o->posval)
397                 return;
398
399         do {
400                 msg = o->posval[i];
401                 if (!msg)
402                         break;
403
404                 if (!i)
405                         printf("%16s: ", "valid values");
406
407                 printf("%s,", msg);
408                 i++;
409         } while (1);
410
411         if (i)
412                 printf("\n");
413 }
414
415 int show_cmd_help(struct fio_option *options, const char *name)
416 {
417         int show_all = !strcmp(name, "all");
418         struct fio_option *o = &options[0];
419         const char *typehelp[] = {
420                 "string (opt=bla)",
421                 "string with possible k/m/g postfix (opt=4k)",
422                 "string with range and postfix (opt=1k-4k)",
423                 "string with time postfix (opt=10s)",
424                 "string (opt=bla)",
425                 "string with dual range (opt=1k-4k,4k-8k)",
426                 "integer value (opt=100)",
427                 "boolean value (opt=1)",
428                 "no argument (opt)",
429         };
430         int found = 0;
431
432         while (o->name) {
433                 int match = !strcmp(name, o->name);
434
435                 if (show_all || match) {
436                         found = 1;
437                         printf("%16s: %s\n", o->name, o->help);
438                         if (match) {
439                                 printf("%16s: %s\n", "type", typehelp[o->type]);
440                                 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
441                                 show_option_range(o);
442                                 show_option_values(o);
443                         }
444                 }
445
446                 o++;
447         }
448
449         if (found)
450                 return 0;
451
452         printf("No such command: %s\n", name);
453         return 1;
454 }
455
456 /*
457  * Handle parsing of default parameters.
458  */
459 void fill_default_options(void *data, struct fio_option *options)
460 {
461         struct fio_option *o = &options[0];
462
463         while (o->name) {
464                 if (o->def)
465                         handle_option(o, o->def, data);
466                 o++;
467         }
468 }
469
470 /*
471  * Sanitize the options structure. For now it just sets min/max for bool
472  * values.
473  */
474 void options_init(struct fio_option *options)
475 {
476         struct fio_option *o = &options[0];
477
478         while (o->name) {
479                 if (o->type == FIO_OPT_BOOL) {
480                         o->minval = 0;
481                         o->maxval = 1;
482                 }
483                 o++;
484         }
485 }