blob: b9466901eebeddbe7c7b2b634bf5d504a3abfff8 [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>
11
12#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010013#include "debug.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020014
Jens Axboe3b8b7132008-06-10 19:46:23 +020015static struct fio_option *fio_options;
16
Jens Axboef0857372007-03-19 13:15:23 +010017static int vp_cmp(const void *p1, const void *p2)
18{
19 const struct value_pair *vp1 = p1;
20 const struct value_pair *vp2 = p2;
21
22 return strlen(vp2->ival) - strlen(vp1->ival);
23}
24
25static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
26{
27 const struct value_pair *vp;
28 int entries;
29
30 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
31
32 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
33 vp = &o->posval[entries];
34 if (!vp->ival || vp->ival[0] == '\0')
35 break;
36
37 memcpy(&vpmap[entries], vp, sizeof(*vp));
38 }
39
40 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
41}
42
Jens Axboeb1ec1da2007-02-23 10:29:16 +010043static void show_option_range(struct fio_option *o)
44{
45 if (!o->minval && !o->maxval)
46 return;
47
Jens Axboeec1aee02007-02-26 13:18:47 +010048 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
Jens Axboeb1ec1da2007-02-23 10:29:16 +010049}
50
51static void show_option_values(struct fio_option *o)
52{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010053 int i = 0;
54
55 do {
Jens Axboe78372132007-03-14 13:24:07 +010056 const struct value_pair *vp = &o->posval[i];
57
58 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010059 break;
60
Jens Axboe78372132007-03-14 13:24:07 +010061 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
62 if (vp->help)
63 printf(" %s", vp->help);
64 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010065 i++;
66 } while (i < PARSE_MAX_VP);
67
68 if (i)
69 printf("\n");
70}
71
Jens Axboecb2c86f2006-10-26 13:48:34 +020072static unsigned long get_mult_time(char c)
73{
74 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010075 case 'm':
76 case 'M':
77 return 60;
78 case 'h':
79 case 'H':
80 return 60 * 60;
81 case 'd':
82 case 'D':
83 return 24 * 60 * 60;
84 default:
85 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020086 }
87}
88
89static unsigned long get_mult_bytes(char c)
90{
91 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010092 case 'k':
93 case 'K':
94 return 1024;
95 case 'm':
96 case 'M':
97 return 1024 * 1024;
98 case 'g':
99 case 'G':
100 return 1024 * 1024 * 1024;
101 case 'e':
102 case 'E':
103 return 1024 * 1024 * 1024 * 1024UL;
104 default:
105 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200106 }
107}
108
109/*
Jens Axboee1f36502006-10-27 10:54:08 +0200110 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200111 */
Jens Axboe564ca972007-12-14 12:21:19 +0100112int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200113{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114 int len;
115
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100117 if (!len)
118 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200119
Jens Axboe675de852007-02-10 19:01:07 +0100120 *val = strtoll(str, NULL, 10);
Jens Axboecda866c2007-01-10 16:02:32 +0100121 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200122 return 1;
123
124 if (kilo)
125 *val *= get_mult_bytes(str[len - 1]);
126 else
127 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100128
Jens Axboecb2c86f2006-10-26 13:48:34 +0200129 return 0;
130}
131
Jens Axboe63f29372007-01-10 13:20:09 +0100132static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200133{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200134 return str_to_decimal(p, val, 1);
135}
136
Jens Axboe63f29372007-01-10 13:20:09 +0100137static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200138{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200139 return str_to_decimal(p, val, 0);
140}
141
142void strip_blank_front(char **p)
143{
144 char *s = *p;
145
146 while (isspace(*s))
147 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200148
149 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200150}
151
152void strip_blank_end(char *p)
153{
Jens Axboe523bfad2007-04-04 11:04:06 +0200154 char *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200155
Jens Axboe523bfad2007-04-04 11:04:06 +0200156 s = strchr(p, ';');
157 if (s)
158 *s = '\0';
159 s = strchr(p, '#');
160 if (s)
161 *s = '\0';
162 if (s)
163 p = s;
164
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200165 s = p + strlen(p);
166 while ((isspace(*s) || iscntrl(*s)) && (s > p))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200167 s--;
168
169 *(s + 1) = '\0';
170}
171
Jens Axboe63f29372007-01-10 13:20:09 +0100172static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200173{
174 char suffix;
175
Jens Axboe787f7e92006-11-06 13:26:29 +0100176 if (!strlen(str))
177 return 1;
178
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
180 *val *= get_mult_bytes(suffix);
181 return 0;
182 }
183
184 if (sscanf(str, "%lu", val) == 1)
185 return 0;
186
187 return 1;
188}
189
Jens Axboe63f29372007-01-10 13:20:09 +0100190static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200191{
Jens Axboe787f7e92006-11-06 13:26:29 +0100192 if (!strlen(p))
193 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200194 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200195 if (sscanf(p, "%x", val) == 1)
196 return 0;
197 } else {
198 if (sscanf(p, "%u", val) == 1)
199 return 0;
200 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200201
202 return 1;
203}
204
Jens Axboee1f36502006-10-27 10:54:08 +0200205static struct fio_option *find_option(struct fio_option *options,
206 const char *opt)
207{
Jens Axboe4945ba12007-01-11 14:36:56 +0100208 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200209
Jens Axboe4945ba12007-01-11 14:36:56 +0100210 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200211 if (!strcmp(o->name, opt))
212 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100213 else if (o->alias && !strcmp(o->alias, opt))
214 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200215 }
Jens Axboee1f36502006-10-27 10:54:08 +0200216
217 return NULL;
218}
219
Jens Axboe17abbe82006-11-06 11:23:10 +0100220#define val_store(ptr, val, off, data) \
221 do { \
222 ptr = td_var((data), (off)); \
223 *ptr = (val); \
224 } while (0)
225
Jens Axboef90eff52006-11-06 11:08:21 +0100226static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100227 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200228{
Jens Axboe63f29372007-01-10 13:20:09 +0100229 int il, *ilp;
230 long long ull, *ullp;
231 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200232 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200233 int ret = 0, is_time = 0;
234
Jens Axboea3d741f2008-02-27 18:32:33 +0100235 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
236 o->type, ptr);
237
Jens Axboe08e26e32006-11-21 13:15:10 +0100238 if (!ptr && o->type != FIO_OPT_STR_SET) {
239 fprintf(stderr, "Option %s requires an argument\n", o->name);
240 return 1;
241 }
242
Jens Axboee1f36502006-10-27 10:54:08 +0200243 switch (o->type) {
244 case FIO_OPT_STR: {
245 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100246 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100247 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100248 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200249
Jens Axboef0857372007-03-19 13:15:23 +0100250 posval_sort(o, posval);
251
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100252 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100253 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100254 if (!vp->ival || vp->ival[0] == '\0')
255 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100256 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100257 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
258 ret = 0;
259 if (!o->off1)
260 break;
261 val_store(ilp, vp->oval, o->off1, data);
262 break;
263 }
264 }
265
266 if (ret)
267 show_option_values(o);
268 else if (fn)
269 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200270 break;
271 }
272 case FIO_OPT_STR_VAL_TIME:
273 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100274 case FIO_OPT_STR_VAL:
275 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200276 fio_opt_str_val_fn *fn = o->cb;
277
278 if (is_time)
279 ret = check_str_time(ptr, &ull);
280 else
281 ret = check_str_bytes(ptr, &ull);
282
283 if (ret)
284 break;
285
Jens Axboedb8e0162007-01-10 13:41:26 +0100286 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100287 fprintf(stderr, "max value out of range: %lld"
288 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100289 return 1;
290 }
291 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100292 fprintf(stderr, "min value out of range: %lld"
293 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100294 return 1;
295 }
Jens Axboee1f36502006-10-27 10:54:08 +0200296
297 if (fn)
298 ret = fn(data, &ull);
299 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100300 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100301 if (first)
302 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100303 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100304 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100305 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100306 if (first)
307 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100308 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100309 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100310 }
Jens Axboee1f36502006-10-27 10:54:08 +0200311 }
312 break;
313 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100314 case FIO_OPT_STR_STORE: {
315 fio_opt_str_fn *fn = o->cb;
316
Jens Axboee1f36502006-10-27 10:54:08 +0200317 cp = td_var(data, o->off1);
318 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100319 if (fn) {
320 ret = fn(data, ptr);
321 if (ret) {
322 free(*cp);
323 *cp = NULL;
324 }
325 }
Jens Axboee1f36502006-10-27 10:54:08 +0200326 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100327 }
Jens Axboee1f36502006-10-27 10:54:08 +0200328 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200329 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200330 char *p1, *p2;
331
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100332 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200333
334 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200335 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100336 p1 = strchr(tmp, ':');
337 if (!p1) {
338 ret = 1;
339 break;
340 }
Jens Axboee1f36502006-10-27 10:54:08 +0200341 }
342
343 p2 = p1 + 1;
344 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200345 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200346
347 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100348 if (!check_range_bytes(p1, &ul1) &&
349 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200350 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200351 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100352 unsigned long foo = ul1;
353
354 ul1 = ul2;
355 ul2 = foo;
356 }
357
358 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100359 val_store(ilp, ul1, o->off1, data);
360 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200361 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100362 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100363 val_store(ilp, ul1, o->off3, data);
364 val_store(ilp, ul2, o->off4, data);
365 }
366 }
367
Jens Axboee1f36502006-10-27 10:54:08 +0200368 break;
369 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100370 case FIO_OPT_INT:
371 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200372 fio_opt_int_fn *fn = o->cb;
373
374 ret = check_int(ptr, &il);
375 if (ret)
376 break;
377
Jens Axboedb8e0162007-01-10 13:41:26 +0100378 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100379 fprintf(stderr, "max value out of range: %d (%d max)\n",
380 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100381 return 1;
382 }
383 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100384 fprintf(stderr, "min value out of range: %d (%d min)\n",
385 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100386 return 1;
387 }
Jens Axboee1f36502006-10-27 10:54:08 +0200388
Jens Axboe76a43db2007-01-11 13:24:44 +0100389 if (o->neg)
390 il = !il;
391
Jens Axboee1f36502006-10-27 10:54:08 +0200392 if (fn)
393 ret = fn(data, &il);
394 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100395 if (first)
396 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100397 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100398 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200399 }
400 break;
401 }
402 case FIO_OPT_STR_SET: {
403 fio_opt_str_set_fn *fn = o->cb;
404
405 if (fn)
406 ret = fn(data);
407 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100408 if (first)
409 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100410 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100411 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200412 }
413 break;
414 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200415 case FIO_OPT_DEPRECATED:
416 fprintf(stdout, "Option %s is deprecated\n", o->name);
417 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200418 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100419 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200420 ret = 1;
421 }
422
Jens Axboee1f36502006-10-27 10:54:08 +0200423 return ret;
424}
425
Jens Axboef90eff52006-11-06 11:08:21 +0100426static int handle_option(struct fio_option *o, const char *ptr, void *data)
427{
Jens Axboe92b586f2006-11-07 14:29:17 +0100428 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100429 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100430
Jens Axboea3d741f2008-02-27 18:32:33 +0100431 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
432
Jens Axboef90eff52006-11-06 11:08:21 +0100433 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100434 * See if we have a second set of parameters, hidden after a comma.
435 * Do this before parsing the first round, to check if we should
436 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100437 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100438 if (ptr &&
439 (o->type != FIO_OPT_STR_STORE) &&
440 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100441 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100442 if (!ptr2)
443 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100444 if (!ptr2)
445 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100446 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100447
448 /*
449 * Don't return early if parsing the first option fails - if
450 * we are doing multiple arguments, we can allow the first one
451 * being empty.
452 */
453 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
454
Jens Axboef90eff52006-11-06 11:08:21 +0100455 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100456 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100457
458 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100459 r2 = __handle_option(o, ptr2, data, 0, 0);
460
461 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100462}
463
Jens Axboe3b8b7132008-06-10 19:46:23 +0200464static struct fio_option *get_option(const char *opt,
465 struct fio_option *options, char **post)
466{
467 struct fio_option *o;
468 char *ret;
469
470 ret = strchr(opt, '=');
471 if (ret) {
472 *post = ret;
473 *ret = '\0';
474 ret = (char *) opt;
475 (*post)++;
476 o = find_option(options, ret);
477 } else {
478 o = find_option(options, opt);
479 *post = NULL;
480 }
481
482 return o;
483}
484
485static int opt_cmp(const void *p1, const void *p2)
486{
487 struct fio_option *o1, *o2;
488 char *s1, *s2, *foo;
489 int ret;
490
491 s1 = strdup(*((char **) p1));
492 s2 = strdup(*((char **) p2));
493
494 o1 = get_option(s1, fio_options, &foo);
495 o2 = get_option(s2, fio_options, &foo);
496
497 if ((!o1 && o2) || (o1 && !o2))
498 ret = 0;
499 else
500 ret = o2->prio - o1->prio;
501
502 free(s1);
503 free(s2);
504 return ret;
505}
506
507void sort_options(char **opts, struct fio_option *options, int num_opts)
508{
509 fio_options = options;
510 qsort(opts, num_opts, sizeof(char *), opt_cmp);
511 fio_options = NULL;
512}
513
Jens Axboeb4692822006-10-27 13:43:22 +0200514int parse_cmd_option(const char *opt, const char *val,
515 struct fio_option *options, void *data)
516{
517 struct fio_option *o;
518
519 o = find_option(options, opt);
520 if (!o) {
521 fprintf(stderr, "Bad option %s\n", opt);
522 return 1;
523 }
524
Jens Axboeb1508cf2006-11-02 09:12:40 +0100525 if (!handle_option(o, val, data))
526 return 0;
527
528 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
529 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200530}
531
Jens Axboee1f36502006-10-27 10:54:08 +0200532int parse_option(const char *opt, struct fio_option *options, void *data)
533{
Jens Axboeb4692822006-10-27 13:43:22 +0200534 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200535 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200536
Jens Axboe0401bca2007-03-29 11:00:43 +0200537 tmp = strdup(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200538
Jens Axboe3b8b7132008-06-10 19:46:23 +0200539 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200540 if (!o) {
541 fprintf(stderr, "Bad option %s\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200542 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200543 return 1;
544 }
545
Jens Axboe0401bca2007-03-29 11:00:43 +0200546 if (!handle_option(o, post, data)) {
547 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100548 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200549 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100550
551 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200552 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100553 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200554}
Jens Axboefd28ca42007-01-09 21:20:13 +0100555
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100556/*
557 * Option match, levenshtein distance. Handy for not quite remembering what
558 * the option name is.
559 */
560static int string_distance(const char *s1, const char *s2)
561{
562 unsigned int s1_len = strlen(s1);
563 unsigned int s2_len = strlen(s2);
564 unsigned int *p, *q, *r;
565 unsigned int i, j;
566
567 p = malloc(sizeof(unsigned int) * (s2_len + 1));
568 q = malloc(sizeof(unsigned int) * (s2_len + 1));
569
570 p[0] = 0;
571 for (i = 1; i <= s2_len; i++)
572 p[i] = p[i - 1] + 1;
573
574 for (i = 1; i <= s1_len; i++) {
575 q[0] = p[0] + 1;
576 for (j = 1; j <= s2_len; j++) {
577 unsigned int sub = p[j - 1];
578
579 if (s1[i - 1] != s2[j - 1])
580 sub++;
581
582 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
583 }
584 r = p;
585 p = q;
586 q = r;
587 }
588
589 i = p[s2_len];
590 free(p);
591 free(q);
592 return i;
593}
594
595static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100596{
Jens Axboefd28ca42007-01-09 21:20:13 +0100597 const char *typehelp[] = {
598 "string (opt=bla)",
599 "string with possible k/m/g postfix (opt=4k)",
600 "string with range and postfix (opt=1k-4k)",
601 "string with time postfix (opt=10s)",
602 "string (opt=bla)",
603 "string with dual range (opt=1k-4k,4k-8k)",
604 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100605 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100606 "no argument (opt)",
607 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100608
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100609 if (o->alias)
610 printf("%20s: %s\n", "alias", o->alias);
611
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100612 printf("%20s: %s\n", "type", typehelp[o->type]);
613 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
614 show_option_range(o);
615 show_option_values(o);
616}
617
Jens Axboeafdf9352007-07-31 16:14:34 +0200618static struct fio_option *find_child(struct fio_option *options,
619 struct fio_option *o)
620{
621 struct fio_option *__o;
622
Jens Axboefdf28742007-07-31 23:06:09 +0200623 for (__o = options + 1; __o->name; __o++)
624 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200625 return __o;
626
627 return NULL;
628}
629
Jens Axboe323d9112008-03-01 15:12:14 +0100630static void __print_option(struct fio_option *o, struct fio_option *org,
631 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200632{
633 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100634 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200635
636 if (!o)
637 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200638 if (!org)
639 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100640
Jens Axboeafdf9352007-07-31 16:14:34 +0200641 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100642 depth = level;
643 while (depth--)
644 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200645
646 sprintf(p, "%s", o->name);
647
648 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100649}
650
651static void print_option(struct fio_option *o)
652{
653 struct fio_option *parent;
654 struct fio_option *__o;
655 unsigned int printed;
656 unsigned int level;
657
658 __print_option(o, NULL, 0);
659 parent = o;
660 level = 0;
661 do {
662 level++;
663 printed = 0;
664
665 while ((__o = find_child(o, parent)) != NULL) {
666 __print_option(__o, o, level);
667 o = __o;
668 printed++;
669 }
670
671 parent = o;
672 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200673}
674
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100675int show_cmd_help(struct fio_option *options, const char *name)
676{
677 struct fio_option *o, *closest;
678 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100679 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100680 int show_all = 0;
681
682 if (!name || !strcmp(name, "all"))
683 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100684
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100685 closest = NULL;
686 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100687 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100688 int match = 0;
689
Jens Axboe15ca1502008-04-07 09:26:02 +0200690 if (o->type == FIO_OPT_DEPRECATED)
691 continue;
692
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100693 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100694 if (!strcmp(name, o->name) ||
695 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100696 match = 1;
697 else {
698 unsigned int dist;
699
700 dist = string_distance(name, o->name);
701 if (dist < best_dist) {
702 best_dist = dist;
703 closest = o;
704 }
705 }
706 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100707
708 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100709 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100710 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200711 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100712 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200713 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100714 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100715 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100716 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100717 }
718
Jens Axboe70df2f12007-01-11 14:04:47 +0100719 if (!match)
720 continue;
721
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100722 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100723 }
724
Jens Axboe29fc6af2007-01-09 21:22:02 +0100725 if (found)
726 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100727
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100728 printf("No such command: %s", name);
729 if (closest) {
730 printf(" - showing closest match\n");
731 printf("%20s: %s\n", closest->name, closest->help);
732 show_option_help(closest);
733 } else
734 printf("\n");
735
Jens Axboe29fc6af2007-01-09 21:22:02 +0100736 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100737}
Jens Axboeee738492007-01-10 11:23:16 +0100738
Jens Axboe13335dd2007-01-10 13:14:02 +0100739/*
740 * Handle parsing of default parameters.
741 */
Jens Axboeee738492007-01-10 11:23:16 +0100742void fill_default_options(void *data, struct fio_option *options)
743{
Jens Axboe4945ba12007-01-11 14:36:56 +0100744 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100745
Jens Axboea3d741f2008-02-27 18:32:33 +0100746 dprint(FD_PARSE, "filling default options\n");
747
Jens Axboe4945ba12007-01-11 14:36:56 +0100748 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100749 if (o->def)
750 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100751}
Jens Axboe13335dd2007-01-10 13:14:02 +0100752
753/*
754 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100755 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100756 */
757void options_init(struct fio_option *options)
758{
Jens Axboe4945ba12007-01-11 14:36:56 +0100759 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100760
Jens Axboea3d741f2008-02-27 18:32:33 +0100761 dprint(FD_PARSE, "init options\n");
762
Jens Axboe4945ba12007-01-11 14:36:56 +0100763 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200764 if (o->type == FIO_OPT_DEPRECATED)
765 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100766 if (o->type == FIO_OPT_BOOL) {
767 o->minval = 0;
768 o->maxval = 1;
769 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100770 if (o->type == FIO_OPT_STR_SET && o->def) {
771 fprintf(stderr, "Option %s: string set option with"
772 " default will always be true\n",
773 o->name);
774 }
775 if (!o->cb && !o->off1) {
776 fprintf(stderr, "Option %s: neither cb nor offset"
777 " given\n", o->name);
778 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100779 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100780 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100781 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
782 fprintf(stderr, "Option %s: both cb and offset given\n",
783 o->name);
784 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100785 }
786}