Commit | Line | Data |
---|---|---|
cb2c86fd JA |
1 | /* |
2 | * This file contains the ini and command liner parser main. | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <unistd.h> | |
7 | #include <ctype.h> | |
8 | #include <string.h> | |
9 | #include <errno.h> | |
10 | #include <limits.h> | |
88b5a391 | 11 | #include <stdlib.h> |
83349190 | 12 | #include <math.h> |
cb2c86fd JA |
13 | |
14 | #include "parse.h" | |
a3d741fa | 15 | #include "debug.h" |
9f988e2e | 16 | #include "options.h" |
cb2c86fd | 17 | |
3b8b7135 | 18 | static struct fio_option *fio_options; |
d6978a32 | 19 | extern unsigned int fio_get_kb_base(void *); |
3b8b7135 | 20 | |
f085737f JA |
21 | static int vp_cmp(const void *p1, const void *p2) |
22 | { | |
23 | const struct value_pair *vp1 = p1; | |
24 | const struct value_pair *vp2 = p2; | |
25 | ||
26 | return strlen(vp2->ival) - strlen(vp1->ival); | |
27 | } | |
28 | ||
ce952ab6 | 29 | static void posval_sort(struct fio_option *o, struct value_pair *vpmap) |
f085737f JA |
30 | { |
31 | const struct value_pair *vp; | |
32 | int entries; | |
33 | ||
34 | memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair)); | |
35 | ||
36 | for (entries = 0; entries < PARSE_MAX_VP; entries++) { | |
37 | vp = &o->posval[entries]; | |
38 | if (!vp->ival || vp->ival[0] == '\0') | |
39 | break; | |
40 | ||
41 | memcpy(&vpmap[entries], vp, sizeof(*vp)); | |
42 | } | |
43 | ||
44 | qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp); | |
45 | } | |
46 | ||
06b0be6e JA |
47 | static void show_option_range(struct fio_option *o, |
48 | int (*logger)(const char *format, ...)) | |
b1ec1da6 | 49 | { |
83349190 YH |
50 | if (o->type == FIO_OPT_FLOAT_LIST){ |
51 | if (isnan(o->minfp) && isnan(o->maxfp)) | |
52 | return; | |
53 | ||
06b0be6e | 54 | logger("%20s: min=%f", "range", o->minfp); |
83349190 | 55 | if (!isnan(o->maxfp)) |
06b0be6e JA |
56 | logger(", max=%f", o->maxfp); |
57 | logger("\n"); | |
83349190 YH |
58 | } else { |
59 | if (!o->minval && !o->maxval) | |
60 | return; | |
b1ec1da6 | 61 | |
06b0be6e | 62 | logger("%20s: min=%d", "range", o->minval); |
83349190 | 63 | if (o->maxval) |
06b0be6e JA |
64 | logger(", max=%d", o->maxval); |
65 | logger("\n"); | |
83349190 | 66 | } |
b1ec1da6 JA |
67 | } |
68 | ||
69 | static void show_option_values(struct fio_option *o) | |
70 | { | |
a3073f4a | 71 | int i; |
b1ec1da6 | 72 | |
a3073f4a | 73 | for (i = 0; i < PARSE_MAX_VP; i++) { |
7837213b | 74 | const struct value_pair *vp = &o->posval[i]; |
b1ec1da6 | 75 | |
7837213b | 76 | if (!vp->ival) |
a3073f4a | 77 | continue; |
b1ec1da6 | 78 | |
06b0be6e | 79 | log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival); |
7837213b | 80 | if (vp->help) |
06b0be6e JA |
81 | log_info(" %s", vp->help); |
82 | log_info("\n"); | |
a3073f4a | 83 | } |
b1ec1da6 JA |
84 | |
85 | if (i) | |
06b0be6e | 86 | log_info("\n"); |
b1ec1da6 JA |
87 | } |
88 | ||
06b0be6e | 89 | static void show_option_help(struct fio_option *o, int is_err) |
d447a8c2 JA |
90 | { |
91 | const char *typehelp[] = { | |
07b3232d | 92 | "invalid", |
d447a8c2 | 93 | "string (opt=bla)", |
ae3fb6fb | 94 | "string (opt=bla)", |
d447a8c2 JA |
95 | "string with possible k/m/g postfix (opt=4k)", |
96 | "string with time postfix (opt=10s)", | |
97 | "string (opt=bla)", | |
98 | "string with dual range (opt=1k-4k,4k-8k)", | |
99 | "integer value (opt=100)", | |
100 | "boolean value (opt=1)", | |
83349190 | 101 | "list of floating point values separated by ':' (opt=5.9:7.8)", |
d447a8c2 | 102 | "no argument (opt)", |
07b3232d | 103 | "deprecated", |
d447a8c2 | 104 | }; |
06b0be6e JA |
105 | int (*logger)(const char *format, ...); |
106 | ||
107 | if (is_err) | |
108 | logger = log_err; | |
109 | else | |
110 | logger = log_info; | |
d447a8c2 JA |
111 | |
112 | if (o->alias) | |
06b0be6e | 113 | logger("%20s: %s\n", "alias", o->alias); |
d447a8c2 | 114 | |
06b0be6e JA |
115 | logger("%20s: %s\n", "type", typehelp[o->type]); |
116 | logger("%20s: %s\n", "default", o->def ? o->def : "no default"); | |
07b3232d | 117 | if (o->prof_name) |
06b0be6e JA |
118 | logger("%20s: only for profile '%s'\n", "valid", o->prof_name); |
119 | show_option_range(o, logger); | |
d447a8c2 JA |
120 | show_option_values(o); |
121 | } | |
122 | ||
cb2c86fd JA |
123 | static unsigned long get_mult_time(char c) |
124 | { | |
125 | switch (c) { | |
5ec10eaa JA |
126 | case 'm': |
127 | case 'M': | |
128 | return 60; | |
129 | case 'h': | |
130 | case 'H': | |
131 | return 60 * 60; | |
132 | case 'd': | |
133 | case 'D': | |
134 | return 24 * 60 * 60; | |
135 | default: | |
136 | return 1; | |
cb2c86fd JA |
137 | } |
138 | } | |
139 | ||
7bb59102 JA |
140 | static unsigned long long __get_mult_bytes(const char *p, void *data, |
141 | int *percent) | |
cb2c86fd | 142 | { |
d6978a32 | 143 | unsigned int kb_base = fio_get_kb_base(data); |
a639f0bb | 144 | unsigned long long ret = 1; |
57fc29fa JA |
145 | unsigned int i, pow = 0, mult = kb_base; |
146 | char *c; | |
a639f0bb | 147 | |
57fc29fa JA |
148 | if (!p) |
149 | return 1; | |
a639f0bb | 150 | |
57fc29fa JA |
151 | c = strdup(p); |
152 | ||
153 | for (i = 0; i < strlen(c); i++) | |
154 | c[i] = tolower(c[i]); | |
155 | ||
156 | if (!strcmp("pib", c)) { | |
157 | pow = 5; | |
158 | mult = 1000; | |
159 | } else if (!strcmp("tib", c)) { | |
160 | pow = 4; | |
161 | mult = 1000; | |
162 | } else if (!strcmp("gib", c)) { | |
163 | pow = 3; | |
164 | mult = 1000; | |
165 | } else if (!strcmp("mib", c)) { | |
166 | pow = 2; | |
167 | mult = 1000; | |
168 | } else if (!strcmp("kib", c)) { | |
169 | pow = 1; | |
170 | mult = 1000; | |
171 | } else if (!strcmp("p", c) || !strcmp("pb", c)) | |
172 | pow = 5; | |
173 | else if (!strcmp("t", c) || !strcmp("tb", c)) | |
174 | pow = 4; | |
175 | else if (!strcmp("g", c) || !strcmp("gb", c)) | |
176 | pow = 3; | |
177 | else if (!strcmp("m", c) || !strcmp("mb", c)) | |
178 | pow = 2; | |
179 | else if (!strcmp("k", c) || !strcmp("kb", c)) | |
180 | pow = 1; | |
7bb59102 JA |
181 | else if (!strcmp("%", c)) { |
182 | *percent = 1; | |
183 | return ret; | |
184 | } | |
57fc29fa JA |
185 | |
186 | while (pow--) | |
187 | ret *= (unsigned long long) mult; | |
188 | ||
189 | free(c); | |
a639f0bb | 190 | return ret; |
cb2c86fd JA |
191 | } |
192 | ||
7bb59102 | 193 | static unsigned long long get_mult_bytes(const char *str, int len, void *data, |
6925dd35 | 194 | int *percent) |
57fc29fa | 195 | { |
55ed9636 | 196 | const char *p = str; |
ba4ddd69 | 197 | int digit_seen = 0; |
57fc29fa | 198 | |
1d1c187b | 199 | if (len < 2) |
7bb59102 | 200 | return __get_mult_bytes(str, data, percent); |
1d1c187b | 201 | |
55ed9636 | 202 | /* |
e2979754 | 203 | * Go forward until we hit a non-digit, or +/- sign |
55ed9636 JA |
204 | */ |
205 | while ((p - str) <= len) { | |
ba4ddd69 JA |
206 | if (!isdigit((int) *p) && |
207 | (((*p != '+') && (*p != '-')) || digit_seen)) | |
55ed9636 | 208 | break; |
3c703d13 | 209 | digit_seen |= isdigit((int) *p); |
55ed9636 JA |
210 | p++; |
211 | } | |
212 | ||
7bb59102 | 213 | if (!isalpha((int) *p) && (*p != '%')) |
57fc29fa JA |
214 | p = NULL; |
215 | ||
7bb59102 | 216 | return __get_mult_bytes(p, data, percent); |
57fc29fa JA |
217 | } |
218 | ||
83349190 YH |
219 | /* |
220 | * Convert string into a floating number. Return 1 for success and 0 otherwise. | |
221 | */ | |
222 | int str_to_float(const char *str, double *val) | |
223 | { | |
224 | return (1 == sscanf(str, "%lf", val)); | |
225 | } | |
226 | ||
cb2c86fd | 227 | /* |
e1f36503 | 228 | * convert string into decimal value, noting any size suffix |
cb2c86fd | 229 | */ |
6925dd35 | 230 | int str_to_decimal(const char *str, long long *val, int kilo, void *data) |
cb2c86fd | 231 | { |
b347f9da | 232 | int len, base; |
cb2c86fd | 233 | |
cb2c86fd | 234 | len = strlen(str); |
f90eff5a JA |
235 | if (!len) |
236 | return 1; | |
cb2c86fd | 237 | |
b347f9da JA |
238 | if (strstr(str, "0x") || strstr(str, "0X")) |
239 | base = 16; | |
240 | else | |
241 | base = 10; | |
242 | ||
243 | *val = strtoll(str, NULL, base); | |
cda866ca | 244 | if (*val == LONG_MAX && errno == ERANGE) |
cb2c86fd JA |
245 | return 1; |
246 | ||
7bb59102 JA |
247 | if (kilo) { |
248 | unsigned long long mult; | |
249 | int perc = 0; | |
250 | ||
6925dd35 | 251 | mult = get_mult_bytes(str, len, data, &perc); |
7bb59102 JA |
252 | if (perc) |
253 | *val = -1ULL - *val; | |
254 | else | |
255 | *val *= mult; | |
256 | } else | |
cb2c86fd | 257 | *val *= get_mult_time(str[len - 1]); |
787f7e95 | 258 | |
cb2c86fd JA |
259 | return 0; |
260 | } | |
261 | ||
6925dd35 | 262 | static int check_str_bytes(const char *p, long long *val, void *data) |
cb2c86fd | 263 | { |
6925dd35 | 264 | return str_to_decimal(p, val, 1, data); |
cb2c86fd JA |
265 | } |
266 | ||
63f29372 | 267 | static int check_str_time(const char *p, long long *val) |
cb2c86fd | 268 | { |
6925dd35 | 269 | return str_to_decimal(p, val, 0, NULL); |
cb2c86fd JA |
270 | } |
271 | ||
272 | void strip_blank_front(char **p) | |
273 | { | |
274 | char *s = *p; | |
275 | ||
76cd9378 | 276 | while (isspace((int) *s)) |
cb2c86fd | 277 | s++; |
4d651dad JA |
278 | |
279 | *p = s; | |
cb2c86fd JA |
280 | } |
281 | ||
282 | void strip_blank_end(char *p) | |
283 | { | |
853ee7fc | 284 | char *start = p, *s; |
523bfadb JA |
285 | |
286 | s = strchr(p, ';'); | |
287 | if (s) | |
288 | *s = '\0'; | |
289 | s = strchr(p, '#'); | |
290 | if (s) | |
291 | *s = '\0'; | |
292 | if (s) | |
293 | p = s; | |
294 | ||
7f7e6e59 | 295 | s = p + strlen(p); |
76cd9378 | 296 | while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start)) |
cb2c86fd JA |
297 | s--; |
298 | ||
299 | *(s + 1) = '\0'; | |
300 | } | |
301 | ||
d6978a32 | 302 | static int check_range_bytes(const char *str, long *val, void *data) |
cb2c86fd | 303 | { |
57fc29fa | 304 | long long __val; |
cb2c86fd | 305 | |
6925dd35 | 306 | if (!str_to_decimal(str, &__val, 1, data)) { |
57fc29fa | 307 | *val = __val; |
cb2c86fd JA |
308 | return 0; |
309 | } | |
310 | ||
cb2c86fd JA |
311 | return 1; |
312 | } | |
313 | ||
63f29372 | 314 | static int check_int(const char *p, int *val) |
cb2c86fd | 315 | { |
787f7e95 JA |
316 | if (!strlen(p)) |
317 | return 1; | |
d78ee463 | 318 | if (strstr(p, "0x") || strstr(p, "0X")) { |
a61bdfd8 JA |
319 | if (sscanf(p, "%x", val) == 1) |
320 | return 0; | |
321 | } else { | |
322 | if (sscanf(p, "%u", val) == 1) | |
323 | return 0; | |
324 | } | |
cb2c86fd | 325 | |
e1f36503 JA |
326 | return 1; |
327 | } | |
cb2c86fd | 328 | |
808def70 JA |
329 | static int opt_len(const char *str) |
330 | { | |
331 | char *postfix; | |
332 | ||
333 | postfix = strchr(str, ':'); | |
334 | if (!postfix) | |
335 | return strlen(str); | |
336 | ||
337 | return (int)(postfix - str); | |
338 | } | |
339 | ||
5f6ddf1e | 340 | #define val_store(ptr, val, off, or, data) \ |
17abbe89 JA |
341 | do { \ |
342 | ptr = td_var((data), (off)); \ | |
5f6ddf1e JA |
343 | if ((or)) \ |
344 | *ptr |= (val); \ | |
345 | else \ | |
346 | *ptr = (val); \ | |
17abbe89 JA |
347 | } while (0) |
348 | ||
f90eff5a | 349 | static int __handle_option(struct fio_option *o, const char *ptr, void *data, |
83349190 | 350 | int first, int more, int curr) |
cb2c86fd | 351 | { |
63f29372 | 352 | int il, *ilp; |
83349190 | 353 | double* flp; |
63f29372 JA |
354 | long long ull, *ullp; |
355 | long ul1, ul2; | |
83349190 | 356 | double uf; |
b4692828 | 357 | char **cp; |
e42b01eb | 358 | int ret = 0, is_time = 0; |
c44b1ff5 JA |
359 | const struct value_pair *vp; |
360 | struct value_pair posval[PARSE_MAX_VP]; | |
361 | int i, all_skipped = 1; | |
cb2c86fd | 362 | |
a3d741fa JA |
363 | dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name, |
364 | o->type, ptr); | |
365 | ||
e3cedca7 | 366 | if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) { |
08e26e35 JA |
367 | fprintf(stderr, "Option %s requires an argument\n", o->name); |
368 | return 1; | |
369 | } | |
370 | ||
e1f36503 | 371 | switch (o->type) { |
5f6ddf1e JA |
372 | case FIO_OPT_STR: |
373 | case FIO_OPT_STR_MULTI: { | |
e1f36503 | 374 | fio_opt_str_fn *fn = o->cb; |
b1ec1da6 | 375 | |
f085737f JA |
376 | posval_sort(o, posval); |
377 | ||
5f6ddf1e | 378 | ret = 1; |
b1ec1da6 | 379 | for (i = 0; i < PARSE_MAX_VP; i++) { |
f085737f | 380 | vp = &posval[i]; |
b1ec1da6 | 381 | if (!vp->ival || vp->ival[0] == '\0') |
a3073f4a | 382 | continue; |
ae2ddba4 | 383 | all_skipped = 0; |
808def70 | 384 | if (!strncmp(vp->ival, ptr, opt_len(ptr))) { |
b1ec1da6 | 385 | ret = 0; |
5f6ddf1e JA |
386 | if (o->roff1) { |
387 | if (vp->or) | |
388 | *(unsigned int *) o->roff1 |= vp->oval; | |
389 | else | |
390 | *(unsigned int *) o->roff1 = vp->oval; | |
391 | } else { | |
02c6aad5 | 392 | if (!o->off1) |
5f6ddf1e JA |
393 | continue; |
394 | val_store(ilp, vp->oval, o->off1, vp->or, data); | |
02c6aad5 | 395 | } |
5f6ddf1e | 396 | continue; |
b1ec1da6 JA |
397 | } |
398 | } | |
cb2c86fd | 399 | |
ae2ddba4 | 400 | if (ret && !all_skipped) |
b1ec1da6 JA |
401 | show_option_values(o); |
402 | else if (fn) | |
403 | ret = fn(data, ptr); | |
e1f36503 JA |
404 | break; |
405 | } | |
e42b01eb JA |
406 | case FIO_OPT_STR_VAL_TIME: |
407 | is_time = 1; | |
1a1137d9 | 408 | case FIO_OPT_INT: |
e42b01eb JA |
409 | case FIO_OPT_STR_VAL: { |
410 | fio_opt_str_val_fn *fn = o->cb; | |
411 | ||
412 | if (is_time) | |
413 | ret = check_str_time(ptr, &ull); | |
414 | else | |
6925dd35 | 415 | ret = check_str_bytes(ptr, &ull, data); |
e1f36503 JA |
416 | |
417 | if (ret) | |
418 | break; | |
419 | ||
db8e0165 | 420 | if (o->maxval && ull > o->maxval) { |
5ec10eaa JA |
421 | fprintf(stderr, "max value out of range: %lld" |
422 | " (%d max)\n", ull, o->maxval); | |
db8e0165 JA |
423 | return 1; |
424 | } | |
425 | if (o->minval && ull < o->minval) { | |
5ec10eaa JA |
426 | fprintf(stderr, "min value out of range: %lld" |
427 | " (%d min)\n", ull, o->minval); | |
db8e0165 JA |
428 | return 1; |
429 | } | |
e1f36503 JA |
430 | |
431 | if (fn) | |
432 | ret = fn(data, &ull); | |
433 | else { | |
e01b22b8 | 434 | if (o->type == FIO_OPT_INT) { |
02c6aad5 JA |
435 | if (first) { |
436 | if (o->roff1) | |
7758d4f0 | 437 | *(unsigned int *) o->roff1 = ull; |
02c6aad5 | 438 | else |
5f6ddf1e | 439 | val_store(ilp, ull, o->off1, 0, data); |
02c6aad5 JA |
440 | } |
441 | if (!more) { | |
442 | if (o->roff2) | |
7758d4f0 | 443 | *(unsigned int *) o->roff2 = ull; |
02c6aad5 | 444 | else if (o->off2) |
5f6ddf1e | 445 | val_store(ilp, ull, o->off2, 0, data); |
02c6aad5 | 446 | } |
75e6f36f | 447 | } else { |
02c6aad5 JA |
448 | if (first) { |
449 | if (o->roff1) | |
450 | *(unsigned long long *) o->roff1 = ull; | |
451 | else | |
5f6ddf1e | 452 | val_store(ullp, ull, o->off1, 0, data); |
02c6aad5 JA |
453 | } |
454 | if (!more) { | |
455 | if (o->roff2) | |
456 | *(unsigned long long *) o->roff2 = ull; | |
457 | else if (o->off2) | |
5f6ddf1e | 458 | val_store(ullp, ull, o->off2, 0, data); |
02c6aad5 | 459 | } |
75e6f36f | 460 | } |
e1f36503 JA |
461 | } |
462 | break; | |
463 | } | |
83349190 YH |
464 | case FIO_OPT_FLOAT_LIST: { |
465 | ||
466 | if (first) { | |
467 | ul2 = 1; | |
468 | ilp = td_var(data, o->off2); | |
469 | *ilp = ul2; | |
470 | } | |
471 | if (curr >= o->maxlen) { | |
472 | fprintf(stderr, "the list exceeding max length %d\n", | |
473 | o->maxlen); | |
474 | return 1; | |
475 | } | |
476 | if(!str_to_float(ptr, &uf)){ | |
477 | fprintf(stderr, "not a floating point value: %s\n", | |
478 | ptr); | |
479 | return 1; | |
480 | } | |
481 | if (!isnan(o->maxfp) && uf > o->maxfp) { | |
482 | fprintf(stderr, "value out of range: %f" | |
483 | " (range max: %f)\n", uf, o->maxfp); | |
484 | return 1; | |
485 | } | |
486 | if (!isnan(o->minfp) && uf < o->minfp) { | |
487 | fprintf(stderr, "value out of range: %f" | |
488 | " (range min: %f)\n", uf, o->minfp); | |
489 | return 1; | |
490 | } | |
491 | ||
492 | flp = td_var(data, o->off1); | |
493 | flp[curr] = uf; | |
494 | ||
495 | break; | |
496 | } | |
af52b345 JA |
497 | case FIO_OPT_STR_STORE: { |
498 | fio_opt_str_fn *fn = o->cb; | |
499 | ||
ce952ab6 JA |
500 | posval_sort(o, posval); |
501 | ||
1c964ce5 JA |
502 | if (!o->posval[0].ival) { |
503 | vp = NULL; | |
e2979754 | 504 | goto match; |
1c964ce5 | 505 | } |
02c6aad5 | 506 | |
c44b1ff5 JA |
507 | ret = 1; |
508 | for (i = 0; i < PARSE_MAX_VP; i++) { | |
509 | vp = &posval[i]; | |
510 | if (!vp->ival || vp->ival[0] == '\0') | |
511 | continue; | |
512 | all_skipped = 0; | |
513 | if (!strncmp(vp->ival, ptr, opt_len(ptr))) { | |
514 | char *rest; | |
515 | ||
516 | ret = 0; | |
517 | if (vp->cb) | |
518 | fn = vp->cb; | |
e2979754 | 519 | match: |
c44b1ff5 JA |
520 | if (o->roff1) |
521 | cp = (char **) o->roff1; | |
522 | else | |
523 | cp = td_var(data, o->off1); | |
524 | *cp = strdup(ptr); | |
525 | rest = strstr(*cp, ":"); | |
526 | if (rest) { | |
527 | *rest = '\0'; | |
528 | ptr = rest + 1; | |
1c964ce5 | 529 | } else if (vp && vp->cb) |
c44b1ff5 JA |
530 | ptr = NULL; |
531 | break; | |
af52b345 JA |
532 | } |
533 | } | |
c44b1ff5 JA |
534 | |
535 | if (ret && !all_skipped) | |
536 | show_option_values(o); | |
537 | else if (fn && ptr) | |
538 | ret = fn(data, ptr); | |
539 | ||
e1f36503 | 540 | break; |
af52b345 | 541 | } |
e1f36503 | 542 | case FIO_OPT_RANGE: { |
b765a372 | 543 | char tmp[128]; |
e1f36503 JA |
544 | char *p1, *p2; |
545 | ||
0bbab0e7 | 546 | strncpy(tmp, ptr, sizeof(tmp) - 1); |
b765a372 | 547 | |
31d23f47 DE |
548 | /* Handle bsrange with separate read,write values: */ |
549 | p1 = strchr(tmp, ','); | |
550 | if (p1) | |
551 | *p1 = '\0'; | |
552 | ||
b765a372 | 553 | p1 = strchr(tmp, '-'); |
e1f36503 | 554 | if (!p1) { |
0c9baf91 JA |
555 | p1 = strchr(tmp, ':'); |
556 | if (!p1) { | |
557 | ret = 1; | |
558 | break; | |
559 | } | |
e1f36503 JA |
560 | } |
561 | ||
562 | p2 = p1 + 1; | |
563 | *p1 = '\0'; | |
b765a372 | 564 | p1 = tmp; |
e1f36503 JA |
565 | |
566 | ret = 1; | |
d6978a32 JA |
567 | if (!check_range_bytes(p1, &ul1, data) && |
568 | !check_range_bytes(p2, &ul2, data)) { | |
e1f36503 | 569 | ret = 0; |
e1f36503 | 570 | if (ul1 > ul2) { |
f90eff5a JA |
571 | unsigned long foo = ul1; |
572 | ||
573 | ul1 = ul2; | |
574 | ul2 = foo; | |
575 | } | |
576 | ||
577 | if (first) { | |
02c6aad5 | 578 | if (o->roff1) |
7758d4f0 | 579 | *(unsigned int *) o->roff1 = ul1; |
02c6aad5 | 580 | else |
5f6ddf1e | 581 | val_store(ilp, ul1, o->off1, 0, data); |
02c6aad5 | 582 | if (o->roff2) |
7758d4f0 | 583 | *(unsigned int *) o->roff2 = ul2; |
02c6aad5 | 584 | else |
5f6ddf1e | 585 | val_store(ilp, ul2, o->off2, 0, data); |
17abbe89 | 586 | } |
02c6aad5 | 587 | if (o->roff3 && o->roff4) { |
7758d4f0 JA |
588 | *(unsigned int *) o->roff3 = ul1; |
589 | *(unsigned int *) o->roff4 = ul2; | |
02c6aad5 | 590 | } else if (o->off3 && o->off4) { |
5f6ddf1e JA |
591 | val_store(ilp, ul1, o->off3, 0, data); |
592 | val_store(ilp, ul2, o->off4, 0, data); | |
e1f36503 | 593 | } |
17abbe89 JA |
594 | } |
595 | ||
e1f36503 JA |
596 | break; |
597 | } | |
74ba1808 JA |
598 | case FIO_OPT_BOOL: |
599 | case FIO_OPT_STR_SET: { | |
e1f36503 JA |
600 | fio_opt_int_fn *fn = o->cb; |
601 | ||
74ba1808 JA |
602 | if (ptr) |
603 | ret = check_int(ptr, &il); | |
604 | else if (o->type == FIO_OPT_BOOL) | |
605 | ret = 1; | |
606 | else | |
607 | il = 1; | |
608 | ||
e1f36503 JA |
609 | if (ret) |
610 | break; | |
611 | ||
db8e0165 | 612 | if (o->maxval && il > (int) o->maxval) { |
5ec10eaa JA |
613 | fprintf(stderr, "max value out of range: %d (%d max)\n", |
614 | il, o->maxval); | |
db8e0165 JA |
615 | return 1; |
616 | } | |
617 | if (o->minval && il < o->minval) { | |
5ec10eaa JA |
618 | fprintf(stderr, "min value out of range: %d (%d min)\n", |
619 | il, o->minval); | |
db8e0165 JA |
620 | return 1; |
621 | } | |
e1f36503 | 622 | |
76a43db4 JA |
623 | if (o->neg) |
624 | il = !il; | |
625 | ||
e1f36503 JA |
626 | if (fn) |
627 | ret = fn(data, &il); | |
628 | else { | |
02c6aad5 JA |
629 | if (first) { |
630 | if (o->roff1) | |
631 | *(unsigned int *)o->roff1 = il; | |
632 | else | |
5f6ddf1e | 633 | val_store(ilp, il, o->off1, 0, data); |
02c6aad5 JA |
634 | } |
635 | if (!more) { | |
636 | if (o->roff2) | |
637 | *(unsigned int *) o->roff2 = il; | |
638 | else if (o->off2) | |
5f6ddf1e | 639 | val_store(ilp, il, o->off2, 0, data); |
02c6aad5 | 640 | } |
e1f36503 JA |
641 | } |
642 | break; | |
643 | } | |
15ca150e | 644 | case FIO_OPT_DEPRECATED: |
06b0be6e | 645 | log_info("Option %s is deprecated\n", o->name); |
15ca150e | 646 | break; |
e1f36503 | 647 | default: |
06b0be6e | 648 | log_err("Bad option type %u\n", o->type); |
e1f36503 JA |
649 | ret = 1; |
650 | } | |
cb2c86fd | 651 | |
70a4c0c8 JA |
652 | if (ret) |
653 | return ret; | |
654 | ||
d447a8c2 | 655 | if (o->verify) { |
70a4c0c8 | 656 | ret = o->verify(o, data); |
d447a8c2 JA |
657 | if (ret) { |
658 | fprintf(stderr,"Correct format for offending option\n"); | |
659 | fprintf(stderr, "%20s: %s\n", o->name, o->help); | |
06b0be6e | 660 | show_option_help(o, 1); |
d447a8c2 JA |
661 | } |
662 | } | |
70a4c0c8 | 663 | |
e1f36503 | 664 | return ret; |
cb2c86fd JA |
665 | } |
666 | ||
7c8f1a5c | 667 | static int handle_option(struct fio_option *o, const char *__ptr, void *data) |
f90eff5a | 668 | { |
b62bdf2c JA |
669 | char *o_ptr, *ptr, *ptr2; |
670 | int ret, done; | |
f90eff5a | 671 | |
7c8f1a5c JA |
672 | dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr); |
673 | ||
b62bdf2c | 674 | o_ptr = ptr = NULL; |
7c8f1a5c | 675 | if (__ptr) |
b62bdf2c | 676 | o_ptr = ptr = strdup(__ptr); |
a3d741fa | 677 | |
f90eff5a | 678 | /* |
b62bdf2c JA |
679 | * See if we have another set of parameters, hidden after a comma. |
680 | * Do this before parsing this round, to check if we should | |
787f7e95 | 681 | * copy set 1 options to set 2. |
f90eff5a | 682 | */ |
b62bdf2c JA |
683 | done = 0; |
684 | ret = 1; | |
685 | do { | |
686 | int __ret; | |
687 | ||
688 | ptr2 = NULL; | |
689 | if (ptr && | |
690 | (o->type != FIO_OPT_STR_STORE) && | |
83349190 YH |
691 | (o->type != FIO_OPT_STR) && |
692 | (o->type != FIO_OPT_FLOAT_LIST)) { | |
b62bdf2c JA |
693 | ptr2 = strchr(ptr, ','); |
694 | if (ptr2 && *(ptr2 + 1) == '\0') | |
695 | *ptr2 = '\0'; | |
696 | if (o->type != FIO_OPT_STR_MULTI) { | |
697 | if (!ptr2) | |
698 | ptr2 = strchr(ptr, ':'); | |
699 | if (!ptr2) | |
700 | ptr2 = strchr(ptr, '-'); | |
701 | } | |
83349190 YH |
702 | } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) { |
703 | ptr2 = strchr(ptr, ':'); | |
b62bdf2c | 704 | } |
787f7e95 | 705 | |
b62bdf2c JA |
706 | /* |
707 | * Don't return early if parsing the first option fails - if | |
708 | * we are doing multiple arguments, we can allow the first one | |
709 | * being empty. | |
710 | */ | |
83349190 | 711 | __ret = __handle_option(o, ptr, data, !done, !!ptr2, done); |
b62bdf2c JA |
712 | if (ret) |
713 | ret = __ret; | |
787f7e95 | 714 | |
b62bdf2c JA |
715 | if (!ptr2) |
716 | break; | |
f90eff5a | 717 | |
b62bdf2c JA |
718 | ptr = ptr2 + 1; |
719 | done++; | |
720 | } while (1); | |
787f7e95 | 721 | |
b62bdf2c JA |
722 | if (o_ptr) |
723 | free(o_ptr); | |
724 | return ret; | |
f90eff5a JA |
725 | } |
726 | ||
3b8b7135 | 727 | static struct fio_option *get_option(const char *opt, |
07b3232d | 728 | struct fio_option *options, char **post) |
3b8b7135 JA |
729 | { |
730 | struct fio_option *o; | |
731 | char *ret; | |
732 | ||
733 | ret = strchr(opt, '='); | |
734 | if (ret) { | |
735 | *post = ret; | |
736 | *ret = '\0'; | |
737 | ret = (char *) opt; | |
738 | (*post)++; | |
43c129b4 | 739 | strip_blank_end(ret); |
07b3232d | 740 | o = find_option(options, ret); |
3b8b7135 | 741 | } else { |
07b3232d | 742 | o = find_option(options, opt); |
3b8b7135 JA |
743 | *post = NULL; |
744 | } | |
745 | ||
746 | return o; | |
747 | } | |
748 | ||
749 | static int opt_cmp(const void *p1, const void *p2) | |
750 | { | |
751 | struct fio_option *o1, *o2; | |
752 | char *s1, *s2, *foo; | |
8cdabc1d | 753 | int prio1, prio2; |
3b8b7135 JA |
754 | |
755 | s1 = strdup(*((char **) p1)); | |
756 | s2 = strdup(*((char **) p2)); | |
757 | ||
07b3232d JA |
758 | o1 = get_option(s1, fio_options, &foo); |
759 | o2 = get_option(s2, fio_options, &foo); | |
0b9d69ec | 760 | |
8cdabc1d JA |
761 | prio1 = prio2 = 0; |
762 | if (o1) | |
763 | prio1 = o1->prio; | |
764 | if (o2) | |
765 | prio2 = o2->prio; | |
3b8b7135 JA |
766 | |
767 | free(s1); | |
768 | free(s2); | |
8cdabc1d | 769 | return prio2 - prio1; |
3b8b7135 JA |
770 | } |
771 | ||
772 | void sort_options(char **opts, struct fio_option *options, int num_opts) | |
773 | { | |
774 | fio_options = options; | |
775 | qsort(opts, num_opts, sizeof(char *), opt_cmp); | |
776 | fio_options = NULL; | |
777 | } | |
778 | ||
b4692828 | 779 | int parse_cmd_option(const char *opt, const char *val, |
07b3232d | 780 | struct fio_option *options, void *data) |
b4692828 JA |
781 | { |
782 | struct fio_option *o; | |
783 | ||
07b3232d | 784 | o = find_option(options, opt); |
b4692828 | 785 | if (!o) { |
06b0be6e | 786 | log_err("Bad option <%s>\n", opt); |
b4692828 JA |
787 | return 1; |
788 | } | |
789 | ||
b1508cf9 JA |
790 | if (!handle_option(o, val, data)) |
791 | return 0; | |
792 | ||
06b0be6e | 793 | log_err("fio: failed parsing %s=%s\n", opt, val); |
b1508cf9 | 794 | return 1; |
b4692828 JA |
795 | } |
796 | ||
88b5a391 AC |
797 | /* |
798 | * Return a copy of the input string with substrings of the form ${VARNAME} | |
799 | * substituted with the value of the environment variable VARNAME. The | |
800 | * substitution always occurs, even if VARNAME is empty or the corresponding | |
801 | * environment variable undefined. | |
802 | */ | |
803 | static char *option_dup_subs(const char *opt) | |
804 | { | |
805 | char out[OPT_LEN_MAX+1]; | |
806 | char in[OPT_LEN_MAX+1]; | |
807 | char *outptr = out; | |
808 | char *inptr = in; | |
809 | char *ch1, *ch2, *env; | |
810 | ssize_t nchr = OPT_LEN_MAX; | |
811 | size_t envlen; | |
812 | ||
a0741cbb | 813 | if (strlen(opt) + 1 > OPT_LEN_MAX) { |
38789b58 JA |
814 | fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX); |
815 | return NULL; | |
816 | } | |
817 | ||
88b5a391 AC |
818 | in[OPT_LEN_MAX] = '\0'; |
819 | strncpy(in, opt, OPT_LEN_MAX); | |
820 | ||
821 | while (*inptr && nchr > 0) { | |
822 | if (inptr[0] == '$' && inptr[1] == '{') { | |
823 | ch2 = strchr(inptr, '}'); | |
824 | if (ch2 && inptr+1 < ch2) { | |
825 | ch1 = inptr+2; | |
826 | inptr = ch2+1; | |
827 | *ch2 = '\0'; | |
828 | ||
829 | env = getenv(ch1); | |
830 | if (env) { | |
831 | envlen = strlen(env); | |
832 | if (envlen <= nchr) { | |
833 | memcpy(outptr, env, envlen); | |
834 | outptr += envlen; | |
835 | nchr -= envlen; | |
836 | } | |
837 | } | |
838 | ||
839 | continue; | |
840 | } | |
841 | } | |
842 | ||
843 | *outptr++ = *inptr++; | |
844 | --nchr; | |
845 | } | |
846 | ||
847 | *outptr = '\0'; | |
848 | return strdup(out); | |
849 | } | |
850 | ||
07b3232d | 851 | int parse_option(const char *opt, struct fio_option *options, void *data) |
cb2c86fd | 852 | { |
b4692828 | 853 | struct fio_option *o; |
3b8b7135 | 854 | char *post, *tmp; |
e1f36503 | 855 | |
88b5a391 | 856 | tmp = option_dup_subs(opt); |
38789b58 JA |
857 | if (!tmp) |
858 | return 1; | |
e1f36503 | 859 | |
07b3232d | 860 | o = get_option(tmp, options, &post); |
e1f36503 | 861 | if (!o) { |
06b0be6e | 862 | log_err("Bad option <%s>\n", tmp); |
0401bca6 | 863 | free(tmp); |
e1f36503 JA |
864 | return 1; |
865 | } | |
866 | ||
0401bca6 JA |
867 | if (!handle_option(o, post, data)) { |
868 | free(tmp); | |
b1508cf9 | 869 | return 0; |
0401bca6 | 870 | } |
b1508cf9 | 871 | |
06b0be6e | 872 | log_err("fio: failed parsing %s\n", opt); |
0401bca6 | 873 | free(tmp); |
b1508cf9 | 874 | return 1; |
e1f36503 | 875 | } |
fd28ca49 | 876 | |
0e9f7fac JA |
877 | /* |
878 | * Option match, levenshtein distance. Handy for not quite remembering what | |
879 | * the option name is. | |
880 | */ | |
881 | static int string_distance(const char *s1, const char *s2) | |
882 | { | |
883 | unsigned int s1_len = strlen(s1); | |
884 | unsigned int s2_len = strlen(s2); | |
885 | unsigned int *p, *q, *r; | |
886 | unsigned int i, j; | |
887 | ||
888 | p = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
889 | q = malloc(sizeof(unsigned int) * (s2_len + 1)); | |
890 | ||
891 | p[0] = 0; | |
892 | for (i = 1; i <= s2_len; i++) | |
893 | p[i] = p[i - 1] + 1; | |
894 | ||
895 | for (i = 1; i <= s1_len; i++) { | |
896 | q[0] = p[0] + 1; | |
897 | for (j = 1; j <= s2_len; j++) { | |
898 | unsigned int sub = p[j - 1]; | |
899 | ||
900 | if (s1[i - 1] != s2[j - 1]) | |
901 | sub++; | |
902 | ||
903 | q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub)); | |
904 | } | |
905 | r = p; | |
906 | p = q; | |
907 | q = r; | |
908 | } | |
909 | ||
910 | i = p[s2_len]; | |
911 | free(p); | |
912 | free(q); | |
913 | return i; | |
914 | } | |
915 | ||
afdf9352 JA |
916 | static struct fio_option *find_child(struct fio_option *options, |
917 | struct fio_option *o) | |
918 | { | |
919 | struct fio_option *__o; | |
920 | ||
fdf28744 JA |
921 | for (__o = options + 1; __o->name; __o++) |
922 | if (__o->parent && !strcmp(__o->parent, o->name)) | |
afdf9352 JA |
923 | return __o; |
924 | ||
925 | return NULL; | |
926 | } | |
927 | ||
323d9113 JA |
928 | static void __print_option(struct fio_option *o, struct fio_option *org, |
929 | int level) | |
afdf9352 JA |
930 | { |
931 | char name[256], *p; | |
323d9113 | 932 | int depth; |
afdf9352 JA |
933 | |
934 | if (!o) | |
935 | return; | |
ef9aff52 JA |
936 | if (!org) |
937 | org = o; | |
5ec10eaa | 938 | |
afdf9352 | 939 | p = name; |
323d9113 JA |
940 | depth = level; |
941 | while (depth--) | |
942 | p += sprintf(p, "%s", " "); | |
afdf9352 JA |
943 | |
944 | sprintf(p, "%s", o->name); | |
945 | ||
946 | printf("%-24s: %s\n", name, o->help); | |
323d9113 JA |
947 | } |
948 | ||
949 | static void print_option(struct fio_option *o) | |
950 | { | |
951 | struct fio_option *parent; | |
952 | struct fio_option *__o; | |
953 | unsigned int printed; | |
954 | unsigned int level; | |
955 | ||
956 | __print_option(o, NULL, 0); | |
957 | parent = o; | |
958 | level = 0; | |
959 | do { | |
960 | level++; | |
961 | printed = 0; | |
962 | ||
963 | while ((__o = find_child(o, parent)) != NULL) { | |
964 | __print_option(__o, o, level); | |
965 | o = __o; | |
966 | printed++; | |
967 | } | |
968 | ||
969 | parent = o; | |
970 | } while (printed); | |
afdf9352 JA |
971 | } |
972 | ||
07b3232d | 973 | int show_cmd_help(struct fio_option *options, const char *name) |
0e9f7fac JA |
974 | { |
975 | struct fio_option *o, *closest; | |
d091d099 | 976 | unsigned int best_dist = -1U; |
29fc6afe | 977 | int found = 0; |
320beefe JA |
978 | int show_all = 0; |
979 | ||
980 | if (!name || !strcmp(name, "all")) | |
981 | show_all = 1; | |
fd28ca49 | 982 | |
0e9f7fac JA |
983 | closest = NULL; |
984 | best_dist = -1; | |
4945ba12 | 985 | for (o = &options[0]; o->name; o++) { |
320beefe JA |
986 | int match = 0; |
987 | ||
15ca150e JA |
988 | if (o->type == FIO_OPT_DEPRECATED) |
989 | continue; | |
07b3232d JA |
990 | if (!exec_profile && o->prof_name) |
991 | continue; | |
15ca150e | 992 | |
0e9f7fac | 993 | if (name) { |
7f9348f8 JA |
994 | if (!strcmp(name, o->name) || |
995 | (o->alias && !strcmp(name, o->alias))) | |
0e9f7fac JA |
996 | match = 1; |
997 | else { | |
998 | unsigned int dist; | |
999 | ||
1000 | dist = string_distance(name, o->name); | |
1001 | if (dist < best_dist) { | |
1002 | best_dist = dist; | |
1003 | closest = o; | |
1004 | } | |
1005 | } | |
1006 | } | |
fd28ca49 JA |
1007 | |
1008 | if (show_all || match) { | |
29fc6afe | 1009 | found = 1; |
c167dedc | 1010 | if (match) |
07b3232d | 1011 | printf("%20s: %s\n", o->name, o->help); |
c167dedc | 1012 | if (show_all) { |
afdf9352 | 1013 | if (!o->parent) |
323d9113 | 1014 | print_option(o); |
4945ba12 | 1015 | continue; |
c167dedc | 1016 | } |
fd28ca49 JA |
1017 | } |
1018 | ||
70df2f19 JA |
1019 | if (!match) |
1020 | continue; | |
1021 | ||
06b0be6e | 1022 | show_option_help(o, 0); |
fd28ca49 JA |
1023 | } |
1024 | ||
29fc6afe JA |
1025 | if (found) |
1026 | return 0; | |
fd28ca49 | 1027 | |
0e9f7fac | 1028 | printf("No such command: %s", name); |
d091d099 JA |
1029 | |
1030 | /* | |
1031 | * Only print an appropriately close option, one where the edit | |
1032 | * distance isn't too big. Otherwise we get crazy matches. | |
1033 | */ | |
1034 | if (closest && best_dist < 3) { | |
0e9f7fac JA |
1035 | printf(" - showing closest match\n"); |
1036 | printf("%20s: %s\n", closest->name, closest->help); | |
06b0be6e | 1037 | show_option_help(closest, 0); |
0e9f7fac JA |
1038 | } else |
1039 | printf("\n"); | |
1040 | ||
29fc6afe | 1041 | return 1; |
fd28ca49 | 1042 | } |
ee738499 | 1043 | |
13335ddb JA |
1044 | /* |
1045 | * Handle parsing of default parameters. | |
1046 | */ | |
ee738499 JA |
1047 | void fill_default_options(void *data, struct fio_option *options) |
1048 | { | |
4945ba12 | 1049 | struct fio_option *o; |
ee738499 | 1050 | |
a3d741fa JA |
1051 | dprint(FD_PARSE, "filling default options\n"); |
1052 | ||
4945ba12 | 1053 | for (o = &options[0]; o->name; o++) |
ee738499 JA |
1054 | if (o->def) |
1055 | handle_option(o, o->def, data); | |
ee738499 | 1056 | } |
13335ddb | 1057 | |
9f988e2e JA |
1058 | void option_init(struct fio_option *o) |
1059 | { | |
1060 | if (o->type == FIO_OPT_DEPRECATED) | |
1061 | return; | |
1062 | if (o->type == FIO_OPT_BOOL) { | |
1063 | o->minval = 0; | |
1064 | o->maxval = 1; | |
1065 | } | |
83349190 YH |
1066 | if (o->type == FIO_OPT_FLOAT_LIST) { |
1067 | o->minfp = NAN; | |
1068 | o->maxfp = NAN; | |
1069 | } | |
9f988e2e | 1070 | if (o->type == FIO_OPT_STR_SET && o->def) { |
06b0be6e | 1071 | log_err("Option %s: string set option with" |
9f988e2e JA |
1072 | " default will always be true\n", o->name); |
1073 | } | |
06b0be6e JA |
1074 | if (!o->cb && (!o->off1 && !o->roff1)) |
1075 | log_err("Option %s: neither cb nor offset given\n", o->name); | |
5f6ddf1e JA |
1076 | if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE || |
1077 | o->type == FIO_OPT_STR_MULTI) | |
9f988e2e | 1078 | return; |
e2de69da JA |
1079 | if (o->cb && ((o->off1 || o->off2 || o->off3 || o->off4) || |
1080 | (o->roff1 || o->roff2 || o->roff3 || o->roff4))) { | |
06b0be6e | 1081 | log_err("Option %s: both cb and offset given\n", o->name); |
9f988e2e JA |
1082 | } |
1083 | } | |
1084 | ||
13335ddb JA |
1085 | /* |
1086 | * Sanitize the options structure. For now it just sets min/max for bool | |
5b0a8880 | 1087 | * values and whether both callback and offsets are given. |
13335ddb JA |
1088 | */ |
1089 | void options_init(struct fio_option *options) | |
1090 | { | |
4945ba12 | 1091 | struct fio_option *o; |
13335ddb | 1092 | |
a3d741fa JA |
1093 | dprint(FD_PARSE, "init options\n"); |
1094 | ||
9f988e2e JA |
1095 | for (o = &options[0]; o->name; o++) |
1096 | option_init(o); | |
13335ddb | 1097 | } |