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