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