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