blob: f838a1f625e111f6aa55a5bdf221a4f6352319a4 [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 Axboecb2c86f2006-10-26 13:48:34 +0200115 int len;
116
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 Axboe675de852007-02-10 19:01:07 +0100121 *val = strtoll(str, NULL, 10);
Jens Axboecda866c2007-01-10 16:02:32 +0100122 if (*val == LONG_MAX && errno == ERANGE)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200123 return 1;
124
125 if (kilo)
126 *val *= get_mult_bytes(str[len - 1]);
127 else
128 *val *= get_mult_time(str[len - 1]);
Jens Axboe787f7e92006-11-06 13:26:29 +0100129
Jens Axboecb2c86f2006-10-26 13:48:34 +0200130 return 0;
131}
132
Jens Axboe63f29372007-01-10 13:20:09 +0100133static int check_str_bytes(const char *p, long long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200134{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200135 return str_to_decimal(p, val, 1);
136}
137
Jens Axboe63f29372007-01-10 13:20:09 +0100138static int check_str_time(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, 0);
141}
142
143void strip_blank_front(char **p)
144{
145 char *s = *p;
146
147 while (isspace(*s))
148 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200149
150 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200151}
152
153void strip_blank_end(char *p)
154{
Jens Axboe523bfad2007-04-04 11:04:06 +0200155 char *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200156
Jens Axboe523bfad2007-04-04 11:04:06 +0200157 s = strchr(p, ';');
158 if (s)
159 *s = '\0';
160 s = strchr(p, '#');
161 if (s)
162 *s = '\0';
163 if (s)
164 p = s;
165
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200166 s = p + strlen(p);
167 while ((isspace(*s) || iscntrl(*s)) && (s > p))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200168 s--;
169
170 *(s + 1) = '\0';
171}
172
Jens Axboe63f29372007-01-10 13:20:09 +0100173static int check_range_bytes(const char *str, long *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200174{
175 char suffix;
176
Jens Axboe787f7e92006-11-06 13:26:29 +0100177 if (!strlen(str))
178 return 1;
179
Jens Axboecb2c86f2006-10-26 13:48:34 +0200180 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
181 *val *= get_mult_bytes(suffix);
182 return 0;
183 }
184
185 if (sscanf(str, "%lu", val) == 1)
186 return 0;
187
188 return 1;
189}
190
Jens Axboe63f29372007-01-10 13:20:09 +0100191static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200192{
Jens Axboe787f7e92006-11-06 13:26:29 +0100193 if (!strlen(p))
194 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200195 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200196 if (sscanf(p, "%x", val) == 1)
197 return 0;
198 } else {
199 if (sscanf(p, "%u", val) == 1)
200 return 0;
201 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200202
203 return 1;
204}
205
Jens Axboee1f36502006-10-27 10:54:08 +0200206static struct fio_option *find_option(struct fio_option *options,
207 const char *opt)
208{
Jens Axboe4945ba12007-01-11 14:36:56 +0100209 struct fio_option *o;
Jens Axboee1f36502006-10-27 10:54:08 +0200210
Jens Axboe4945ba12007-01-11 14:36:56 +0100211 for (o = &options[0]; o->name; o++) {
Jens Axboee1f36502006-10-27 10:54:08 +0200212 if (!strcmp(o->name, opt))
213 return o;
Jens Axboe03b74b32007-01-11 11:04:31 +0100214 else if (o->alias && !strcmp(o->alias, opt))
215 return o;
Jens Axboe33963c62006-10-27 11:33:01 +0200216 }
Jens Axboee1f36502006-10-27 10:54:08 +0200217
218 return NULL;
219}
220
Jens Axboe17abbe82006-11-06 11:23:10 +0100221#define val_store(ptr, val, off, data) \
222 do { \
223 ptr = td_var((data), (off)); \
224 *ptr = (val); \
225 } while (0)
226
Jens Axboef90eff52006-11-06 11:08:21 +0100227static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Jens Axboe787f7e92006-11-06 13:26:29 +0100228 int first, int more)
Jens Axboee1f36502006-10-27 10:54:08 +0200229{
Jens Axboe63f29372007-01-10 13:20:09 +0100230 int il, *ilp;
231 long long ull, *ullp;
232 long ul1, ul2;
Jens Axboeb4692822006-10-27 13:43:22 +0200233 char **cp;
Jens Axboee1f36502006-10-27 10:54:08 +0200234 int ret = 0, is_time = 0;
235
Jens Axboea3d741f2008-02-27 18:32:33 +0100236 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
237 o->type, ptr);
238
Jens Axboee3cedca2008-11-19 19:57:52 +0100239 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe08e26e32006-11-21 13:15:10 +0100240 fprintf(stderr, "Option %s requires an argument\n", o->name);
241 return 1;
242 }
243
Jens Axboee1f36502006-10-27 10:54:08 +0200244 switch (o->type) {
245 case FIO_OPT_STR: {
246 fio_opt_str_fn *fn = o->cb;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100247 const struct value_pair *vp;
Jens Axboef0857372007-03-19 13:15:23 +0100248 struct value_pair posval[PARSE_MAX_VP];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100249 int i;
Jens Axboee1f36502006-10-27 10:54:08 +0200250
Jens Axboef0857372007-03-19 13:15:23 +0100251 posval_sort(o, posval);
252
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100253 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100254 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 if (!vp->ival || vp->ival[0] == '\0')
256 break;
Jens Axboe6612a272007-03-13 08:54:06 +0100257 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100258 if (!strncmp(vp->ival, ptr, strlen(vp->ival))) {
259 ret = 0;
260 if (!o->off1)
261 break;
262 val_store(ilp, vp->oval, o->off1, data);
263 break;
264 }
265 }
266
267 if (ret)
268 show_option_values(o);
269 else if (fn)
270 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200271 break;
272 }
273 case FIO_OPT_STR_VAL_TIME:
274 is_time = 1;
Jens Axboe75e6f362006-11-03 08:17:09 +0100275 case FIO_OPT_STR_VAL:
276 case FIO_OPT_STR_VAL_INT: {
Jens Axboee1f36502006-10-27 10:54:08 +0200277 fio_opt_str_val_fn *fn = o->cb;
278
279 if (is_time)
280 ret = check_str_time(ptr, &ull);
281 else
282 ret = check_str_bytes(ptr, &ull);
283
284 if (ret)
285 break;
286
Jens Axboedb8e0162007-01-10 13:41:26 +0100287 if (o->maxval && ull > o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100288 fprintf(stderr, "max value out of range: %lld"
289 " (%d max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100290 return 1;
291 }
292 if (o->minval && ull < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100293 fprintf(stderr, "min value out of range: %lld"
294 " (%d min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100295 return 1;
296 }
Jens Axboee1f36502006-10-27 10:54:08 +0200297
298 if (fn)
299 ret = fn(data, &ull);
300 else {
Jens Axboe75e6f362006-11-03 08:17:09 +0100301 if (o->type == FIO_OPT_STR_VAL_INT) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100302 if (first)
303 val_store(ilp, ull, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100304 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100305 val_store(ilp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100306 } else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100307 if (first)
308 val_store(ullp, 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(ullp, ull, o->off2, data);
Jens Axboe75e6f362006-11-03 08:17:09 +0100311 }
Jens Axboee1f36502006-10-27 10:54:08 +0200312 }
313 break;
314 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100315 case FIO_OPT_STR_STORE: {
316 fio_opt_str_fn *fn = o->cb;
317
Jens Axboee1f36502006-10-27 10:54:08 +0200318 cp = td_var(data, o->off1);
319 *cp = strdup(ptr);
Jens Axboeaf52b342007-03-13 10:07:47 +0100320 if (fn) {
321 ret = fn(data, ptr);
322 if (ret) {
323 free(*cp);
324 *cp = NULL;
325 }
326 }
Jens Axboee1f36502006-10-27 10:54:08 +0200327 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100328 }
Jens Axboee1f36502006-10-27 10:54:08 +0200329 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200330 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200331 char *p1, *p2;
332
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100333 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200334
335 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200336 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100337 p1 = strchr(tmp, ':');
338 if (!p1) {
339 ret = 1;
340 break;
341 }
Jens Axboee1f36502006-10-27 10:54:08 +0200342 }
343
344 p2 = p1 + 1;
345 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200346 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200347
348 ret = 1;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100349 if (!check_range_bytes(p1, &ul1) &&
350 !check_range_bytes(p2, &ul2)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200351 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200352 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100353 unsigned long foo = ul1;
354
355 ul1 = ul2;
356 ul2 = foo;
357 }
358
359 if (first) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100360 val_store(ilp, ul1, o->off1, data);
361 val_store(ilp, ul2, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200362 }
Jens Axboe97e8cd42008-02-05 09:57:39 +0100363 if (o->off3 && o->off4) {
Jens Axboe17abbe82006-11-06 11:23:10 +0100364 val_store(ilp, ul1, o->off3, data);
365 val_store(ilp, ul2, o->off4, data);
366 }
367 }
368
Jens Axboee1f36502006-10-27 10:54:08 +0200369 break;
370 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100371 case FIO_OPT_INT:
372 case FIO_OPT_BOOL: {
Jens Axboee1f36502006-10-27 10:54:08 +0200373 fio_opt_int_fn *fn = o->cb;
374
375 ret = check_int(ptr, &il);
376 if (ret)
377 break;
378
Jens Axboedb8e0162007-01-10 13:41:26 +0100379 if (o->maxval && il > (int) o->maxval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100380 fprintf(stderr, "max value out of range: %d (%d max)\n",
381 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100382 return 1;
383 }
384 if (o->minval && il < o->minval) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100385 fprintf(stderr, "min value out of range: %d (%d min)\n",
386 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100387 return 1;
388 }
Jens Axboee1f36502006-10-27 10:54:08 +0200389
Jens Axboe76a43db2007-01-11 13:24:44 +0100390 if (o->neg)
391 il = !il;
392
Jens Axboee1f36502006-10-27 10:54:08 +0200393 if (fn)
394 ret = fn(data, &il);
395 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100396 if (first)
397 val_store(ilp, il, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100398 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100399 val_store(ilp, il, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200400 }
401 break;
402 }
403 case FIO_OPT_STR_SET: {
404 fio_opt_str_set_fn *fn = o->cb;
405
406 if (fn)
407 ret = fn(data);
408 else {
Jens Axboe17abbe82006-11-06 11:23:10 +0100409 if (first)
410 val_store(ilp, 1, o->off1, data);
Jens Axboe787f7e92006-11-06 13:26:29 +0100411 if (!more && o->off2)
Jens Axboe17abbe82006-11-06 11:23:10 +0100412 val_store(ilp, 1, o->off2, data);
Jens Axboee1f36502006-10-27 10:54:08 +0200413 }
414 break;
415 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200416 case FIO_OPT_DEPRECATED:
417 fprintf(stdout, "Option %s is deprecated\n", o->name);
418 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200419 default:
Jens Axboe1e97cce2006-12-05 11:44:16 +0100420 fprintf(stderr, "Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200421 ret = 1;
422 }
423
Jens Axboee1f36502006-10-27 10:54:08 +0200424 return ret;
425}
426
Jens Axboef90eff52006-11-06 11:08:21 +0100427static int handle_option(struct fio_option *o, const char *ptr, void *data)
428{
Jens Axboe92b586f2006-11-07 14:29:17 +0100429 const char *ptr2 = NULL;
Jens Axboe787f7e92006-11-06 13:26:29 +0100430 int r1, r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100431
Jens Axboea3d741f2008-02-27 18:32:33 +0100432 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, ptr);
433
Jens Axboef90eff52006-11-06 11:08:21 +0100434 /*
Jens Axboe787f7e92006-11-06 13:26:29 +0100435 * See if we have a second set of parameters, hidden after a comma.
436 * Do this before parsing the first round, to check if we should
437 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100438 */
Joel Beckerad231bc2007-03-01 08:22:42 +0100439 if (ptr &&
440 (o->type != FIO_OPT_STR_STORE) &&
441 (o->type != FIO_OPT_STR)) {
Jens Axboe92b586f2006-11-07 14:29:17 +0100442 ptr2 = strchr(ptr, ',');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100443 if (!ptr2)
444 ptr2 = strchr(ptr, ':');
Jens Axboeb486f762007-03-15 09:15:12 +0100445 if (!ptr2)
446 ptr2 = strchr(ptr, '-');
Jens Axboe0c9baf92007-01-11 15:59:26 +0100447 }
Jens Axboe787f7e92006-11-06 13:26:29 +0100448
449 /*
450 * Don't return early if parsing the first option fails - if
451 * we are doing multiple arguments, we can allow the first one
452 * being empty.
453 */
454 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
455
Jens Axboef90eff52006-11-06 11:08:21 +0100456 if (!ptr2)
Jens Axboe787f7e92006-11-06 13:26:29 +0100457 return r1;
Jens Axboef90eff52006-11-06 11:08:21 +0100458
459 ptr2++;
Jens Axboe787f7e92006-11-06 13:26:29 +0100460 r2 = __handle_option(o, ptr2, data, 0, 0);
461
462 return r1 && r2;
Jens Axboef90eff52006-11-06 11:08:21 +0100463}
464
Jens Axboe3b8b7132008-06-10 19:46:23 +0200465static struct fio_option *get_option(const char *opt,
466 struct fio_option *options, char **post)
467{
468 struct fio_option *o;
469 char *ret;
470
471 ret = strchr(opt, '=');
472 if (ret) {
473 *post = ret;
474 *ret = '\0';
475 ret = (char *) opt;
476 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100477 strip_blank_end(ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200478 o = find_option(options, ret);
479 } else {
480 o = find_option(options, opt);
481 *post = NULL;
482 }
483
484 return o;
485}
486
487static int opt_cmp(const void *p1, const void *p2)
488{
489 struct fio_option *o1, *o2;
490 char *s1, *s2, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100491 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200492
493 s1 = strdup(*((char **) p1));
494 s2 = strdup(*((char **) p2));
495
496 o1 = get_option(s1, fio_options, &foo);
497 o2 = get_option(s2, fio_options, &foo);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100498
499 prio1 = prio2 = 0;
500 if (o1)
501 prio1 = o1->prio;
502 if (o2)
503 prio2 = o2->prio;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200504
505 free(s1);
506 free(s2);
Jens Axboe8cdabc12008-11-19 12:31:43 +0100507 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200508}
509
510void sort_options(char **opts, struct fio_option *options, int num_opts)
511{
512 fio_options = options;
513 qsort(opts, num_opts, sizeof(char *), opt_cmp);
514 fio_options = NULL;
515}
516
Jens Axboeb4692822006-10-27 13:43:22 +0200517int parse_cmd_option(const char *opt, const char *val,
518 struct fio_option *options, void *data)
519{
520 struct fio_option *o;
521
522 o = find_option(options, opt);
523 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100524 fprintf(stderr, "Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +0200525 return 1;
526 }
527
Jens Axboeb1508cf2006-11-02 09:12:40 +0100528 if (!handle_option(o, val, data))
529 return 0;
530
531 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
532 return 1;
Jens Axboeb4692822006-10-27 13:43:22 +0200533}
534
Aaron Carroll88b5a392008-10-07 11:25:20 +0200535/*
536 * Return a copy of the input string with substrings of the form ${VARNAME}
537 * substituted with the value of the environment variable VARNAME. The
538 * substitution always occurs, even if VARNAME is empty or the corresponding
539 * environment variable undefined.
540 */
541static char *option_dup_subs(const char *opt)
542{
543 char out[OPT_LEN_MAX+1];
544 char in[OPT_LEN_MAX+1];
545 char *outptr = out;
546 char *inptr = in;
547 char *ch1, *ch2, *env;
548 ssize_t nchr = OPT_LEN_MAX;
549 size_t envlen;
550
551 in[OPT_LEN_MAX] = '\0';
552 strncpy(in, opt, OPT_LEN_MAX);
553
554 while (*inptr && nchr > 0) {
555 if (inptr[0] == '$' && inptr[1] == '{') {
556 ch2 = strchr(inptr, '}');
557 if (ch2 && inptr+1 < ch2) {
558 ch1 = inptr+2;
559 inptr = ch2+1;
560 *ch2 = '\0';
561
562 env = getenv(ch1);
563 if (env) {
564 envlen = strlen(env);
565 if (envlen <= nchr) {
566 memcpy(outptr, env, envlen);
567 outptr += envlen;
568 nchr -= envlen;
569 }
570 }
571
572 continue;
573 }
574 }
575
576 *outptr++ = *inptr++;
577 --nchr;
578 }
579
580 *outptr = '\0';
581 return strdup(out);
582}
583
Jens Axboee1f36502006-10-27 10:54:08 +0200584int parse_option(const char *opt, struct fio_option *options, void *data)
585{
Jens Axboeb4692822006-10-27 13:43:22 +0200586 struct fio_option *o;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200587 char *post, *tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200588
Aaron Carroll88b5a392008-10-07 11:25:20 +0200589 tmp = option_dup_subs(opt);
Jens Axboee1f36502006-10-27 10:54:08 +0200590
Jens Axboe3b8b7132008-06-10 19:46:23 +0200591 o = get_option(tmp, options, &post);
Jens Axboee1f36502006-10-27 10:54:08 +0200592 if (!o) {
Jens Axboe43c129b2008-12-08 14:06:42 +0100593 fprintf(stderr, "Bad option <%s>\n", tmp);
Jens Axboe0401bca2007-03-29 11:00:43 +0200594 free(tmp);
Jens Axboee1f36502006-10-27 10:54:08 +0200595 return 1;
596 }
597
Jens Axboe0401bca2007-03-29 11:00:43 +0200598 if (!handle_option(o, post, data)) {
599 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100600 return 0;
Jens Axboe0401bca2007-03-29 11:00:43 +0200601 }
Jens Axboeb1508cf2006-11-02 09:12:40 +0100602
603 fprintf(stderr, "fio: failed parsing %s\n", opt);
Jens Axboe0401bca2007-03-29 11:00:43 +0200604 free(tmp);
Jens Axboeb1508cf2006-11-02 09:12:40 +0100605 return 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200606}
Jens Axboefd28ca42007-01-09 21:20:13 +0100607
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100608/*
609 * Option match, levenshtein distance. Handy for not quite remembering what
610 * the option name is.
611 */
612static int string_distance(const char *s1, const char *s2)
613{
614 unsigned int s1_len = strlen(s1);
615 unsigned int s2_len = strlen(s2);
616 unsigned int *p, *q, *r;
617 unsigned int i, j;
618
619 p = malloc(sizeof(unsigned int) * (s2_len + 1));
620 q = malloc(sizeof(unsigned int) * (s2_len + 1));
621
622 p[0] = 0;
623 for (i = 1; i <= s2_len; i++)
624 p[i] = p[i - 1] + 1;
625
626 for (i = 1; i <= s1_len; i++) {
627 q[0] = p[0] + 1;
628 for (j = 1; j <= s2_len; j++) {
629 unsigned int sub = p[j - 1];
630
631 if (s1[i - 1] != s2[j - 1])
632 sub++;
633
634 q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
635 }
636 r = p;
637 p = q;
638 q = r;
639 }
640
641 i = p[s2_len];
642 free(p);
643 free(q);
644 return i;
645}
646
647static void show_option_help(struct fio_option *o)
Jens Axboefd28ca42007-01-09 21:20:13 +0100648{
Jens Axboefd28ca42007-01-09 21:20:13 +0100649 const char *typehelp[] = {
650 "string (opt=bla)",
651 "string with possible k/m/g postfix (opt=4k)",
652 "string with range and postfix (opt=1k-4k)",
653 "string with time postfix (opt=10s)",
654 "string (opt=bla)",
655 "string with dual range (opt=1k-4k,4k-8k)",
656 "integer value (opt=100)",
Jens Axboe13335dd2007-01-10 13:14:02 +0100657 "boolean value (opt=1)",
Jens Axboefd28ca42007-01-09 21:20:13 +0100658 "no argument (opt)",
659 };
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100660
Jens Axboed2bb7fe2007-03-15 14:11:38 +0100661 if (o->alias)
662 printf("%20s: %s\n", "alias", o->alias);
663
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100664 printf("%20s: %s\n", "type", typehelp[o->type]);
665 printf("%20s: %s\n", "default", o->def ? o->def : "no default");
666 show_option_range(o);
667 show_option_values(o);
668}
669
Jens Axboeafdf9352007-07-31 16:14:34 +0200670static struct fio_option *find_child(struct fio_option *options,
671 struct fio_option *o)
672{
673 struct fio_option *__o;
674
Jens Axboefdf28742007-07-31 23:06:09 +0200675 for (__o = options + 1; __o->name; __o++)
676 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +0200677 return __o;
678
679 return NULL;
680}
681
Jens Axboe323d9112008-03-01 15:12:14 +0100682static void __print_option(struct fio_option *o, struct fio_option *org,
683 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +0200684{
685 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +0100686 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +0200687
688 if (!o)
689 return;
Jens Axboeef9aff52007-07-31 22:56:53 +0200690 if (!org)
691 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100692
Jens Axboeafdf9352007-07-31 16:14:34 +0200693 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +0100694 depth = level;
695 while (depth--)
696 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +0200697
698 sprintf(p, "%s", o->name);
699
700 printf("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +0100701}
702
703static void print_option(struct fio_option *o)
704{
705 struct fio_option *parent;
706 struct fio_option *__o;
707 unsigned int printed;
708 unsigned int level;
709
710 __print_option(o, NULL, 0);
711 parent = o;
712 level = 0;
713 do {
714 level++;
715 printed = 0;
716
717 while ((__o = find_child(o, parent)) != NULL) {
718 __print_option(__o, o, level);
719 o = __o;
720 printed++;
721 }
722
723 parent = o;
724 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +0200725}
726
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100727int show_cmd_help(struct fio_option *options, const char *name)
728{
729 struct fio_option *o, *closest;
730 unsigned int best_dist;
Jens Axboe29fc6af2007-01-09 21:22:02 +0100731 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +0100732 int show_all = 0;
733
734 if (!name || !strcmp(name, "all"))
735 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100736
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100737 closest = NULL;
738 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +0100739 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +0100740 int match = 0;
741
Jens Axboe15ca1502008-04-07 09:26:02 +0200742 if (o->type == FIO_OPT_DEPRECATED)
743 continue;
744
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100745 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +0100746 if (!strcmp(name, o->name) ||
747 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100748 match = 1;
749 else {
750 unsigned int dist;
751
752 dist = string_distance(name, o->name);
753 if (dist < best_dist) {
754 best_dist = dist;
755 closest = o;
756 }
757 }
758 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100759
760 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +0100761 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +0100762 if (match)
Jens Axboeafdf9352007-07-31 16:14:34 +0200763 printf("%24s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +0100764 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +0200765 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +0100766 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +0100767 continue;
Jens Axboec167ded2007-03-14 13:02:53 +0100768 }
Jens Axboefd28ca42007-01-09 21:20:13 +0100769 }
770
Jens Axboe70df2f12007-01-11 14:04:47 +0100771 if (!match)
772 continue;
773
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100774 show_option_help(o);
Jens Axboefd28ca42007-01-09 21:20:13 +0100775 }
776
Jens Axboe29fc6af2007-01-09 21:22:02 +0100777 if (found)
778 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +0100779
Jens Axboe0e9f7fa2007-03-01 14:05:30 +0100780 printf("No such command: %s", name);
781 if (closest) {
782 printf(" - showing closest match\n");
783 printf("%20s: %s\n", closest->name, closest->help);
784 show_option_help(closest);
785 } else
786 printf("\n");
787
Jens Axboe29fc6af2007-01-09 21:22:02 +0100788 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +0100789}
Jens Axboeee738492007-01-10 11:23:16 +0100790
Jens Axboe13335dd2007-01-10 13:14:02 +0100791/*
792 * Handle parsing of default parameters.
793 */
Jens Axboeee738492007-01-10 11:23:16 +0100794void fill_default_options(void *data, struct fio_option *options)
795{
Jens Axboe4945ba12007-01-11 14:36:56 +0100796 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +0100797
Jens Axboea3d741f2008-02-27 18:32:33 +0100798 dprint(FD_PARSE, "filling default options\n");
799
Jens Axboe4945ba12007-01-11 14:36:56 +0100800 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +0100801 if (o->def)
802 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +0100803}
Jens Axboe13335dd2007-01-10 13:14:02 +0100804
805/*
806 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +0100807 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +0100808 */
809void options_init(struct fio_option *options)
810{
Jens Axboe4945ba12007-01-11 14:36:56 +0100811 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +0100812
Jens Axboea3d741f2008-02-27 18:32:33 +0100813 dprint(FD_PARSE, "init options\n");
814
Jens Axboe4945ba12007-01-11 14:36:56 +0100815 for (o = &options[0]; o->name; o++) {
Jens Axboe15ca1502008-04-07 09:26:02 +0200816 if (o->type == FIO_OPT_DEPRECATED)
817 continue;
Jens Axboe13335dd2007-01-10 13:14:02 +0100818 if (o->type == FIO_OPT_BOOL) {
819 o->minval = 0;
820 o->maxval = 1;
821 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100822 if (o->type == FIO_OPT_STR_SET && o->def) {
823 fprintf(stderr, "Option %s: string set option with"
824 " default will always be true\n",
825 o->name);
826 }
827 if (!o->cb && !o->off1) {
828 fprintf(stderr, "Option %s: neither cb nor offset"
829 " given\n", o->name);
830 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100831 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100832 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100833 if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
834 fprintf(stderr, "Option %s: both cb and offset given\n",
835 o->name);
836 }
Jens Axboe13335dd2007-01-10 13:14:02 +0100837 }
838}