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