blob: 5043374405d8a1e262687a5a1bdcac90f18990cf [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;
17
Jens Axboef0857372007-03-19 13:15:23 +010018static int vp_cmp(const void *p1, const void *p2)
19{
20 const struct value_pair *vp1 = p1;
21 const struct value_pair *vp2 = p2;
22
23 return strlen(vp2->ival) - strlen(vp1->ival);
24}
25
26static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
27{
28 const struct value_pair *vp;
29 int entries;
30
31 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
32
33 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
34 vp = &o->posval[entries];
35 if (!vp->ival || vp->ival[0] == '\0')
36 break;
37
38 memcpy(&vpmap[entries], vp, sizeof(*vp));
39 }
40
41 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
42}
43
Jens Axboeb1ec1da2007-02-23 10:29:16 +010044static void show_option_range(struct fio_option *o)
45{
46 if (!o->minval && !o->maxval)
47 return;
48
Jens Axboe39801272009-07-01 09:06:12 +020049 printf("%20s: min=%d", "range", o->minval);
50 if (o->maxval)
51 printf(", max=%d", o->maxval);
52 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010053}
54
55static void show_option_values(struct fio_option *o)
56{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010057 int i = 0;
58
59 do {
Jens Axboe78372132007-03-14 13:24:07 +010060 const struct value_pair *vp = &o->posval[i];
61
62 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010063 break;
64
Jens Axboe78372132007-03-14 13:24:07 +010065 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
66 if (vp->help)
67 printf(" %s", vp->help);
68 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010069 i++;
70 } while (i < PARSE_MAX_VP);
71
72 if (i)
73 printf("\n");
74}
75
Jens Axboecb2c86f2006-10-26 13:48:34 +020076static unsigned long get_mult_time(char c)
77{
78 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010079 case 'm':
80 case 'M':
81 return 60;
82 case 'h':
83 case 'H':
84 return 60 * 60;
85 case 'd':
86 case 'D':
87 return 24 * 60 * 60;
88 default:
89 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020090 }
91}
92
93static unsigned long get_mult_bytes(char c)
94{
95 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010096 case 'k':
97 case 'K':
98 return 1024;
99 case 'm':
100 case 'M':
101 return 1024 * 1024;
102 case 'g':
103 case 'G':
104 return 1024 * 1024 * 1024;
105 case 'e':
106 case 'E':
107 return 1024 * 1024 * 1024 * 1024UL;
108 default:
109 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200110 }
111}
112
113/*
Jens Axboee1f36502006-10-27 10:54:08 +0200114 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200115 */
Jens Axboe564ca972007-12-14 12:21:19 +0100116int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100118 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200119
Jens Axboecb2c86f2006-10-26 13:48:34 +0200120 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100121 if (!len)
122 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200123
Jens Axboeb347f9d2009-03-09 14:15:21 +0100124 if (strstr(str, "0x") || strstr(str, "0X"))
125 base = 16;
126 else
127 base = 10;
128
129 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100130 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200131 return 1;
132
133 if (kilo)
134 *val *= get_mult_bytes(str[len - 1]);
135 else
136 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100137
Jens Axboecb2c86f2006-10-26 13:48:34 +0200138 return 0;
139}
140
Jens Axboe63f29372007-01-10 13:20:09 +0100141static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200142{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200143 return str_to_decimal(p, val, 1);
144}
145
Jens Axboe63f29372007-01-10 13:20:09 +0100146static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148 return str_to_decimal(p, val, 0);
149}
150
151void strip_blank_front(char **p)
152{
153 char *s = *p;
154
155 while (isspace(*s))
156 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200157
158 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200159}
160
161void strip_blank_end(char *p)
162{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100163 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200164
Jens Axboe523bfad2007-04-04 11:04:06 +0200165 s = strchr(p, ';');
166 if (s)
167 *s = '\0';
168 s = strchr(p, '#');
169 if (s)
170 *s = '\0';
171 if (s)
172 p = s;
173
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200174 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100175 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200176 s--;
177
178 *(s + 1) = '\0';
179}
180
Jens Axboe63f29372007-01-10 13:20:09 +0100181static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200182{
183 char suffix;
184
Jens Axboe787f7e92006-11-06 13:26:29 +0100185 if (!strlen(str))
186 return 1;
187
Jens Axboecb2c86f2006-10-26 13:48:34 +0200188 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
189 *val *= get_mult_bytes(suffix);
190 return 0;
191 }
192
193 if (sscanf(str, "%lu", val) == 1)
194 return 0;
195
196 return 1;
197}
198
Jens Axboe63f29372007-01-10 13:20:09 +0100199static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200200{
Jens Axboe787f7e92006-11-06 13:26:29 +0100201 if (!strlen(p))
202 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200203 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200204 if (sscanf(p, "%x", val) == 1)
205 return 0;
206 } else {
207 if (sscanf(p, "%u", val) == 1)
208 return 0;
209 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200210
211 return 1;
212}
213
Jens Axboee1f36502006-10-27 10:54:08 +0200214static struct fio_option *find_option(struct fio_option *options,
215 const char *opt)
216{
Jens Axboe4945ba12007-01-11 14:36:56 +0100217 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200218
Jens Axboe4945ba12007-01-11 14:36:56 +0100219 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200220 if (!strcmp(o->name, opt))
221 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100222 else if (o->alias && !strcmp(o->alias, opt))
223 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200224 }
Jens Axboee1f36502006-10-27 10:54:08 +0200225
226 return NULL;
227}
228
Jens Axboe17abbe82006-11-06 11:23:10 +0100229#define val_store(ptr, val, off, data) \
230 do { \
231 ptr = td_var((data), (off)); \
232 *ptr = (val); \
233 } while (0)
234
Jens Axboef90eff52006-11-06 11:08:21 +0100235static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100236 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200237{
Jens Axboe63f29372007-01-10 13:20:09 +0100238 int il, *ilp;
239 long long ull, *ullp;
240 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200241 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200242 int ret = 0, is_time = 0;
243
Jens Axboea3d741f2008-02-27 18:32:33 +0100244 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
245 o->type, ptr);
246
Jens Axboee3cedca2008-11-19 19:57:52 +0100247 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100248 fprintf(stderr, "Option %s requires an argument\n", o->name);
249 return 1;
250 }
251
Jens Axboee1f36502006-10-27 10:54:08 +0200252 switch (o->type) {
253 case FIO_OPT_STR: {
254 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100256 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100257 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200258
Jens Axboef0857372007-03-19 13:15:23 +0100259 posval_sort(o, posval);
260
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100261 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100262 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100263 if (!vp->ival || vp->ival[0] == '\0')
264 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100265 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100266 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
267 ret = 0;
268 if (!o->off1)
269 break;
270 val_store(ilp, vp->oval, o->off1, data);
271 break;
272 }
273 }
274
275 if (ret)
276 show_option_values(o);
277 else if (fn)
278 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200279 break;
280 }
281 case FIO_OPT_STR_VAL_TIME:
282 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100283 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200284 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200285 fio_opt_str_val_fn *fn = o->cb;
286
287 if (is_time)
288 ret = check_str_time(ptr, &ull);
289 else
290 ret = check_str_bytes(ptr, &ull);
291
292 if (ret)
293 break;
294
Jens Axboedb8e0162007-01-10 13:41:26 +0100295 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100296 fprintf(stderr, "max value out of range: %lld"
297 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100298 return 1;
299 }
300 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100301 fprintf(stderr, "min value out of range: %lld"
302 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100303 return 1;
304 }
Jens Axboee1f36502006-10-27 10:54:08 +0200305
306 if (fn)
307 ret = fn(data, &ull);
308 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200309 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100310 if (first)
311 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100312 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100313 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100314 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100315 if (first)
316 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100317 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100318 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100319 }
Jens Axboee1f36502006-10-27 10:54:08 +0200320 }
321 break;
322 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100323 case FIO_OPT_STR_STORE: {
324 fio_opt_str_fn *fn = o->cb;
325
Jens Axboee1f36502006-10-27 10:54:08 +0200326 cp = td_var(data, o->off1);
327 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100328 if (fn) {
329 ret = fn(data, ptr);
330 if (ret) {
331 free(*cp);
332 *cp = NULL;
333 }
334 }
Jens Axboee1f36502006-10-27 10:54:08 +0200335 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100336 }
Jens Axboee1f36502006-10-27 10:54:08 +0200337 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200338 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200339 char *p1, *p2;
340
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100341 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200342
343 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200344 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100345 p1 = strchr(tmp, ':');
346 if (!p1) {
347 ret = 1;
348 break;
349 }
Jens Axboee1f36502006-10-27 10:54:08 +0200350 }
351
352 p2 = p1 + 1;
353 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200354 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200355
356 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100357 if (!check_range_bytes(p1, &ul1) &&
358 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200359 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200360 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100361 unsigned long foo = ul1;
362
363 ul1 = ul2;
364 ul2 = foo;
365 }
366
367 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100368 val_store(ilp, ul1, o->off1, data);
369 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200370 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100371 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100372 val_store(ilp, ul1, o->off3, data);
373 val_store(ilp, ul2, o->off4, data);
374 }
375 }
376
Jens Axboee1f36502006-10-27 10:54:08 +0200377 break;
378 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100379 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200380 fio_opt_int_fn *fn = o->cb;
381
382 ret = check_int(ptr, &il);
383 if (ret)
384 break;
385
Jens Axboedb8e0162007-01-10 13:41:26 +0100386 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100387 fprintf(stderr, "max value out of range: %d (%d max)\n",
388 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100389 return 1;
390 }
391 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100392 fprintf(stderr, "min value out of range: %d (%d min)\n",
393 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100394 return 1;
395 }
Jens Axboee1f36502006-10-27 10:54:08 +0200396
Jens Axboe76a43db2007-01-11 13:24:44 +0100397 if (o->neg)
398 il = !il;
399
Jens Axboee1f36502006-10-27 10:54:08 +0200400 if (fn)
401 ret = fn(data, &il);
402 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100403 if (first)
404 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100405 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100406 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200407 }
408 break;
409 }
410 case FIO_OPT_STR_SET: {
411 fio_opt_str_set_fn *fn = o->cb;
412
413 if (fn)
414 ret = fn(data);
415 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100416 if (first)
417 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100418 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100419 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200420 }
421 break;
422 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200423 case FIO_OPT_DEPRECATED:
424 fprintf(stdout, "Option %s is deprecated\n", o->name);
425 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200426 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100427 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200428 ret = 1;
429 }
430
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200431 if (ret)
432 return ret;
433
434 if (o->verify)
435 ret = o->verify(o, data);
436
Jens Axboee1f36502006-10-27 10:54:08 +0200437 return ret;
438}
439
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200440static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100441{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200442 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100443 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100444
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200445 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
446
447 ptr = NULL;
448 if (__ptr)
449 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100450
Jens Axboef90eff52006-11-06 11:08:21 +0100451 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100452 * See if we have a second set of parameters, hidden after a comma.
453 * Do this before parsing the first round, to check if we should
454 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100455 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100456 if (ptr &&
457 (o->type != FIO_OPT_STR_STORE) &&
458 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100459 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200460 if (ptr2 && *(ptr2 + 1) == '\0')
461 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100462 if (!ptr2)
463 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100464 if (!ptr2)
465 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100466 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100467
468 /*
469 * Don't return early if parsing the first option fails - if
470 * we are doing multiple arguments, we can allow the first one
471 * being empty.
472 */
473 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
474
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200475 if (!ptr2) {
476 if (ptr)
477 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100478 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200479 }
Jens Axboef90eff52006-11-06 11:08:21 +0100480
481 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100482 r2 = __handle_option(o, ptr2, data, 0, 0);
483
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200484 if (ptr)
485 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100486 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100487}
488
Jens Axboe3b8b7132008-06-10 19:46:23 +0200489static struct fio_option *get_option(const char *opt,
490 struct fio_option *options, char **post)
491{
492 struct fio_option *o;
493 char *ret;
494
495 ret = strchr(opt, '=');
496 if (ret) {
497 *post = ret;
498 *ret = '\0';
499 ret = (char *) opt;
500 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100501 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200502 o = find_option(options, ret);
503 } else {
504 o = find_option(options, opt);
505 *post = NULL;
506 }
507
508 return o;
509}
510
511static int opt_cmp(const void *p1, const void *p2)
512{
513 struct fio_option *o1, *o2;
514 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100515 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200516
517 s1 = strdup(*((char **) p1));
518 s2 = strdup(*((char **) p2));
519
520 o1 = get_option(s1, fio_options, &foo);
521 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100522
523 prio1 = prio2 = 0;
524 if (o1)
525 prio1 = o1->prio;
526 if (o2)
527 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200528
529 free(s1);
530 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100531 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200532}
533
534void sort_options(char **opts, struct fio_option *options, int num_opts)
535{
536 fio_options = options;
537 qsort(opts, num_opts, sizeof(char *), opt_cmp);
538 fio_options = NULL;
539}
540
Jens Axboeb4692822006-10-27 13:43:22 +0200541int parse_cmd_option(const char *opt, const char *val,
542 struct fio_option *options, void *data)
543{
544 struct fio_option *o;
545
546 o = find_option(options, opt);
547 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100548 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200549 return 1;
550 }
551
Jens Axboeb1508cf2006-11-02 09:12:40 +0100552 if (!handle_option(o, val, data))
553 return 0;
554
555 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
556 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200557}
558
Aaron Carroll88b5a392008-10-07 11:25:20 +0200559/*
560 * Return a copy of the input string with substrings of the form ${VARNAME}
561 * substituted with the value of the environment variable VARNAME. The
562 * substitution always occurs, even if VARNAME is empty or the corresponding
563 * environment variable undefined.
564 */
565static char *option_dup_subs(const char *opt)
566{
567 char out[OPT_LEN_MAX+1];
568 char in[OPT_LEN_MAX+1];
569 char *outptr = out;
570 char *inptr = in;
571 char *ch1, *ch2, *env;
572 ssize_t nchr = OPT_LEN_MAX;
573 size_t envlen;
574
575 in[OPT_LEN_MAX] = '\0';
576 strncpy(in, opt, OPT_LEN_MAX);
577
578 while (*inptr && nchr > 0) {
579 if (inptr[0] == '$' && inptr[1] == '{') {
580 ch2 = strchr(inptr, '}');
581 if (ch2 && inptr+1 < ch2) {
582 ch1 = inptr+2;
583 inptr = ch2+1;
584 *ch2 = '\0';
585
586 env = getenv(ch1);
587 if (env) {
588 envlen = strlen(env);
589 if (envlen <= nchr) {
590 memcpy(outptr, env, envlen);
591 outptr += envlen;
592 nchr -= envlen;
593 }
594 }
595
596 continue;
597 }
598 }
599
600 *outptr++ = *inptr++;
601 --nchr;
602 }
603
604 *outptr = '\0';
605 return strdup(out);
606}
607
Jens Axboee1f36502006-10-27 10:54:08 +0200608int parse_option(const char *opt, struct fio_option *options, void *data)
609{
Jens Axboeb4692822006-10-27 13:43:22 +0200610 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200611 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200612
Aaron Carroll88b5a392008-10-07 11:25:20 +0200613 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200614
Jens Axboe3b8b7132008-06-10 19:46:23 +0200615 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200616 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100617 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200618 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200619 return 1;
620 }
621
Jens Axboe0401bca2007-03-29 11:00:43 +0200622 if (!handle_option(o, post, data)) {
623 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100624 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200625 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100626
627 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200628 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100629 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200630}
Jens Axboefd28ca42007-01-09 21:20:13 +0100631
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100632/*
633 * Option match, levenshtein distance. Handy for not quite remembering what
634 * the option name is.
635 */
636static int string_distance(const char *s1, const char *s2)
637{
638 unsigned int s1_len = strlen(s1);
639 unsigned int s2_len = strlen(s2);
640 unsigned int *p, *q, *r;
641 unsigned int i, j;
642
643 p = malloc(sizeof(unsigned int) * (s2_len + 1));
644 q = malloc(sizeof(unsigned int) * (s2_len + 1));
645
646 p[0] = 0;
647 for (i = 1; i <= s2_len; i++)
648 p[i] = p[i - 1] + 1;
649
650 for (i = 1; i <= s1_len; i++) {
651 q[0] = p[0] + 1;
652 for (j = 1; j <= s2_len; j++) {
653 unsigned int sub = p[j - 1];
654
655 if (s1[i - 1] != s2[j - 1])
656 sub++;
657
658 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
659 }
660 r = p;
661 p = q;
662 q = r;
663 }
664
665 i = p[s2_len];
666 free(p);
667 free(q);
668 return i;
669}
670
671static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100672{
Jens Axboefd28ca42007-01-09 21:20:13 +0100673 const char *typehelp[] = {
674 "string (opt=bla)",
675 "string with possible k/m/g postfix (opt=4k)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100676 "string with time postfix (opt=10s)",
677 "string (opt=bla)",
678 "string with dual range (opt=1k-4k,4k-8k)",
679 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100680 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100681 "no argument (opt)",
682 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100683
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100684 if (o->alias)
685 printf("%20s: %s\n", "alias", o->alias);
686
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100687 printf("%20s: %s\n", "type", typehelp[o->type]);
688 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
689 show_option_range(o);
690 show_option_values(o);
691}
692
Jens Axboeafdf9352007-07-31 16:14:34 +0200693static struct fio_option *find_child(struct fio_option *options,
694 struct fio_option *o)
695{
696 struct fio_option *__o;
697
Jens Axboefdf28742007-07-31 23:06:09 +0200698 for (__o = options + 1; __o->name; __o++)
699 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200700 return __o;
701
702 return NULL;
703}
704
Jens Axboe323d9112008-03-01 15:12:14 +0100705static void __print_option(struct fio_option *o, struct fio_option *org,
706 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200707{
708 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100709 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200710
711 if (!o)
712 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200713 if (!org)
714 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100715
Jens Axboeafdf9352007-07-31 16:14:34 +0200716 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100717 depth = level;
718 while (depth--)
719 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200720
721 sprintf(p, "%s", o->name);
722
723 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100724}
725
726static void print_option(struct fio_option *o)
727{
728 struct fio_option *parent;
729 struct fio_option *__o;
730 unsigned int printed;
731 unsigned int level;
732
733 __print_option(o, NULL, 0);
734 parent = o;
735 level = 0;
736 do {
737 level++;
738 printed = 0;
739
740 while ((__o = find_child(o, parent)) != NULL) {
741 __print_option(__o, o, level);
742 o = __o;
743 printed++;
744 }
745
746 parent = o;
747 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200748}
749
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100750int show_cmd_help(struct fio_option *options, const char *name)
751{
752 struct fio_option *o, *closest;
753 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100754 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100755 int show_all = 0;
756
757 if (!name || !strcmp(name, "all"))
758 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100759
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100760 closest = NULL;
761 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100762 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100763 int match = 0;
764
Jens Axboe15ca1502008-04-07 09:26:02 +0200765 if (o->type == FIO_OPT_DEPRECATED)
766 continue;
767
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100768 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100769 if (!strcmp(name, o->name) ||
770 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100771 match = 1;
772 else {
773 unsigned int dist;
774
775 dist = string_distance(name, o->name);
776 if (dist < best_dist) {
777 best_dist = dist;
778 closest = o;
779 }
780 }
781 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100782
783 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100784 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100785 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200786 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100787 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200788 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100789 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100790 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100791 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100792 }
793
Jens Axboe70df2f12007-01-11 14:04:47 +0100794 if (!match)
795 continue;
796
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100797 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100798 }
799
Jens Axboe29fc6af2007-01-09 21:22:02 +0100800 if (found)
801 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100802
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100803 printf("No such command: %s", name);
804 if (closest) {
805 printf(" - showing closest match\n");
806 printf("%20s: %s\n", closest->name, closest->help);
807 show_option_help(closest);
808 } else
809 printf("\n");
810
Jens Axboe29fc6af2007-01-09 21:22:02 +0100811 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100812}
Jens Axboeee738492007-01-10 11:23:16 +0100813
Jens Axboe13335dd2007-01-10 13:14:02 +0100814/*
815 * Handle parsing of default parameters.
816 */
Jens Axboeee738492007-01-10 11:23:16 +0100817void fill_default_options(void *data, struct fio_option *options)
818{
Jens Axboe4945ba12007-01-11 14:36:56 +0100819 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100820
Jens Axboea3d741f2008-02-27 18:32:33 +0100821 dprint(FD_PARSE, "filling default options\n");
822
Jens Axboe4945ba12007-01-11 14:36:56 +0100823 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100824 if (o->def)
825 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100826}
Jens Axboe13335dd2007-01-10 13:14:02 +0100827
828/*
829 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100830 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100831 */
832void options_init(struct fio_option *options)
833{
Jens Axboe4945ba12007-01-11 14:36:56 +0100834 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100835
Jens Axboea3d741f2008-02-27 18:32:33 +0100836 dprint(FD_PARSE, "init options\n");
837
Jens Axboe4945ba12007-01-11 14:36:56 +0100838 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200839 if (o->type == FIO_OPT_DEPRECATED)
840 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100841 if (o->type == FIO_OPT_BOOL) {
842 o->minval = 0;
843 o->maxval = 1;
844 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100845 if (o->type == FIO_OPT_STR_SET && o->def) {
846 fprintf(stderr, "Option %s: string set option with"
847 " default will always be true\n",
848 o->name);
849 }
850 if (!o->cb && !o->off1) {
851 fprintf(stderr, "Option %s: neither cb nor offset"
852 " given\n", o->name);
853 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100854 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100855 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100856 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
857 fprintf(stderr, "Option %s: both cb and offset given\n",
858 o->name);
859 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100860 }
861}