blob: d44d13082c91dd8951cbe0be7edf2c51dd73d7e0 [file] [log] [blame]
Jens Axboecb2c86f2006-10-26 13:48:34 +02001/*
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>
Aaron Carroll88b5a392008-10-07 11:25:20 +020011#include <stdlib.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020012
13#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010014#include "debug.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020015
Jens Axboe3b8b7132008-06-10 19:46:23 +020016static struct fio_option *fio_options;
Jens Axboed6978a32009-07-18 21:04:09 +020017extern unsigned int fio_get_kb_base(void *);
Jens Axboe3b8b7132008-06-10 19:46:23 +020018
Jens Axboef0857372007-03-19 13:15:23 +010019static 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
27static 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
Jens Axboed447a8c2009-07-17 22:26:15 +020045static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010046{
47 if (!o->minval && !o->maxval)
48 return;
49
Jens Axboed447a8c2009-07-17 22:26:15 +020050 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020051 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020052 fprintf(out, ", max=%d", o->maxval);
53 fprintf(out, "\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010054}
55
56static void show_option_values(struct fio_option *o)
57{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010058 int i = 0;
59
60 do {
Jens Axboe78372132007-03-14 13:24:07 +010061 const struct value_pair *vp = &o->posval[i];
62
63 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010064 break;
65
Jens Axboe78372132007-03-14 13:24:07 +010066 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
67 if (vp->help)
68 printf(" %s", vp->help);
69 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010070 i++;
71 } while (i < PARSE_MAX_VP);
72
73 if (i)
74 printf("\n");
75}
76
Jens Axboed447a8c2009-07-17 22:26:15 +020077static 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
Jens Axboecb2c86f2006-10-26 13:48:34 +020099static unsigned long get_mult_time(char c)
100{
101 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100102 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;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200113 }
114}
115
Jens Axboed6978a32009-07-18 21:04:09 +0200116static unsigned long long get_mult_bytes(char c, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117{
Jens Axboed6978a32009-07-18 21:04:09 +0200118 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200119 unsigned long long ret = 1;
120
Jens Axboecb2c86f2006-10-26 13:48:34 +0200121 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200122 default:
123 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200124 case 'p':
125 case 'P':
Jens Axboed6978a32009-07-18 21:04:09 +0200126 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200127 case 't':
128 case 'T':
Jens Axboed6978a32009-07-18 21:04:09 +0200129 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200130 case 'g':
131 case 'G':
Jens Axboed6978a32009-07-18 21:04:09 +0200132 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200133 case 'm':
134 case 'M':
Jens Axboed6978a32009-07-18 21:04:09 +0200135 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200136 case 'k':
137 case 'K':
Jens Axboed6978a32009-07-18 21:04:09 +0200138 ret *= (unsigned long long) kb_base;
Jens Axboea639f0b2009-07-18 08:25:35 +0200139 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200140 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200141
142 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200143}
144
145/*
Jens Axboee1f36502006-10-27 10:54:08 +0200146 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147 */
Jens Axboed6978a32009-07-18 21:04:09 +0200148int str_to_decimal(const char *str, long long *val, int kilo, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200149{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100150 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200151
Jens Axboecb2c86f2006-10-26 13:48:34 +0200152 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100153 if (!len)
154 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200155
Jens Axboeb347f9d2009-03-09 14:15:21 +0100156 if (strstr(str, "0x") || strstr(str, "0X"))
157 base = 16;
158 else
159 base = 10;
160
161 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100162 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200163 return 1;
164
Jens Axboe975462a2009-12-23 08:54:52 +0100165 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
Jens Axboecb2c86f2006-10-26 13:48:34 +0200178 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100179
Jens Axboecb2c86f2006-10-26 13:48:34 +0200180 return 0;
181}
182
Jens Axboed6978a32009-07-18 21:04:09 +0200183static int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200184{
Jens Axboed6978a32009-07-18 21:04:09 +0200185 return str_to_decimal(p, val, 1, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200186}
187
Jens Axboe63f29372007-01-10 13:20:09 +0100188static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200189{
Jens Axboed6978a32009-07-18 21:04:09 +0200190 return str_to_decimal(p, val, 0, NULL);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191}
192
193void strip_blank_front(char **p)
194{
195 char *s = *p;
196
197 while (isspace(*s))
198 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200199
200 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200201}
202
203void strip_blank_end(char *p)
204{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100205 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200206
Jens Axboe523bfad2007-04-04 11:04:06 +0200207 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
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200216 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100217 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200218 s--;
219
220 *(s + 1) = '\0';
221}
222
Jens Axboed6978a32009-07-18 21:04:09 +0200223static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200224{
225 char suffix;
226
Jens Axboe787f7e92006-11-06 13:26:29 +0100227 if (!strlen(str))
228 return 1;
229
Jens Axboecb2c86f2006-10-26 13:48:34 +0200230 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
Jens Axboed6978a32009-07-18 21:04:09 +0200231 *val *= get_mult_bytes(suffix, data);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200232 return 0;
233 }
234
235 if (sscanf(str, "%lu", val) == 1)
236 return 0;
237
238 return 1;
239}
240
Jens Axboe63f29372007-01-10 13:20:09 +0100241static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200242{
Jens Axboe787f7e92006-11-06 13:26:29 +0100243 if (!strlen(p))
244 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200245 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200246 if (sscanf(p, "%x", val) == 1)
247 return 0;
248 } else {
249 if (sscanf(p, "%u", val) == 1)
250 return 0;
251 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200252
253 return 1;
254}
255
Jens Axboee1f36502006-10-27 10:54:08 +0200256static struct fio_option *find_option(struct fio_option *options,
257 const char *opt)
258{
Jens Axboe4945ba12007-01-11 14:36:56 +0100259 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200260
Jens Axboe4945ba12007-01-11 14:36:56 +0100261 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200262 if (!strcmp(o->name, opt))
263 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100264 else if (o->alias && !strcmp(o->alias, opt))
265 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200266 }
Jens Axboee1f36502006-10-27 10:54:08 +0200267
268 return NULL;
269}
270
Jens Axboe17abbe82006-11-06 11:23:10 +0100271#define val_store(ptr, val, off, data) \
272 do { \
273 ptr = td_var((data), (off)); \
274 *ptr = (val); \
275 } while (0)
276
Jens Axboef90eff52006-11-06 11:08:21 +0100277static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100278 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200279{
Jens Axboe63f29372007-01-10 13:20:09 +0100280 int il, *ilp;
281 long long ull, *ullp;
282 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200283 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200284 int ret = 0, is_time = 0;
285
Jens Axboea3d741f2008-02-27 18:32:33 +0100286 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
287 o->type, ptr);
288
Jens Axboee3cedca2008-11-19 19:57:52 +0100289 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100290 fprintf(stderr, "Option %s requires an argument\n", o->name);
291 return 1;
292 }
293
Jens Axboee1f36502006-10-27 10:54:08 +0200294 switch (o->type) {
295 case FIO_OPT_STR: {
296 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100297 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100298 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100299 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200300
Jens Axboef0857372007-03-19 13:15:23 +0100301 posval_sort(o, posval);
302
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100303 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100304 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100305 if (!vp->ival || vp->ival[0] == '\0')
306 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100307 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100308 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 }
316
317 if (ret)
318 show_option_values(o);
319 else if (fn)
320 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200321 break;
322 }
323 case FIO_OPT_STR_VAL_TIME:
324 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100325 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200326 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200327 fio_opt_str_val_fn *fn = o->cb;
328
329 if (is_time)
330 ret = check_str_time(ptr, &ull);
331 else
Jens Axboed6978a32009-07-18 21:04:09 +0200332 ret = check_str_bytes(ptr, &ull, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200333
334 if (ret)
335 break;
336
Jens Axboedb8e0162007-01-10 13:41:26 +0100337 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100338 fprintf(stderr, "max value out of range: %lld"
339 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100340 return 1;
341 }
342 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100343 fprintf(stderr, "min value out of range: %lld"
344 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100345 return 1;
346 }
Jens Axboee1f36502006-10-27 10:54:08 +0200347
348 if (fn)
349 ret = fn(data, &ull);
350 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200351 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100352 if (first)
353 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100354 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100355 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100356 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100357 if (first)
358 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100359 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100360 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100361 }
Jens Axboee1f36502006-10-27 10:54:08 +0200362 }
363 break;
364 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100365 case FIO_OPT_STR_STORE: {
366 fio_opt_str_fn *fn = o->cb;
367
Jens Axboee1f36502006-10-27 10:54:08 +0200368 cp = td_var(data, o->off1);
369 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100370 if (fn) {
371 ret = fn(data, ptr);
372 if (ret) {
373 free(*cp);
374 *cp = NULL;
375 }
376 }
Jens Axboee1f36502006-10-27 10:54:08 +0200377 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100378 }
Jens Axboee1f36502006-10-27 10:54:08 +0200379 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200380 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200381 char *p1, *p2;
382
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100383 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200384
385 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200386 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100387 p1 = strchr(tmp, ':');
388 if (!p1) {
389 ret = 1;
390 break;
391 }
Jens Axboee1f36502006-10-27 10:54:08 +0200392 }
393
394 p2 = p1 + 1;
395 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200396 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200397
398 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200399 if (!check_range_bytes(p1, &ul1, data) &&
400 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200401 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200402 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100403 unsigned long foo = ul1;
404
405 ul1 = ul2;
406 ul2 = foo;
407 }
408
409 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100410 val_store(ilp, ul1, o->off1, data);
411 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200412 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100413 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100414 val_store(ilp, ul1, o->off3, data);
415 val_store(ilp, ul2, o->off4, data);
416 }
417 }
418
Jens Axboee1f36502006-10-27 10:54:08 +0200419 break;
420 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100421 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200422 fio_opt_int_fn *fn = o->cb;
423
424 ret = check_int(ptr, &il);
425 if (ret)
426 break;
427
Jens Axboedb8e0162007-01-10 13:41:26 +0100428 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100429 fprintf(stderr, "max value out of range: %d (%d max)\n",
430 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100431 return 1;
432 }
433 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100434 fprintf(stderr, "min value out of range: %d (%d min)\n",
435 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100436 return 1;
437 }
Jens Axboee1f36502006-10-27 10:54:08 +0200438
Jens Axboe76a43db2007-01-11 13:24:44 +0100439 if (o->neg)
440 il = !il;
441
Jens Axboee1f36502006-10-27 10:54:08 +0200442 if (fn)
443 ret = fn(data, &il);
444 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100445 if (first)
446 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100447 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100448 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200449 }
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 {
Jens Axboe17abbe82006-11-06 11:23:10 +0100458 if (first)
459 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100460 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100461 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200462 }
463 break;
464 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200465 case FIO_OPT_DEPRECATED:
466 fprintf(stdout, "Option %s is deprecated\n", o->name);
467 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200468 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100469 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200470 ret = 1;
471 }
472
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200473 if (ret)
474 return ret;
475
Jens Axboed447a8c2009-07-17 22:26:15 +0200476 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200477 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200478 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 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200484
Jens Axboee1f36502006-10-27 10:54:08 +0200485 return ret;
486}
487
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200488static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100489{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200490 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100491 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100492
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200493 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
494
495 ptr = NULL;
496 if (__ptr)
497 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100498
Jens Axboef90eff52006-11-06 11:08:21 +0100499 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100500 * 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.
Jens Axboef90eff52006-11-06 11:08:21 +0100503 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100504 if (ptr &&
505 (o->type != FIO_OPT_STR_STORE) &&
506 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100507 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200508 if (ptr2 && *(ptr2 + 1) == '\0')
509 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100510 if (!ptr2)
511 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100512 if (!ptr2)
513 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100514 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100515
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
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200523 if (!ptr2) {
524 if (ptr)
525 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100526 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200527 }
Jens Axboef90eff52006-11-06 11:08:21 +0100528
529 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100530 r2 = __handle_option(o, ptr2, data, 0, 0);
531
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200532 if (ptr)
533 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100534 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100535}
536
Jens Axboe3b8b7132008-06-10 19:46:23 +0200537static 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)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100549 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200550 o = find_option(options, ret);
551 } else {
552 o = find_option(options, opt);
553 *post = NULL;
554 }
555
556 return o;
557}
558
559static int opt_cmp(const void *p1, const void *p2)
560{
561 struct fio_option *o1, *o2;
562 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100563 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200564
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);
Jens Axboe0b9d69e2009-09-11 22:29:54 +0200570
Jens Axboe8cdabc12008-11-19 12:31:43 +0100571 prio1 = prio2 = 0;
572 if (o1)
573 prio1 = o1->prio;
574 if (o2)
575 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200576
577 free(s1);
578 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100579 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200580}
581
582void 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
Jens Axboeb4692822006-10-27 13:43:22 +0200589int 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) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100596 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200597 return 1;
598 }
599
Jens Axboeb1508cf2006-11-02 09:12:40 +0100600 if (!handle_option(o, val, data))
601 return 0;
602
603 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
604 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200605}
606
Aaron Carroll88b5a392008-10-07 11:25:20 +0200607/*
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 */
613static 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
Jens Axboe38789b52010-03-03 09:23:20 +0100623 if (strlen(in) + 1 > OPT_LEN_MAX) {
624 fprintf(stderr, "OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
625 return NULL;
626 }
627
Aaron Carroll88b5a392008-10-07 11:25:20 +0200628 in[OPT_LEN_MAX] = '\0';
629 strncpy(in, opt, OPT_LEN_MAX);
630
631 while (*inptr && nchr > 0) {
632 if (inptr[0] == '$' && inptr[1] == '{') {
633 ch2 = strchr(inptr, '}');
634 if (ch2 && inptr+1 < ch2) {
635 ch1 = inptr+2;
636 inptr = ch2+1;
637 *ch2 = '\0';
638
639 env = getenv(ch1);
640 if (env) {
641 envlen = strlen(env);
642 if (envlen <= nchr) {
643 memcpy(outptr, env, envlen);
644 outptr += envlen;
645 nchr -= envlen;
646 }
647 }
648
649 continue;
650 }
651 }
652
653 *outptr++ = *inptr++;
654 --nchr;
655 }
656
657 *outptr = '\0';
658 return strdup(out);
659}
660
Jens Axboee1f36502006-10-27 10:54:08 +0200661int parse_option(const char *opt, struct fio_option *options, void *data)
662{
Jens Axboeb4692822006-10-27 13:43:22 +0200663 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200664 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200665
Aaron Carroll88b5a392008-10-07 11:25:20 +0200666 tmp = option_dup_subs(opt);
Jens Axboe38789b52010-03-03 09:23:20 +0100667 if (!tmp)
668 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200669
Jens Axboe3b8b7132008-06-10 19:46:23 +0200670 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200671 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100672 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200673 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200674 return 1;
675 }
676
Jens Axboe0401bca2007-03-29 11:00:43 +0200677 if (!handle_option(o, post, data)) {
678 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100679 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200680 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100681
682 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200683 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100684 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200685}
Jens Axboefd28ca42007-01-09 21:20:13 +0100686
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100687/*
688 * Option match, levenshtein distance. Handy for not quite remembering what
689 * the option name is.
690 */
691static int string_distance(const char *s1, const char *s2)
692{
693 unsigned int s1_len = strlen(s1);
694 unsigned int s2_len = strlen(s2);
695 unsigned int *p, *q, *r;
696 unsigned int i, j;
697
698 p = malloc(sizeof(unsigned int) * (s2_len + 1));
699 q = malloc(sizeof(unsigned int) * (s2_len + 1));
700
701 p[0] = 0;
702 for (i = 1; i <= s2_len; i++)
703 p[i] = p[i - 1] + 1;
704
705 for (i = 1; i <= s1_len; i++) {
706 q[0] = p[0] + 1;
707 for (j = 1; j <= s2_len; j++) {
708 unsigned int sub = p[j - 1];
709
710 if (s1[i - 1] != s2[j - 1])
711 sub++;
712
713 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
714 }
715 r = p;
716 p = q;
717 q = r;
718 }
719
720 i = p[s2_len];
721 free(p);
722 free(q);
723 return i;
724}
725
Jens Axboeafdf9352007-07-31 16:14:34 +0200726static struct fio_option *find_child(struct fio_option *options,
727 struct fio_option *o)
728{
729 struct fio_option *__o;
730
Jens Axboefdf28742007-07-31 23:06:09 +0200731 for (__o = options + 1; __o->name; __o++)
732 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200733 return __o;
734
735 return NULL;
736}
737
Jens Axboe323d9112008-03-01 15:12:14 +0100738static void __print_option(struct fio_option *o, struct fio_option *org,
739 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200740{
741 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100742 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200743
744 if (!o)
745 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200746 if (!org)
747 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100748
Jens Axboeafdf9352007-07-31 16:14:34 +0200749 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100750 depth = level;
751 while (depth--)
752 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200753
754 sprintf(p, "%s", o->name);
755
756 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100757}
758
759static void print_option(struct fio_option *o)
760{
761 struct fio_option *parent;
762 struct fio_option *__o;
763 unsigned int printed;
764 unsigned int level;
765
766 __print_option(o, NULL, 0);
767 parent = o;
768 level = 0;
769 do {
770 level++;
771 printed = 0;
772
773 while ((__o = find_child(o, parent)) != NULL) {
774 __print_option(__o, o, level);
775 o = __o;
776 printed++;
777 }
778
779 parent = o;
780 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200781}
782
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100783int show_cmd_help(struct fio_option *options, const char *name)
784{
785 struct fio_option *o, *closest;
786 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100787 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100788 int show_all = 0;
789
790 if (!name || !strcmp(name, "all"))
791 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100792
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100793 closest = NULL;
794 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100795 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100796 int match = 0;
797
Jens Axboe15ca1502008-04-07 09:26:02 +0200798 if (o->type == FIO_OPT_DEPRECATED)
799 continue;
800
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100801 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100802 if (!strcmp(name, o->name) ||
803 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100804 match = 1;
805 else {
806 unsigned int dist;
807
808 dist = string_distance(name, o->name);
809 if (dist < best_dist) {
810 best_dist = dist;
811 closest = o;
812 }
813 }
814 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100815
816 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100817 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100818 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200819 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100820 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200821 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100822 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100823 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100824 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100825 }
826
Jens Axboe70df2f12007-01-11 14:04:47 +0100827 if (!match)
828 continue;
829
Jens Axboed447a8c2009-07-17 22:26:15 +0200830 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100831 }
832
Jens Axboe29fc6af2007-01-09 21:22:02 +0100833 if (found)
834 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100835
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100836 printf("No such command: %s", name);
837 if (closest) {
838 printf(" - showing closest match\n");
839 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200840 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100841 } else
842 printf("\n");
843
Jens Axboe29fc6af2007-01-09 21:22:02 +0100844 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100845}
Jens Axboeee738492007-01-10 11:23:16 +0100846
Jens Axboe13335dd2007-01-10 13:14:02 +0100847/*
848 * Handle parsing of default parameters.
849 */
Jens Axboeee738492007-01-10 11:23:16 +0100850void fill_default_options(void *data, struct fio_option *options)
851{
Jens Axboe4945ba12007-01-11 14:36:56 +0100852 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100853
Jens Axboea3d741f2008-02-27 18:32:33 +0100854 dprint(FD_PARSE, "filling default options\n");
855
Jens Axboe4945ba12007-01-11 14:36:56 +0100856 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100857 if (o->def)
858 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100859}
Jens Axboe13335dd2007-01-10 13:14:02 +0100860
861/*
862 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100863 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100864 */
865void options_init(struct fio_option *options)
866{
Jens Axboe4945ba12007-01-11 14:36:56 +0100867 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100868
Jens Axboea3d741f2008-02-27 18:32:33 +0100869 dprint(FD_PARSE, "init options\n");
870
Jens Axboe4945ba12007-01-11 14:36:56 +0100871 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200872 if (o->type == FIO_OPT_DEPRECATED)
873 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100874 if (o->type == FIO_OPT_BOOL) {
875 o->minval = 0;
876 o->maxval = 1;
877 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100878 if (o->type == FIO_OPT_STR_SET && o->def) {
879 fprintf(stderr, "Option %s: string set option with"
880 " default will always be true\n",
881 o->name);
882 }
883 if (!o->cb && !o->off1) {
884 fprintf(stderr, "Option %s: neither cb nor offset"
885 " given\n", o->name);
886 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100887 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100888 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100889 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
890 fprintf(stderr, "Option %s: both cb and offset given\n",
891 o->name);
892 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100893 }
894}