blob: 2a49b63c1065c30bde38bd664f6bd622f859f04b [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 Axboed447a8c2009-07-17 22:26:15 +020044static void show_option_range(struct fio_option *o, FILE *out)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010045{
46 if (!o->minval && !o->maxval)
47 return;
48
Jens Axboed447a8c2009-07-17 22:26:15 +020049 fprintf(out, "%20s: min=%d", "range", o->minval);
Jens Axboe39801272009-07-01 09:06:12 +020050 if (o->maxval)
Jens Axboed447a8c2009-07-17 22:26:15 +020051 fprintf(out, ", max=%d", o->maxval);
52 fprintf(out, "\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 Axboed447a8c2009-07-17 22:26:15 +020076static void show_option_help(struct fio_option *o, FILE *out)
77{
78 const char *typehelp[] = {
79 "string (opt=bla)",
80 "string with possible k/m/g postfix (opt=4k)",
81 "string with time postfix (opt=10s)",
82 "string (opt=bla)",
83 "string with dual range (opt=1k-4k,4k-8k)",
84 "integer value (opt=100)",
85 "boolean value (opt=1)",
86 "no argument (opt)",
87 };
88
89 if (o->alias)
90 fprintf(out, "%20s: %s\n", "alias", o->alias);
91
92 fprintf(out, "%20s: %s\n", "type", typehelp[o->type]);
93 fprintf(out, "%20s: %s\n", "default", o->def ? o->def : "no default");
94 show_option_range(o, stdout);
95 show_option_values(o);
96}
97
Jens Axboecb2c86f2006-10-26 13:48:34 +020098static unsigned long get_mult_time(char c)
99{
100 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100101 case 'm':
102 case 'M':
103 return 60;
104 case 'h':
105 case 'H':
106 return 60 * 60;
107 case 'd':
108 case 'D':
109 return 24 * 60 * 60;
110 default:
111 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200112 }
113}
114
Jens Axboeb09da8f2009-07-17 23:16:17 +0200115static unsigned long long get_mult_bytes(char c)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116{
117 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100118 case 'k':
119 case 'K':
120 return 1024;
121 case 'm':
122 case 'M':
123 return 1024 * 1024;
124 case 'g':
125 case 'G':
126 return 1024 * 1024 * 1024;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200127 case 't':
128 case 'T':
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100129 return 1024 * 1024 * 1024 * 1024UL;
Jens Axboeb09da8f2009-07-17 23:16:17 +0200130 case 'p':
131 case 'P':
132 return 1024 * 1024 * 1024 * 1024ULL * 1024ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100133 default:
134 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200135 }
136}
137
138/*
Jens Axboee1f36502006-10-27 10:54:08 +0200139 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200140 */
Jens Axboe564ca972007-12-14 12:21:19 +0100141int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200142{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100143 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200144
Jens Axboecb2c86f2006-10-26 13:48:34 +0200145 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100146 if (!len)
147 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148
Jens Axboeb347f9d2009-03-09 14:15:21 +0100149 if (strstr(str, "0x") || strstr(str, "0X"))
150 base = 16;
151 else
152 base = 10;
153
154 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100155 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156 return 1;
157
158 if (kilo)
159 *val *= get_mult_bytes(str[len - 1]);
160 else
161 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100162
Jens Axboecb2c86f2006-10-26 13:48:34 +0200163 return 0;
164}
165
Jens Axboe63f29372007-01-10 13:20:09 +0100166static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200167{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200168 return str_to_decimal(p, val, 1);
169}
170
Jens Axboe63f29372007-01-10 13:20:09 +0100171static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200172{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200173 return str_to_decimal(p, val, 0);
174}
175
176void strip_blank_front(char **p)
177{
178 char *s = *p;
179
180 while (isspace(*s))
181 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200182
183 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200184}
185
186void strip_blank_end(char *p)
187{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100188 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200189
Jens Axboe523bfad2007-04-04 11:04:06 +0200190 s = strchr(p, ';');
191 if (s)
192 *s = '\0';
193 s = strchr(p, '#');
194 if (s)
195 *s = '\0';
196 if (s)
197 p = s;
198
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200199 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100200 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200201 s--;
202
203 *(s + 1) = '\0';
204}
205
Jens Axboe63f29372007-01-10 13:20:09 +0100206static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207{
208 char suffix;
209
Jens Axboe787f7e92006-11-06 13:26:29 +0100210 if (!strlen(str))
211 return 1;
212
Jens Axboecb2c86f2006-10-26 13:48:34 +0200213 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
214 *val *= get_mult_bytes(suffix);
215 return 0;
216 }
217
218 if (sscanf(str, "%lu", val) == 1)
219 return 0;
220
221 return 1;
222}
223
Jens Axboe63f29372007-01-10 13:20:09 +0100224static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200225{
Jens Axboe787f7e92006-11-06 13:26:29 +0100226 if (!strlen(p))
227 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200228 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200229 if (sscanf(p, "%x", val) == 1)
230 return 0;
231 } else {
232 if (sscanf(p, "%u", val) == 1)
233 return 0;
234 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200235
236 return 1;
237}
238
Jens Axboee1f36502006-10-27 10:54:08 +0200239static struct fio_option *find_option(struct fio_option *options,
240 const char *opt)
241{
Jens Axboe4945ba12007-01-11 14:36:56 +0100242 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200243
Jens Axboe4945ba12007-01-11 14:36:56 +0100244 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200245 if (!strcmp(o->name, opt))
246 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100247 else if (o->alias && !strcmp(o->alias, opt))
248 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200249 }
Jens Axboee1f36502006-10-27 10:54:08 +0200250
251 return NULL;
252}
253
Jens Axboe17abbe82006-11-06 11:23:10 +0100254#define val_store(ptr, val, off, data) \
255 do { \
256 ptr = td_var((data), (off)); \
257 *ptr = (val); \
258 } while (0)
259
Jens Axboef90eff52006-11-06 11:08:21 +0100260static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100261 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200262{
Jens Axboe63f29372007-01-10 13:20:09 +0100263 int il, *ilp;
264 long long ull, *ullp;
265 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200266 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200267 int ret = 0, is_time = 0;
268
Jens Axboea3d741f2008-02-27 18:32:33 +0100269 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
270 o->type, ptr);
271
Jens Axboee3cedca2008-11-19 19:57:52 +0100272 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100273 fprintf(stderr, "Option %s requires an argument\n", o->name);
274 return 1;
275 }
276
Jens Axboee1f36502006-10-27 10:54:08 +0200277 switch (o->type) {
278 case FIO_OPT_STR: {
279 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100280 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100281 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100282 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200283
Jens Axboef0857372007-03-19 13:15:23 +0100284 posval_sort(o, posval);
285
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100286 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100287 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100288 if (!vp->ival || vp->ival[0] == '\0')
289 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100290 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100291 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
292 ret = 0;
293 if (!o->off1)
294 break;
295 val_store(ilp, vp->oval, o->off1, data);
296 break;
297 }
298 }
299
300 if (ret)
301 show_option_values(o);
302 else if (fn)
303 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200304 break;
305 }
306 case FIO_OPT_STR_VAL_TIME:
307 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100308 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200309 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200310 fio_opt_str_val_fn *fn = o->cb;
311
312 if (is_time)
313 ret = check_str_time(ptr, &ull);
314 else
315 ret = check_str_bytes(ptr, &ull);
316
317 if (ret)
318 break;
319
Jens Axboedb8e0162007-01-10 13:41:26 +0100320 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100321 fprintf(stderr, "max value out of range: %lld"
322 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100323 return 1;
324 }
325 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100326 fprintf(stderr, "min value out of range: %lld"
327 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100328 return 1;
329 }
Jens Axboee1f36502006-10-27 10:54:08 +0200330
331 if (fn)
332 ret = fn(data, &ull);
333 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200334 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100335 if (first)
336 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100337 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100338 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100339 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100340 if (first)
341 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100342 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100343 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100344 }
Jens Axboee1f36502006-10-27 10:54:08 +0200345 }
346 break;
347 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100348 case FIO_OPT_STR_STORE: {
349 fio_opt_str_fn *fn = o->cb;
350
Jens Axboee1f36502006-10-27 10:54:08 +0200351 cp = td_var(data, o->off1);
352 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100353 if (fn) {
354 ret = fn(data, ptr);
355 if (ret) {
356 free(*cp);
357 *cp = NULL;
358 }
359 }
Jens Axboee1f36502006-10-27 10:54:08 +0200360 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100361 }
Jens Axboee1f36502006-10-27 10:54:08 +0200362 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200363 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200364 char *p1, *p2;
365
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100366 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200367
368 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200369 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100370 p1 = strchr(tmp, ':');
371 if (!p1) {
372 ret = 1;
373 break;
374 }
Jens Axboee1f36502006-10-27 10:54:08 +0200375 }
376
377 p2 = p1 + 1;
378 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200379 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200380
381 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100382 if (!check_range_bytes(p1, &ul1) &&
383 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200384 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200385 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100386 unsigned long foo = ul1;
387
388 ul1 = ul2;
389 ul2 = foo;
390 }
391
392 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100393 val_store(ilp, ul1, o->off1, data);
394 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200395 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100396 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100397 val_store(ilp, ul1, o->off3, data);
398 val_store(ilp, ul2, o->off4, data);
399 }
400 }
401
Jens Axboee1f36502006-10-27 10:54:08 +0200402 break;
403 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100404 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200405 fio_opt_int_fn *fn = o->cb;
406
407 ret = check_int(ptr, &il);
408 if (ret)
409 break;
410
Jens Axboedb8e0162007-01-10 13:41:26 +0100411 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100412 fprintf(stderr, "max value out of range: %d (%d max)\n",
413 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100414 return 1;
415 }
416 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100417 fprintf(stderr, "min value out of range: %d (%d min)\n",
418 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100419 return 1;
420 }
Jens Axboee1f36502006-10-27 10:54:08 +0200421
Jens Axboe76a43db2007-01-11 13:24:44 +0100422 if (o->neg)
423 il = !il;
424
Jens Axboee1f36502006-10-27 10:54:08 +0200425 if (fn)
426 ret = fn(data, &il);
427 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100428 if (first)
429 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100430 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100431 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200432 }
433 break;
434 }
435 case FIO_OPT_STR_SET: {
436 fio_opt_str_set_fn *fn = o->cb;
437
438 if (fn)
439 ret = fn(data);
440 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100441 if (first)
442 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100443 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100444 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200445 }
446 break;
447 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200448 case FIO_OPT_DEPRECATED:
449 fprintf(stdout, "Option %s is deprecated\n", o->name);
450 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200451 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100452 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200453 ret = 1;
454 }
455
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200456 if (ret)
457 return ret;
458
Jens Axboed447a8c2009-07-17 22:26:15 +0200459 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200460 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200461 if (ret) {
462 fprintf(stderr,"Correct format for offending option\n");
463 fprintf(stderr, "%20s: %s\n", o->name, o->help);
464 show_option_help(o, stderr);
465 }
466 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200467
Jens Axboee1f36502006-10-27 10:54:08 +0200468 return ret;
469}
470
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200471static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100472{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200473 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100474 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100475
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200476 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
477
478 ptr = NULL;
479 if (__ptr)
480 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100481
Jens Axboef90eff52006-11-06 11:08:21 +0100482 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100483 * See if we have a second set of parameters, hidden after a comma.
484 * Do this before parsing the first round, to check if we should
485 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100486 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100487 if (ptr &&
488 (o->type != FIO_OPT_STR_STORE) &&
489 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100490 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200491 if (ptr2 && *(ptr2 + 1) == '\0')
492 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100493 if (!ptr2)
494 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100495 if (!ptr2)
496 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100497 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100498
499 /*
500 * Don't return early if parsing the first option fails - if
501 * we are doing multiple arguments, we can allow the first one
502 * being empty.
503 */
504 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
505
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200506 if (!ptr2) {
507 if (ptr)
508 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100509 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200510 }
Jens Axboef90eff52006-11-06 11:08:21 +0100511
512 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100513 r2 = __handle_option(o, ptr2, data, 0, 0);
514
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200515 if (ptr)
516 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100517 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100518}
519
Jens Axboe3b8b7132008-06-10 19:46:23 +0200520static struct fio_option *get_option(const char *opt,
521 struct fio_option *options, char **post)
522{
523 struct fio_option *o;
524 char *ret;
525
526 ret = strchr(opt, '=');
527 if (ret) {
528 *post = ret;
529 *ret = '\0';
530 ret = (char *) opt;
531 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100532 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200533 o = find_option(options, ret);
534 } else {
535 o = find_option(options, opt);
536 *post = NULL;
537 }
538
539 return o;
540}
541
542static int opt_cmp(const void *p1, const void *p2)
543{
544 struct fio_option *o1, *o2;
545 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100546 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200547
548 s1 = strdup(*((char **) p1));
549 s2 = strdup(*((char **) p2));
550
551 o1 = get_option(s1, fio_options, &foo);
552 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100553
554 prio1 = prio2 = 0;
555 if (o1)
556 prio1 = o1->prio;
557 if (o2)
558 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200559
560 free(s1);
561 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100562 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200563}
564
565void sort_options(char **opts, struct fio_option *options, int num_opts)
566{
567 fio_options = options;
568 qsort(opts, num_opts, sizeof(char *), opt_cmp);
569 fio_options = NULL;
570}
571
Jens Axboeb4692822006-10-27 13:43:22 +0200572int parse_cmd_option(const char *opt, const char *val,
573 struct fio_option *options, void *data)
574{
575 struct fio_option *o;
576
577 o = find_option(options, opt);
578 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100579 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200580 return 1;
581 }
582
Jens Axboeb1508cf2006-11-02 09:12:40 +0100583 if (!handle_option(o, val, data))
584 return 0;
585
586 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
587 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200588}
589
Aaron Carroll88b5a392008-10-07 11:25:20 +0200590/*
591 * Return a copy of the input string with substrings of the form ${VARNAME}
592 * substituted with the value of the environment variable VARNAME. The
593 * substitution always occurs, even if VARNAME is empty or the corresponding
594 * environment variable undefined.
595 */
596static char *option_dup_subs(const char *opt)
597{
598 char out[OPT_LEN_MAX+1];
599 char in[OPT_LEN_MAX+1];
600 char *outptr = out;
601 char *inptr = in;
602 char *ch1, *ch2, *env;
603 ssize_t nchr = OPT_LEN_MAX;
604 size_t envlen;
605
606 in[OPT_LEN_MAX] = '\0';
607 strncpy(in, opt, OPT_LEN_MAX);
608
609 while (*inptr && nchr > 0) {
610 if (inptr[0] == '$' && inptr[1] == '{') {
611 ch2 = strchr(inptr, '}');
612 if (ch2 && inptr+1 < ch2) {
613 ch1 = inptr+2;
614 inptr = ch2+1;
615 *ch2 = '\0';
616
617 env = getenv(ch1);
618 if (env) {
619 envlen = strlen(env);
620 if (envlen <= nchr) {
621 memcpy(outptr, env, envlen);
622 outptr += envlen;
623 nchr -= envlen;
624 }
625 }
626
627 continue;
628 }
629 }
630
631 *outptr++ = *inptr++;
632 --nchr;
633 }
634
635 *outptr = '\0';
636 return strdup(out);
637}
638
Jens Axboee1f36502006-10-27 10:54:08 +0200639int parse_option(const char *opt, struct fio_option *options, void *data)
640{
Jens Axboeb4692822006-10-27 13:43:22 +0200641 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200642 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200643
Aaron Carroll88b5a392008-10-07 11:25:20 +0200644 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200645
Jens Axboe3b8b7132008-06-10 19:46:23 +0200646 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200647 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100648 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200649 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200650 return 1;
651 }
652
Jens Axboe0401bca2007-03-29 11:00:43 +0200653 if (!handle_option(o, post, data)) {
654 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100655 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200656 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100657
658 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200659 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100660 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200661}
Jens Axboefd28ca42007-01-09 21:20:13 +0100662
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100663/*
664 * Option match, levenshtein distance. Handy for not quite remembering what
665 * the option name is.
666 */
667static int string_distance(const char *s1, const char *s2)
668{
669 unsigned int s1_len = strlen(s1);
670 unsigned int s2_len = strlen(s2);
671 unsigned int *p, *q, *r;
672 unsigned int i, j;
673
674 p = malloc(sizeof(unsigned int) * (s2_len + 1));
675 q = malloc(sizeof(unsigned int) * (s2_len + 1));
676
677 p[0] = 0;
678 for (i = 1; i <= s2_len; i++)
679 p[i] = p[i - 1] + 1;
680
681 for (i = 1; i <= s1_len; i++) {
682 q[0] = p[0] + 1;
683 for (j = 1; j <= s2_len; j++) {
684 unsigned int sub = p[j - 1];
685
686 if (s1[i - 1] != s2[j - 1])
687 sub++;
688
689 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
690 }
691 r = p;
692 p = q;
693 q = r;
694 }
695
696 i = p[s2_len];
697 free(p);
698 free(q);
699 return i;
700}
701
Jens Axboeafdf9352007-07-31 16:14:34 +0200702static struct fio_option *find_child(struct fio_option *options,
703 struct fio_option *o)
704{
705 struct fio_option *__o;
706
Jens Axboefdf28742007-07-31 23:06:09 +0200707 for (__o = options + 1; __o->name; __o++)
708 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200709 return __o;
710
711 return NULL;
712}
713
Jens Axboe323d9112008-03-01 15:12:14 +0100714static void __print_option(struct fio_option *o, struct fio_option *org,
715 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200716{
717 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100718 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200719
720 if (!o)
721 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200722 if (!org)
723 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100724
Jens Axboeafdf9352007-07-31 16:14:34 +0200725 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100726 depth = level;
727 while (depth--)
728 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200729
730 sprintf(p, "%s", o->name);
731
732 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100733}
734
735static void print_option(struct fio_option *o)
736{
737 struct fio_option *parent;
738 struct fio_option *__o;
739 unsigned int printed;
740 unsigned int level;
741
742 __print_option(o, NULL, 0);
743 parent = o;
744 level = 0;
745 do {
746 level++;
747 printed = 0;
748
749 while ((__o = find_child(o, parent)) != NULL) {
750 __print_option(__o, o, level);
751 o = __o;
752 printed++;
753 }
754
755 parent = o;
756 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200757}
758
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100759int show_cmd_help(struct fio_option *options, const char *name)
760{
761 struct fio_option *o, *closest;
762 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100763 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100764 int show_all = 0;
765
766 if (!name || !strcmp(name, "all"))
767 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100768
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100769 closest = NULL;
770 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100771 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100772 int match = 0;
773
Jens Axboe15ca1502008-04-07 09:26:02 +0200774 if (o->type == FIO_OPT_DEPRECATED)
775 continue;
776
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100777 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100778 if (!strcmp(name, o->name) ||
779 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100780 match = 1;
781 else {
782 unsigned int dist;
783
784 dist = string_distance(name, o->name);
785 if (dist < best_dist) {
786 best_dist = dist;
787 closest = o;
788 }
789 }
790 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100791
792 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100793 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100794 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200795 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100796 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200797 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100798 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100799 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100800 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100801 }
802
Jens Axboe70df2f12007-01-11 14:04:47 +0100803 if (!match)
804 continue;
805
Jens Axboed447a8c2009-07-17 22:26:15 +0200806 show_option_help(o, stdout);
Jens Axboefd28ca42007-01-09 21:20:13 +0100807 }
808
Jens Axboe29fc6af2007-01-09 21:22:02 +0100809 if (found)
810 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100811
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100812 printf("No such command: %s", name);
813 if (closest) {
814 printf(" - showing closest match\n");
815 printf("%20s: %s\n", closest->name, closest->help);
Jens Axboed447a8c2009-07-17 22:26:15 +0200816 show_option_help(closest, stdout);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100817 } else
818 printf("\n");
819
Jens Axboe29fc6af2007-01-09 21:22:02 +0100820 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100821}
Jens Axboeee738492007-01-10 11:23:16 +0100822
Jens Axboe13335dd2007-01-10 13:14:02 +0100823/*
824 * Handle parsing of default parameters.
825 */
Jens Axboeee738492007-01-10 11:23:16 +0100826void fill_default_options(void *data, struct fio_option *options)
827{
Jens Axboe4945ba12007-01-11 14:36:56 +0100828 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100829
Jens Axboea3d741f2008-02-27 18:32:33 +0100830 dprint(FD_PARSE, "filling default options\n");
831
Jens Axboe4945ba12007-01-11 14:36:56 +0100832 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100833 if (o->def)
834 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100835}
Jens Axboe13335dd2007-01-10 13:14:02 +0100836
837/*
838 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100839 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100840 */
841void options_init(struct fio_option *options)
842{
Jens Axboe4945ba12007-01-11 14:36:56 +0100843 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100844
Jens Axboea3d741f2008-02-27 18:32:33 +0100845 dprint(FD_PARSE, "init options\n");
846
Jens Axboe4945ba12007-01-11 14:36:56 +0100847 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200848 if (o->type == FIO_OPT_DEPRECATED)
849 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100850 if (o->type == FIO_OPT_BOOL) {
851 o->minval = 0;
852 o->maxval = 1;
853 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100854 if (o->type == FIO_OPT_STR_SET && o->def) {
855 fprintf(stderr, "Option %s: string set option with"
856 " default will always be true\n",
857 o->name);
858 }
859 if (!o->cb && !o->off1) {
860 fprintf(stderr, "Option %s: neither cb nor offset"
861 " given\n", o->name);
862 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100863 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100864 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100865 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
866 fprintf(stderr, "Option %s: both cb and offset given\n",
867 o->name);
868 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100869 }
870}