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