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