blob: 0bf28a5bd72463d574560d828859669b9b0122c2 [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 Axboea639f0b2009-07-18 08:25:35 +020017extern unsigned int fio_kb_base;
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 Axboeb09da8f2009-07-17 23:16:17 +0200116static unsigned long long get_mult_bytes(char c)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117{
Jens Axboea639f0b2009-07-18 08:25:35 +0200118 unsigned long long ret = 1;
119
Jens Axboecb2c86f2006-10-26 13:48:34 +0200120 switch (c) {
Jens Axboea639f0b2009-07-18 08:25:35 +0200121 default:
122 break;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200123 case 'p':
124 case 'P':
Jens Axboea639f0b2009-07-18 08:25:35 +0200125 ret *= (unsigned long long) fio_kb_base;
126 case 't':
127 case 'T':
128 ret *= (unsigned long long) fio_kb_base;
129 case 'g':
130 case 'G':
131 ret *= (unsigned long long) fio_kb_base;
132 case 'm':
133 case 'M':
134 ret *= (unsigned long long) fio_kb_base;
135 case 'k':
136 case 'K':
137 ret *= (unsigned long long) fio_kb_base;
138 break;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200139 }
Jens Axboea639f0b2009-07-18 08:25:35 +0200140
141 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200142}
143
144/*
Jens Axboee1f36502006-10-27 10:54:08 +0200145 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200146 */
Jens Axboe564ca972007-12-14 12:21:19 +0100147int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100149 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200150
Jens Axboecb2c86f2006-10-26 13:48:34 +0200151 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100152 if (!len)
153 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200154
Jens Axboeb347f9d2009-03-09 14:15:21 +0100155 if (strstr(str, "0x") || strstr(str, "0X"))
156 base = 16;
157 else
158 base = 10;
159
160 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100161 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200162 return 1;
163
164 if (kilo)
165 *val *= get_mult_bytes(str[len - 1]);
166 else
167 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100168
Jens Axboecb2c86f2006-10-26 13:48:34 +0200169 return 0;
170}
171
Jens Axboe63f29372007-01-10 13:20:09 +0100172static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200173{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200174 return str_to_decimal(p, val, 1);
175}
176
Jens Axboe63f29372007-01-10 13:20:09 +0100177static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200178{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179 return str_to_decimal(p, val, 0);
180}
181
182void strip_blank_front(char **p)
183{
184 char *s = *p;
185
186 while (isspace(*s))
187 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200188
189 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200190}
191
192void strip_blank_end(char *p)
193{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100194 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200195
Jens Axboe523bfad2007-04-04 11:04:06 +0200196 s = strchr(p, ';');
197 if (s)
198 *s = '\0';
199 s = strchr(p, '#');
200 if (s)
201 *s = '\0';
202 if (s)
203 p = s;
204
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200205 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100206 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207 s--;
208
209 *(s + 1) = '\0';
210}
211
Jens Axboe63f29372007-01-10 13:20:09 +0100212static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200213{
214 char suffix;
215
Jens Axboe787f7e92006-11-06 13:26:29 +0100216 if (!strlen(str))
217 return 1;
218
Jens Axboecb2c86f2006-10-26 13:48:34 +0200219 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
220 *val *= get_mult_bytes(suffix);
221 return 0;
222 }
223
224 if (sscanf(str, "%lu", val) == 1)
225 return 0;
226
227 return 1;
228}
229
Jens Axboe63f29372007-01-10 13:20:09 +0100230static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200231{
Jens Axboe787f7e92006-11-06 13:26:29 +0100232 if (!strlen(p))
233 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200234 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200235 if (sscanf(p, "%x", val) == 1)
236 return 0;
237 } else {
238 if (sscanf(p, "%u", val) == 1)
239 return 0;
240 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200241
242 return 1;
243}
244
Jens Axboee1f36502006-10-27 10:54:08 +0200245static struct fio_option *find_option(struct fio_option *options,
246 const char *opt)
247{
Jens Axboe4945ba12007-01-11 14:36:56 +0100248 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200249
Jens Axboe4945ba12007-01-11 14:36:56 +0100250 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200251 if (!strcmp(o->name, opt))
252 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100253 else if (o->alias && !strcmp(o->alias, opt))
254 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200255 }
Jens Axboee1f36502006-10-27 10:54:08 +0200256
257 return NULL;
258}
259
Jens Axboe17abbe82006-11-06 11:23:10 +0100260#define val_store(ptr, val, off, data) \
261 do { \
262 ptr = td_var((data), (off)); \
263 *ptr = (val); \
264 } while (0)
265
Jens Axboef90eff52006-11-06 11:08:21 +0100266static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100267 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200268{
Jens Axboe63f29372007-01-10 13:20:09 +0100269 int il, *ilp;
270 long long ull, *ullp;
271 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200272 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200273 int ret = 0, is_time = 0;
274
Jens Axboea3d741f2008-02-27 18:32:33 +0100275 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
276 o->type, ptr);
277
Jens Axboee3cedca2008-11-19 19:57:52 +0100278 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100279 fprintf(stderr, "Option %s requires an argument\n", o->name);
280 return 1;
281 }
282
Jens Axboee1f36502006-10-27 10:54:08 +0200283 switch (o->type) {
284 case FIO_OPT_STR: {
285 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100286 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100287 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100288 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200289
Jens Axboef0857372007-03-19 13:15:23 +0100290 posval_sort(o, posval);
291
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100292 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100293 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100294 if (!vp->ival || vp->ival[0] == '\0')
295 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100296 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100297 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
298 ret = 0;
299 if (!o->off1)
300 break;
301 val_store(ilp, vp->oval, o->off1, data);
302 break;
303 }
304 }
305
306 if (ret)
307 show_option_values(o);
308 else if (fn)
309 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200310 break;
311 }
312 case FIO_OPT_STR_VAL_TIME:
313 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100314 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200315 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200316 fio_opt_str_val_fn *fn = o->cb;
317
318 if (is_time)
319 ret = check_str_time(ptr, &ull);
320 else
321 ret = check_str_bytes(ptr, &ull);
322
323 if (ret)
324 break;
325
Jens Axboedb8e0162007-01-10 13:41:26 +0100326 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100327 fprintf(stderr, "max value out of range: %lld"
328 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100329 return 1;
330 }
331 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100332 fprintf(stderr, "min value out of range: %lld"
333 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100334 return 1;
335 }
Jens Axboee1f36502006-10-27 10:54:08 +0200336
337 if (fn)
338 ret = fn(data, &ull);
339 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200340 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100341 if (first)
342 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100343 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100344 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100345 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100346 if (first)
347 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100348 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100349 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100350 }
Jens Axboee1f36502006-10-27 10:54:08 +0200351 }
352 break;
353 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100354 case FIO_OPT_STR_STORE: {
355 fio_opt_str_fn *fn = o->cb;
356
Jens Axboee1f36502006-10-27 10:54:08 +0200357 cp = td_var(data, o->off1);
358 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100359 if (fn) {
360 ret = fn(data, ptr);
361 if (ret) {
362 free(*cp);
363 *cp = NULL;
364 }
365 }
Jens Axboee1f36502006-10-27 10:54:08 +0200366 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100367 }
Jens Axboee1f36502006-10-27 10:54:08 +0200368 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200369 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200370 char *p1, *p2;
371
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100372 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200373
374 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200375 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100376 p1 = strchr(tmp, ':');
377 if (!p1) {
378 ret = 1;
379 break;
380 }
Jens Axboee1f36502006-10-27 10:54:08 +0200381 }
382
383 p2 = p1 + 1;
384 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200385 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200386
387 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100388 if (!check_range_bytes(p1, &ul1) &&
389 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200390 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200391 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100392 unsigned long foo = ul1;
393
394 ul1 = ul2;
395 ul2 = foo;
396 }
397
398 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100399 val_store(ilp, ul1, o->off1, data);
400 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200401 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100402 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100403 val_store(ilp, ul1, o->off3, data);
404 val_store(ilp, ul2, o->off4, data);
405 }
406 }
407
Jens Axboee1f36502006-10-27 10:54:08 +0200408 break;
409 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100410 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200411 fio_opt_int_fn *fn = o->cb;
412
413 ret = check_int(ptr, &il);
414 if (ret)
415 break;
416
Jens Axboedb8e0162007-01-10 13:41:26 +0100417 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100418 fprintf(stderr, "max value out of range: %d (%d max)\n",
419 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100420 return 1;
421 }
422 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100423 fprintf(stderr, "min value out of range: %d (%d min)\n",
424 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100425 return 1;
426 }
Jens Axboee1f36502006-10-27 10:54:08 +0200427
Jens Axboe76a43db2007-01-11 13:24:44 +0100428 if (o->neg)
429 il = !il;
430
Jens Axboee1f36502006-10-27 10:54:08 +0200431 if (fn)
432 ret = fn(data, &il);
433 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100434 if (first)
435 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100436 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100437 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200438 }
439 break;
440 }
441 case FIO_OPT_STR_SET: {
442 fio_opt_str_set_fn *fn = o->cb;
443
444 if (fn)
445 ret = fn(data);
446 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100447 if (first)
448 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100449 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100450 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200451 }
452 break;
453 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200454 case FIO_OPT_DEPRECATED:
455 fprintf(stdout, "Option %s is deprecated\n", o->name);
456 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200457 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100458 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200459 ret = 1;
460 }
461
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200462 if (ret)
463 return ret;
464
Jens Axboed447a8c2009-07-17 22:26:15 +0200465 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200466 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200467 if (ret) {
468 fprintf(stderr,"Correct format for offending option\n");
469 fprintf(stderr, "%20s: %s\n", o->name, o->help);
470 show_option_help(o, stderr);
471 }
472 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200473
Jens Axboee1f36502006-10-27 10:54:08 +0200474 return ret;
475}
476
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200477static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100478{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200479 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100480 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100481
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200482 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
483
484 ptr = NULL;
485 if (__ptr)
486 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100487
Jens Axboef90eff52006-11-06 11:08:21 +0100488 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100489 * See if we have a second set of parameters, hidden after a comma.
490 * Do this before parsing the first round, to check if we should
491 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100492 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100493 if (ptr &&
494 (o->type != FIO_OPT_STR_STORE) &&
495 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100496 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200497 if (ptr2 && *(ptr2 + 1) == '\0')
498 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100499 if (!ptr2)
500 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100501 if (!ptr2)
502 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100503 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100504
505 /*
506 * Don't return early if parsing the first option fails - if
507 * we are doing multiple arguments, we can allow the first one
508 * being empty.
509 */
510 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
511
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200512 if (!ptr2) {
513 if (ptr)
514 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100515 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200516 }
Jens Axboef90eff52006-11-06 11:08:21 +0100517
518 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100519 r2 = __handle_option(o, ptr2, data, 0, 0);
520
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200521 if (ptr)
522 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100523 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100524}
525
Jens Axboe3b8b7132008-06-10 19:46:23 +0200526static struct fio_option *get_option(const char *opt,
527 struct fio_option *options, char **post)
528{
529 struct fio_option *o;
530 char *ret;
531
532 ret = strchr(opt, '=');
533 if (ret) {
534 *post = ret;
535 *ret = '\0';
536 ret = (char *) opt;
537 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100538 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200539 o = find_option(options, ret);
540 } else {
541 o = find_option(options, opt);
542 *post = NULL;
543 }
544
545 return o;
546}
547
548static int opt_cmp(const void *p1, const void *p2)
549{
550 struct fio_option *o1, *o2;
551 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100552 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200553
554 s1 = strdup(*((char **) p1));
555 s2 = strdup(*((char **) p2));
556
557 o1 = get_option(s1, fio_options, &foo);
558 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100559
560 prio1 = prio2 = 0;
561 if (o1)
562 prio1 = o1->prio;
563 if (o2)
564 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200565
566 free(s1);
567 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100568 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200569}
570
571void sort_options(char **opts, struct fio_option *options, int num_opts)
572{
573 fio_options = options;
574 qsort(opts, num_opts, sizeof(char *), opt_cmp);
575 fio_options = NULL;
576}
577
Jens Axboeb4692822006-10-27 13:43:22 +0200578int parse_cmd_option(const char *opt, const char *val,
579 struct fio_option *options, void *data)
580{
581 struct fio_option *o;
582
583 o = find_option(options, opt);
584 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100585 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200586 return 1;
587 }
588
Jens Axboeb1508cf2006-11-02 09:12:40 +0100589 if (!handle_option(o, val, data))
590 return 0;
591
592 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
593 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200594}
595
Aaron Carroll88b5a392008-10-07 11:25:20 +0200596/*
597 * Return a copy of the input string with substrings of the form ${VARNAME}
598 * substituted with the value of the environment variable VARNAME. The
599 * substitution always occurs, even if VARNAME is empty or the corresponding
600 * environment variable undefined.
601 */
602static char *option_dup_subs(const char *opt)
603{
604 char out[OPT_LEN_MAX+1];
605 char in[OPT_LEN_MAX+1];
606 char *outptr = out;
607 char *inptr = in;
608 char *ch1, *ch2, *env;
609 ssize_t nchr = OPT_LEN_MAX;
610 size_t envlen;
611
612 in[OPT_LEN_MAX] = '\0';
613 strncpy(in, opt, OPT_LEN_MAX);
614
615 while (*inptr && nchr > 0) {
616 if (inptr[0] == '$' && inptr[1] == '{') {
617 ch2 = strchr(inptr, '}');
618 if (ch2 && inptr+1 < ch2) {
619 ch1 = inptr+2;
620 inptr = ch2+1;
621 *ch2 = '\0';
622
623 env = getenv(ch1);
624 if (env) {
625 envlen = strlen(env);
626 if (envlen <= nchr) {
627 memcpy(outptr, env, envlen);
628 outptr += envlen;
629 nchr -= envlen;
630 }
631 }
632
633 continue;
634 }
635 }
636
637 *outptr++ = *inptr++;
638 --nchr;
639 }
640
641 *outptr = '\0';
642 return strdup(out);
643}
644
Jens Axboee1f36502006-10-27 10:54:08 +0200645int parse_option(const char *opt, struct fio_option *options, void *data)
646{
Jens Axboeb4692822006-10-27 13:43:22 +0200647 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200648 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200649
Aaron Carroll88b5a392008-10-07 11:25:20 +0200650 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200651
Jens Axboe3b8b7132008-06-10 19:46:23 +0200652 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200653 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100654 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200655 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200656 return 1;
657 }
658
Jens Axboe0401bca2007-03-29 11:00:43 +0200659 if (!handle_option(o, post, data)) {
660 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100661 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200662 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100663
664 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200665 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100666 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200667}
Jens Axboefd28ca42007-01-09 21:20:13 +0100668
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100669/*
670 * Option match, levenshtein distance. Handy for not quite remembering what
671 * the option name is.
672 */
673static int string_distance(const char *s1, const char *s2)
674{
675 unsigned int s1_len = strlen(s1);
676 unsigned int s2_len = strlen(s2);
677 unsigned int *p, *q, *r;
678 unsigned int i, j;
679
680 p = malloc(sizeof(unsigned int) * (s2_len + 1));
681 q = malloc(sizeof(unsigned int) * (s2_len + 1));
682
683 p[0] = 0;
684 for (i = 1; i <= s2_len; i++)
685 p[i] = p[i - 1] + 1;
686
687 for (i = 1; i <= s1_len; i++) {
688 q[0] = p[0] + 1;
689 for (j = 1; j <= s2_len; j++) {
690 unsigned int sub = p[j - 1];
691
692 if (s1[i - 1] != s2[j - 1])
693 sub++;
694
695 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
696 }
697 r = p;
698 p = q;
699 q = r;
700 }
701
702 i = p[s2_len];
703 free(p);
704 free(q);
705 return i;
706}
707
Jens Axboeafdf9352007-07-31 16:14:34 +0200708static struct fio_option *find_child(struct fio_option *options,
709 struct fio_option *o)
710{
711 struct fio_option *__o;
712
Jens Axboefdf28742007-07-31 23:06:09 +0200713 for (__o = options + 1; __o->name; __o++)
714 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200715 return __o;
716
717 return NULL;
718}
719
Jens Axboe323d9112008-03-01 15:12:14 +0100720static void __print_option(struct fio_option *o, struct fio_option *org,
721 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200722{
723 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100724 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200725
726 if (!o)
727 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200728 if (!org)
729 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100730
Jens Axboeafdf9352007-07-31 16:14:34 +0200731 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100732 depth = level;
733 while (depth--)
734 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200735
736 sprintf(p, "%s", o->name);
737
738 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100739}
740
741static void print_option(struct fio_option *o)
742{
743 struct fio_option *parent;
744 struct fio_option *__o;
745 unsigned int printed;
746 unsigned int level;
747
748 __print_option(o, NULL, 0);
749 parent = o;
750 level = 0;
751 do {
752 level++;
753 printed = 0;
754
755 while ((__o = find_child(o, parent)) != NULL) {
756 __print_option(__o, o, level);
757 o = __o;
758 printed++;
759 }
760
761 parent = o;
762 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200763}
764
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100765int show_cmd_help(struct fio_option *options, const char *name)
766{
767 struct fio_option *o, *closest;
768 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100769 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100770 int show_all = 0;
771
772 if (!name || !strcmp(name, "all"))
773 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100774
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100775 closest = NULL;
776 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100777 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100778 int match = 0;
779
Jens Axboe15ca1502008-04-07 09:26:02 +0200780 if (o->type == FIO_OPT_DEPRECATED)
781 continue;
782
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100783 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100784 if (!strcmp(name, o->name) ||
785 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100786 match = 1;
787 else {
788 unsigned int dist;
789
790 dist = string_distance(name, o->name);
791 if (dist < best_dist) {
792 best_dist = dist;
793 closest = o;
794 }
795 }
796 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100797
798 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100799 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100800 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200801 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100802 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200803 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100804 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100805 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100806 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100807 }
808
Jens Axboe70df2f12007-01-11 14:04:47 +0100809 if (!match)
810 continue;
811
Jens Axboed447a8c2009-07-17 22:26:15 +0200812 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100813 }
814
Jens Axboe29fc6af2007-01-09 21:22:02 +0100815 if (found)
816 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100817
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100818 printf("No such command: %s", name);
819 if (closest) {
820 printf(" - showing closest match\n");
821 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200822 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100823 } else
824 printf("\n");
825
Jens Axboe29fc6af2007-01-09 21:22:02 +0100826 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100827}
Jens Axboeee738492007-01-10 11:23:16 +0100828
Jens Axboe13335dd2007-01-10 13:14:02 +0100829/*
830 * Handle parsing of default parameters.
831 */
Jens Axboeee738492007-01-10 11:23:16 +0100832void fill_default_options(void *data, struct fio_option *options)
833{
Jens Axboe4945ba12007-01-11 14:36:56 +0100834 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100835
Jens Axboea3d741f2008-02-27 18:32:33 +0100836 dprint(FD_PARSE, "filling default options\n");
837
Jens Axboe4945ba12007-01-11 14:36:56 +0100838 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100839 if (o->def)
840 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100841}
Jens Axboe13335dd2007-01-10 13:14:02 +0100842
843/*
844 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100845 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100846 */
847void options_init(struct fio_option *options)
848{
Jens Axboe4945ba12007-01-11 14:36:56 +0100849 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100850
Jens Axboea3d741f2008-02-27 18:32:33 +0100851 dprint(FD_PARSE, "init options\n");
852
Jens Axboe4945ba12007-01-11 14:36:56 +0100853 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200854 if (o->type == FIO_OPT_DEPRECATED)
855 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100856 if (o->type == FIO_OPT_BOOL) {
857 o->minval = 0;
858 o->maxval = 1;
859 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100860 if (o->type == FIO_OPT_STR_SET && o->def) {
861 fprintf(stderr, "Option %s: string set option with"
862 " default will always be true\n",
863 o->name);
864 }
865 if (!o->cb && !o->off1) {
866 fprintf(stderr, "Option %s: neither cb nor offset"
867 " given\n", o->name);
868 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100869 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100870 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100871 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
872 fprintf(stderr, "Option %s: both cb and offset given\n",
873 o->name);
874 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100875 }
876}