parse: fix bssplit option
[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>
cb2c86fd
JA
6#include <ctype.h>
7#include <string.h>
8#include <errno.h>
9#include <limits.h>
ab9461ea 10#include <float.h>
cb2c86fd 11
c26438ad 12#include "compiler/compiler.h"
cb2c86fd 13#include "parse.h"
a3d741fa 14#include "debug.h"
3d2d14bc 15#include "log.h"
9f988e2e 16#include "options.h"
d220c761 17#include "optgroup.h"
e25839d4 18#include "minmax.h"
fd112d34 19#include "lib/ieee754.h"
0f38bbef 20#include "lib/pow2.h"
cb2c86fd 21
b470a02c
SC
22#ifdef CONFIG_ARITHMETIC
23#include "y.tab.h"
24#endif
25
c26438ad
JA
26static const char *opt_type_names[] = {
27 "OPT_INVALID",
28 "OPT_STR",
5fff9543 29 "OPT_STR_ULL",
c26438ad
JA
30 "OPT_STR_MULTI",
31 "OPT_STR_VAL",
32 "OPT_STR_VAL_TIME",
33 "OPT_STR_STORE",
34 "OPT_RANGE",
35 "OPT_INT",
5fff9543 36 "OPT_ULL",
c26438ad
JA
37 "OPT_BOOL",
38 "OPT_FLOAT_LIST",
39 "OPT_STR_SET",
40 "OPT_DEPRECATED",
c47537ee 41 "OPT_SOFT_DEPRECATED",
c26438ad
JA
42 "OPT_UNSUPPORTED",
43};
44
9109883a 45static const struct fio_option *__fio_options;
3b8b7135 46
f085737f
JA
47static int vp_cmp(const void *p1, const void *p2)
48{
49 const struct value_pair *vp1 = p1;
50 const struct value_pair *vp2 = p2;
51
52 return strlen(vp2->ival) - strlen(vp1->ival);
53}
54
9109883a 55static void posval_sort(const struct fio_option *o, struct value_pair *vpmap)
f085737f
JA
56{
57 const struct value_pair *vp;
58 int entries;
59
60 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
61
62 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
63 vp = &o->posval[entries];
64 if (!vp->ival || vp->ival[0] == '\0')
65 break;
66
67 memcpy(&vpmap[entries], vp, sizeof(*vp));
68 }
69
70 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
71}
72
9109883a 73static void show_option_range(const struct fio_option *o,
34a3a855 74 ssize_t (*logger)(const char *format, ...))
b1ec1da6 75{
3c037bcf 76 if (o->type == FIO_OPT_FLOAT_LIST) {
a1e070c8 77 const char *sep = "";
45b3e8d7 78 if (!o->minfp && !o->maxfp)
83349190
YH
79 return;
80
a1e070c8
BVA
81 logger("%20s: ", "range");
82 if (o->minfp != DBL_MIN) {
83 logger("min=%f", o->minfp);
84 sep = ", ";
85 }
ab9461ea 86 if (o->maxfp != DBL_MAX)
a1e070c8 87 logger("%smax=%f", sep, o->maxfp);
06b0be6e 88 logger("\n");
3c037bcf 89 } else if (!o->posval[0].ival) {
83349190
YH
90 if (!o->minval && !o->maxval)
91 return;
b1ec1da6 92
06b0be6e 93 logger("%20s: min=%d", "range", o->minval);
83349190 94 if (o->maxval)
06b0be6e
JA
95 logger(", max=%d", o->maxval);
96 logger("\n");
83349190 97 }
b1ec1da6
JA
98}
99
9109883a 100static void show_option_values(const struct fio_option *o)
b1ec1da6 101{
a3073f4a 102 int i;
b1ec1da6 103
a3073f4a 104 for (i = 0; i < PARSE_MAX_VP; i++) {
7837213b 105 const struct value_pair *vp = &o->posval[i];
b1ec1da6 106
7837213b 107 if (!vp->ival)
a3073f4a 108 continue;
b1ec1da6 109
06b0be6e 110 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
7837213b 111 if (vp->help)
06b0be6e
JA
112 log_info(" %s", vp->help);
113 log_info("\n");
a3073f4a 114 }
b1ec1da6
JA
115
116 if (i)
06b0be6e 117 log_info("\n");
b1ec1da6
JA
118}
119
9109883a 120static void show_option_help(const struct fio_option *o, int is_err)
d447a8c2
JA
121{
122 const char *typehelp[] = {
07b3232d 123 "invalid",
d447a8c2 124 "string (opt=bla)",
ae3fb6fb 125 "string (opt=bla)",
d447a8c2
JA
126 "string with possible k/m/g postfix (opt=4k)",
127 "string with time postfix (opt=10s)",
128 "string (opt=bla)",
129 "string with dual range (opt=1k-4k,4k-8k)",
130 "integer value (opt=100)",
131 "boolean value (opt=1)",
83349190 132 "list of floating point values separated by ':' (opt=5.9:7.8)",
d447a8c2 133 "no argument (opt)",
07b3232d 134 "deprecated",
675d2d65 135 "unsupported",
d447a8c2 136 };
34a3a855 137 ssize_t (*logger)(const char *format, ...);
06b0be6e
JA
138
139 if (is_err)
140 logger = log_err;
141 else
142 logger = log_info;
d447a8c2
JA
143
144 if (o->alias)
06b0be6e 145 logger("%20s: %s\n", "alias", o->alias);
d447a8c2 146
06b0be6e
JA
147 logger("%20s: %s\n", "type", typehelp[o->type]);
148 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
07b3232d 149 if (o->prof_name)
06b0be6e
JA
150 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
151 show_option_range(o, logger);
d447a8c2
JA
152 show_option_values(o);
153}
154
0de5b26f
JA
155static unsigned long long get_mult_time(const char *str, int len,
156 int is_seconds)
cb2c86fd 157{
74454ce4
CE
158 const char *p = str;
159 char *c;
e4668264 160 unsigned long long mult = 1;
1c9f0761 161 int i;
74454ce4
CE
162
163 /*
164 * Go forward until we hit a non-digit, or +/- sign
165 */
166 while ((p - str) <= len) {
167 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
168 break;
169 p++;
cb2c86fd 170 }
74454ce4 171
0de5b26f
JA
172 if (!isalpha((int) *p)) {
173 if (is_seconds)
174 return 1000000UL;
175 else
176 return 1;
177 }
74454ce4
CE
178
179 c = strdup(p);
1c9f0761 180 for (i = 0; i < strlen(c); i++)
e99161b4 181 c[i] = tolower((unsigned char)c[i]);
74454ce4 182
e4668264 183 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
74454ce4 184 mult = 1;
e4668264 185 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
74454ce4 186 mult = 1000;
e4668264
JA
187 else if (!strcmp("s", c))
188 mult = 1000000;
74454ce4 189 else if (!strcmp("m", c))
e4668264 190 mult = 60 * 1000000UL;
74454ce4 191 else if (!strcmp("h", c))
e4668264 192 mult = 60 * 60 * 1000000UL;
74454ce4 193 else if (!strcmp("d", c))
4ed22fe5 194 mult = 24 * 60 * 60 * 1000000ULL;
74454ce4
CE
195
196 free(c);
197 return mult;
cb2c86fd
JA
198}
199
a03fb65f
JA
200static int is_separator(char c)
201{
202 switch (c) {
203 case ':':
204 case '-':
205 case ',':
206 case '/':
207 return 1;
208 default:
209 return 0;
210 }
211}
212
7bb59102
JA
213static unsigned long long __get_mult_bytes(const char *p, void *data,
214 int *percent)
cb2c86fd 215{
d6978a32 216 unsigned int kb_base = fio_get_kb_base(data);
a639f0bb 217 unsigned long long ret = 1;
57fc29fa
JA
218 unsigned int i, pow = 0, mult = kb_base;
219 char *c;
a639f0bb 220
57fc29fa
JA
221 if (!p)
222 return 1;
a639f0bb 223
57fc29fa
JA
224 c = strdup(p);
225
a03fb65f 226 for (i = 0; i < strlen(c); i++) {
e99161b4 227 c[i] = tolower((unsigned char)c[i]);
a03fb65f
JA
228 if (is_separator(c[i])) {
229 c[i] = '\0';
230 break;
231 }
232 }
57fc29fa 233
d694a6a7
RE
234 /* If kb_base is 1000, use true units.
235 * If kb_base is 1024, use opposite units.
236 */
a04f158d 237 if (!strncmp("pib", c, 3)) {
57fc29fa 238 pow = 5;
d694a6a7
RE
239 if (kb_base == 1000)
240 mult = 1024;
241 else if (kb_base == 1024)
242 mult = 1000;
a04f158d 243 } else if (!strncmp("tib", c, 3)) {
57fc29fa 244 pow = 4;
d694a6a7
RE
245 if (kb_base == 1000)
246 mult = 1024;
247 else if (kb_base == 1024)
248 mult = 1000;
a04f158d 249 } else if (!strncmp("gib", c, 3)) {
57fc29fa 250 pow = 3;
d694a6a7
RE
251 if (kb_base == 1000)
252 mult = 1024;
253 else if (kb_base == 1024)
254 mult = 1000;
a04f158d 255 } else if (!strncmp("mib", c, 3)) {
57fc29fa 256 pow = 2;
d694a6a7
RE
257 if (kb_base == 1000)
258 mult = 1024;
259 else if (kb_base == 1024)
260 mult = 1000;
a04f158d 261 } else if (!strncmp("kib", c, 3)) {
57fc29fa 262 pow = 1;
d694a6a7
RE
263 if (kb_base == 1000)
264 mult = 1024;
265 else if (kb_base == 1024)
266 mult = 1000;
267 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
57fc29fa 268 pow = 5;
d694a6a7 269 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
57fc29fa 270 pow = 4;
d694a6a7 271 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
57fc29fa 272 pow = 3;
d694a6a7 273 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
57fc29fa 274 pow = 2;
d694a6a7 275 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
57fc29fa 276 pow = 1;
d694a6a7 277 } else if (!strncmp("%", c, 1)) {
7bb59102 278 *percent = 1;
e721c57f 279 free(c);
7bb59102
JA
280 return ret;
281 }
57fc29fa
JA
282
283 while (pow--)
284 ret *= (unsigned long long) mult;
285
286 free(c);
a639f0bb 287 return ret;
cb2c86fd
JA
288}
289
7bb59102 290static unsigned long long get_mult_bytes(const char *str, int len, void *data,
6925dd35 291 int *percent)
57fc29fa 292{
55ed9636 293 const char *p = str;
ba4ddd69 294 int digit_seen = 0;
57fc29fa 295
1d1c187b 296 if (len < 2)
7bb59102 297 return __get_mult_bytes(str, data, percent);
1d1c187b 298
d0c814ec
SL
299 /*
300 * Go forward until we hit a non-digit, or +/- sign
301 */
55ed9636 302 while ((p - str) <= len) {
ba4ddd69
JA
303 if (!isdigit((int) *p) &&
304 (((*p != '+') && (*p != '-')) || digit_seen))
55ed9636 305 break;
3c703d13 306 digit_seen |= isdigit((int) *p);
55ed9636
JA
307 p++;
308 }
309
7bb59102 310 if (!isalpha((int) *p) && (*p != '%'))
57fc29fa
JA
311 p = NULL;
312
7bb59102 313 return __get_mult_bytes(p, data, percent);
57fc29fa
JA
314}
315
b470a02c 316extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
88038bc7
SC
317 double *dval, double implied_units,
318 int is_time);
b470a02c 319
83349190
YH
320/*
321 * Convert string into a floating number. Return 1 for success and 0 otherwise.
322 */
88038bc7 323int str_to_float(const char *str, double *val, int is_time)
83349190 324{
b470a02c
SC
325#ifdef CONFIG_ARITHMETIC
326 int rc;
327 long long ival;
328 double dval;
329
330 if (str[0] == '(') {
88038bc7 331 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
b470a02c
SC
332 if (!rc) {
333 *val = dval;
334 return 1;
335 }
336 }
337#endif
338 return 1 == sscanf(str, "%lf", val);
83349190
YH
339}
340
cb2c86fd 341/*
e1f36503 342 * convert string into decimal value, noting any size suffix
cb2c86fd 343 */
0de5b26f 344int str_to_decimal(const char *str, long long *val, int kilo, void *data,
88038bc7 345 int is_seconds, int is_time)
cb2c86fd 346{
b347f9da 347 int len, base;
b470a02c
SC
348 int rc = 1;
349#ifdef CONFIG_ARITHMETIC
350 long long ival;
351 double dval;
8e2c678f 352 double implied_units = 1.0;
b470a02c 353#endif
cb2c86fd 354
cb2c86fd 355 len = strlen(str);
f90eff5a
JA
356 if (!len)
357 return 1;
cb2c86fd 358
b470a02c 359#ifdef CONFIG_ARITHMETIC
8e2c678f
SC
360 if (is_seconds)
361 implied_units = 1000000.0;
b470a02c 362 if (str[0] == '(')
88038bc7 363 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
18722a18
SC
364 if (str[0] == '(' && !rc) {
365 if (!kilo && is_seconds)
366 *val = ival / 1000000LL;
367 else
368 *val = ival;
369 }
b470a02c 370#endif
b347f9da 371
b470a02c
SC
372 if (rc == 1) {
373 if (strstr(str, "0x") || strstr(str, "0X"))
374 base = 16;
375 else
376 base = 10;
377
378 *val = strtoll(str, NULL, base);
379 if (*val == LONG_MAX && errno == ERANGE)
380 return 1;
381 }
cb2c86fd 382
7bb59102
JA
383 if (kilo) {
384 unsigned long long mult;
385 int perc = 0;
386
6925dd35 387 mult = get_mult_bytes(str, len, data, &perc);
7bb59102
JA
388 if (perc)
389 *val = -1ULL - *val;
390 else
391 *val *= mult;
392 } else
0de5b26f 393 *val *= get_mult_time(str, len, is_seconds);
1d31d1bc 394
cb2c86fd
JA
395 return 0;
396}
397
9af4a244 398int check_str_bytes(const char *p, long long *val, void *data)
cb2c86fd 399{
88038bc7 400 return str_to_decimal(p, val, 1, data, 0, 0);
cb2c86fd
JA
401}
402
0de5b26f 403int check_str_time(const char *p, long long *val, int is_seconds)
cb2c86fd 404{
88038bc7 405 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
cb2c86fd
JA
406}
407
408void strip_blank_front(char **p)
409{
410 char *s = *p;
411
4c8e9640
JA
412 if (!strlen(s))
413 return;
76cd9378 414 while (isspace((int) *s))
cb2c86fd 415 s++;
4d651dad
JA
416
417 *p = s;
cb2c86fd
JA
418}
419
420void strip_blank_end(char *p)
421{
853ee7fc 422 char *start = p, *s;
523bfadb 423
4c8e9640
JA
424 if (!strlen(p))
425 return;
426
523bfadb
JA
427 s = strchr(p, ';');
428 if (s)
429 *s = '\0';
430 s = strchr(p, '#');
431 if (s)
432 *s = '\0';
433 if (s)
434 p = s;
435
7f7e6e59 436 s = p + strlen(p);
76cd9378 437 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
cb2c86fd
JA
438 s--;
439
440 *(s + 1) = '\0';
441}
442
5fff9543 443static int check_range_bytes(const char *str, long long *val, void *data)
cb2c86fd 444{
57fc29fa 445 long long __val;
cb2c86fd 446
88038bc7 447 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
57fc29fa 448 *val = __val;
cb2c86fd
JA
449 return 0;
450 }
451
cb2c86fd
JA
452 return 1;
453}
454
63f29372 455static int check_int(const char *p, int *val)
cb2c86fd 456{
787f7e95
JA
457 if (!strlen(p))
458 return 1;
d78ee463 459 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
460 if (sscanf(p, "%x", val) == 1)
461 return 0;
462 } else {
463 if (sscanf(p, "%u", val) == 1)
464 return 0;
465 }
cb2c86fd 466
e1f36503
JA
467 return 1;
468}
cb2c86fd 469
e6f735f0 470static size_t opt_len(const char *str)
808def70
JA
471{
472 char *postfix;
473
474 postfix = strchr(str, ':');
475 if (!postfix)
476 return strlen(str);
477
478 return (int)(postfix - str);
479}
480
119cd939
JA
481static int str_match_len(const struct value_pair *vp, const char *str)
482{
483 return max(strlen(vp->ival), opt_len(str));
484}
485
f0fdbcaf
JA
486#define val_store(ptr, val, off, or, data, o) \
487 do { \
488 ptr = td_var((data), (o), (off)); \
489 if ((or)) \
490 *ptr |= (val); \
491 else \
492 *ptr = (val); \
17abbe89
JA
493 } while (0)
494
9109883a 495static const char *opt_type_name(const struct fio_option *o)
c26438ad
JA
496{
497 compiletime_assert(ARRAY_SIZE(opt_type_names) - 1 == FIO_OPT_UNSUPPORTED,
498 "opt_type_names[] index");
499
47844764 500 if (o->type <= FIO_OPT_UNSUPPORTED)
c26438ad
JA
501 return opt_type_names[o->type];
502
503 return "OPT_UNKNOWN?";
504}
505
9109883a
BVA
506static int __handle_option(const struct fio_option *o, const char *ptr,
507 void *data, int first, int more, int curr)
cb2c86fd 508{
2fdbefdd 509 int il=0, *ilp;
fd112d34 510 fio_fp64_t *flp;
63f29372 511 long long ull, *ullp;
5fff9543
JF
512 long ul2;
513 long long ull1, ull2;
83349190 514 double uf;
bfe1d592 515 char **cp = NULL;
e42b01eb 516 int ret = 0, is_time = 0;
c44b1ff5
JA
517 const struct value_pair *vp;
518 struct value_pair posval[PARSE_MAX_VP];
519 int i, all_skipped = 1;
cb2c86fd 520
c26438ad
JA
521 dprint(FD_PARSE, "__handle_option=%s, type=%s, ptr=%s\n", o->name,
522 opt_type_name(o), ptr);
a3d741fa 523
e3cedca7 524 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
28d7c363 525 log_err("Option %s requires an argument\n", o->name);
08e26e35
JA
526 return 1;
527 }
528
e1f36503 529 switch (o->type) {
5f6ddf1e 530 case FIO_OPT_STR:
5fff9543 531 case FIO_OPT_STR_ULL:
5f6ddf1e 532 case FIO_OPT_STR_MULTI: {
e1f36503 533 fio_opt_str_fn *fn = o->cb;
b1ec1da6 534
f085737f
JA
535 posval_sort(o, posval);
536
5f6ddf1e 537 ret = 1;
b1ec1da6 538 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 539 vp = &posval[i];
b1ec1da6 540 if (!vp->ival || vp->ival[0] == '\0')
a3073f4a 541 continue;
ae2ddba4 542 all_skipped = 0;
078b46d1
JA
543 if (!ptr)
544 break;
119cd939 545 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
b1ec1da6 546 ret = 0;
5fff9543
JF
547 if (!o->off1)
548 continue;
549 if (o->type == FIO_OPT_STR_ULL)
550 val_store(ullp, vp->oval, o->off1, vp->orval, data, o);
551 else
ebadc0ce 552 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
5f6ddf1e 553 continue;
b1ec1da6
JA
554 }
555 }
cb2c86fd 556
ae2ddba4 557 if (ret && !all_skipped)
b1ec1da6
JA
558 show_option_values(o);
559 else if (fn)
560 ret = fn(data, ptr);
e1f36503
JA
561 break;
562 }
e42b01eb
JA
563 case FIO_OPT_STR_VAL_TIME:
564 is_time = 1;
9854d2e3 565 /* fall through */
5fff9543 566 case FIO_OPT_ULL:
1a1137d9 567 case FIO_OPT_INT:
e42b01eb
JA
568 case FIO_OPT_STR_VAL: {
569 fio_opt_str_val_fn *fn = o->cb;
6eaf09d6
SL
570 char tmp[128], *p;
571
88038bc7
SC
572 if (!is_time && o->is_time)
573 is_time = o->is_time;
574
209e1037 575 tmp[sizeof(tmp) - 1] = '\0';
6eaf09d6
SL
576 strncpy(tmp, ptr, sizeof(tmp) - 1);
577 p = strchr(tmp, ',');
578 if (p)
579 *p = '\0';
e42b01eb
JA
580
581 if (is_time)
0de5b26f 582 ret = check_str_time(tmp, &ull, o->is_seconds);
e42b01eb 583 else
6eaf09d6 584 ret = check_str_bytes(tmp, &ull, data);
e1f36503 585
ae3fcb5a
JA
586 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
587
e1f36503
JA
588 if (ret)
589 break;
0f38bbef
JA
590 if (o->pow2 && !is_power_of_2(ull)) {
591 log_err("%s: must be a power-of-2\n", o->name);
592 return 1;
593 }
e1f36503 594
db8e0165 595 if (o->maxval && ull > o->maxval) {
d4fc2f04 596 log_err("max value out of range: %llu"
5fff9543 597 " (%llu max)\n", ull, o->maxval);
db8e0165
JA
598 return 1;
599 }
600 if (o->minval && ull < o->minval) {
ff523a66
JA
601 log_err("min value out of range: %lld"
602 " (%d min)\n", ull, o->minval);
db8e0165
JA
603 return 1;
604 }
d926d535
JA
605 if (o->posval[0].ival) {
606 posval_sort(o, posval);
607
608 ret = 1;
609 for (i = 0; i < PARSE_MAX_VP; i++) {
610 vp = &posval[i];
611 if (!vp->ival || vp->ival[0] == '\0')
612 continue;
613 if (vp->oval == ull) {
614 ret = 0;
615 break;
616 }
617 }
618 if (ret) {
128e4caf
JA
619 log_err("fio: value %llu not allowed:\n", ull);
620 show_option_values(o);
d926d535
JA
621 return 1;
622 }
623 }
e1f36503
JA
624
625 if (fn)
626 ret = fn(data, &ull);
627 else {
e01b22b8 628 if (o->type == FIO_OPT_INT) {
7b504edd
JA
629 if (first)
630 val_store(ilp, ull, o->off1, 0, data, o);
6eaf09d6 631 if (curr == 1) {
7b504edd
JA
632 if (o->off2)
633 val_store(ilp, ull, o->off2, 0, data, o);
02c6aad5 634 }
6eaf09d6 635 if (curr == 2) {
7b504edd
JA
636 if (o->off3)
637 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
638 }
639 if (!more) {
640 if (curr < 1) {
7b504edd
JA
641 if (o->off2)
642 val_store(ilp, ull, o->off2, 0, data, o);
6eaf09d6
SL
643 }
644 if (curr < 2) {
7b504edd
JA
645 if (o->off3)
646 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
647 }
648 }
5fff9543
JF
649 } else if (o->type == FIO_OPT_ULL) {
650 if (first)
651 val_store(ullp, ull, o->off1, 0, data, o);
652 if (curr == 1) {
653 if (o->off2)
654 val_store(ullp, ull, o->off2, 0, data, o);
655 }
656 if (curr == 2) {
657 if (o->off3)
658 val_store(ullp, ull, o->off3, 0, data, o);
659 }
660 if (!more) {
661 if (curr < 1) {
662 if (o->off2)
663 val_store(ullp, ull, o->off2, 0, data, o);
664 }
665 if (curr < 2) {
666 if (o->off3)
667 val_store(ullp, ull, o->off3, 0, data, o);
668 }
669 }
75e6f36f 670 } else {
7b504edd
JA
671 if (first)
672 val_store(ullp, ull, o->off1, 0, data, o);
02c6aad5 673 if (!more) {
7b504edd
JA
674 if (o->off2)
675 val_store(ullp, ull, o->off2, 0, data, o);
02c6aad5 676 }
75e6f36f 677 }
e1f36503
JA
678 }
679 break;
680 }
83349190 681 case FIO_OPT_FLOAT_LIST: {
eef02441
JA
682 char *cp2;
683
435d195a
VKF
684 if (first) {
685 /*
686 ** Initialize precision to 0 and zero out list
687 ** in case specified list is shorter than default
688 */
3e260a46
JA
689 if (o->off2) {
690 ul2 = 0;
f0fdbcaf 691 ilp = td_var(data, o, o->off2);
3e260a46
JA
692 *ilp = ul2;
693 }
435d195a 694
f0fdbcaf 695 flp = td_var(data, o, o->off1);
435d195a
VKF
696 for(i = 0; i < o->maxlen; i++)
697 flp[i].u.f = 0.0;
698 }
83349190 699 if (curr >= o->maxlen) {
28d7c363 700 log_err("the list exceeding max length %d\n",
83349190
YH
701 o->maxlen);
702 return 1;
703 }
88038bc7 704 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
28d7c363 705 log_err("not a floating point value: %s\n", ptr);
83349190
YH
706 return 1;
707 }
45b3e8d7
BVA
708 if (o->minfp || o->maxfp) {
709 if (uf > o->maxfp) {
710 log_err("value out of range: %f"
711 " (range max: %f)\n", uf, o->maxfp);
712 return 1;
713 }
714 if (uf < o->minfp) {
715 log_err("value out of range: %f"
716 " (range min: %f)\n", uf, o->minfp);
717 return 1;
718 }
83349190
YH
719 }
720
f0fdbcaf 721 flp = td_var(data, o, o->off1);
fd112d34 722 flp[curr].u.f = uf;
83349190 723
ae3fcb5a
JA
724 dprint(FD_PARSE, " out=%f\n", uf);
725
435d195a
VKF
726 /*
727 ** Calculate precision for output by counting
728 ** number of digits after period. Find first
729 ** period in entire remaining list each time
730 */
731 cp2 = strchr(ptr, '.');
732 if (cp2 != NULL) {
eef02441 733 int len = 0;
435d195a
VKF
734
735 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
736 len++;
737
3e260a46 738 if (o->off2) {
f0fdbcaf 739 ilp = td_var(data, o, o->off2);
3e260a46
JA
740 if (len > *ilp)
741 *ilp = len;
742 }
435d195a
VKF
743 }
744
83349190
YH
745 break;
746 }
af52b345
JA
747 case FIO_OPT_STR_STORE: {
748 fio_opt_str_fn *fn = o->cb;
749
f75c69a1
JA
750 if (!strlen(ptr))
751 return 1;
752
7b504edd 753 if (o->off1) {
f0fdbcaf 754 cp = td_var(data, o, o->off1);
184b4098 755 *cp = strdup(ptr);
1c964ce5 756 }
02c6aad5 757
184b4098
SL
758 if (fn)
759 ret = fn(data, ptr);
760 else if (o->posval[0].ival) {
761 posval_sort(o, posval);
762
763 ret = 1;
764 for (i = 0; i < PARSE_MAX_VP; i++) {
765 vp = &posval[i];
ab50817f 766 if (!vp->ival || vp->ival[0] == '\0' || !cp)
184b4098
SL
767 continue;
768 all_skipped = 0;
119cd939 769 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
184b4098
SL
770 char *rest;
771
772 ret = 0;
773 if (vp->cb)
774 fn = vp->cb;
775 rest = strstr(*cp ?: ptr, ":");
776 if (rest) {
777 if (*cp)
778 *rest = '\0';
779 ptr = rest + 1;
780 } else
781 ptr = NULL;
782 break;
783 }
af52b345
JA
784 }
785 }
c44b1ff5 786
184b4098
SL
787 if (!all_skipped) {
788 if (ret && !*cp)
789 show_option_values(o);
790 else if (ret && *cp)
791 ret = 0;
792 else if (fn && ptr)
793 ret = fn(data, ptr);
794 }
c44b1ff5 795
e1f36503 796 break;
af52b345 797 }
e1f36503 798 case FIO_OPT_RANGE: {
b765a372 799 char tmp[128];
e1f36503
JA
800 char *p1, *p2;
801
209e1037 802 tmp[sizeof(tmp) - 1] = '\0';
0bbab0e7 803 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372 804
31d23f47
DE
805 /* Handle bsrange with separate read,write values: */
806 p1 = strchr(tmp, ',');
807 if (p1)
808 *p1 = '\0';
809
b765a372 810 p1 = strchr(tmp, '-');
e1f36503 811 if (!p1) {
0c9baf91
JA
812 p1 = strchr(tmp, ':');
813 if (!p1) {
814 ret = 1;
815 break;
816 }
e1f36503
JA
817 }
818
819 p2 = p1 + 1;
820 *p1 = '\0';
b765a372 821 p1 = tmp;
e1f36503
JA
822
823 ret = 1;
5fff9543
JF
824 if (!check_range_bytes(p1, &ull1, data) &&
825 !check_range_bytes(p2, &ull2, data)) {
e1f36503 826 ret = 0;
5fff9543
JF
827 if (ull1 > ull2) {
828 unsigned long long foo = ull1;
f90eff5a 829
5fff9543
JF
830 ull1 = ull2;
831 ull2 = foo;
f90eff5a
JA
832 }
833
834 if (first) {
5fff9543
JF
835 val_store(ullp, ull1, o->off1, 0, data, o);
836 val_store(ullp, ull2, o->off2, 0, data, o);
17abbe89 837 }
6eaf09d6 838 if (curr == 1) {
7b504edd 839 if (o->off3 && o->off4) {
5fff9543
JF
840 val_store(ullp, ull1, o->off3, 0, data, o);
841 val_store(ullp, ull2, o->off4, 0, data, o);
6eaf09d6
SL
842 }
843 }
844 if (curr == 2) {
7b504edd 845 if (o->off5 && o->off6) {
5fff9543
JF
846 val_store(ullp, ull1, o->off5, 0, data, o);
847 val_store(ullp, ull2, o->off6, 0, data, o);
6eaf09d6
SL
848 }
849 }
850 if (!more) {
851 if (curr < 1) {
7b504edd 852 if (o->off3 && o->off4) {
5fff9543
JF
853 val_store(ullp, ull1, o->off3, 0, data, o);
854 val_store(ullp, ull2, o->off4, 0, data, o);
6eaf09d6
SL
855 }
856 }
857 if (curr < 2) {
7b504edd 858 if (o->off5 && o->off6) {
5fff9543
JF
859 val_store(ullp, ull1, o->off5, 0, data, o);
860 val_store(ullp, ull2, o->off6, 0, data, o);
6eaf09d6
SL
861 }
862 }
e1f36503 863 }
17abbe89
JA
864 }
865
e1f36503
JA
866 break;
867 }
74ba1808
JA
868 case FIO_OPT_BOOL:
869 case FIO_OPT_STR_SET: {
e1f36503
JA
870 fio_opt_int_fn *fn = o->cb;
871
74ba1808
JA
872 if (ptr)
873 ret = check_int(ptr, &il);
874 else if (o->type == FIO_OPT_BOOL)
875 ret = 1;
876 else
877 il = 1;
878
ae3fcb5a
JA
879 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
880
e1f36503
JA
881 if (ret)
882 break;
883
db8e0165 884 if (o->maxval && il > (int) o->maxval) {
5fff9543 885 log_err("max value out of range: %d (%llu max)\n",
5ec10eaa 886 il, o->maxval);
db8e0165
JA
887 return 1;
888 }
889 if (o->minval && il < o->minval) {
28d7c363 890 log_err("min value out of range: %d (%d min)\n",
5ec10eaa 891 il, o->minval);
db8e0165
JA
892 return 1;
893 }
e1f36503 894
76a43db4
JA
895 if (o->neg)
896 il = !il;
897
e1f36503
JA
898 if (fn)
899 ret = fn(data, &il);
900 else {
7b504edd
JA
901 if (first)
902 val_store(ilp, il, o->off1, 0, data, o);
02c6aad5 903 if (!more) {
7b504edd
JA
904 if (o->off2)
905 val_store(ilp, il, o->off2, 0, data, o);
02c6aad5 906 }
e1f36503
JA
907 }
908 break;
909 }
15ca150e 910 case FIO_OPT_DEPRECATED:
d9472271 911 ret = 1;
a8d8f828 912 /* fall through */
c47537ee
JA
913 case FIO_OPT_SOFT_DEPRECATED:
914 log_info("Option %s is deprecated\n", o->name);
15ca150e 915 break;
e1f36503 916 default:
06b0be6e 917 log_err("Bad option type %u\n", o->type);
e1f36503
JA
918 ret = 1;
919 }
cb2c86fd 920
70a4c0c8
JA
921 if (ret)
922 return ret;
923
d447a8c2 924 if (o->verify) {
70a4c0c8 925 ret = o->verify(o, data);
d447a8c2 926 if (ret) {
28d7c363
JA
927 log_err("Correct format for offending option\n");
928 log_err("%20s: %s\n", o->name, o->help);
06b0be6e 929 show_option_help(o, 1);
d447a8c2
JA
930 }
931 }
70a4c0c8 932
e1f36503 933 return ret;
cb2c86fd
JA
934}
935
9109883a
BVA
936static int handle_option(const struct fio_option *o, const char *__ptr,
937 void *data)
f90eff5a 938{
b62bdf2c
JA
939 char *o_ptr, *ptr, *ptr2;
940 int ret, done;
f90eff5a 941
7c8f1a5c
JA
942 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
943
b62bdf2c 944 o_ptr = ptr = NULL;
7c8f1a5c 945 if (__ptr)
b62bdf2c 946 o_ptr = ptr = strdup(__ptr);
a3d741fa 947
f90eff5a 948 /*
b62bdf2c
JA
949 * See if we have another set of parameters, hidden after a comma.
950 * Do this before parsing this round, to check if we should
787f7e95 951 * copy set 1 options to set 2.
f90eff5a 952 */
b62bdf2c
JA
953 done = 0;
954 ret = 1;
955 do {
956 int __ret;
957
958 ptr2 = NULL;
959 if (ptr &&
960 (o->type != FIO_OPT_STR_STORE) &&
83349190 961 (o->type != FIO_OPT_STR) &&
e97163ad 962 (o->type != FIO_OPT_STR_ULL) &&
83349190 963 (o->type != FIO_OPT_FLOAT_LIST)) {
b62bdf2c
JA
964 ptr2 = strchr(ptr, ',');
965 if (ptr2 && *(ptr2 + 1) == '\0')
966 *ptr2 = '\0';
6eaf09d6 967 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
b62bdf2c
JA
968 if (!ptr2)
969 ptr2 = strchr(ptr, ':');
970 if (!ptr2)
971 ptr2 = strchr(ptr, '-');
972 }
83349190
YH
973 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
974 ptr2 = strchr(ptr, ':');
b62bdf2c 975 }
787f7e95 976
b62bdf2c
JA
977 /*
978 * Don't return early if parsing the first option fails - if
979 * we are doing multiple arguments, we can allow the first one
980 * being empty.
981 */
83349190 982 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
b62bdf2c
JA
983 if (ret)
984 ret = __ret;
787f7e95 985
b62bdf2c
JA
986 if (!ptr2)
987 break;
f90eff5a 988
b62bdf2c
JA
989 ptr = ptr2 + 1;
990 done++;
991 } while (1);
787f7e95 992
b62bdf2c
JA
993 if (o_ptr)
994 free(o_ptr);
995 return ret;
f90eff5a
JA
996}
997
75e6bcba
JA
998struct fio_option *find_option(struct fio_option *options, const char *opt)
999{
1000 struct fio_option *o;
1001
1002 for (o = &options[0]; o->name; o++) {
1003 if (!o_match(o, opt))
1004 continue;
1005 if (o->type == FIO_OPT_UNSUPPORTED) {
1006 log_err("Option <%s>: %s\n", o->name, o->help);
1007 continue;
1008 }
1009
1010 return o;
1011 }
1012
1013 return NULL;
1014}
1015
9109883a
BVA
1016const struct fio_option *
1017find_option_c(const struct fio_option *options, const char *opt)
1018{
1019 return find_option((struct fio_option *)options, opt);
1020}
75e6bcba 1021
9109883a
BVA
1022static const struct fio_option *
1023get_option(char *opt, const struct fio_option *options, char **post)
3b8b7135 1024{
9109883a 1025 const struct fio_option *o;
3b8b7135
JA
1026 char *ret;
1027
1028 ret = strchr(opt, '=');
1029 if (ret) {
1030 *post = ret;
1031 *ret = '\0';
de890a1e 1032 ret = opt;
3b8b7135 1033 (*post)++;
43c129b4 1034 strip_blank_end(ret);
9109883a 1035 o = find_option_c(options, ret);
3b8b7135 1036 } else {
9109883a 1037 o = find_option_c(options, opt);
3b8b7135
JA
1038 *post = NULL;
1039 }
1040
1041 return o;
1042}
1043
1044static int opt_cmp(const void *p1, const void *p2)
1045{
9109883a 1046 const struct fio_option *o;
de890a1e 1047 char *s, *foo;
8cdabc1d 1048 int prio1, prio2;
3b8b7135 1049
8cdabc1d 1050 prio1 = prio2 = 0;
3b8b7135 1051
de890a1e
SL
1052 if (*(char **)p1) {
1053 s = strdup(*((char **) p1));
9af4a244 1054 o = get_option(s, __fio_options, &foo);
de890a1e
SL
1055 if (o)
1056 prio1 = o->prio;
1057 free(s);
1058 }
1059 if (*(char **)p2) {
1060 s = strdup(*((char **) p2));
9af4a244 1061 o = get_option(s, __fio_options, &foo);
de890a1e
SL
1062 if (o)
1063 prio2 = o->prio;
1064 free(s);
1065 }
1066
8cdabc1d 1067 return prio2 - prio1;
3b8b7135
JA
1068}
1069
9109883a 1070void sort_options(char **opts, const struct fio_option *options, int num_opts)
3b8b7135 1071{
9af4a244 1072 __fio_options = options;
3b8b7135 1073 qsort(opts, num_opts, sizeof(char *), opt_cmp);
9af4a244 1074 __fio_options = NULL;
3b8b7135
JA
1075}
1076
9109883a
BVA
1077static void add_to_dump_list(const struct fio_option *o,
1078 struct flist_head *dump_list, const char *post)
d8b4f395
JA
1079{
1080 struct print_option *p;
1081
1082 if (!dump_list)
1083 return;
1084
1085 p = malloc(sizeof(*p));
1086 p->name = strdup(o->name);
1087 if (post)
1088 p->value = strdup(post);
1089 else
1090 p->value = NULL;
1091
1092 flist_add_tail(&p->list, dump_list);
1093}
1094
b4692828 1095int parse_cmd_option(const char *opt, const char *val,
9109883a 1096 const struct fio_option *options, void *data,
d8b4f395 1097 struct flist_head *dump_list)
b4692828 1098{
9109883a 1099 const struct fio_option *o;
b4692828 1100
9109883a 1101 o = find_option_c(options, opt);
b4692828 1102 if (!o) {
06b0be6e 1103 log_err("Bad option <%s>\n", opt);
b4692828
JA
1104 return 1;
1105 }
1106
d8b4f395
JA
1107 if (handle_option(o, val, data)) {
1108 log_err("fio: failed parsing %s=%s\n", opt, val);
1109 return 1;
1110 }
b1508cf9 1111
d8b4f395
JA
1112 add_to_dump_list(o, dump_list, val);
1113 return 0;
b4692828
JA
1114}
1115
9109883a
BVA
1116int parse_option(char *opt, const char *input, const struct fio_option *options,
1117 const struct fio_option **o, void *data,
c2292325 1118 struct flist_head *dump_list)
cb2c86fd 1119{
d0c814ec 1120 char *post;
e1f36503 1121
d0c814ec
SL
1122 if (!opt) {
1123 log_err("fio: failed parsing %s\n", input);
de890a1e 1124 *o = NULL;
38789b58 1125 return 1;
d0c814ec 1126 }
e1f36503 1127
de890a1e
SL
1128 *o = get_option(opt, options, &post);
1129 if (!*o) {
1130 if (post) {
1131 int len = strlen(opt);
1132 if (opt + len + 1 != post)
1133 memmove(opt + len + 1, post, strlen(post));
1134 opt[len] = '=';
1135 }
e1f36503
JA
1136 return 1;
1137 }
1138
292cc475
JA
1139 if (handle_option(*o, post, data)) {
1140 log_err("fio: failed parsing %s\n", input);
1141 return 1;
1142 }
b1508cf9 1143
d8b4f395 1144 add_to_dump_list(*o, dump_list, post);
292cc475 1145 return 0;
e1f36503 1146}
fd28ca49 1147
0e9f7fac
JA
1148/*
1149 * Option match, levenshtein distance. Handy for not quite remembering what
1150 * the option name is.
1151 */
a893c261 1152int string_distance(const char *s1, const char *s2)
0e9f7fac
JA
1153{
1154 unsigned int s1_len = strlen(s1);
1155 unsigned int s2_len = strlen(s2);
1156 unsigned int *p, *q, *r;
1157 unsigned int i, j;
1158
1159 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1160 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1161
1162 p[0] = 0;
1163 for (i = 1; i <= s2_len; i++)
1164 p[i] = p[i - 1] + 1;
1165
1166 for (i = 1; i <= s1_len; i++) {
1167 q[0] = p[0] + 1;
1168 for (j = 1; j <= s2_len; j++) {
1169 unsigned int sub = p[j - 1];
8a1db9a1 1170 unsigned int pmin;
0e9f7fac
JA
1171
1172 if (s1[i - 1] != s2[j - 1])
1173 sub++;
1174
8a1db9a1
JA
1175 pmin = min(q[j - 1] + 1, sub);
1176 q[j] = min(p[j] + 1, pmin);
0e9f7fac
JA
1177 }
1178 r = p;
1179 p = q;
1180 q = r;
1181 }
1182
1183 i = p[s2_len];
1184 free(p);
1185 free(q);
1186 return i;
1187}
1188
3701636d
JA
1189/*
1190 * Make a guess of whether the distance from 's1' is significant enough
1191 * to warrant printing the guess. We set this to a 1/2 match.
1192 */
1193int string_distance_ok(const char *opt, int distance)
1194{
1195 size_t len;
1196
1197 len = strlen(opt);
1198 len = (len + 1) / 2;
1199 return distance <= len;
1200}
1201
9109883a
BVA
1202static const struct fio_option *find_child(const struct fio_option *options,
1203 const struct fio_option *o)
afdf9352 1204{
9109883a 1205 const struct fio_option *__o;
afdf9352 1206
fdf28744
JA
1207 for (__o = options + 1; __o->name; __o++)
1208 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
1209 return __o;
1210
1211 return NULL;
1212}
1213
9109883a
BVA
1214static void __print_option(const struct fio_option *o,
1215 const struct fio_option *org,
323d9113 1216 int level)
afdf9352
JA
1217{
1218 char name[256], *p;
323d9113 1219 int depth;
afdf9352
JA
1220
1221 if (!o)
1222 return;
5ec10eaa 1223
afdf9352 1224 p = name;
323d9113
JA
1225 depth = level;
1226 while (depth--)
1227 p += sprintf(p, "%s", " ");
afdf9352
JA
1228
1229 sprintf(p, "%s", o->name);
1230
28d7c363 1231 log_info("%-24s: %s\n", name, o->help);
323d9113
JA
1232}
1233
9109883a 1234static void print_option(const struct fio_option *o)
323d9113 1235{
9109883a
BVA
1236 const struct fio_option *parent;
1237 const struct fio_option *__o;
323d9113
JA
1238 unsigned int printed;
1239 unsigned int level;
1240
1241 __print_option(o, NULL, 0);
1242 parent = o;
1243 level = 0;
1244 do {
1245 level++;
1246 printed = 0;
1247
1248 while ((__o = find_child(o, parent)) != NULL) {
1249 __print_option(__o, o, level);
1250 o = __o;
1251 printed++;
1252 }
1253
1254 parent = o;
1255 } while (printed);
afdf9352
JA
1256}
1257
9109883a 1258int show_cmd_help(const struct fio_option *options, const char *name)
0e9f7fac 1259{
9109883a 1260 const struct fio_option *o, *closest;
d091d099 1261 unsigned int best_dist = -1U;
29fc6afe 1262 int found = 0;
320beefe
JA
1263 int show_all = 0;
1264
1265 if (!name || !strcmp(name, "all"))
1266 show_all = 1;
fd28ca49 1267
0e9f7fac
JA
1268 closest = NULL;
1269 best_dist = -1;
4945ba12 1270 for (o = &options[0]; o->name; o++) {
320beefe
JA
1271 int match = 0;
1272
c47537ee
JA
1273 if (o->type == FIO_OPT_DEPRECATED ||
1274 o->type == FIO_OPT_SOFT_DEPRECATED)
15ca150e 1275 continue;
07b3232d
JA
1276 if (!exec_profile && o->prof_name)
1277 continue;
4ac23d27
JA
1278 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1279 continue;
15ca150e 1280
0e9f7fac 1281 if (name) {
7f9348f8
JA
1282 if (!strcmp(name, o->name) ||
1283 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
1284 match = 1;
1285 else {
1286 unsigned int dist;
1287
1288 dist = string_distance(name, o->name);
1289 if (dist < best_dist) {
1290 best_dist = dist;
1291 closest = o;
1292 }
1293 }
1294 }
fd28ca49
JA
1295
1296 if (show_all || match) {
29fc6afe 1297 found = 1;
c167dedc 1298 if (match)
28d7c363 1299 log_info("%20s: %s\n", o->name, o->help);
c167dedc 1300 if (show_all) {
afdf9352 1301 if (!o->parent)
323d9113 1302 print_option(o);
4945ba12 1303 continue;
c167dedc 1304 }
fd28ca49
JA
1305 }
1306
70df2f19
JA
1307 if (!match)
1308 continue;
1309
06b0be6e 1310 show_option_help(o, 0);
fd28ca49
JA
1311 }
1312
29fc6afe
JA
1313 if (found)
1314 return 0;
fd28ca49 1315
28d7c363 1316 log_err("No such command: %s", name);
d091d099
JA
1317
1318 /*
1319 * Only print an appropriately close option, one where the edit
1320 * distance isn't too big. Otherwise we get crazy matches.
1321 */
1322 if (closest && best_dist < 3) {
28d7c363
JA
1323 log_info(" - showing closest match\n");
1324 log_info("%20s: %s\n", closest->name, closest->help);
06b0be6e 1325 show_option_help(closest, 0);
0e9f7fac 1326 } else
28d7c363 1327 log_info("\n");
0e9f7fac 1328
29fc6afe 1329 return 1;
fd28ca49 1330}
ee738499 1331
13335ddb
JA
1332/*
1333 * Handle parsing of default parameters.
1334 */
9109883a 1335void fill_default_options(void *data, const struct fio_option *options)
ee738499 1336{
9109883a 1337 const struct fio_option *o;
ee738499 1338
a3d741fa
JA
1339 dprint(FD_PARSE, "filling default options\n");
1340
4945ba12 1341 for (o = &options[0]; o->name; o++)
ee738499
JA
1342 if (o->def)
1343 handle_option(o, o->def, data);
ee738499 1344}
13335ddb 1345
8aa89d70 1346static void option_init(struct fio_option *o)
9f988e2e 1347{
c47537ee
JA
1348 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED ||
1349 o->type == FIO_OPT_SOFT_DEPRECATED)
9f988e2e 1350 return;
bbcacb72
JA
1351 if (o->name && !o->lname)
1352 log_err("Option %s: missing long option name\n", o->name);
9f988e2e
JA
1353 if (o->type == FIO_OPT_BOOL) {
1354 o->minval = 0;
1355 o->maxval = 1;
1356 }
d4fc2f04
JA
1357 if (o->type == FIO_OPT_INT) {
1358 if (!o->maxval)
1359 o->maxval = UINT_MAX;
1360 }
5fff9543
JF
1361 if (o->type == FIO_OPT_ULL) {
1362 if (!o->maxval)
1363 o->maxval = ULLONG_MAX;
1364 }
c8931876 1365 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
06b0be6e 1366 log_err("Option %s: string set option with"
9f988e2e
JA
1367 " default will always be true\n", o->name);
1368 }
7b504edd 1369 if (!o->cb && !o->off1)
06b0be6e 1370 log_err("Option %s: neither cb nor offset given\n", o->name);
22754746 1371 if (!o->category) {
cca5b5bc 1372 log_info("Option %s: no category defined. Setting to misc\n", o->name);
22754746 1373 o->category = FIO_OPT_C_GENERAL;
ffd7821f 1374 o->group = FIO_OPT_G_INVALID;
22754746 1375 }
9f988e2e
JA
1376}
1377
13335ddb
JA
1378/*
1379 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 1380 * values and whether both callback and offsets are given.
13335ddb
JA
1381 */
1382void options_init(struct fio_option *options)
1383{
4945ba12 1384 struct fio_option *o;
13335ddb 1385
a3d741fa
JA
1386 dprint(FD_PARSE, "init options\n");
1387
90265353 1388 for (o = &options[0]; o->name; o++) {
9f988e2e 1389 option_init(o);
90265353
JA
1390 if (o->inverse)
1391 o->inv_opt = find_option(options, o->inverse);
1392 }
13335ddb 1393}
7e356b2d 1394
9109883a 1395void options_mem_dupe(const struct fio_option *options, void *data)
53b5693d 1396{
9109883a 1397 const struct fio_option *o;
53b5693d
TK
1398 char **ptr;
1399
1400 dprint(FD_PARSE, "dup options\n");
1401
1402 for (o = &options[0]; o->name; o++) {
1403 if (o->type != FIO_OPT_STR_STORE)
1404 continue;
1405
1406 ptr = td_var(data, o, o->off1);
1407 if (*ptr)
1408 *ptr = strdup(*ptr);
1409 }
1410}
1411
9109883a 1412void options_free(const struct fio_option *options, void *data)
7e356b2d 1413{
9109883a 1414 const struct fio_option *o;
7e356b2d
JA
1415 char **ptr;
1416
1417 dprint(FD_PARSE, "free options\n");
1418
1419 for (o = &options[0]; o->name; o++) {
43f466e6 1420 if (o->type != FIO_OPT_STR_STORE || !o->off1 || o->no_free)
7e356b2d
JA
1421 continue;
1422
f0fdbcaf 1423 ptr = td_var(data, o, o->off1);
7e356b2d
JA
1424 if (*ptr) {
1425 free(*ptr);
1426 *ptr = NULL;
1427 }
1428 }
1429}