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 SC |
271 | extern int evaluate_arithmetic_expression(const char *buffer, long long *ival, |
272 | double *dval); | |
273 | ||
83349190 YH |
274 | /* |
275 | * Convert string into a floating number. Return 1 for success and 0 otherwise. | |
276 | */ | |
e25839d4 | 277 | int str_to_float(const char *str, double *val) |
83349190 | 278 | { |
b470a02c SC |
279 | #ifdef CONFIG_ARITHMETIC |
280 | int rc; | |
281 | long long ival; | |
282 | double dval; | |
283 | ||
284 | if (str[0] == '(') { | |
285 | rc = evaluate_arithmetic_expression(str, &ival, &dval); | |
286 | if (!rc) { | |
287 | *val = dval; | |
288 | return 1; | |
289 | } | |
290 | } | |
291 | #endif | |
292 | return 1 == sscanf(str, "%lf", val); | |
83349190 YH |
293 | } |
294 | ||
cb2c86fd | 295 | /* |
e1f36503 | 296 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 297 | */ |
0de5b26f JA |
298 | int str_to_decimal(const char *str, long long *val, int kilo, void *data, |
299 | int is_seconds) | |
cb2c86fd | 300 | { |
b347f9da | 301 | int len, base; |
b470a02c SC |
302 | int rc = 1; |
303 | #ifdef CONFIG_ARITHMETIC | |
304 | long long ival; | |
305 | double dval; | |
306 | #endif | |
cb2c86fd | 307 | |
cb2c86fd | 308 | len = strlen(str); |
f90eff5a JA |
309 | if (!len) |
310 | return 1; | |
cb2c86fd | 311 | |
b470a02c SC |
312 | #ifdef CONFIG_ARITHMETIC |
313 | if (str[0] == '(') | |
314 | rc = evaluate_arithmetic_expression(str, &ival, &dval); | |
315 | if (str[0] == '(' && !rc) | |
316 | *val = ival; | |
317 | #endif | |
b347f9da | 318 | |
b470a02c SC |
319 | if (rc == 1) { |
320 | if (strstr(str, "0x") || strstr(str, "0X")) | |
321 | base = 16; | |
322 | else | |
323 | base = 10; | |
324 | ||
325 | *val = strtoll(str, NULL, base); | |
326 | if (*val == LONG_MAX && errno == ERANGE) | |
327 | return 1; | |
328 | } | |
cb2c86fd | 329 | |
7bb59102 JA |
330 | if (kilo) { |
331 | unsigned long long mult; | |
332 | int perc = 0; | |
333 | ||
6925dd35 | 334 | mult = get_mult_bytes(str, len, data, &perc); |
7bb59102 JA |
335 | if (perc) |
336 | *val = -1ULL - *val; | |
337 | else | |
338 | *val *= mult; | |
339 | } else | |
0de5b26f | 340 | *val *= get_mult_time(str, len, is_seconds); |
787f7e95 | 341 | |
cb2c86fd JA |
342 | return 0; |
343 | } | |
344 | ||
9af4a244 | 345 | int check_str_bytes(const char *p, long long *val, void *data) |
cb2c86fd | 346 | { |
0de5b26f | 347 | return str_to_decimal(p, val, 1, data, 0); |
cb2c86fd JA |
348 | } |
349 | ||
0de5b26f | 350 | int check_str_time(const char *p, long long *val, int is_seconds) |
cb2c86fd | 351 | { |
0de5b26f | 352 | return str_to_decimal(p, val, 0, NULL, is_seconds); |
cb2c86fd JA |
353 | } |
354 | ||
355 | void strip_blank_front(char **p) | |
356 | { | |
357 | char *s = *p; | |
358 | ||
4c8e9640 JA |
359 | if (!strlen(s)) |
360 | return; | |
76cd9378 | 361 | while (isspace((int) *s)) |
cb2c86fd | 362 | s++; |
4d651dad JA |
363 | |
364 | *p = s; | |
cb2c86fd JA |
365 | } |
366 | ||
367 | void strip_blank_end(char *p) | |
368 | { | |
853ee7fc | 369 | char *start = p, *s; |
523bfadb | 370 | |
4c8e9640 JA |
371 | if (!strlen(p)) |
372 | return; | |
373 | ||
523bfadb JA |
374 | s = strchr(p, ';'); |
375 | if (s) | |
376 | *s = '\0'; | |
377 | s = strchr(p, '#'); | |
378 | if (s) | |
379 | *s = '\0'; | |
380 | if (s) | |
381 | p = s; | |
382 | ||
7f7e6e59 | 383 | s = p + strlen(p); |
76cd9378 | 384 | while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start)) |
cb2c86fd JA |
385 | s--; |
386 | ||
387 | *(s + 1) = '\0'; | |
388 | } | |
389 | ||
d6978a32 | 390 | static int check_range_bytes(const char *str, long *val, void *data) |
cb2c86fd | 391 | { |
57fc29fa | 392 | long long __val; |
cb2c86fd | 393 | |
0de5b26f | 394 | if (!str_to_decimal(str, &__val, 1, data, 0)) { |
57fc29fa | 395 | *val = __val; |
cb2c86fd JA |
396 | return 0; |
397 | } | |
398 | ||
cb2c86fd JA |
399 | return 1; |
400 | } | |
401 | ||
63f29372 | 402 | static int check_int(const char *p, int *val) |
cb2c86fd | 403 | { |
787f7e95 JA |
404 | if (!strlen(p)) |
405 | return 1; | |
d78ee463 | 406 | if (strstr(p, "0x") || strstr(p, "0X")) { |
a61bdfd8 JA |
407 | if (sscanf(p, "%x", val) == 1) |
408 | return 0; | |
409 | } else { | |
410 | if (sscanf(p, "%u", val) == 1) | |
411 | return 0; | |
412 | } | |
cb2c86fd | 413 | |
e1f36503 JA |
414 | return 1; |
415 | } | |
cb2c86fd | 416 | |
e6f735f0 | 417 | static size_t opt_len(const char *str) |
808def70 JA |
418 | { |
419 | char *postfix; | |
420 | ||
421 | postfix = strchr(str, ':'); | |
422 | if (!postfix) | |
423 | return strlen(str); | |
424 | ||
425 | return (int)(postfix - str); | |
426 | } | |
427 | ||
119cd939 JA |
428 | static int str_match_len(const struct value_pair *vp, const char *str) |
429 | { | |
430 | return max(strlen(vp->ival), opt_len(str)); | |
431 | } | |
432 | ||
f0fdbcaf JA |
433 | #define val_store(ptr, val, off, or, data, o) \ |
434 | do { \ | |
435 | ptr = td_var((data), (o), (off)); \ | |
436 | if ((or)) \ | |
437 | *ptr |= (val); \ | |
438 | else \ | |
439 | *ptr = (val); \ | |
17abbe89 JA |
440 | } while (0) |
441 | ||
f90eff5a | 442 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
83349190 | 443 | int first, int more, int curr) |
cb2c86fd | 444 | { |
2fdbefdd | 445 | int il=0, *ilp; |
fd112d34 | 446 | fio_fp64_t *flp; |
63f29372 JA |
447 | long long ull, *ullp; |
448 | long ul1, ul2; | |
83349190 | 449 | double uf; |
bfe1d592 | 450 | char **cp = NULL; |
e42b01eb | 451 | int ret = 0, is_time = 0; |
c44b1ff5 JA |
452 | const struct value_pair *vp; |
453 | struct value_pair posval[PARSE_MAX_VP]; | |
454 | int i, all_skipped = 1; | |
cb2c86fd | 455 | |
a3d741fa JA |
456 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
457 | o->type, ptr); | |
458 | ||
e3cedca7 | 459 | if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) { |
28d7c363 | 460 | log_err("Option %s requires an argument\n", o->name); |
08e26e35 JA |
461 | return 1; |
462 | } | |
463 | ||
e1f36503 | 464 | switch (o->type) { |
5f6ddf1e JA |
465 | case FIO_OPT_STR: |
466 | case FIO_OPT_STR_MULTI: { | |
e1f36503 | 467 | fio_opt_str_fn *fn = o->cb; |
b1ec1da6 | 468 | |
f085737f JA |
469 | posval_sort(o, posval); |
470 | ||
5f6ddf1e | 471 | ret = 1; |
b1ec1da6 | 472 | for (i = 0; i < PARSE_MAX_VP; i++) { |
f085737f | 473 | vp = &posval[i]; |
b1ec1da6 | 474 | if (!vp->ival || vp->ival[0] == '\0') |
a3073f4a | 475 | continue; |
ae2ddba4 | 476 | all_skipped = 0; |
119cd939 | 477 | if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) { |
b1ec1da6 | 478 | ret = 0; |
7b504edd | 479 | if (o->off1) |
ebadc0ce | 480 | val_store(ilp, vp->oval, o->off1, vp->orval, data, o); |
5f6ddf1e | 481 | continue; |
b1ec1da6 JA |
482 | } |
483 | } | |
cb2c86fd | 484 | |
ae2ddba4 | 485 | if (ret && !all_skipped) |
b1ec1da6 JA |
486 | show_option_values(o); |
487 | else if (fn) | |
488 | ret = fn(data, ptr); | |
e1f36503 JA |
489 | break; |
490 | } | |
e42b01eb JA |
491 | case FIO_OPT_STR_VAL_TIME: |
492 | is_time = 1; | |
1a1137d9 | 493 | case FIO_OPT_INT: |
e42b01eb JA |
494 | case FIO_OPT_STR_VAL: { |
495 | fio_opt_str_val_fn *fn = o->cb; | |
6eaf09d6 SL |
496 | char tmp[128], *p; |
497 | ||
498 | strncpy(tmp, ptr, sizeof(tmp) - 1); | |
499 | p = strchr(tmp, ','); | |
500 | if (p) | |
501 | *p = '\0'; | |
e42b01eb JA |
502 | |
503 | if (is_time) | |
0de5b26f | 504 | ret = check_str_time(tmp, &ull, o->is_seconds); |
e42b01eb | 505 | else |
6eaf09d6 | 506 | ret = check_str_bytes(tmp, &ull, data); |
e1f36503 | 507 | |
ae3fcb5a JA |
508 | dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull); |
509 | ||
e1f36503 JA |
510 | if (ret) |
511 | break; | |
512 | ||
db8e0165 | 513 | if (o->maxval && ull > o->maxval) { |
d4fc2f04 JA |
514 | log_err("max value out of range: %llu" |
515 | " (%u max)\n", ull, o->maxval); | |
db8e0165 JA |
516 | return 1; |
517 | } | |
518 | if (o->minval && ull < o->minval) { | |
d4fc2f04 JA |
519 | log_err("min value out of range: %llu" |
520 | " (%u min)\n", ull, o->minval); | |
db8e0165 JA |
521 | return 1; |
522 | } | |
d926d535 JA |
523 | if (o->posval[0].ival) { |
524 | posval_sort(o, posval); | |
525 | ||
526 | ret = 1; | |
527 | for (i = 0; i < PARSE_MAX_VP; i++) { | |
528 | vp = &posval[i]; | |
529 | if (!vp->ival || vp->ival[0] == '\0') | |
530 | continue; | |
531 | if (vp->oval == ull) { | |
532 | ret = 0; | |
533 | break; | |
534 | } | |
535 | } | |
536 | if (ret) { | |
128e4caf JA |
537 | log_err("fio: value %llu not allowed:\n", ull); |
538 | show_option_values(o); | |
d926d535 JA |
539 | return 1; |
540 | } | |
541 | } | |
e1f36503 JA |
542 | |
543 | if (fn) | |
544 | ret = fn(data, &ull); | |
545 | else { | |
e01b22b8 | 546 | if (o->type == FIO_OPT_INT) { |
7b504edd JA |
547 | if (first) |
548 | val_store(ilp, ull, o->off1, 0, data, o); | |
6eaf09d6 | 549 | if (curr == 1) { |
7b504edd JA |
550 | if (o->off2) |
551 | val_store(ilp, ull, o->off2, 0, data, o); | |
02c6aad5 | 552 | } |
6eaf09d6 | 553 | if (curr == 2) { |
7b504edd JA |
554 | if (o->off3) |
555 | val_store(ilp, ull, o->off3, 0, data, o); | |
6eaf09d6 SL |
556 | } |
557 | if (!more) { | |
558 | if (curr < 1) { | |
7b504edd JA |
559 | if (o->off2) |
560 | val_store(ilp, ull, o->off2, 0, data, o); | |
6eaf09d6 SL |
561 | } |
562 | if (curr < 2) { | |
7b504edd JA |
563 | if (o->off3) |
564 | val_store(ilp, ull, o->off3, 0, data, o); | |
6eaf09d6 SL |
565 | } |
566 | } | |
75e6f36f | 567 | } else { |
7b504edd JA |
568 | if (first) |
569 | val_store(ullp, ull, o->off1, 0, data, o); | |
02c6aad5 | 570 | if (!more) { |
7b504edd JA |
571 | if (o->off2) |
572 | val_store(ullp, ull, o->off2, 0, data, o); | |
02c6aad5 | 573 | } |
75e6f36f | 574 | } |
e1f36503 JA |
575 | } |
576 | break; | |
577 | } | |
83349190 | 578 | case FIO_OPT_FLOAT_LIST: { |
eef02441 JA |
579 | char *cp2; |
580 | ||
435d195a VKF |
581 | if (first) { |
582 | /* | |
583 | ** Initialize precision to 0 and zero out list | |
584 | ** in case specified list is shorter than default | |
585 | */ | |
3e260a46 JA |
586 | if (o->off2) { |
587 | ul2 = 0; | |
f0fdbcaf | 588 | ilp = td_var(data, o, o->off2); |
3e260a46 JA |
589 | *ilp = ul2; |
590 | } | |
435d195a | 591 | |
f0fdbcaf | 592 | flp = td_var(data, o, o->off1); |
435d195a VKF |
593 | for(i = 0; i < o->maxlen; i++) |
594 | flp[i].u.f = 0.0; | |
595 | } | |
83349190 | 596 | if (curr >= o->maxlen) { |
28d7c363 | 597 | log_err("the list exceeding max length %d\n", |
83349190 YH |
598 | o->maxlen); |
599 | return 1; | |
600 | } | |
3c3ed070 | 601 | if (!str_to_float(ptr, &uf)) { |
28d7c363 | 602 | log_err("not a floating point value: %s\n", ptr); |
83349190 YH |
603 | return 1; |
604 | } | |
26650695 | 605 | if (uf > o->maxfp) { |
28d7c363 | 606 | log_err("value out of range: %f" |
83349190 YH |
607 | " (range max: %f)\n", uf, o->maxfp); |
608 | return 1; | |
609 | } | |
26650695 | 610 | if (uf < o->minfp) { |
28d7c363 | 611 | log_err("value out of range: %f" |
83349190 YH |
612 | " (range min: %f)\n", uf, o->minfp); |
613 | return 1; | |
614 | } | |
615 | ||
f0fdbcaf | 616 | flp = td_var(data, o, o->off1); |
fd112d34 | 617 | flp[curr].u.f = uf; |
83349190 | 618 | |
ae3fcb5a JA |
619 | dprint(FD_PARSE, " out=%f\n", uf); |
620 | ||
435d195a VKF |
621 | /* |
622 | ** Calculate precision for output by counting | |
623 | ** number of digits after period. Find first | |
624 | ** period in entire remaining list each time | |
625 | */ | |
626 | cp2 = strchr(ptr, '.'); | |
627 | if (cp2 != NULL) { | |
eef02441 | 628 | int len = 0; |
435d195a VKF |
629 | |
630 | while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9') | |
631 | len++; | |
632 | ||
3e260a46 | 633 | if (o->off2) { |
f0fdbcaf | 634 | ilp = td_var(data, o, o->off2); |
3e260a46 JA |
635 | if (len > *ilp) |
636 | *ilp = len; | |
637 | } | |
435d195a VKF |
638 | } |
639 | ||
83349190 YH |
640 | break; |
641 | } | |
af52b345 JA |
642 | case FIO_OPT_STR_STORE: { |
643 | fio_opt_str_fn *fn = o->cb; | |
644 | ||
f75c69a1 JA |
645 | if (!strlen(ptr)) |
646 | return 1; | |
647 | ||
7b504edd | 648 | if (o->off1) { |
f0fdbcaf | 649 | cp = td_var(data, o, o->off1); |
184b4098 | 650 | *cp = strdup(ptr); |
1c964ce5 | 651 | } |
02c6aad5 | 652 | |
184b4098 SL |
653 | if (fn) |
654 | ret = fn(data, ptr); | |
655 | else if (o->posval[0].ival) { | |
656 | posval_sort(o, posval); | |
657 | ||
658 | ret = 1; | |
659 | for (i = 0; i < PARSE_MAX_VP; i++) { | |
660 | vp = &posval[i]; | |
ab50817f | 661 | if (!vp->ival || vp->ival[0] == '\0' || !cp) |
184b4098 SL |
662 | continue; |
663 | all_skipped = 0; | |
119cd939 | 664 | if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) { |
184b4098 SL |
665 | char *rest; |
666 | ||
667 | ret = 0; | |
668 | if (vp->cb) | |
669 | fn = vp->cb; | |
670 | rest = strstr(*cp ?: ptr, ":"); | |
671 | if (rest) { | |
672 | if (*cp) | |
673 | *rest = '\0'; | |
674 | ptr = rest + 1; | |
675 | } else | |
676 | ptr = NULL; | |
677 | break; | |
678 | } | |
af52b345 JA |
679 | } |
680 | } | |
c44b1ff5 | 681 | |
184b4098 SL |
682 | if (!all_skipped) { |
683 | if (ret && !*cp) | |
684 | show_option_values(o); | |
685 | else if (ret && *cp) | |
686 | ret = 0; | |
687 | else if (fn && ptr) | |
688 | ret = fn(data, ptr); | |
689 | } | |
c44b1ff5 | 690 | |
e1f36503 | 691 | break; |
af52b345 | 692 | } |
e1f36503 | 693 | case FIO_OPT_RANGE: { |
b765a372 | 694 | char tmp[128]; |
e1f36503 JA |
695 | char *p1, *p2; |
696 | ||
0bbab0e7 | 697 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 | 698 | |
31d23f47 DE |
699 | /* Handle bsrange with separate read,write values: */ |
700 | p1 = strchr(tmp, ','); | |
701 | if (p1) | |
702 | *p1 = '\0'; | |
703 | ||
b765a372 | 704 | p1 = strchr(tmp, '-'); |
e1f36503 | 705 | if (!p1) { |
0c9baf91 JA |
706 | p1 = strchr(tmp, ':'); |
707 | if (!p1) { | |
708 | ret = 1; | |
709 | break; | |
710 | } | |
e1f36503 JA |
711 | } |
712 | ||
713 | p2 = p1 + 1; | |
714 | *p1 = '\0'; | |
b765a372 | 715 | p1 = tmp; |
e1f36503 JA |
716 | |
717 | ret = 1; | |
d6978a32 JA |
718 | if (!check_range_bytes(p1, &ul1, data) && |
719 | !check_range_bytes(p2, &ul2, data)) { | |
e1f36503 | 720 | ret = 0; |
e1f36503 | 721 | if (ul1 > ul2) { |
f90eff5a JA |
722 | unsigned long foo = ul1; |
723 | ||
724 | ul1 = ul2; | |
725 | ul2 = foo; | |
726 | } | |
727 | ||
728 | if (first) { | |
7b504edd JA |
729 | val_store(ilp, ul1, o->off1, 0, data, o); |
730 | val_store(ilp, ul2, o->off2, 0, data, o); | |
17abbe89 | 731 | } |
6eaf09d6 | 732 | if (curr == 1) { |
7b504edd JA |
733 | if (o->off3 && o->off4) { |
734 | val_store(ilp, ul1, o->off3, 0, data, o); | |
735 | val_store(ilp, ul2, o->off4, 0, data, o); | |
6eaf09d6 SL |
736 | } |
737 | } | |
738 | if (curr == 2) { | |
7b504edd JA |
739 | if (o->off5 && o->off6) { |
740 | val_store(ilp, ul1, o->off5, 0, data, o); | |
741 | val_store(ilp, ul2, o->off6, 0, data, o); | |
6eaf09d6 SL |
742 | } |
743 | } | |
744 | if (!more) { | |
745 | if (curr < 1) { | |
7b504edd JA |
746 | if (o->off3 && o->off4) { |
747 | val_store(ilp, ul1, o->off3, 0, data, o); | |
748 | val_store(ilp, ul2, o->off4, 0, data, o); | |
6eaf09d6 SL |
749 | } |
750 | } | |
751 | if (curr < 2) { | |
7b504edd JA |
752 | if (o->off5 && o->off6) { |
753 | val_store(ilp, ul1, o->off5, 0, data, o); | |
754 | val_store(ilp, ul2, o->off6, 0, data, o); | |
6eaf09d6 SL |
755 | } |
756 | } | |
e1f36503 | 757 | } |
17abbe89 JA |
758 | } |
759 | ||
e1f36503 JA |
760 | break; |
761 | } | |
74ba1808 JA |
762 | case FIO_OPT_BOOL: |
763 | case FIO_OPT_STR_SET: { | |
e1f36503 JA |
764 | fio_opt_int_fn *fn = o->cb; |
765 | ||
74ba1808 JA |
766 | if (ptr) |
767 | ret = check_int(ptr, &il); | |
768 | else if (o->type == FIO_OPT_BOOL) | |
769 | ret = 1; | |
770 | else | |
771 | il = 1; | |
772 | ||
ae3fcb5a JA |
773 | dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il); |
774 | ||
e1f36503 JA |
775 | if (ret) |
776 | break; | |
777 | ||
db8e0165 | 778 | if (o->maxval && il > (int) o->maxval) { |
28d7c363 | 779 | log_err("max value out of range: %d (%d max)\n", |
5ec10eaa | 780 | il, o->maxval); |
db8e0165 JA |
781 | return 1; |
782 | } | |
783 | if (o->minval && il < o->minval) { | |
28d7c363 | 784 | log_err("min value out of range: %d (%d min)\n", |
5ec10eaa | 785 | il, o->minval); |
db8e0165 JA |
786 | return 1; |
787 | } | |
e1f36503 | 788 | |
76a43db4 JA |
789 | if (o->neg) |
790 | il = !il; | |
791 | ||
e1f36503 JA |
792 | if (fn) |
793 | ret = fn(data, &il); | |
794 | else { | |
7b504edd JA |
795 | if (first) |
796 | val_store(ilp, il, o->off1, 0, data, o); | |
02c6aad5 | 797 | if (!more) { |
7b504edd JA |
798 | if (o->off2) |
799 | val_store(ilp, il, o->off2, 0, data, o); | |
02c6aad5 | 800 | } |
e1f36503 JA |
801 | } |
802 | break; | |
803 | } | |
15ca150e | 804 | case FIO_OPT_DEPRECATED: |
06b0be6e | 805 | log_info("Option %s is deprecated\n", o->name); |
d9472271 | 806 | ret = 1; |
15ca150e | 807 | break; |
e1f36503 | 808 | default: |
06b0be6e | 809 | log_err("Bad option type %u\n", o->type); |
e1f36503 JA |
810 | ret = 1; |
811 | } | |
cb2c86fd | 812 | |
70a4c0c8 JA |
813 | if (ret) |
814 | return ret; | |
815 | ||
d447a8c2 | 816 | if (o->verify) { |
70a4c0c8 | 817 | ret = o->verify(o, data); |
d447a8c2 | 818 | if (ret) { |
28d7c363 JA |
819 | log_err("Correct format for offending option\n"); |
820 | log_err("%20s: %s\n", o->name, o->help); | |
06b0be6e | 821 | show_option_help(o, 1); |
d447a8c2 JA |
822 | } |
823 | } | |
70a4c0c8 | 824 | |
e1f36503 | 825 | return ret; |
cb2c86fd JA |
826 | } |
827 | ||
7c8f1a5c | 828 | static int handle_option(struct fio_option *o, const char *__ptr, void *data) |
f90eff5a | 829 | { |
b62bdf2c JA |
830 | char *o_ptr, *ptr, *ptr2; |
831 | int ret, done; | |
f90eff5a | 832 | |
7c8f1a5c JA |
833 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
834 | ||
b62bdf2c | 835 | o_ptr = ptr = NULL; |
7c8f1a5c | 836 | if (__ptr) |
b62bdf2c | 837 | o_ptr = ptr = strdup(__ptr); |
a3d741fa | 838 | |
f90eff5a | 839 | /* |
b62bdf2c JA |
840 | * See if we have another set of parameters, hidden after a comma. |
841 | * Do this before parsing this round, to check if we should | |
787f7e95 | 842 | * copy set 1 options to set 2. |
f90eff5a | 843 | */ |
b62bdf2c JA |
844 | done = 0; |
845 | ret = 1; | |
846 | do { | |
847 | int __ret; | |
848 | ||
849 | ptr2 = NULL; | |
850 | if (ptr && | |
851 | (o->type != FIO_OPT_STR_STORE) && | |
83349190 YH |
852 | (o->type != FIO_OPT_STR) && |
853 | (o->type != FIO_OPT_FLOAT_LIST)) { | |
b62bdf2c JA |
854 | ptr2 = strchr(ptr, ','); |
855 | if (ptr2 && *(ptr2 + 1) == '\0') | |
856 | *ptr2 = '\0'; | |
6eaf09d6 | 857 | if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) { |
b62bdf2c JA |
858 | if (!ptr2) |
859 | ptr2 = strchr(ptr, ':'); | |
860 | if (!ptr2) | |
861 | ptr2 = strchr(ptr, '-'); | |
862 | } | |
83349190 YH |
863 | } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) { |
864 | ptr2 = strchr(ptr, ':'); | |
b62bdf2c | 865 | } |
787f7e95 | 866 | |
b62bdf2c JA |
867 | /* |
868 | * Don't return early if parsing the first option fails - if | |
869 | * we are doing multiple arguments, we can allow the first one | |
870 | * being empty. | |
871 | */ | |
83349190 | 872 | __ret = __handle_option(o, ptr, data, !done, !!ptr2, done); |
b62bdf2c JA |
873 | if (ret) |
874 | ret = __ret; | |
787f7e95 | 875 | |
b62bdf2c JA |
876 | if (!ptr2) |
877 | break; | |
f90eff5a | 878 | |
b62bdf2c JA |
879 | ptr = ptr2 + 1; |
880 | done++; | |
881 | } while (1); | |
787f7e95 | 882 | |
b62bdf2c JA |
883 | if (o_ptr) |
884 | free(o_ptr); | |
885 | return ret; | |
f90eff5a JA |
886 | } |
887 | ||
de890a1e | 888 | static struct fio_option *get_option(char *opt, |
07b3232d | 889 | struct fio_option *options, char **post) |
3b8b7135 JA |
890 | { |
891 | struct fio_option *o; | |
892 | char *ret; | |
893 | ||
894 | ret = strchr(opt, '='); | |
895 | if (ret) { | |
896 | *post = ret; | |
897 | *ret = '\0'; | |
de890a1e | 898 | ret = opt; |
3b8b7135 | 899 | (*post)++; |
43c129b4 | 900 | strip_blank_end(ret); |
07b3232d | 901 | o = find_option(options, ret); |
3b8b7135 | 902 | } else { |
07b3232d | 903 | o = find_option(options, opt); |
3b8b7135 JA |
904 | *post = NULL; |
905 | } | |
906 | ||
907 | return o; | |
908 | } | |
909 | ||
910 | static int opt_cmp(const void *p1, const void *p2) | |
911 | { | |
de890a1e SL |
912 | struct fio_option *o; |
913 | char *s, *foo; | |
8cdabc1d | 914 | int prio1, prio2; |
3b8b7135 | 915 | |
8cdabc1d | 916 | prio1 = prio2 = 0; |
3b8b7135 | 917 | |
de890a1e SL |
918 | if (*(char **)p1) { |
919 | s = strdup(*((char **) p1)); | |
9af4a244 | 920 | o = get_option(s, __fio_options, &foo); |
de890a1e SL |
921 | if (o) |
922 | prio1 = o->prio; | |
923 | free(s); | |
924 | } | |
925 | if (*(char **)p2) { | |
926 | s = strdup(*((char **) p2)); | |
9af4a244 | 927 | o = get_option(s, __fio_options, &foo); |
de890a1e SL |
928 | if (o) |
929 | prio2 = o->prio; | |
930 | free(s); | |
931 | } | |
932 | ||
8cdabc1d | 933 | return prio2 - prio1; |
3b8b7135 JA |
934 | } |
935 | ||
936 | void sort_options(char **opts, struct fio_option *options, int num_opts) | |
937 | { | |
9af4a244 | 938 | __fio_options = options; |
3b8b7135 | 939 | qsort(opts, num_opts, sizeof(char *), opt_cmp); |
9af4a244 | 940 | __fio_options = NULL; |
3b8b7135 JA |
941 | } |
942 | ||
b4692828 | 943 | int parse_cmd_option(const char *opt, const char *val, |
07b3232d | 944 | struct fio_option *options, void *data) |
b4692828 JA |
945 | { |
946 | struct fio_option *o; | |
947 | ||
07b3232d | 948 | o = find_option(options, opt); |
b4692828 | 949 | if (!o) { |
06b0be6e | 950 | log_err("Bad option <%s>\n", opt); |
b4692828 JA |
951 | return 1; |
952 | } | |
953 | ||
b1508cf9 JA |
954 | if (!handle_option(o, val, data)) |
955 | return 0; | |
956 | ||
06b0be6e | 957 | log_err("fio: failed parsing %s=%s\n", opt, val); |
b1508cf9 | 958 | return 1; |
b4692828 JA |
959 | } |
960 | ||
de890a1e | 961 | int parse_option(char *opt, const char *input, |
292cc475 JA |
962 | struct fio_option *options, struct fio_option **o, void *data, |
963 | int dump_cmdline) | |
cb2c86fd | 964 | { |
d0c814ec | 965 | char *post; |
e1f36503 | 966 | |
d0c814ec SL |
967 | if (!opt) { |
968 | log_err("fio: failed parsing %s\n", input); | |
de890a1e | 969 | *o = NULL; |
38789b58 | 970 | return 1; |
d0c814ec | 971 | } |
e1f36503 | 972 | |
de890a1e SL |
973 | *o = get_option(opt, options, &post); |
974 | if (!*o) { | |
975 | if (post) { | |
976 | int len = strlen(opt); | |
977 | if (opt + len + 1 != post) | |
978 | memmove(opt + len + 1, post, strlen(post)); | |
979 | opt[len] = '='; | |
980 | } | |
e1f36503 JA |
981 | return 1; |
982 | } | |
983 | ||
292cc475 JA |
984 | if (handle_option(*o, post, data)) { |
985 | log_err("fio: failed parsing %s\n", input); | |
986 | return 1; | |
987 | } | |
b1508cf9 | 988 | |
292cc475 JA |
989 | if (dump_cmdline) { |
990 | const char *delim; | |
991 | ||
992 | if (!strcmp("description", (*o)->name)) | |
993 | delim = "\""; | |
994 | else | |
995 | delim = ""; | |
996 | ||
997 | log_info("--%s%s", (*o)->name, post ? "" : " "); | |
998 | if (post) | |
999 | log_info("=%s%s%s ", delim, post, delim); | |
1000 | } | |
1001 | ||
1002 | return 0; | |
e1f36503 | 1003 | } |
fd28ca49 | 1004 | |
0e9f7fac JA |
1005 | /* |
1006 | * Option match, levenshtein distance. Handy for not quite remembering what | |
1007 | * the option name is. | |
1008 | */ | |
1009 | static int string_distance(const char *s1, const char *s2) | |
1010 | { | |
1011 | unsigned int s1_len = strlen(s1); | |
1012 | unsigned int s2_len = strlen(s2); | |
1013 | unsigned int *p, *q, *r; | |
1014 | unsigned int i, j; | |
1015 | ||
1016 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
1017 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
1018 | ||
1019 | p[0] = 0; | |
1020 | for (i = 1; i <= s2_len; i++) | |
1021 | p[i] = p[i - 1] + 1; | |
1022 | ||
1023 | for (i = 1; i <= s1_len; i++) { | |
1024 | q[0] = p[0] + 1; | |
1025 | for (j = 1; j <= s2_len; j++) { | |
1026 | unsigned int sub = p[j - 1]; | |
1027 | ||
1028 | if (s1[i - 1] != s2[j - 1]) | |
1029 | sub++; | |
1030 | ||
1031 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); | |
1032 | } | |
1033 | r = p; | |
1034 | p = q; | |
1035 | q = r; | |
1036 | } | |
1037 | ||
1038 | i = p[s2_len]; | |
1039 | free(p); | |
1040 | free(q); | |
1041 | return i; | |
1042 | } | |
1043 | ||
afdf9352 JA |
1044 | static struct fio_option *find_child(struct fio_option *options, |
1045 | struct fio_option *o) | |
1046 | { | |
1047 | struct fio_option *__o; | |
1048 | ||
fdf28744 JA |
1049 | for (__o = options + 1; __o->name; __o++) |
1050 | if (__o->parent && !strcmp(__o->parent, o->name)) | |
afdf9352 JA |
1051 | return __o; |
1052 | ||
1053 | return NULL; | |
1054 | } | |
1055 | ||
323d9113 JA |
1056 | static void __print_option(struct fio_option *o, struct fio_option *org, |
1057 | int level) | |
afdf9352 JA |
1058 | { |
1059 | char name[256], *p; | |
323d9113 | 1060 | int depth; |
afdf9352 JA |
1061 | |
1062 | if (!o) | |
1063 | return; | |
ef9aff52 JA |
1064 | if (!org) |
1065 | org = o; | |
5ec10eaa | 1066 | |
afdf9352 | 1067 | p = name; |
323d9113 JA |
1068 | depth = level; |
1069 | while (depth--) | |
1070 | p += sprintf(p, "%s", " "); | |
afdf9352 JA |
1071 | |
1072 | sprintf(p, "%s", o->name); | |
1073 | ||
28d7c363 | 1074 | log_info("%-24s: %s\n", name, o->help); |
323d9113 JA |
1075 | } |
1076 | ||
1077 | static void print_option(struct fio_option *o) | |
1078 | { | |
1079 | struct fio_option *parent; | |
1080 | struct fio_option *__o; | |
1081 | unsigned int printed; | |
1082 | unsigned int level; | |
1083 | ||
1084 | __print_option(o, NULL, 0); | |
1085 | parent = o; | |
1086 | level = 0; | |
1087 | do { | |
1088 | level++; | |
1089 | printed = 0; | |
1090 | ||
1091 | while ((__o = find_child(o, parent)) != NULL) { | |
1092 | __print_option(__o, o, level); | |
1093 | o = __o; | |
1094 | printed++; | |
1095 | } | |
1096 | ||
1097 | parent = o; | |
1098 | } while (printed); | |
afdf9352 JA |
1099 | } |
1100 | ||
07b3232d | 1101 | int show_cmd_help(struct fio_option *options, const char *name) |
0e9f7fac JA |
1102 | { |
1103 | struct fio_option *o, *closest; | |
d091d099 | 1104 | unsigned int best_dist = -1U; |
29fc6afe | 1105 | int found = 0; |
320beefe JA |
1106 | int show_all = 0; |
1107 | ||
1108 | if (!name || !strcmp(name, "all")) | |
1109 | show_all = 1; | |
fd28ca49 | 1110 | |
0e9f7fac JA |
1111 | closest = NULL; |
1112 | best_dist = -1; | |
4945ba12 | 1113 | for (o = &options[0]; o->name; o++) { |
320beefe JA |
1114 | int match = 0; |
1115 | ||
15ca150e JA |
1116 | if (o->type == FIO_OPT_DEPRECATED) |
1117 | continue; | |
07b3232d JA |
1118 | if (!exec_profile && o->prof_name) |
1119 | continue; | |
4ac23d27 JA |
1120 | if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name))) |
1121 | continue; | |
15ca150e | 1122 | |
0e9f7fac | 1123 | if (name) { |
7f9348f8 JA |
1124 | if (!strcmp(name, o->name) || |
1125 | (o->alias && !strcmp(name, o->alias))) | |
0e9f7fac JA |
1126 | match = 1; |
1127 | else { | |
1128 | unsigned int dist; | |
1129 | ||
1130 | dist = string_distance(name, o->name); | |
1131 | if (dist < best_dist) { | |
1132 | best_dist = dist; | |
1133 | closest = o; | |
1134 | } | |
1135 | } | |
1136 | } | |
fd28ca49 JA |
1137 | |
1138 | if (show_all || match) { | |
29fc6afe | 1139 | found = 1; |
c167dedc | 1140 | if (match) |
28d7c363 | 1141 | log_info("%20s: %s\n", o->name, o->help); |
c167dedc | 1142 | if (show_all) { |
afdf9352 | 1143 | if (!o->parent) |
323d9113 | 1144 | print_option(o); |
4945ba12 | 1145 | continue; |
c167dedc | 1146 | } |
fd28ca49 JA |
1147 | } |
1148 | ||
70df2f19 JA |
1149 | if (!match) |
1150 | continue; | |
1151 | ||
06b0be6e | 1152 | show_option_help(o, 0); |
fd28ca49 JA |
1153 | } |
1154 | ||
29fc6afe JA |
1155 | if (found) |
1156 | return 0; | |
fd28ca49 | 1157 | |
28d7c363 | 1158 | log_err("No such command: %s", name); |
d091d099 JA |
1159 | |
1160 | /* | |
1161 | * Only print an appropriately close option, one where the edit | |
1162 | * distance isn't too big. Otherwise we get crazy matches. | |
1163 | */ | |
1164 | if (closest && best_dist < 3) { | |
28d7c363 JA |
1165 | log_info(" - showing closest match\n"); |
1166 | log_info("%20s: %s\n", closest->name, closest->help); | |
06b0be6e | 1167 | show_option_help(closest, 0); |
0e9f7fac | 1168 | } else |
28d7c363 | 1169 | log_info("\n"); |
0e9f7fac | 1170 | |
29fc6afe | 1171 | return 1; |
fd28ca49 | 1172 | } |
ee738499 | 1173 | |
13335ddb JA |
1174 | /* |
1175 | * Handle parsing of default parameters. | |
1176 | */ | |
ee738499 JA |
1177 | void fill_default_options(void *data, struct fio_option *options) |
1178 | { | |
4945ba12 | 1179 | struct fio_option *o; |
ee738499 | 1180 | |
a3d741fa JA |
1181 | dprint(FD_PARSE, "filling default options\n"); |
1182 | ||
4945ba12 | 1183 | for (o = &options[0]; o->name; o++) |
ee738499 JA |
1184 | if (o->def) |
1185 | handle_option(o, o->def, data); | |
ee738499 | 1186 | } |
13335ddb | 1187 | |
9f988e2e JA |
1188 | void option_init(struct fio_option *o) |
1189 | { | |
1190 | if (o->type == FIO_OPT_DEPRECATED) | |
1191 | return; | |
1192 | if (o->type == FIO_OPT_BOOL) { | |
1193 | o->minval = 0; | |
1194 | o->maxval = 1; | |
1195 | } | |
d4fc2f04 JA |
1196 | if (o->type == FIO_OPT_INT) { |
1197 | if (!o->maxval) | |
1198 | o->maxval = UINT_MAX; | |
1199 | } | |
83349190 | 1200 | if (o->type == FIO_OPT_FLOAT_LIST) { |
ab9461ea BC |
1201 | o->minfp = DBL_MIN; |
1202 | o->maxfp = DBL_MAX; | |
83349190 | 1203 | } |
c8931876 | 1204 | if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) { |
06b0be6e | 1205 | log_err("Option %s: string set option with" |
9f988e2e JA |
1206 | " default will always be true\n", o->name); |
1207 | } | |
7b504edd | 1208 | if (!o->cb && !o->off1) |
06b0be6e | 1209 | log_err("Option %s: neither cb nor offset given\n", o->name); |
22754746 | 1210 | if (!o->category) { |
cca5b5bc | 1211 | log_info("Option %s: no category defined. Setting to misc\n", o->name); |
22754746 | 1212 | o->category = FIO_OPT_C_GENERAL; |
ffd7821f | 1213 | o->group = FIO_OPT_G_INVALID; |
22754746 | 1214 | } |
5f6ddf1e JA |
1215 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE || |
1216 | o->type == FIO_OPT_STR_MULTI) | |
9f988e2e | 1217 | return; |
7b504edd | 1218 | if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) |
06b0be6e | 1219 | log_err("Option %s: both cb and offset given\n", o->name); |
9f988e2e JA |
1220 | } |
1221 | ||
13335ddb JA |
1222 | /* |
1223 | * Sanitize the options structure. For now it just sets min/max for bool | |
5b0a8880 | 1224 | * values and whether both callback and offsets are given. |
13335ddb JA |
1225 | */ |
1226 | void options_init(struct fio_option *options) | |
1227 | { | |
4945ba12 | 1228 | struct fio_option *o; |
13335ddb | 1229 | |
a3d741fa JA |
1230 | dprint(FD_PARSE, "init options\n"); |
1231 | ||
90265353 | 1232 | for (o = &options[0]; o->name; o++) { |
9f988e2e | 1233 | option_init(o); |
90265353 JA |
1234 | if (o->inverse) |
1235 | o->inv_opt = find_option(options, o->inverse); | |
1236 | } | |
13335ddb | 1237 | } |
7e356b2d JA |
1238 | |
1239 | void options_free(struct fio_option *options, void *data) | |
1240 | { | |
1241 | struct fio_option *o; | |
1242 | char **ptr; | |
1243 | ||
1244 | dprint(FD_PARSE, "free options\n"); | |
1245 | ||
1246 | for (o = &options[0]; o->name; o++) { | |
de890a1e | 1247 | if (o->type != FIO_OPT_STR_STORE || !o->off1) |
7e356b2d JA |
1248 | continue; |
1249 | ||
f0fdbcaf | 1250 | ptr = td_var(data, o, o->off1); |
7e356b2d JA |
1251 | if (*ptr) { |
1252 | free(*ptr); | |
1253 | *ptr = NULL; | |
1254 | } | |
1255 | } | |
1256 | } |