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