Sort option possible value map
[fio.git] / parse.c
CommitLineData
cb2c86fd
JA
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
f085737f
JA
14static 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
22static 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
b1ec1da6
JA
40static void show_option_range(struct fio_option *o)
41{
42 if (!o->minval && !o->maxval)
43 return;
44
ec1aee01 45 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
b1ec1da6
JA
46}
47
48static void show_option_values(struct fio_option *o)
49{
b1ec1da6
JA
50 int i = 0;
51
52 do {
7837213b 53 const struct value_pair *vp = &o->posval[i];
b1ec1da6 54
7837213b
JA
55 if (!vp->ival)
56 break;
b1ec1da6 57
7837213b
JA
58 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
59 if (vp->help)
60 printf(" %s", vp->help);
61 printf("\n");
b1ec1da6
JA
62 i++;
63 } while (i < PARSE_MAX_VP);
64
65 if (i)
66 printf("\n");
67}
68
cb2c86fd
JA
69static 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
86static 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;
f3502ba2
JA
98 case 'e':
99 case 'E':
100 return 1024 * 1024 * 1024 * 1024UL;
cb2c86fd
JA
101 default:
102 return 1;
103 }
104}
105
106/*
e1f36503 107 * convert string into decimal value, noting any size suffix
cb2c86fd 108 */
63f29372 109static int str_to_decimal(const char *str, long long *val, int kilo)
cb2c86fd 110{
cb2c86fd
JA
111 int len;
112
cb2c86fd 113 len = strlen(str);
f90eff5a
JA
114 if (!len)
115 return 1;
cb2c86fd 116
675de85a 117 *val = strtoll(str, NULL, 10);
cda866ca 118 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
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]);
787f7e95 125
cb2c86fd
JA
126 return 0;
127}
128
63f29372 129static int check_str_bytes(const char *p, long long *val)
cb2c86fd 130{
cb2c86fd
JA
131 return str_to_decimal(p, val, 1);
132}
133
63f29372 134static int check_str_time(const char *p, long long *val)
cb2c86fd 135{
cb2c86fd
JA
136 return str_to_decimal(p, val, 0);
137}
138
139void strip_blank_front(char **p)
140{
141 char *s = *p;
142
143 while (isspace(*s))
144 s++;
145}
146
147void 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
63f29372 157static int check_range_bytes(const char *str, long *val)
cb2c86fd
JA
158{
159 char suffix;
160
787f7e95
JA
161 if (!strlen(str))
162 return 1;
163
cb2c86fd
JA
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
63f29372 175static int check_int(const char *p, int *val)
cb2c86fd 176{
787f7e95
JA
177 if (!strlen(p))
178 return 1;
e1f36503
JA
179 if (sscanf(p, "%u", val) == 1)
180 return 0;
cb2c86fd 181
e1f36503
JA
182 return 1;
183}
cb2c86fd 184
e1f36503
JA
185static struct fio_option *find_option(struct fio_option *options,
186 const char *opt)
187{
4945ba12 188 struct fio_option *o;
cb2c86fd 189
4945ba12 190 for (o = &options[0]; o->name; o++) {
e1f36503
JA
191 if (!strcmp(o->name, opt))
192 return o;
03b74b3e
JA
193 else if (o->alias && !strcmp(o->alias, opt))
194 return o;
33963c6c 195 }
cb2c86fd 196
e1f36503 197 return NULL;
cb2c86fd
JA
198}
199
17abbe89
JA
200#define val_store(ptr, val, off, data) \
201 do { \
202 ptr = td_var((data), (off)); \
203 *ptr = (val); \
204 } while (0)
205
f90eff5a 206static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 207 int first, int more)
cb2c86fd 208{
63f29372
JA
209 int il, *ilp;
210 long long ull, *ullp;
211 long ul1, ul2;
b4692828 212 char **cp;
e1f36503 213 int ret = 0, is_time = 0;
cb2c86fd 214
08e26e35
JA
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
e1f36503
JA
220 switch (o->type) {
221 case FIO_OPT_STR: {
222 fio_opt_str_fn *fn = o->cb;
b1ec1da6 223 const struct value_pair *vp;
f085737f 224 struct value_pair posval[PARSE_MAX_VP];
b1ec1da6
JA
225 int i;
226
f085737f
JA
227 posval_sort(o, posval);
228
b1ec1da6 229 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 230 vp = &posval[i];
b1ec1da6
JA
231 if (!vp->ival || vp->ival[0] == '\0')
232 break;
6612a27b 233 ret = 1;
b1ec1da6
JA
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 }
cb2c86fd 242
b1ec1da6
JA
243 if (ret)
244 show_option_values(o);
245 else if (fn)
246 ret = fn(data, ptr);
e1f36503
JA
247 break;
248 }
249 case FIO_OPT_STR_VAL_TIME:
250 is_time = 1;
75e6f36f
JA
251 case FIO_OPT_STR_VAL:
252 case FIO_OPT_STR_VAL_INT: {
e1f36503
JA
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
db8e0165
JA
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 }
e1f36503
JA
271
272 if (fn)
273 ret = fn(data, &ull);
274 else {
75e6f36f 275 if (o->type == FIO_OPT_STR_VAL_INT) {
17abbe89
JA
276 if (first)
277 val_store(ilp, ull, o->off1, data);
787f7e95 278 if (!more && o->off2)
17abbe89 279 val_store(ilp, ull, o->off2, data);
75e6f36f 280 } else {
17abbe89
JA
281 if (first)
282 val_store(ullp, ull, o->off1, data);
787f7e95 283 if (!more && o->off2)
17abbe89 284 val_store(ullp, ull, o->off2, data);
75e6f36f 285 }
e1f36503
JA
286 }
287 break;
288 }
af52b345
JA
289 case FIO_OPT_STR_STORE: {
290 fio_opt_str_fn *fn = o->cb;
291
e1f36503
JA
292 cp = td_var(data, o->off1);
293 *cp = strdup(ptr);
af52b345
JA
294 if (fn) {
295 ret = fn(data, ptr);
296 if (ret) {
297 free(*cp);
298 *cp = NULL;
299 }
300 }
e1f36503 301 break;
af52b345 302 }
e1f36503 303 case FIO_OPT_RANGE: {
b765a372 304 char tmp[128];
e1f36503
JA
305 char *p1, *p2;
306
0bbab0e7 307 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
308
309 p1 = strchr(tmp, '-');
e1f36503 310 if (!p1) {
0c9baf91
JA
311 p1 = strchr(tmp, ':');
312 if (!p1) {
313 ret = 1;
314 break;
315 }
e1f36503
JA
316 }
317
318 p2 = p1 + 1;
319 *p1 = '\0';
b765a372 320 p1 = tmp;
e1f36503
JA
321
322 ret = 1;
323 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
324 ret = 0;
e1f36503 325 if (ul1 > ul2) {
f90eff5a
JA
326 unsigned long foo = ul1;
327
328 ul1 = ul2;
329 ul2 = foo;
330 }
331
332 if (first) {
17abbe89
JA
333 val_store(ilp, ul1, o->off1, data);
334 val_store(ilp, ul2, o->off2, data);
335 }
787f7e95 336 if (!more && o->off3 && o->off4) {
17abbe89
JA
337 val_store(ilp, ul1, o->off3, data);
338 val_store(ilp, ul2, o->off4, data);
e1f36503 339 }
17abbe89
JA
340 }
341
e1f36503
JA
342 break;
343 }
13335ddb
JA
344 case FIO_OPT_INT:
345 case FIO_OPT_BOOL: {
e1f36503
JA
346 fio_opt_int_fn *fn = o->cb;
347
348 ret = check_int(ptr, &il);
349 if (ret)
350 break;
351
db8e0165
JA
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 }
e1f36503 360
76a43db4
JA
361 if (o->neg)
362 il = !il;
363
e1f36503
JA
364 if (fn)
365 ret = fn(data, &il);
366 else {
17abbe89
JA
367 if (first)
368 val_store(ilp, il, o->off1, data);
787f7e95 369 if (!more && o->off2)
17abbe89 370 val_store(ilp, il, o->off2, data);
e1f36503
JA
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 {
17abbe89
JA
380 if (first)
381 val_store(ilp, 1, o->off1, data);
787f7e95 382 if (!more && o->off2)
17abbe89 383 val_store(ilp, 1, o->off2, data);
e1f36503
JA
384 }
385 break;
386 }
387 default:
1e97cce9 388 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
389 ret = 1;
390 }
cb2c86fd 391
e1f36503 392 return ret;
cb2c86fd
JA
393}
394
f90eff5a
JA
395static int handle_option(struct fio_option *o, const char *ptr, void *data)
396{
92b586f8 397 const char *ptr2 = NULL;
787f7e95 398 int r1, r2;
f90eff5a
JA
399
400 /*
787f7e95
JA
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.
f90eff5a 404 */
ad231bc4
JB
405 if (ptr &&
406 (o->type != FIO_OPT_STR_STORE) &&
407 (o->type != FIO_OPT_STR)) {
92b586f8 408 ptr2 = strchr(ptr, ',');
0c9baf91
JA
409 if (!ptr2)
410 ptr2 = strchr(ptr, ':');
b486f76a
JA
411 if (!ptr2)
412 ptr2 = strchr(ptr, '-');
0c9baf91 413 }
787f7e95
JA
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
f90eff5a 422 if (!ptr2)
787f7e95 423 return r1;
f90eff5a
JA
424
425 ptr2++;
787f7e95
JA
426 r2 = __handle_option(o, ptr2, data, 0, 0);
427
428 return r1 && r2;
f90eff5a
JA
429}
430
b4692828
JA
431int 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
b1508cf9
JA
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;
b4692828
JA
447}
448
e1f36503 449int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 450{
b4692828 451 struct fio_option *o;
e1f36503
JA
452 char *pre, *post;
453 char tmp[64];
454
0bbab0e7 455 strncpy(tmp, opt, sizeof(tmp) - 1);
e1f36503
JA
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 }
cb2c86fd 468
e1f36503
JA
469 if (!o) {
470 fprintf(stderr, "Bad option %s\n", tmp);
471 return 1;
472 }
473
b1508cf9
JA
474 if (!handle_option(o, post, data))
475 return 0;
476
477 fprintf(stderr, "fio: failed parsing %s\n", opt);
478 return 1;
e1f36503 479}
fd28ca49 480
0e9f7fac
JA
481/*
482 * Option match, levenshtein distance. Handy for not quite remembering what
483 * the option name is.
484 */
485static 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
520static void show_option_help(struct fio_option *o)
fd28ca49 521{
fd28ca49
JA
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)",
13335ddb 530 "boolean value (opt=1)",
fd28ca49
JA
531 "no argument (opt)",
532 };
0e9f7fac 533
d2bb7fea
JA
534 if (o->alias)
535 printf("%20s: %s\n", "alias", o->alias);
536
0e9f7fac
JA
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
543int show_cmd_help(struct fio_option *options, const char *name)
544{
545 struct fio_option *o, *closest;
546 unsigned int best_dist;
29fc6afe 547 int found = 0;
320beefe
JA
548 int show_all = 0;
549
550 if (!name || !strcmp(name, "all"))
551 show_all = 1;
fd28ca49 552
0e9f7fac
JA
553 closest = NULL;
554 best_dist = -1;
4945ba12 555 for (o = &options[0]; o->name; o++) {
320beefe
JA
556 int match = 0;
557
0e9f7fac 558 if (name) {
7f9348f8
JA
559 if (!strcmp(name, o->name) ||
560 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
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 }
fd28ca49
JA
572
573 if (show_all || match) {
29fc6afe 574 found = 1;
c167dedc
JA
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);
4945ba12 579 continue;
c167dedc 580 }
fd28ca49
JA
581 }
582
70df2f19
JA
583 if (!match)
584 continue;
585
0e9f7fac 586 show_option_help(o);
fd28ca49
JA
587 }
588
29fc6afe
JA
589 if (found)
590 return 0;
fd28ca49 591
0e9f7fac
JA
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
29fc6afe 600 return 1;
fd28ca49 601}
ee738499 602
13335ddb
JA
603/*
604 * Handle parsing of default parameters.
605 */
ee738499
JA
606void fill_default_options(void *data, struct fio_option *options)
607{
4945ba12 608 struct fio_option *o;
ee738499 609
4945ba12 610 for (o = &options[0]; o->name; o++)
ee738499
JA
611 if (o->def)
612 handle_option(o, o->def, data);
ee738499 613}
13335ddb
JA
614
615/*
616 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 617 * values and whether both callback and offsets are given.
13335ddb
JA
618 */
619void options_init(struct fio_option *options)
620{
4945ba12 621 struct fio_option *o;
13335ddb 622
4945ba12 623 for (o = &options[0]; o->name; o++) {
13335ddb
JA
624 if (o->type == FIO_OPT_BOOL) {
625 o->minval = 0;
626 o->maxval = 1;
627 }
b1ec1da6
JA
628 if (!o->cb && !o->off1)
629 fprintf(stderr, "Option %s: neither cb nor offset given\n", o->name);
af52b345 630 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
b1ec1da6 631 continue;
5b0a8880
JA
632 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4))
633 fprintf(stderr, "Option %s: both cb and offset given\n", o->name);
13335ddb
JA
634 }
635}