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