blob: 1071d4031693449fe165fc1852edad4596ee9d91 [file] [log] [blame]
Jens Axboecb2c86f2006-10-26 13:48:34 +02001/*
2 * This file contains the ini and command liner parser main.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <ctype.h>
8#include <string.h>
9#include <errno.h>
10#include <limits.h>
Aaron Carroll88b5a392008-10-07 11:25:20 +020011#include <stdlib.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020012
13#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010014#include "debug.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020015
Jens Axboe3b8b7132008-06-10 19:46:23 +020016static struct fio_option *fio_options;
17
Jens Axboef0857372007-03-19 13:15:23 +010018static int vp_cmp(const void *p1, const void *p2)
19{
20 const struct value_pair *vp1 = p1;
21 const struct value_pair *vp2 = p2;
22
23 return strlen(vp2->ival) - strlen(vp1->ival);
24}
25
26static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
27{
28 const struct value_pair *vp;
29 int entries;
30
31 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
32
33 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
34 vp = &o->posval[entries];
35 if (!vp->ival || vp->ival[0] == '\0')
36 break;
37
38 memcpy(&vpmap[entries], vp, sizeof(*vp));
39 }
40
41 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
42}
43
Jens Axboeb1ec1da2007-02-23 10:29:16 +010044static void show_option_range(struct fio_option *o)
45{
46 if (!o->minval && !o->maxval)
47 return;
48
Jens Axboeec1aee02007-02-26 13:18:47 +010049 printf("%20s: min=%d, max=%d\n", "range", o->minval, o->maxval);
Jens Axboeb1ec1da2007-02-23 10:29:16 +010050}
51
52static void show_option_values(struct fio_option *o)
53{
Jens Axboeb1ec1da2007-02-23 10:29:16 +010054 int i = 0;
55
56 do {
Jens Axboe78372132007-03-14 13:24:07 +010057 const struct value_pair *vp = &o->posval[i];
58
59 if (!vp->ival)
Jens Axboeb1ec1da2007-02-23 10:29:16 +010060 break;
61
Jens Axboe78372132007-03-14 13:24:07 +010062 printf("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
63 if (vp->help)
64 printf(" %s", vp->help);
65 printf("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010066 i++;
67 } while (i < PARSE_MAX_VP);
68
69 if (i)
70 printf("\n");
71}
72
Jens Axboecb2c86f2006-10-26 13:48:34 +020073static unsigned long get_mult_time(char c)
74{
75 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010076 case 'm':
77 case 'M':
78 return 60;
79 case 'h':
80 case 'H':
81 return 60 * 60;
82 case 'd':
83 case 'D':
84 return 24 * 60 * 60;
85 default:
86 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +020087 }
88}
89
90static unsigned long get_mult_bytes(char c)
91{
92 switch (c) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010093 case 'k':
94 case 'K':
95 return 1024;
96 case 'm':
97 case 'M':
98 return 1024 * 1024;
99 case 'g':
100 case 'G':
101 return 1024 * 1024 * 1024;
102 case 'e':
103 case 'E':
104 return 1024 * 1024 * 1024 * 1024UL;
105 default:
106 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200107 }
108}
109
110/*
Jens Axboee1f36502006-10-27 10:54:08 +0200111 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200112 */
Jens Axboe564ca972007-12-14 12:21:19 +0100113int str_to_decimal(const char *str, long long *val, int kilo)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200114{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100115 int len, base;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200116
Jens Axboecb2c86f2006-10-26 13:48:34 +0200117 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100118 if (!len)
119 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200120
Jens Axboeb347f9d2009-03-09 14:15:21 +0100121 if (strstr(str, "0x") || strstr(str, "0X"))
122 base = 16;
123 else
124 base = 10;
125
126 *val = strtoll(str, NULL, base);
Jens Axboecda866c2007-01-10 16:02:32 +0100127 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200128 return 1;
129
130 if (kilo)
131 *val *= get_mult_bytes(str[len - 1]);
132 else
133 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100134
Jens Axboecb2c86f2006-10-26 13:48:34 +0200135 return 0;
136}
137
Jens Axboe63f29372007-01-10 13:20:09 +0100138static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200139{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200140 return str_to_decimal(p, val, 1);
141}
142
Jens Axboe63f29372007-01-10 13:20:09 +0100143static int check_str_time(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200144{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200145 return str_to_decimal(p, val, 0);
146}
147
148void strip_blank_front(char **p)
149{
150 char *s = *p;
151
152 while (isspace(*s))
153 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200154
155 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156}
157
158void strip_blank_end(char *p)
159{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100160 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200161
Jens Axboe523bfad2007-04-04 11:04:06 +0200162 s = strchr(p, ';');
163 if (s)
164 *s = '\0';
165 s = strchr(p, '#');
166 if (s)
167 *s = '\0';
168 if (s)
169 p = s;
170
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200171 s = p + strlen(p);
Jens Axboe853ee7f2009-03-06 20:29:29 +0100172 while ((isspace(*s) || iscntrl(*s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200173 s--;
174
175 *(s + 1) = '\0';
176}
177
Jens Axboe63f29372007-01-10 13:20:09 +0100178static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200179{
180 char suffix;
181
Jens Axboe787f7e92006-11-06 13:26:29 +0100182 if (!strlen(str))
183 return 1;
184
Jens Axboecb2c86f2006-10-26 13:48:34 +0200185 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
186 *val *= get_mult_bytes(suffix);
187 return 0;
188 }
189
190 if (sscanf(str, "%lu", val) == 1)
191 return 0;
192
193 return 1;
194}
195
Jens Axboe63f29372007-01-10 13:20:09 +0100196static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200197{
Jens Axboe787f7e92006-11-06 13:26:29 +0100198 if (!strlen(p))
199 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200200 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200201 if (sscanf(p, "%x", val) == 1)
202 return 0;
203 } else {
204 if (sscanf(p, "%u", val) == 1)
205 return 0;
206 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200207
208 return 1;
209}
210
Jens Axboee1f36502006-10-27 10:54:08 +0200211static struct fio_option *find_option(struct fio_option *options,
212 const char *opt)
213{
Jens Axboe4945ba12007-01-11 14:36:56 +0100214 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200215
Jens Axboe4945ba12007-01-11 14:36:56 +0100216 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200217 if (!strcmp(o->name, opt))
218 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100219 else if (o->alias && !strcmp(o->alias, opt))
220 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200221 }
Jens Axboee1f36502006-10-27 10:54:08 +0200222
223 return NULL;
224}
225
Jens Axboe17abbe82006-11-06 11:23:10 +0100226#define val_store(ptr, val, off, data) \
227 do { \
228 ptr = td_var((data), (off)); \
229 *ptr = (val); \
230 } while (0)
231
Jens Axboef90eff52006-11-06 11:08:21 +0100232static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100233 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200234{
Jens Axboe63f29372007-01-10 13:20:09 +0100235 int il, *ilp;
236 long long ull, *ullp;
237 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200238 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200239 int ret = 0, is_time = 0;
240
Jens Axboea3d741f2008-02-27 18:32:33 +0100241 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
242 o->type, ptr);
243
Jens Axboee3cedca2008-11-19 19:57:52 +0100244 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100245 fprintf(stderr, "Option %s requires an argument\n", o->name);
246 return 1;
247 }
248
Jens Axboee1f36502006-10-27 10:54:08 +0200249 switch (o->type) {
250 case FIO_OPT_STR: {
251 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100252 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100253 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100254 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200255
Jens Axboef0857372007-03-19 13:15:23 +0100256 posval_sort(o, posval);
257
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100258 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100259 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100260 if (!vp->ival || vp->ival[0] == '\0')
261 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100262 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100263 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
264 ret = 0;
265 if (!o->off1)
266 break;
267 val_store(ilp, vp->oval, o->off1, data);
268 break;
269 }
270 }
271
272 if (ret)
273 show_option_values(o);
274 else if (fn)
275 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200276 break;
277 }
278 case FIO_OPT_STR_VAL_TIME:
279 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100280 case FIO_OPT_INT:
Jens Axboee01b22b2009-06-09 15:43:25 +0200281 case FIO_OPT_STR_VAL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200282 fio_opt_str_val_fn *fn = o->cb;
283
284 if (is_time)
285 ret = check_str_time(ptr, &ull);
286 else
287 ret = check_str_bytes(ptr, &ull);
288
289 if (ret)
290 break;
291
Jens Axboedb8e0162007-01-10 13:41:26 +0100292 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100293 fprintf(stderr, "max value out of range: %lld"
294 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100295 return 1;
296 }
297 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100298 fprintf(stderr, "min value out of range: %lld"
299 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100300 return 1;
301 }
Jens Axboee1f36502006-10-27 10:54:08 +0200302
303 if (fn)
304 ret = fn(data, &ull);
305 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200306 if (o->type == FIO_OPT_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100307 if (first)
308 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100309 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100310 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100311 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100312 if (first)
313 val_store(ullp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100314 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100315 val_store(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100316 }
Jens Axboee1f36502006-10-27 10:54:08 +0200317 }
318 break;
319 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100320 case FIO_OPT_STR_STORE: {
321 fio_opt_str_fn *fn = o->cb;
322
Jens Axboee1f36502006-10-27 10:54:08 +0200323 cp = td_var(data, o->off1);
324 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100325 if (fn) {
326 ret = fn(data, ptr);
327 if (ret) {
328 free(*cp);
329 *cp = NULL;
330 }
331 }
Jens Axboee1f36502006-10-27 10:54:08 +0200332 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100333 }
Jens Axboee1f36502006-10-27 10:54:08 +0200334 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200335 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200336 char *p1, *p2;
337
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100338 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200339
340 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200341 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100342 p1 = strchr(tmp, ':');
343 if (!p1) {
344 ret = 1;
345 break;
346 }
Jens Axboee1f36502006-10-27 10:54:08 +0200347 }
348
349 p2 = p1 + 1;
350 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200351 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200352
353 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100354 if (!check_range_bytes(p1, &ul1) &&
355 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200356 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200357 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100358 unsigned long foo = ul1;
359
360 ul1 = ul2;
361 ul2 = foo;
362 }
363
364 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100365 val_store(ilp, ul1, o->off1, data);
366 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200367 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100368 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100369 val_store(ilp, ul1, o->off3, data);
370 val_store(ilp, ul2, o->off4, data);
371 }
372 }
373
Jens Axboee1f36502006-10-27 10:54:08 +0200374 break;
375 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100376 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200377 fio_opt_int_fn *fn = o->cb;
378
379 ret = check_int(ptr, &il);
380 if (ret)
381 break;
382
Jens Axboedb8e0162007-01-10 13:41:26 +0100383 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100384 fprintf(stderr, "max value out of range: %d (%d max)\n",
385 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100386 return 1;
387 }
388 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100389 fprintf(stderr, "min value out of range: %d (%d min)\n",
390 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100391 return 1;
392 }
Jens Axboee1f36502006-10-27 10:54:08 +0200393
Jens Axboe76a43db2007-01-11 13:24:44 +0100394 if (o->neg)
395 il = !il;
396
Jens Axboee1f36502006-10-27 10:54:08 +0200397 if (fn)
398 ret = fn(data, &il);
399 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100400 if (first)
401 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100402 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100403 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200404 }
405 break;
406 }
407 case FIO_OPT_STR_SET: {
408 fio_opt_str_set_fn *fn = o->cb;
409
410 if (fn)
411 ret = fn(data);
412 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100413 if (first)
414 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100415 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100416 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200417 }
418 break;
419 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200420 case FIO_OPT_DEPRECATED:
421 fprintf(stdout, "Option %s is deprecated\n", o->name);
422 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200423 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100424 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200425 ret = 1;
426 }
427
Jens Axboee1f36502006-10-27 10:54:08 +0200428 return ret;
429}
430
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200431static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100432{
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200433 char *ptr, *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100434 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100435
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200436 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
437
438 ptr = NULL;
439 if (__ptr)
440 ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100441
Jens Axboef90eff52006-11-06 11:08:21 +0100442 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100443 * See if we have a second set of parameters, hidden after a comma.
444 * Do this before parsing the first round, to check if we should
445 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100446 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100447 if (ptr &&
448 (o->type != FIO_OPT_STR_STORE) &&
449 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100450 ptr2 = strchr(ptr, ',');
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200451 if (ptr2 && *(ptr2 + 1) == '\0')
452 *ptr2 = '\0';
Jens Axboe0c9baf92007-01-11 15:59:26 +0100453 if (!ptr2)
454 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100455 if (!ptr2)
456 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100457 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100458
459 /*
460 * Don't return early if parsing the first option fails - if
461 * we are doing multiple arguments, we can allow the first one
462 * being empty.
463 */
464 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
465
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200466 if (!ptr2) {
467 if (ptr)
468 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100469 return r1;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200470 }
Jens Axboef90eff52006-11-06 11:08:21 +0100471
472 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100473 r2 = __handle_option(o, ptr2, data, 0, 0);
474
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200475 if (ptr)
476 free(ptr);
Jens Axboe787f7e92006-11-06 13:26:29 +0100477 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100478}
479
Jens Axboe3b8b7132008-06-10 19:46:23 +0200480static struct fio_option *get_option(const char *opt,
481 struct fio_option *options, char **post)
482{
483 struct fio_option *o;
484 char *ret;
485
486 ret = strchr(opt, '=');
487 if (ret) {
488 *post = ret;
489 *ret = '\0';
490 ret = (char *) opt;
491 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100492 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200493 o = find_option(options, ret);
494 } else {
495 o = find_option(options, opt);
496 *post = NULL;
497 }
498
499 return o;
500}
501
502static int opt_cmp(const void *p1, const void *p2)
503{
504 struct fio_option *o1, *o2;
505 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100506 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200507
508 s1 = strdup(*((char **) p1));
509 s2 = strdup(*((char **) p2));
510
511 o1 = get_option(s1, fio_options, &foo);
512 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100513
514 prio1 = prio2 = 0;
515 if (o1)
516 prio1 = o1->prio;
517 if (o2)
518 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200519
520 free(s1);
521 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100522 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200523}
524
525void sort_options(char **opts, struct fio_option *options, int num_opts)
526{
527 fio_options = options;
528 qsort(opts, num_opts, sizeof(char *), opt_cmp);
529 fio_options = NULL;
530}
531
Jens Axboeb4692822006-10-27 13:43:22 +0200532int parse_cmd_option(const char *opt, const char *val,
533 struct fio_option *options, void *data)
534{
535 struct fio_option *o;
536
537 o = find_option(options, opt);
538 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100539 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200540 return 1;
541 }
542
Jens Axboeb1508cf2006-11-02 09:12:40 +0100543 if (!handle_option(o, val, data))
544 return 0;
545
546 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
547 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200548}
549
Aaron Carroll88b5a392008-10-07 11:25:20 +0200550/*
551 * Return a copy of the input string with substrings of the form ${VARNAME}
552 * substituted with the value of the environment variable VARNAME. The
553 * substitution always occurs, even if VARNAME is empty or the corresponding
554 * environment variable undefined.
555 */
556static char *option_dup_subs(const char *opt)
557{
558 char out[OPT_LEN_MAX+1];
559 char in[OPT_LEN_MAX+1];
560 char *outptr = out;
561 char *inptr = in;
562 char *ch1, *ch2, *env;
563 ssize_t nchr = OPT_LEN_MAX;
564 size_t envlen;
565
566 in[OPT_LEN_MAX] = '\0';
567 strncpy(in, opt, OPT_LEN_MAX);
568
569 while (*inptr && nchr > 0) {
570 if (inptr[0] == '$' && inptr[1] == '{') {
571 ch2 = strchr(inptr, '}');
572 if (ch2 && inptr+1 < ch2) {
573 ch1 = inptr+2;
574 inptr = ch2+1;
575 *ch2 = '\0';
576
577 env = getenv(ch1);
578 if (env) {
579 envlen = strlen(env);
580 if (envlen <= nchr) {
581 memcpy(outptr, env, envlen);
582 outptr += envlen;
583 nchr -= envlen;
584 }
585 }
586
587 continue;
588 }
589 }
590
591 *outptr++ = *inptr++;
592 --nchr;
593 }
594
595 *outptr = '\0';
596 return strdup(out);
597}
598
Jens Axboee1f36502006-10-27 10:54:08 +0200599int parse_option(const char *opt, struct fio_option *options, void *data)
600{
Jens Axboeb4692822006-10-27 13:43:22 +0200601 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200602 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200603
Aaron Carroll88b5a392008-10-07 11:25:20 +0200604 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200605
Jens Axboe3b8b7132008-06-10 19:46:23 +0200606 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200607 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100608 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200609 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200610 return 1;
611 }
612
Jens Axboe0401bca2007-03-29 11:00:43 +0200613 if (!handle_option(o, post, data)) {
614 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100615 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200616 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100617
618 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200619 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100620 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200621}
Jens Axboefd28ca42007-01-09 21:20:13 +0100622
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100623/*
624 * Option match, levenshtein distance. Handy for not quite remembering what
625 * the option name is.
626 */
627static int string_distance(const char *s1, const char *s2)
628{
629 unsigned int s1_len = strlen(s1);
630 unsigned int s2_len = strlen(s2);
631 unsigned int *p, *q, *r;
632 unsigned int i, j;
633
634 p = malloc(sizeof(unsigned int) * (s2_len + 1));
635 q = malloc(sizeof(unsigned int) * (s2_len + 1));
636
637 p[0] = 0;
638 for (i = 1; i <= s2_len; i++)
639 p[i] = p[i - 1] + 1;
640
641 for (i = 1; i <= s1_len; i++) {
642 q[0] = p[0] + 1;
643 for (j = 1; j <= s2_len; j++) {
644 unsigned int sub = p[j - 1];
645
646 if (s1[i - 1] != s2[j - 1])
647 sub++;
648
649 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
650 }
651 r = p;
652 p = q;
653 q = r;
654 }
655
656 i = p[s2_len];
657 free(p);
658 free(q);
659 return i;
660}
661
662static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100663{
Jens Axboefd28ca42007-01-09 21:20:13 +0100664 const char *typehelp[] = {
665 "string (opt=bla)",
666 "string with possible k/m/g postfix (opt=4k)",
667 "string with range and postfix (opt=1k-4k)",
668 "string with time postfix (opt=10s)",
669 "string (opt=bla)",
670 "string with dual range (opt=1k-4k,4k-8k)",
671 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100672 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100673 "no argument (opt)",
674 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100675
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100676 if (o->alias)
677 printf("%20s: %s\n", "alias", o->alias);
678
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100679 printf("%20s: %s\n", "type", typehelp[o->type]);
680 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
681 show_option_range(o);
682 show_option_values(o);
683}
684
Jens Axboeafdf9352007-07-31 16:14:34 +0200685static struct fio_option *find_child(struct fio_option *options,
686 struct fio_option *o)
687{
688 struct fio_option *__o;
689
Jens Axboefdf28742007-07-31 23:06:09 +0200690 for (__o = options + 1; __o->name; __o++)
691 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200692 return __o;
693
694 return NULL;
695}
696
Jens Axboe323d9112008-03-01 15:12:14 +0100697static void __print_option(struct fio_option *o, struct fio_option *org,
698 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200699{
700 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100701 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200702
703 if (!o)
704 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200705 if (!org)
706 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100707
Jens Axboeafdf9352007-07-31 16:14:34 +0200708 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100709 depth = level;
710 while (depth--)
711 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200712
713 sprintf(p, "%s", o->name);
714
715 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100716}
717
718static void print_option(struct fio_option *o)
719{
720 struct fio_option *parent;
721 struct fio_option *__o;
722 unsigned int printed;
723 unsigned int level;
724
725 __print_option(o, NULL, 0);
726 parent = o;
727 level = 0;
728 do {
729 level++;
730 printed = 0;
731
732 while ((__o = find_child(o, parent)) != NULL) {
733 __print_option(__o, o, level);
734 o = __o;
735 printed++;
736 }
737
738 parent = o;
739 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200740}
741
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100742int show_cmd_help(struct fio_option *options, const char *name)
743{
744 struct fio_option *o, *closest;
745 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100746 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100747 int show_all = 0;
748
749 if (!name || !strcmp(name, "all"))
750 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100751
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100752 closest = NULL;
753 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100754 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100755 int match = 0;
756
Jens Axboe15ca1502008-04-07 09:26:02 +0200757 if (o->type == FIO_OPT_DEPRECATED)
758 continue;
759
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100760 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100761 if (!strcmp(name, o->name) ||
762 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100763 match = 1;
764 else {
765 unsigned int dist;
766
767 dist = string_distance(name, o->name);
768 if (dist < best_dist) {
769 best_dist = dist;
770 closest = o;
771 }
772 }
773 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100774
775 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100776 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100777 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200778 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100779 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200780 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100781 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100782 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100783 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100784 }
785
Jens Axboe70df2f12007-01-11 14:04:47 +0100786 if (!match)
787 continue;
788
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100789 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100790 }
791
Jens Axboe29fc6af2007-01-09 21:22:02 +0100792 if (found)
793 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100794
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100795 printf("No such command: %s", name);
796 if (closest) {
797 printf(" - showing closest match\n");
798 printf("%20s: %s\n", closest->name, closest->help);
799 show_option_help(closest);
800 } else
801 printf("\n");
802
Jens Axboe29fc6af2007-01-09 21:22:02 +0100803 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100804}
Jens Axboeee738492007-01-10 11:23:16 +0100805
Jens Axboe13335dd2007-01-10 13:14:02 +0100806/*
807 * Handle parsing of default parameters.
808 */
Jens Axboeee738492007-01-10 11:23:16 +0100809void fill_default_options(void *data, struct fio_option *options)
810{
Jens Axboe4945ba12007-01-11 14:36:56 +0100811 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100812
Jens Axboea3d741f2008-02-27 18:32:33 +0100813 dprint(FD_PARSE, "filling default options\n");
814
Jens Axboe4945ba12007-01-11 14:36:56 +0100815 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100816 if (o->def)
817 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100818}
Jens Axboe13335dd2007-01-10 13:14:02 +0100819
820/*
821 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100822 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100823 */
824void options_init(struct fio_option *options)
825{
Jens Axboe4945ba12007-01-11 14:36:56 +0100826 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100827
Jens Axboea3d741f2008-02-27 18:32:33 +0100828 dprint(FD_PARSE, "init options\n");
829
Jens Axboe4945ba12007-01-11 14:36:56 +0100830 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200831 if (o->type == FIO_OPT_DEPRECATED)
832 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100833 if (o->type == FIO_OPT_BOOL) {
834 o->minval = 0;
835 o->maxval = 1;
836 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100837 if (o->type == FIO_OPT_STR_SET && o->def) {
838 fprintf(stderr, "Option %s: string set option with"
839 " default will always be true\n",
840 o->name);
841 }
842 if (!o->cb && !o->off1) {
843 fprintf(stderr, "Option %s: neither cb nor offset"
844 " given\n", o->name);
845 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100846 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100847 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100848 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
849 fprintf(stderr, "Option %s: both cb and offset given\n",
850 o->name);
851 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100852 }
853}