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