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