blob: 8e29fabf8e64e87129d900fea6300a9b80a5daa7 [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 Axboef0857372007-03-19 13:15:23 +010015static int vp_cmp(const void *p1, const void *p2)
16{
17 const struct value_pair *vp1 = p1;
18 const struct value_pair *vp2 = p2;
19
20 return strlen(vp2->ival) - strlen(vp1->ival);
21}
22
23static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
24{
25 const struct value_pair *vp;
26 int entries;
27
28 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
29
30 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
31 vp = &o->posval[entries];
32 if (!vp->ival || vp->ival[0] == '\0')
33 break;
34
35 memcpy(&vpmap[entries], vp, sizeof(*vp));
36 }
37
38 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
39}
40
Jens Axboeb1ec1da2007-02-23 10:29:16 +010041static void show_option_range(struct fio_option *o)
42{
43 if (!o->minval && !o->maxval)
44 return;
45
Jens Axboeec1aee02007-02-26 13:18:47 +010046 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
Jens Axboeb1ec1da2007-02-23 10:29:16 +010047}
48
49static void show_option_values(struct fio_option *o)
50{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010051 int i = 0;
52
53 do {
Jens Axboe78372132007-03-14 13:24:07 +010054 const struct value_pair *vp = &o->posval[i];
55
56 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010057 break;
58
Jens Axboe78372132007-03-14 13:24:07 +010059 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
60 if (vp->help)
61 printf(" %s", vp->help);
62 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010063 i++;
64 } while (i < PARSE_MAX_VP);
65
66 if (i)
67 printf("\n");
68}
69
Jens Axboecb2c86f2006-10-26 13:48:34 +020070static unsigned long get_mult_time(char c)
71{
72 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010073 case 'm':
74 case 'M':
75 return 60;
76 case 'h':
77 case 'H':
78 return 60 * 60;
79 case 'd':
80 case 'D':
81 return 24 * 60 * 60;
82 default:
83 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020084 }
85}
86
87static unsigned long get_mult_bytes(char c)
88{
89 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010090 case 'k':
91 case 'K':
92 return 1024;
93 case 'm':
94 case 'M':
95 return 1024 * 1024;
96 case 'g':
97 case 'G':
98 return 1024 * 1024 * 1024;
99 case 'e':
100 case 'E':
101 return 1024 * 1024 * 1024 * 1024UL;
102 default:
103 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200104 }
105}
106
107/*
Jens Axboee1f36502006-10-27 10:54:08 +0200108 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200109 */
Jens Axboe564ca972007-12-14 12:21:19 +0100110int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200111{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200112 int len;
113
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100115 if (!len)
116 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117
Jens Axboe675de852007-02-10 19:01:07 +0100118 *val = strtoll(str, NULL, 10);
Jens Axboecda866c2007-01-10 16:02:32 +0100119 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200120 return 1;
121
122 if (kilo)
123 *val *= get_mult_bytes(str[len - 1]);
124 else
125 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100126
Jens Axboecb2c86f2006-10-26 13:48:34 +0200127 return 0;
128}
129
Jens Axboe63f29372007-01-10 13:20:09 +0100130static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200131{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200132 return str_to_decimal(p, val, 1);
133}
134
Jens Axboe63f29372007-01-10 13:20:09 +0100135static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200136{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200137 return str_to_decimal(p, val, 0);
138}
139
140void strip_blank_front(char **p)
141{
142 char *s = *p;
143
144 while (isspace(*s))
145 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200146
147 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200148}
149
150void strip_blank_end(char *p)
151{
Jens Axboe523bfad2007-04-04 11:04:06 +0200152 char *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200153
Jens Axboe523bfad2007-04-04 11:04:06 +0200154 s = strchr(p, ';');
155 if (s)
156 *s = '\0';
157 s = strchr(p, '#');
158 if (s)
159 *s = '\0';
160 if (s)
161 p = s;
162
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200163 s = p + strlen(p);
164 while ((isspace(*s) || iscntrl(*s)) && (s > p))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200165 s--;
166
167 *(s + 1) = '\0';
168}
169
Jens Axboe63f29372007-01-10 13:20:09 +0100170static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200171{
172 char suffix;
173
Jens Axboe787f7e92006-11-06 13:26:29 +0100174 if (!strlen(str))
175 return 1;
176
Jens Axboecb2c86f2006-10-26 13:48:34 +0200177 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
178 *val *= get_mult_bytes(suffix);
179 return 0;
180 }
181
182 if (sscanf(str, "%lu", val) == 1)
183 return 0;
184
185 return 1;
186}
187
Jens Axboe63f29372007-01-10 13:20:09 +0100188static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200189{
Jens Axboe787f7e92006-11-06 13:26:29 +0100190 if (!strlen(p))
191 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200192 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200193 if (sscanf(p, "%x", val) == 1)
194 return 0;
195 } else {
196 if (sscanf(p, "%u", val) == 1)
197 return 0;
198 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200199
200 return 1;
201}
202
Jens Axboee1f36502006-10-27 10:54:08 +0200203static struct fio_option *find_option(struct fio_option *options,
204 const char *opt)
205{
Jens Axboe4945ba12007-01-11 14:36:56 +0100206 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200207
Jens Axboe4945ba12007-01-11 14:36:56 +0100208 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200209 if (!strcmp(o->name, opt))
210 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100211 else if (o->alias && !strcmp(o->alias, opt))
212 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200213 }
Jens Axboee1f36502006-10-27 10:54:08 +0200214
215 return NULL;
216}
217
Jens Axboe17abbe82006-11-06 11:23:10 +0100218#define val_store(ptr, val, off, data) \
219 do { \
220 ptr = td_var((data), (off)); \
221 *ptr = (val); \
222 } while (0)
223
Jens Axboef90eff52006-11-06 11:08:21 +0100224static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100225 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200226{
Jens Axboe63f29372007-01-10 13:20:09 +0100227 int il, *ilp;
228 long long ull, *ullp;
229 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200230 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200231 int ret = 0, is_time = 0;
232
Jens Axboea3d741f2008-02-27 18:32:33 +0100233 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
234 o->type, ptr);
235
Jens Axboe08e26e32006-11-21 13:15:10 +0100236 if (!ptr && o->type != FIO_OPT_STR_SET) {
237 fprintf(stderr, "Option %s requires an argument\n", o->name);
238 return 1;
239 }
240
Jens Axboee1f36502006-10-27 10:54:08 +0200241 switch (o->type) {
242 case FIO_OPT_STR: {
243 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100244 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100245 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100246 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200247
Jens Axboef0857372007-03-19 13:15:23 +0100248 posval_sort(o, posval);
249
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100250 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100251 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100252 if (!vp->ival || vp->ival[0] == '\0')
253 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100254 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
256 ret = 0;
257 if (!o->off1)
258 break;
259 val_store(ilp, vp->oval, o->off1, data);
260 break;
261 }
262 }
263
264 if (ret)
265 show_option_values(o);
266 else if (fn)
267 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200268 break;
269 }
270 case FIO_OPT_STR_VAL_TIME:
271 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100272 case FIO_OPT_STR_VAL:
273 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200274 fio_opt_str_val_fn *fn = o->cb;
275
276 if (is_time)
277 ret = check_str_time(ptr, &ull);
278 else
279 ret = check_str_bytes(ptr, &ull);
280
281 if (ret)
282 break;
283
Jens Axboedb8e0162007-01-10 13:41:26 +0100284 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100285 fprintf(stderr, "max value out of range: %lld"
286 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100287 return 1;
288 }
289 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100290 fprintf(stderr, "min value out of range: %lld"
291 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100292 return 1;
293 }
Jens Axboee1f36502006-10-27 10:54:08 +0200294
295 if (fn)
296 ret = fn(data, &ull);
297 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100298 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100299 if (first)
300 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100301 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100302 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100303 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100304 if (first)
305 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100306 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100307 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100308 }
Jens Axboee1f36502006-10-27 10:54:08 +0200309 }
310 break;
311 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100312 case FIO_OPT_STR_STORE: {
313 fio_opt_str_fn *fn = o->cb;
314
Jens Axboee1f36502006-10-27 10:54:08 +0200315 cp = td_var(data, o->off1);
316 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100317 if (fn) {
318 ret = fn(data, ptr);
319 if (ret) {
320 free(*cp);
321 *cp = NULL;
322 }
323 }
Jens Axboee1f36502006-10-27 10:54:08 +0200324 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100325 }
Jens Axboee1f36502006-10-27 10:54:08 +0200326 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200327 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200328 char *p1, *p2;
329
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100330 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200331
332 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200333 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100334 p1 = strchr(tmp, ':');
335 if (!p1) {
336 ret = 1;
337 break;
338 }
Jens Axboee1f36502006-10-27 10:54:08 +0200339 }
340
341 p2 = p1 + 1;
342 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200343 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200344
345 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100346 if (!check_range_bytes(p1, &ul1) &&
347 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200348 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200349 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100350 unsigned long foo = ul1;
351
352 ul1 = ul2;
353 ul2 = foo;
354 }
355
356 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100357 val_store(ilp, ul1, o->off1, data);
358 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200359 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100360 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100361 val_store(ilp, ul1, o->off3, data);
362 val_store(ilp, ul2, o->off4, data);
363 }
364 }
365
Jens Axboee1f36502006-10-27 10:54:08 +0200366 break;
367 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100368 case FIO_OPT_INT:
369 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200370 fio_opt_int_fn *fn = o->cb;
371
372 ret = check_int(ptr, &il);
373 if (ret)
374 break;
375
Jens Axboedb8e0162007-01-10 13:41:26 +0100376 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100377 fprintf(stderr, "max value out of range: %d (%d max)\n",
378 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100379 return 1;
380 }
381 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100382 fprintf(stderr, "min value out of range: %d (%d min)\n",
383 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100384 return 1;
385 }
Jens Axboee1f36502006-10-27 10:54:08 +0200386
Jens Axboe76a43db2007-01-11 13:24:44 +0100387 if (o->neg)
388 il = !il;
389
Jens Axboee1f36502006-10-27 10:54:08 +0200390 if (fn)
391 ret = fn(data, &il);
392 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100393 if (first)
394 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100395 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100396 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200397 }
398 break;
399 }
400 case FIO_OPT_STR_SET: {
401 fio_opt_str_set_fn *fn = o->cb;
402
403 if (fn)
404 ret = fn(data);
405 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100406 if (first)
407 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100408 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100409 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200410 }
411 break;
412 }
413 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100414 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200415 ret = 1;
416 }
417
Jens Axboee1f36502006-10-27 10:54:08 +0200418 return ret;
419}
420
Jens Axboef90eff52006-11-06 11:08:21 +0100421static int handle_option(struct fio_option *o, const char *ptr, void *data)
422{
Jens Axboe92b586f2006-11-07 14:29:17 +0100423 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100424 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100425
Jens Axboea3d741f2008-02-27 18:32:33 +0100426 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
427
Jens Axboef90eff52006-11-06 11:08:21 +0100428 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100429 * See if we have a second set of parameters, hidden after a comma.
430 * Do this before parsing the first round, to check if we should
431 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100432 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100433 if (ptr &&
434 (o->type != FIO_OPT_STR_STORE) &&
435 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100436 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100437 if (!ptr2)
438 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100439 if (!ptr2)
440 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100441 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100442
443 /*
444 * Don't return early if parsing the first option fails - if
445 * we are doing multiple arguments, we can allow the first one
446 * being empty.
447 */
448 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
449
Jens Axboef90eff52006-11-06 11:08:21 +0100450 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100451 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100452
453 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100454 r2 = __handle_option(o, ptr2, data, 0, 0);
455
456 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100457}
458
Jens Axboeb4692822006-10-27 13:43:22 +0200459int parse_cmd_option(const char *opt, const char *val,
460 struct fio_option *options, void *data)
461{
462 struct fio_option *o;
463
464 o = find_option(options, opt);
465 if (!o) {
466 fprintf(stderr, "Bad option %s\n", opt);
467 return 1;
468 }
469
Jens Axboeb1508cf2006-11-02 09:12:40 +0100470 if (!handle_option(o, val, data))
471 return 0;
472
473 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
474 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200475}
476
Jens Axboee1f36502006-10-27 10:54:08 +0200477int parse_option(const char *opt, struct fio_option *options, void *data)
478{
Jens Axboeb4692822006-10-27 13:43:22 +0200479 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200480 char *pre, *post;
Jens Axboe0401bca2007-03-29 11:00:43 +0200481 char *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200482
Jens Axboe0401bca2007-03-29 11:00:43 +0200483 tmp = strdup(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200484
485 pre = strchr(tmp, '=');
486 if (pre) {
487 post = pre;
488 *pre = '\0';
489 pre = tmp;
490 post++;
491 o = find_option(options, pre);
492 } else {
493 o = find_option(options, tmp);
494 post = NULL;
495 }
496
497 if (!o) {
498 fprintf(stderr, "Bad option %s\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200499 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200500 return 1;
501 }
502
Jens Axboe0401bca2007-03-29 11:00:43 +0200503 if (!handle_option(o, post, data)) {
504 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100505 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200506 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100507
508 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200509 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100510 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200511}
Jens Axboefd28ca42007-01-09 21:20:13 +0100512
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100513/*
514 * Option match, levenshtein distance. Handy for not quite remembering what
515 * the option name is.
516 */
517static int string_distance(const char *s1, const char *s2)
518{
519 unsigned int s1_len = strlen(s1);
520 unsigned int s2_len = strlen(s2);
521 unsigned int *p, *q, *r;
522 unsigned int i, j;
523
524 p = malloc(sizeof(unsigned int) * (s2_len + 1));
525 q = malloc(sizeof(unsigned int) * (s2_len + 1));
526
527 p[0] = 0;
528 for (i = 1; i <= s2_len; i++)
529 p[i] = p[i - 1] + 1;
530
531 for (i = 1; i <= s1_len; i++) {
532 q[0] = p[0] + 1;
533 for (j = 1; j <= s2_len; j++) {
534 unsigned int sub = p[j - 1];
535
536 if (s1[i - 1] != s2[j - 1])
537 sub++;
538
539 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
540 }
541 r = p;
542 p = q;
543 q = r;
544 }
545
546 i = p[s2_len];
547 free(p);
548 free(q);
549 return i;
550}
551
552static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100553{
Jens Axboefd28ca42007-01-09 21:20:13 +0100554 const char *typehelp[] = {
555 "string (opt=bla)",
556 "string with possible k/m/g postfix (opt=4k)",
557 "string with range and postfix (opt=1k-4k)",
558 "string with time postfix (opt=10s)",
559 "string (opt=bla)",
560 "string with dual range (opt=1k-4k,4k-8k)",
561 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100562 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100563 "no argument (opt)",
564 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100565
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100566 if (o->alias)
567 printf("%20s: %s\n", "alias", o->alias);
568
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100569 printf("%20s: %s\n", "type", typehelp[o->type]);
570 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
571 show_option_range(o);
572 show_option_values(o);
573}
574
Jens Axboeafdf9352007-07-31 16:14:34 +0200575static struct fio_option *find_child(struct fio_option *options,
576 struct fio_option *o)
577{
578 struct fio_option *__o;
579
Jens Axboefdf28742007-07-31 23:06:09 +0200580 for (__o = options + 1; __o->name; __o++)
581 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200582 return __o;
583
584 return NULL;
585}
586
Jens Axboe323d9112008-03-01 15:12:14 +0100587static void __print_option(struct fio_option *o, struct fio_option *org,
588 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200589{
590 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100591 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200592
593 if (!o)
594 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200595 if (!org)
596 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100597
Jens Axboeafdf9352007-07-31 16:14:34 +0200598 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100599 depth = level;
600 while (depth--)
601 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200602
603 sprintf(p, "%s", o->name);
604
605 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100606}
607
608static void print_option(struct fio_option *o)
609{
610 struct fio_option *parent;
611 struct fio_option *__o;
612 unsigned int printed;
613 unsigned int level;
614
615 __print_option(o, NULL, 0);
616 parent = o;
617 level = 0;
618 do {
619 level++;
620 printed = 0;
621
622 while ((__o = find_child(o, parent)) != NULL) {
623 __print_option(__o, o, level);
624 o = __o;
625 printed++;
626 }
627
628 parent = o;
629 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200630}
631
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100632int show_cmd_help(struct fio_option *options, const char *name)
633{
634 struct fio_option *o, *closest;
635 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100636 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100637 int show_all = 0;
638
639 if (!name || !strcmp(name, "all"))
640 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100641
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100642 closest = NULL;
643 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100644 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100645 int match = 0;
646
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100647 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100648 if (!strcmp(name, o->name) ||
649 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100650 match = 1;
651 else {
652 unsigned int dist;
653
654 dist = string_distance(name, o->name);
655 if (dist < best_dist) {
656 best_dist = dist;
657 closest = o;
658 }
659 }
660 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100661
662 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100663 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100664 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200665 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100666 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200667 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100668 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100669 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100670 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100671 }
672
Jens Axboe70df2f12007-01-11 14:04:47 +0100673 if (!match)
674 continue;
675
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100676 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100677 }
678
Jens Axboe29fc6af2007-01-09 21:22:02 +0100679 if (found)
680 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100681
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100682 printf("No such command: %s", name);
683 if (closest) {
684 printf(" - showing closest match\n");
685 printf("%20s: %s\n", closest->name, closest->help);
686 show_option_help(closest);
687 } else
688 printf("\n");
689
Jens Axboe29fc6af2007-01-09 21:22:02 +0100690 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100691}
Jens Axboeee738492007-01-10 11:23:16 +0100692
Jens Axboe13335dd2007-01-10 13:14:02 +0100693/*
694 * Handle parsing of default parameters.
695 */
Jens Axboeee738492007-01-10 11:23:16 +0100696void fill_default_options(void *data, struct fio_option *options)
697{
Jens Axboe4945ba12007-01-11 14:36:56 +0100698 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100699
Jens Axboea3d741f2008-02-27 18:32:33 +0100700 dprint(FD_PARSE, "filling default options\n");
701
Jens Axboe4945ba12007-01-11 14:36:56 +0100702 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100703 if (o->def)
704 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100705}
Jens Axboe13335dd2007-01-10 13:14:02 +0100706
707/*
708 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100709 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100710 */
711void options_init(struct fio_option *options)
712{
Jens Axboe4945ba12007-01-11 14:36:56 +0100713 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100714
Jens Axboea3d741f2008-02-27 18:32:33 +0100715 dprint(FD_PARSE, "init options\n");
716
Jens Axboe4945ba12007-01-11 14:36:56 +0100717 for (o = &options[0]; o->name; o++) {
Jens Axboe13335dd2007-01-10 13:14:02 +0100718 if (o->type == FIO_OPT_BOOL) {
719 o->minval = 0;
720 o->maxval = 1;
721 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100722 if (o->type == FIO_OPT_STR_SET && o->def) {
723 fprintf(stderr, "Option %s: string set option with"
724 " default will always be true\n",
725 o->name);
726 }
727 if (!o->cb && !o->off1) {
728 fprintf(stderr, "Option %s: neither cb nor offset"
729 " given\n", o->name);
730 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100731 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100732 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100733 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
734 fprintf(stderr, "Option %s: both cb and offset given\n",
735 o->name);
736 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100737 }
738}