Fix killing of threads that haven't started
[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>
88b5a391 11#include <stdlib.h>
cb2c86fd
JA
12
13#include "parse.h"
a3d741fa 14#include "debug.h"
9f988e2e 15#include "options.h"
cb2c86fd 16
3b8b7135 17static struct fio_option *fio_options;
d6978a32 18extern unsigned int fio_get_kb_base(void *);
3b8b7135 19
f085737f
JA
20static int vp_cmp(const void *p1, const void *p2)
21{
22 const struct value_pair *vp1 = p1;
23 const struct value_pair *vp2 = p2;
24
25 return strlen(vp2->ival) - strlen(vp1->ival);
26}
27
28static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
29{
30 const struct value_pair *vp;
31 int entries;
32
33 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
34
35 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
36 vp = &o->posval[entries];
37 if (!vp->ival || vp->ival[0] == '\0')
38 break;
39
40 memcpy(&vpmap[entries], vp, sizeof(*vp));
41 }
42
43 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
44}
45
d447a8c2 46static void show_option_range(struct fio_option *o, FILE *out)
b1ec1da6
JA
47{
48 if (!o->minval && !o->maxval)
49 return;
50
d447a8c2 51 fprintf(out, "%20s: min=%d", "range", o->minval);
3980127d 52 if (o->maxval)
d447a8c2
JA
53 fprintf(out, ", max=%d", o->maxval);
54 fprintf(out, "\n");
b1ec1da6
JA
55}
56
57static void show_option_values(struct fio_option *o)
58{
a3073f4a 59 int i;
b1ec1da6 60
a3073f4a 61 for (i = 0; i < PARSE_MAX_VP; i++) {
7837213b 62 const struct value_pair *vp = &o->posval[i];
b1ec1da6 63
7837213b 64 if (!vp->ival)
a3073f4a 65 continue;
b1ec1da6 66
7837213b
JA
67 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
68 if (vp->help)
69 printf(" %s", vp->help);
70 printf("\n");
a3073f4a 71 }
b1ec1da6
JA
72
73 if (i)
74 printf("\n");
75}
76
d447a8c2
JA
77static void show_option_help(struct fio_option *o, FILE *out)
78{
79 const char *typehelp[] = {
07b3232d 80 "invalid",
d447a8c2 81 "string (opt=bla)",
ae3fb6fb 82 "string (opt=bla)",
d447a8c2
JA
83 "string with possible k/m/g postfix (opt=4k)",
84 "string with time postfix (opt=10s)",
85 "string (opt=bla)",
86 "string with dual range (opt=1k-4k,4k-8k)",
87 "integer value (opt=100)",
88 "boolean value (opt=1)",
89 "no argument (opt)",
07b3232d 90 "deprecated",
d447a8c2
JA
91 };
92
93 if (o->alias)
94 fprintf(out, "%20s: %s\n", "alias", o->alias);
95
96 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
97 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
07b3232d
JA
98 if (o->prof_name)
99 fprintf(out, "%20s: only for profile '%s'\n", "valid", o->prof_name);
d447a8c2
JA
100 show_option_range(o, stdout);
101 show_option_values(o);
102}
103
cb2c86fd
JA
104static unsigned long get_mult_time(char c)
105{
106 switch (c) {
5ec10eaa
JA
107 case 'm':
108 case 'M':
109 return 60;
110 case 'h':
111 case 'H':
112 return 60 * 60;
113 case 'd':
114 case 'D':
115 return 24 * 60 * 60;
116 default:
117 return 1;
cb2c86fd
JA
118 }
119}
120
7bb59102
JA
121static unsigned long long __get_mult_bytes(const char *p, void *data,
122 int *percent)
cb2c86fd 123{
d6978a32 124 unsigned int kb_base = fio_get_kb_base(data);
a639f0bb 125 unsigned long long ret = 1;
57fc29fa
JA
126 unsigned int i, pow = 0, mult = kb_base;
127 char *c;
a639f0bb 128
57fc29fa
JA
129 if (!p)
130 return 1;
a639f0bb 131
57fc29fa
JA
132 c = strdup(p);
133
134 for (i = 0; i < strlen(c); i++)
135 c[i] = tolower(c[i]);
136
137 if (!strcmp("pib", c)) {
138 pow = 5;
139 mult = 1000;
140 } else if (!strcmp("tib", c)) {
141 pow = 4;
142 mult = 1000;
143 } else if (!strcmp("gib", c)) {
144 pow = 3;
145 mult = 1000;
146 } else if (!strcmp("mib", c)) {
147 pow = 2;
148 mult = 1000;
149 } else if (!strcmp("kib", c)) {
150 pow = 1;
151 mult = 1000;
152 } else if (!strcmp("p", c) || !strcmp("pb", c))
153 pow = 5;
154 else if (!strcmp("t", c) || !strcmp("tb", c))
155 pow = 4;
156 else if (!strcmp("g", c) || !strcmp("gb", c))
157 pow = 3;
158 else if (!strcmp("m", c) || !strcmp("mb", c))
159 pow = 2;
160 else if (!strcmp("k", c) || !strcmp("kb", c))
161 pow = 1;
7bb59102
JA
162 else if (!strcmp("%", c)) {
163 *percent = 1;
164 return ret;
165 }
57fc29fa
JA
166
167 while (pow--)
168 ret *= (unsigned long long) mult;
169
170 free(c);
a639f0bb 171 return ret;
cb2c86fd
JA
172}
173
7bb59102
JA
174static unsigned long long get_mult_bytes(const char *str, int len, void *data,
175 int *percent)
57fc29fa 176{
55ed9636 177 const char *p = str;
57fc29fa 178
1d1c187b 179 if (len < 2)
7bb59102 180 return __get_mult_bytes(str, data, percent);
1d1c187b 181
55ed9636
JA
182 /*
183 * Go forward until we hit a non-digit
184 */
185 while ((p - str) <= len) {
76cd9378 186 if (!isdigit((int) *p))
55ed9636
JA
187 break;
188 p++;
189 }
190
7bb59102 191 if (!isalpha((int) *p) && (*p != '%'))
57fc29fa
JA
192 p = NULL;
193
7bb59102 194 return __get_mult_bytes(p, data, percent);
57fc29fa
JA
195}
196
cb2c86fd 197/*
e1f36503 198 * convert string into decimal value, noting any size suffix
cb2c86fd 199 */
d6978a32 200int str_to_decimal(const char *str, long long *val, int kilo, void *data)
cb2c86fd 201{
b347f9da 202 int len, base;
cb2c86fd 203
cb2c86fd 204 len = strlen(str);
f90eff5a
JA
205 if (!len)
206 return 1;
cb2c86fd 207
b347f9da
JA
208 if (strstr(str, "0x") || strstr(str, "0X"))
209 base = 16;
210 else
211 base = 10;
212
213 *val = strtoll(str, NULL, base);
cda866ca 214 if (*val == LONG_MAX && errno == ERANGE)
cb2c86fd
JA
215 return 1;
216
7bb59102
JA
217 if (kilo) {
218 unsigned long long mult;
219 int perc = 0;
220
221 mult = get_mult_bytes(str, len, data, &perc);
222 if (perc)
223 *val = -1ULL - *val;
224 else
225 *val *= mult;
226 } else
cb2c86fd 227 *val *= get_mult_time(str[len - 1]);
787f7e95 228
cb2c86fd
JA
229 return 0;
230}
231
d6978a32 232static int check_str_bytes(const char *p, long long *val, void *data)
cb2c86fd 233{
d6978a32 234 return str_to_decimal(p, val, 1, data);
cb2c86fd
JA
235}
236
63f29372 237static int check_str_time(const char *p, long long *val)
cb2c86fd 238{
d6978a32 239 return str_to_decimal(p, val, 0, NULL);
cb2c86fd
JA
240}
241
242void strip_blank_front(char **p)
243{
244 char *s = *p;
245
76cd9378 246 while (isspace((int) *s))
cb2c86fd 247 s++;
4d651dad
JA
248
249 *p = s;
cb2c86fd
JA
250}
251
252void strip_blank_end(char *p)
253{
853ee7fc 254 char *start = p, *s;
523bfadb
JA
255
256 s = strchr(p, ';');
257 if (s)
258 *s = '\0';
259 s = strchr(p, '#');
260 if (s)
261 *s = '\0';
262 if (s)
263 p = s;
264
7f7e6e59 265 s = p + strlen(p);
76cd9378 266 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
cb2c86fd
JA
267 s--;
268
269 *(s + 1) = '\0';
270}
271
d6978a32 272static int check_range_bytes(const char *str, long *val, void *data)
cb2c86fd 273{
57fc29fa 274 long long __val;
cb2c86fd 275
57fc29fa
JA
276 if (!str_to_decimal(str, &__val, 1, data)) {
277 *val = __val;
cb2c86fd
JA
278 return 0;
279 }
280
cb2c86fd
JA
281 return 1;
282}
283
63f29372 284static int check_int(const char *p, int *val)
cb2c86fd 285{
787f7e95
JA
286 if (!strlen(p))
287 return 1;
d78ee463 288 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
289 if (sscanf(p, "%x", val) == 1)
290 return 0;
291 } else {
292 if (sscanf(p, "%u", val) == 1)
293 return 0;
294 }
cb2c86fd 295
e1f36503
JA
296 return 1;
297}
cb2c86fd 298
808def70
JA
299static int opt_len(const char *str)
300{
301 char *postfix;
302
303 postfix = strchr(str, ':');
304 if (!postfix)
305 return strlen(str);
306
307 return (int)(postfix - str);
308}
309
5f6ddf1e 310#define val_store(ptr, val, off, or, data) \
17abbe89
JA
311 do { \
312 ptr = td_var((data), (off)); \
5f6ddf1e
JA
313 if ((or)) \
314 *ptr |= (val); \
315 else \
316 *ptr = (val); \
17abbe89
JA
317 } while (0)
318
f90eff5a 319static int __handle_option(struct fio_option *o, const char *ptr, void *data,
787f7e95 320 int first, int more)
cb2c86fd 321{
63f29372
JA
322 int il, *ilp;
323 long long ull, *ullp;
324 long ul1, ul2;
b4692828 325 char **cp;
e42b01eb 326 int ret = 0, is_time = 0;
cb2c86fd 327
a3d741fa
JA
328 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
329 o->type, ptr);
330
e3cedca7 331 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
08e26e35
JA
332 fprintf(stderr, "Option %s requires an argument\n", o->name);
333 return 1;
334 }
335
e1f36503 336 switch (o->type) {
5f6ddf1e
JA
337 case FIO_OPT_STR:
338 case FIO_OPT_STR_MULTI: {
e1f36503 339 fio_opt_str_fn *fn = o->cb;
b1ec1da6 340 const struct value_pair *vp;
f085737f 341 struct value_pair posval[PARSE_MAX_VP];
ae2ddba4 342 int i, all_skipped = 1;
b1ec1da6 343
f085737f
JA
344 posval_sort(o, posval);
345
5f6ddf1e 346 ret = 1;
b1ec1da6 347 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 348 vp = &posval[i];
b1ec1da6 349 if (!vp->ival || vp->ival[0] == '\0')
a3073f4a 350 continue;
ae2ddba4 351 all_skipped = 0;
808def70 352 if (!strncmp(vp->ival, ptr, opt_len(ptr))) {
b1ec1da6 353 ret = 0;
5f6ddf1e
JA
354 if (o->roff1) {
355 if (vp->or)
356 *(unsigned int *) o->roff1 |= vp->oval;
357 else
358 *(unsigned int *) o->roff1 = vp->oval;
359 } else {
02c6aad5 360 if (!o->off1)
5f6ddf1e
JA
361 continue;
362 val_store(ilp, vp->oval, o->off1, vp->or, data);
02c6aad5 363 }
5f6ddf1e 364 continue;
b1ec1da6
JA
365 }
366 }
cb2c86fd 367
ae2ddba4 368 if (ret && !all_skipped)
b1ec1da6
JA
369 show_option_values(o);
370 else if (fn)
371 ret = fn(data, ptr);
e1f36503
JA
372 break;
373 }
e42b01eb
JA
374 case FIO_OPT_STR_VAL_TIME:
375 is_time = 1;
1a1137d9 376 case FIO_OPT_INT:
e42b01eb
JA
377 case FIO_OPT_STR_VAL: {
378 fio_opt_str_val_fn *fn = o->cb;
379
380 if (is_time)
381 ret = check_str_time(ptr, &ull);
382 else
383 ret = check_str_bytes(ptr, &ull, data);
e1f36503
JA
384
385 if (ret)
386 break;
387
db8e0165 388 if (o->maxval && ull > o->maxval) {
5ec10eaa
JA
389 fprintf(stderr, "max value out of range: %lld"
390 " (%d max)\n", ull, o->maxval);
db8e0165
JA
391 return 1;
392 }
393 if (o->minval && ull < o->minval) {
5ec10eaa
JA
394 fprintf(stderr, "min value out of range: %lld"
395 " (%d min)\n", ull, o->minval);
db8e0165
JA
396 return 1;
397 }
e1f36503
JA
398
399 if (fn)
400 ret = fn(data, &ull);
401 else {
e01b22b8 402 if (o->type == FIO_OPT_INT) {
02c6aad5
JA
403 if (first) {
404 if (o->roff1)
7758d4f0 405 *(unsigned int *) o->roff1 = ull;
02c6aad5 406 else
5f6ddf1e 407 val_store(ilp, ull, o->off1, 0, data);
02c6aad5
JA
408 }
409 if (!more) {
410 if (o->roff2)
7758d4f0 411 *(unsigned int *) o->roff2 = ull;
02c6aad5 412 else if (o->off2)
5f6ddf1e 413 val_store(ilp, ull, o->off2, 0, data);
02c6aad5 414 }
75e6f36f 415 } else {
02c6aad5
JA
416 if (first) {
417 if (o->roff1)
418 *(unsigned long long *) o->roff1 = ull;
419 else
5f6ddf1e 420 val_store(ullp, ull, o->off1, 0, data);
02c6aad5
JA
421 }
422 if (!more) {
423 if (o->roff2)
424 *(unsigned long long *) o->roff2 = ull;
425 else if (o->off2)
5f6ddf1e 426 val_store(ullp, ull, o->off2, 0, data);
02c6aad5 427 }
75e6f36f 428 }
e1f36503
JA
429 }
430 break;
431 }
af52b345
JA
432 case FIO_OPT_STR_STORE: {
433 fio_opt_str_fn *fn = o->cb;
434
02c6aad5
JA
435 if (o->roff1)
436 cp = (char **) o->roff1;
437 else
438 cp = td_var(data, o->off1);
439
e1f36503 440 *cp = strdup(ptr);
af52b345
JA
441 if (fn) {
442 ret = fn(data, ptr);
443 if (ret) {
444 free(*cp);
445 *cp = NULL;
446 }
447 }
e1f36503 448 break;
af52b345 449 }
e1f36503 450 case FIO_OPT_RANGE: {
b765a372 451 char tmp[128];
e1f36503
JA
452 char *p1, *p2;
453
0bbab0e7 454 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372
JA
455
456 p1 = strchr(tmp, '-');
e1f36503 457 if (!p1) {
0c9baf91
JA
458 p1 = strchr(tmp, ':');
459 if (!p1) {
460 ret = 1;
461 break;
462 }
e1f36503
JA
463 }
464
465 p2 = p1 + 1;
466 *p1 = '\0';
b765a372 467 p1 = tmp;
e1f36503
JA
468
469 ret = 1;
d6978a32
JA
470 if (!check_range_bytes(p1, &ul1, data) &&
471 !check_range_bytes(p2, &ul2, data)) {
e1f36503 472 ret = 0;
e1f36503 473 if (ul1 > ul2) {
f90eff5a
JA
474 unsigned long foo = ul1;
475
476 ul1 = ul2;
477 ul2 = foo;
478 }
479
480 if (first) {
02c6aad5 481 if (o->roff1)
7758d4f0 482 *(unsigned int *) o->roff1 = ul1;
02c6aad5 483 else
5f6ddf1e 484 val_store(ilp, ul1, o->off1, 0, data);
02c6aad5 485 if (o->roff2)
7758d4f0 486 *(unsigned int *) o->roff2 = ul2;
02c6aad5 487 else
5f6ddf1e 488 val_store(ilp, ul2, o->off2, 0, data);
17abbe89 489 }
02c6aad5 490 if (o->roff3 && o->roff4) {
7758d4f0
JA
491 *(unsigned int *) o->roff3 = ul1;
492 *(unsigned int *) o->roff4 = ul2;
02c6aad5 493 } else if (o->off3 && o->off4) {
5f6ddf1e
JA
494 val_store(ilp, ul1, o->off3, 0, data);
495 val_store(ilp, ul2, o->off4, 0, data);
e1f36503 496 }
17abbe89
JA
497 }
498
e1f36503
JA
499 break;
500 }
13335ddb 501 case FIO_OPT_BOOL: {
e1f36503
JA
502 fio_opt_int_fn *fn = o->cb;
503
504 ret = check_int(ptr, &il);
505 if (ret)
506 break;
507
db8e0165 508 if (o->maxval && il > (int) o->maxval) {
5ec10eaa
JA
509 fprintf(stderr, "max value out of range: %d (%d max)\n",
510 il, o->maxval);
db8e0165
JA
511 return 1;
512 }
513 if (o->minval && il < o->minval) {
5ec10eaa
JA
514 fprintf(stderr, "min value out of range: %d (%d min)\n",
515 il, o->minval);
db8e0165
JA
516 return 1;
517 }
e1f36503 518
76a43db4
JA
519 if (o->neg)
520 il = !il;
521
e1f36503
JA
522 if (fn)
523 ret = fn(data, &il);
524 else {
02c6aad5
JA
525 if (first) {
526 if (o->roff1)
527 *(unsigned int *)o->roff1 = il;
528 else
5f6ddf1e 529 val_store(ilp, il, o->off1, 0, data);
02c6aad5
JA
530 }
531 if (!more) {
532 if (o->roff2)
533 *(unsigned int *) o->roff2 = il;
534 else if (o->off2)
5f6ddf1e 535 val_store(ilp, il, o->off2, 0, data);
02c6aad5 536 }
e1f36503
JA
537 }
538 break;
539 }
540 case FIO_OPT_STR_SET: {
541 fio_opt_str_set_fn *fn = o->cb;
542
543 if (fn)
544 ret = fn(data);
545 else {
02c6aad5
JA
546 if (first) {
547 if (o->roff1)
548 *(unsigned int *) o->roff1 = 1;
549 else
5f6ddf1e 550 val_store(ilp, 1, o->off1, 0, data);
02c6aad5
JA
551 }
552 if (!more) {
553 if (o->roff2)
554 *(unsigned int *) o->roff2 = 1;
555 else if (o->off2)
5f6ddf1e 556 val_store(ilp, 1, o->off2, 0, data);
02c6aad5 557 }
e1f36503
JA
558 }
559 break;
560 }
15ca150e
JA
561 case FIO_OPT_DEPRECATED:
562 fprintf(stdout, "Option %s is deprecated\n", o->name);
563 break;
e1f36503 564 default:
1e97cce9 565 fprintf(stderr, "Bad option type %u\n", o->type);
e1f36503
JA
566 ret = 1;
567 }
cb2c86fd 568
70a4c0c8
JA
569 if (ret)
570 return ret;
571
d447a8c2 572 if (o->verify) {
70a4c0c8 573 ret = o->verify(o, data);
d447a8c2
JA
574 if (ret) {
575 fprintf(stderr,"Correct format for offending option\n");
576 fprintf(stderr, "%20s: %s\n", o->name, o->help);
577 show_option_help(o, stderr);
578 }
579 }
70a4c0c8 580
e1f36503 581 return ret;
cb2c86fd
JA
582}
583
7c8f1a5c 584static int handle_option(struct fio_option *o, const char *__ptr, void *data)
f90eff5a 585{
b62bdf2c
JA
586 char *o_ptr, *ptr, *ptr2;
587 int ret, done;
f90eff5a 588
7c8f1a5c
JA
589 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
590
b62bdf2c 591 o_ptr = ptr = NULL;
7c8f1a5c 592 if (__ptr)
b62bdf2c 593 o_ptr = ptr = strdup(__ptr);
a3d741fa 594
f90eff5a 595 /*
b62bdf2c
JA
596 * See if we have another set of parameters, hidden after a comma.
597 * Do this before parsing this round, to check if we should
787f7e95 598 * copy set 1 options to set 2.
f90eff5a 599 */
b62bdf2c
JA
600 done = 0;
601 ret = 1;
602 do {
603 int __ret;
604
605 ptr2 = NULL;
606 if (ptr &&
607 (o->type != FIO_OPT_STR_STORE) &&
608 (o->type != FIO_OPT_STR)) {
609 ptr2 = strchr(ptr, ',');
610 if (ptr2 && *(ptr2 + 1) == '\0')
611 *ptr2 = '\0';
612 if (o->type != FIO_OPT_STR_MULTI) {
613 if (!ptr2)
614 ptr2 = strchr(ptr, ':');
615 if (!ptr2)
616 ptr2 = strchr(ptr, '-');
617 }
618 }
787f7e95 619
b62bdf2c
JA
620 /*
621 * Don't return early if parsing the first option fails - if
622 * we are doing multiple arguments, we can allow the first one
623 * being empty.
624 */
625 __ret = __handle_option(o, ptr, data, !done, !!ptr2);
626 if (ret)
627 ret = __ret;
787f7e95 628
b62bdf2c
JA
629 if (!ptr2)
630 break;
f90eff5a 631
b62bdf2c
JA
632 ptr = ptr2 + 1;
633 done++;
634 } while (1);
787f7e95 635
b62bdf2c
JA
636 if (o_ptr)
637 free(o_ptr);
638 return ret;
f90eff5a
JA
639}
640
3b8b7135 641static struct fio_option *get_option(const char *opt,
07b3232d 642 struct fio_option *options, char **post)
3b8b7135
JA
643{
644 struct fio_option *o;
645 char *ret;
646
647 ret = strchr(opt, '=');
648 if (ret) {
649 *post = ret;
650 *ret = '\0';
651 ret = (char *) opt;
652 (*post)++;
43c129b4 653 strip_blank_end(ret);
07b3232d 654 o = find_option(options, ret);
3b8b7135 655 } else {
07b3232d 656 o = find_option(options, opt);
3b8b7135
JA
657 *post = NULL;
658 }
659
660 return o;
661}
662
663static int opt_cmp(const void *p1, const void *p2)
664{
665 struct fio_option *o1, *o2;
666 char *s1, *s2, *foo;
8cdabc1d 667 int prio1, prio2;
3b8b7135
JA
668
669 s1 = strdup(*((char **) p1));
670 s2 = strdup(*((char **) p2));
671
07b3232d
JA
672 o1 = get_option(s1, fio_options, &foo);
673 o2 = get_option(s2, fio_options, &foo);
0b9d69ec 674
8cdabc1d
JA
675 prio1 = prio2 = 0;
676 if (o1)
677 prio1 = o1->prio;
678 if (o2)
679 prio2 = o2->prio;
3b8b7135
JA
680
681 free(s1);
682 free(s2);
8cdabc1d 683 return prio2 - prio1;
3b8b7135
JA
684}
685
686void sort_options(char **opts, struct fio_option *options, int num_opts)
687{
688 fio_options = options;
689 qsort(opts, num_opts, sizeof(char *), opt_cmp);
690 fio_options = NULL;
691}
692
b4692828 693int parse_cmd_option(const char *opt, const char *val,
07b3232d 694 struct fio_option *options, void *data)
b4692828
JA
695{
696 struct fio_option *o;
697
07b3232d 698 o = find_option(options, opt);
b4692828 699 if (!o) {
43c129b4 700 fprintf(stderr, "Bad option <%s>\n", opt);
b4692828
JA
701 return 1;
702 }
703
b1508cf9
JA
704 if (!handle_option(o, val, data))
705 return 0;
706
707 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
708 return 1;
b4692828
JA
709}
710
88b5a391
AC
711/*
712 * Return a copy of the input string with substrings of the form ${VARNAME}
713 * substituted with the value of the environment variable VARNAME. The
714 * substitution always occurs, even if VARNAME is empty or the corresponding
715 * environment variable undefined.
716 */
717static char *option_dup_subs(const char *opt)
718{
719 char out[OPT_LEN_MAX+1];
720 char in[OPT_LEN_MAX+1];
721 char *outptr = out;
722 char *inptr = in;
723 char *ch1, *ch2, *env;
724 ssize_t nchr = OPT_LEN_MAX;
725 size_t envlen;
726
a0741cbb 727 if (strlen(opt) + 1 > OPT_LEN_MAX) {
38789b58
JA
728 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
729 return NULL;
730 }
731
88b5a391
AC
732 in[OPT_LEN_MAX] = '\0';
733 strncpy(in, opt, OPT_LEN_MAX);
734
735 while (*inptr && nchr > 0) {
736 if (inptr[0] == '$' && inptr[1] == '{') {
737 ch2 = strchr(inptr, '}');
738 if (ch2 && inptr+1 < ch2) {
739 ch1 = inptr+2;
740 inptr = ch2+1;
741 *ch2 = '\0';
742
743 env = getenv(ch1);
744 if (env) {
745 envlen = strlen(env);
746 if (envlen <= nchr) {
747 memcpy(outptr, env, envlen);
748 outptr += envlen;
749 nchr -= envlen;
750 }
751 }
752
753 continue;
754 }
755 }
756
757 *outptr++ = *inptr++;
758 --nchr;
759 }
760
761 *outptr = '\0';
762 return strdup(out);
763}
764
07b3232d 765int parse_option(const char *opt, struct fio_option *options, void *data)
cb2c86fd 766{
b4692828 767 struct fio_option *o;
3b8b7135 768 char *post, *tmp;
e1f36503 769
88b5a391 770 tmp = option_dup_subs(opt);
38789b58
JA
771 if (!tmp)
772 return 1;
e1f36503 773
07b3232d 774 o = get_option(tmp, options, &post);
e1f36503 775 if (!o) {
43c129b4 776 fprintf(stderr, "Bad option <%s>\n", tmp);
0401bca6 777 free(tmp);
e1f36503
JA
778 return 1;
779 }
780
0401bca6
JA
781 if (!handle_option(o, post, data)) {
782 free(tmp);
b1508cf9 783 return 0;
0401bca6 784 }
b1508cf9
JA
785
786 fprintf(stderr, "fio: failed parsing %s\n", opt);
0401bca6 787 free(tmp);
b1508cf9 788 return 1;
e1f36503 789}
fd28ca49 790
0e9f7fac
JA
791/*
792 * Option match, levenshtein distance. Handy for not quite remembering what
793 * the option name is.
794 */
795static int string_distance(const char *s1, const char *s2)
796{
797 unsigned int s1_len = strlen(s1);
798 unsigned int s2_len = strlen(s2);
799 unsigned int *p, *q, *r;
800 unsigned int i, j;
801
802 p = malloc(sizeof(unsigned int) * (s2_len + 1));
803 q = malloc(sizeof(unsigned int) * (s2_len + 1));
804
805 p[0] = 0;
806 for (i = 1; i <= s2_len; i++)
807 p[i] = p[i - 1] + 1;
808
809 for (i = 1; i <= s1_len; i++) {
810 q[0] = p[0] + 1;
811 for (j = 1; j <= s2_len; j++) {
812 unsigned int sub = p[j - 1];
813
814 if (s1[i - 1] != s2[j - 1])
815 sub++;
816
817 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
818 }
819 r = p;
820 p = q;
821 q = r;
822 }
823
824 i = p[s2_len];
825 free(p);
826 free(q);
827 return i;
828}
829
afdf9352
JA
830static struct fio_option *find_child(struct fio_option *options,
831 struct fio_option *o)
832{
833 struct fio_option *__o;
834
fdf28744
JA
835 for (__o = options + 1; __o->name; __o++)
836 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
837 return __o;
838
839 return NULL;
840}
841
323d9113
JA
842static void __print_option(struct fio_option *o, struct fio_option *org,
843 int level)
afdf9352
JA
844{
845 char name[256], *p;
323d9113 846 int depth;
afdf9352
JA
847
848 if (!o)
849 return;
ef9aff52
JA
850 if (!org)
851 org = o;
5ec10eaa 852
afdf9352 853 p = name;
323d9113
JA
854 depth = level;
855 while (depth--)
856 p += sprintf(p, "%s", " ");
afdf9352
JA
857
858 sprintf(p, "%s", o->name);
859
860 printf("%-24s: %s\n", name, o->help);
323d9113
JA
861}
862
863static void print_option(struct fio_option *o)
864{
865 struct fio_option *parent;
866 struct fio_option *__o;
867 unsigned int printed;
868 unsigned int level;
869
870 __print_option(o, NULL, 0);
871 parent = o;
872 level = 0;
873 do {
874 level++;
875 printed = 0;
876
877 while ((__o = find_child(o, parent)) != NULL) {
878 __print_option(__o, o, level);
879 o = __o;
880 printed++;
881 }
882
883 parent = o;
884 } while (printed);
afdf9352
JA
885}
886
07b3232d 887int show_cmd_help(struct fio_option *options, const char *name)
0e9f7fac
JA
888{
889 struct fio_option *o, *closest;
d091d099 890 unsigned int best_dist = -1U;
29fc6afe 891 int found = 0;
320beefe
JA
892 int show_all = 0;
893
894 if (!name || !strcmp(name, "all"))
895 show_all = 1;
fd28ca49 896
0e9f7fac
JA
897 closest = NULL;
898 best_dist = -1;
4945ba12 899 for (o = &options[0]; o->name; o++) {
320beefe
JA
900 int match = 0;
901
15ca150e
JA
902 if (o->type == FIO_OPT_DEPRECATED)
903 continue;
07b3232d
JA
904 if (!exec_profile && o->prof_name)
905 continue;
15ca150e 906
0e9f7fac 907 if (name) {
7f9348f8
JA
908 if (!strcmp(name, o->name) ||
909 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
910 match = 1;
911 else {
912 unsigned int dist;
913
914 dist = string_distance(name, o->name);
915 if (dist < best_dist) {
916 best_dist = dist;
917 closest = o;
918 }
919 }
920 }
fd28ca49
JA
921
922 if (show_all || match) {
29fc6afe 923 found = 1;
c167dedc 924 if (match)
07b3232d 925 printf("%20s: %s\n", o->name, o->help);
c167dedc 926 if (show_all) {
afdf9352 927 if (!o->parent)
323d9113 928 print_option(o);
4945ba12 929 continue;
c167dedc 930 }
fd28ca49
JA
931 }
932
70df2f19
JA
933 if (!match)
934 continue;
935
d447a8c2 936 show_option_help(o, stdout);
fd28ca49
JA
937 }
938
29fc6afe
JA
939 if (found)
940 return 0;
fd28ca49 941
0e9f7fac 942 printf("No such command: %s", name);
d091d099
JA
943
944 /*
945 * Only print an appropriately close option, one where the edit
946 * distance isn't too big. Otherwise we get crazy matches.
947 */
948 if (closest && best_dist < 3) {
0e9f7fac
JA
949 printf(" - showing closest match\n");
950 printf("%20s: %s\n", closest->name, closest->help);
d447a8c2 951 show_option_help(closest, stdout);
0e9f7fac
JA
952 } else
953 printf("\n");
954
29fc6afe 955 return 1;
fd28ca49 956}
ee738499 957
13335ddb
JA
958/*
959 * Handle parsing of default parameters.
960 */
ee738499
JA
961void fill_default_options(void *data, struct fio_option *options)
962{
4945ba12 963 struct fio_option *o;
ee738499 964
a3d741fa
JA
965 dprint(FD_PARSE, "filling default options\n");
966
4945ba12 967 for (o = &options[0]; o->name; o++)
ee738499
JA
968 if (o->def)
969 handle_option(o, o->def, data);
ee738499 970}
13335ddb 971
9f988e2e
JA
972void option_init(struct fio_option *o)
973{
974 if (o->type == FIO_OPT_DEPRECATED)
975 return;
976 if (o->type == FIO_OPT_BOOL) {
977 o->minval = 0;
978 o->maxval = 1;
979 }
980 if (o->type == FIO_OPT_STR_SET && o->def) {
981 fprintf(stderr, "Option %s: string set option with"
982 " default will always be true\n", o->name);
983 }
e2de69da 984 if (!o->cb && (!o->off1 && !o->roff1)) {
9f988e2e
JA
985 fprintf(stderr, "Option %s: neither cb nor offset given\n",
986 o->name);
987 }
5f6ddf1e
JA
988 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
989 o->type == FIO_OPT_STR_MULTI)
9f988e2e 990 return;
e2de69da
JA
991 if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) ||
992 (o->roff1 || o->roff2 || o->roff3 || o->roff4))) {
9f988e2e
JA
993 fprintf(stderr, "Option %s: both cb and offset given\n",
994 o->name);
995 }
996}
997
13335ddb
JA
998/*
999 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 1000 * values and whether both callback and offsets are given.
13335ddb
JA
1001 */
1002void options_init(struct fio_option *options)
1003{
4945ba12 1004 struct fio_option *o;
13335ddb 1005
a3d741fa
JA
1006 dprint(FD_PARSE, "init options\n");
1007
9f988e2e
JA
1008 for (o = &options[0]; o->name; o++)
1009 option_init(o);
13335ddb 1010}