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