[PATCH] Pretty up command help output
[fio.git] / parse.c
... / ...
CommitLineData
1/*
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"
13
14static unsigned long get_mult_time(char c)
15{
16 switch (c) {
17 case 'm':
18 case 'M':
19 return 60;
20 case 'h':
21 case 'H':
22 return 60 * 60;
23 case 'd':
24 case 'D':
25 return 24 * 60 * 60;
26 default:
27 return 1;
28 }
29}
30
31static unsigned long get_mult_bytes(char c)
32{
33 switch (c) {
34 case 'k':
35 case 'K':
36 return 1024;
37 case 'm':
38 case 'M':
39 return 1024 * 1024;
40 case 'g':
41 case 'G':
42 return 1024 * 1024 * 1024;
43 default:
44 return 1;
45 }
46}
47
48/*
49 * convert string into decimal value, noting any size suffix
50 */
51static int str_to_decimal(const char *str, unsigned long long *val, int kilo)
52{
53 int len;
54
55 len = strlen(str);
56 if (!len)
57 return 1;
58
59 *val = strtoul(str, NULL, 10);
60 if (*val == ULONG_MAX && errno == ERANGE)
61 return 1;
62
63 if (kilo)
64 *val *= get_mult_bytes(str[len - 1]);
65 else
66 *val *= get_mult_time(str[len - 1]);
67
68 return 0;
69}
70
71static int check_str_bytes(const char *p, unsigned long long *val)
72{
73 return str_to_decimal(p, val, 1);
74}
75
76static int check_str_time(const char *p, unsigned long long *val)
77{
78 return str_to_decimal(p, val, 0);
79}
80
81void strip_blank_front(char **p)
82{
83 char *s = *p;
84
85 while (isspace(*s))
86 s++;
87}
88
89void strip_blank_end(char *p)
90{
91 char *s = p + strlen(p) - 1;
92
93 while (isspace(*s) || iscntrl(*s))
94 s--;
95
96 *(s + 1) = '\0';
97}
98
99static int check_range_bytes(const char *str, unsigned long *val)
100{
101 char suffix;
102
103 if (!strlen(str))
104 return 1;
105
106 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
107 *val *= get_mult_bytes(suffix);
108 return 0;
109 }
110
111 if (sscanf(str, "%lu", val) == 1)
112 return 0;
113
114 return 1;
115}
116
117static int check_int(const char *p, unsigned int *val)
118{
119 if (!strlen(p))
120 return 1;
121 if (sscanf(p, "%u", val) == 1)
122 return 0;
123
124 return 1;
125}
126
127static struct fio_option *find_option(struct fio_option *options,
128 const char *opt)
129{
130 struct fio_option *o = &options[0];
131
132 while (o->name) {
133 if (!strcmp(o->name, opt))
134 return o;
135
136 o++;
137 }
138
139 return NULL;
140}
141
142#define val_store(ptr, val, off, data) \
143 do { \
144 ptr = td_var((data), (off)); \
145 *ptr = (val); \
146 } while (0)
147
148static int __handle_option(struct fio_option *o, const char *ptr, void *data,
149 int first, int more)
150{
151 unsigned int il, *ilp;
152 unsigned long long ull, *ullp;
153 unsigned long ul1, ul2;
154 char **cp;
155 int ret = 0, is_time = 0;
156
157 if (!ptr && o->type != FIO_OPT_STR_SET) {
158 fprintf(stderr, "Option %s requires an argument\n", o->name);
159 return 1;
160 }
161
162 switch (o->type) {
163 case FIO_OPT_STR: {
164 fio_opt_str_fn *fn = o->cb;
165
166 ret = fn(data, ptr);
167 break;
168 }
169 case FIO_OPT_STR_VAL_TIME:
170 is_time = 1;
171 case FIO_OPT_STR_VAL:
172 case FIO_OPT_STR_VAL_INT: {
173 fio_opt_str_val_fn *fn = o->cb;
174
175 if (is_time)
176 ret = check_str_time(ptr, &ull);
177 else
178 ret = check_str_bytes(ptr, &ull);
179
180 if (ret)
181 break;
182
183 if (o->maxval && ull > o->maxval)
184 ull = o->maxval;
185 if (o->minval && ull < o->minval)
186 ull = o->minval;
187
188 if (fn)
189 ret = fn(data, &ull);
190 else {
191 if (o->type == FIO_OPT_STR_VAL_INT) {
192 if (first)
193 val_store(ilp, ull, o->off1, data);
194 if (!more && o->off2)
195 val_store(ilp, ull, o->off2, data);
196 } else {
197 if (first)
198 val_store(ullp, ull, o->off1, data);
199 if (!more && o->off2)
200 val_store(ullp, ull, o->off2, data);
201 }
202 }
203 break;
204 }
205 case FIO_OPT_STR_STORE:
206 cp = td_var(data, o->off1);
207 *cp = strdup(ptr);
208 break;
209 case FIO_OPT_RANGE: {
210 char tmp[128];
211 char *p1, *p2;
212
213 strncpy(tmp, ptr, sizeof(tmp) - 1);
214
215 p1 = strchr(tmp, '-');
216 if (!p1) {
217 ret = 1;
218 break;
219 }
220
221 p2 = p1 + 1;
222 *p1 = '\0';
223 p1 = tmp;
224
225 ret = 1;
226 if (!check_range_bytes(p1, &ul1) && !check_range_bytes(p2, &ul2)) {
227 ret = 0;
228 if (ul1 > ul2) {
229 unsigned long foo = ul1;
230
231 ul1 = ul2;
232 ul2 = foo;
233 }
234
235 if (first) {
236 val_store(ilp, ul1, o->off1, data);
237 val_store(ilp, ul2, o->off2, data);
238 }
239 if (!more && o->off3 && o->off4) {
240 val_store(ilp, ul1, o->off3, data);
241 val_store(ilp, ul2, o->off4, data);
242 }
243 }
244
245 break;
246 }
247 case FIO_OPT_INT: {
248 fio_opt_int_fn *fn = o->cb;
249
250 ret = check_int(ptr, &il);
251 if (ret)
252 break;
253
254 if (o->maxval && il > o->maxval)
255 il = o->maxval;
256 if (o->minval && il < o->minval)
257 il = o->minval;
258
259 if (fn)
260 ret = fn(data, &il);
261 else {
262 if (first)
263 val_store(ilp, il, o->off1, data);
264 if (!more && o->off2)
265 val_store(ilp, il, o->off2, data);
266 }
267 break;
268 }
269 case FIO_OPT_STR_SET: {
270 fio_opt_str_set_fn *fn = o->cb;
271
272 if (fn)
273 ret = fn(data);
274 else {
275 if (first)
276 val_store(ilp, 1, o->off1, data);
277 if (!more && o->off2)
278 val_store(ilp, 1, o->off2, data);
279 }
280 break;
281 }
282 default:
283 fprintf(stderr, "Bad option type %u\n", o->type);
284 ret = 1;
285 }
286
287 return ret;
288}
289
290static int handle_option(struct fio_option *o, const char *ptr, void *data)
291{
292 const char *ptr2 = NULL;
293 int r1, r2;
294
295 /*
296 * See if we have a second set of parameters, hidden after a comma.
297 * Do this before parsing the first round, to check if we should
298 * copy set 1 options to set 2.
299 */
300 if (ptr)
301 ptr2 = strchr(ptr, ',');
302
303 /*
304 * Don't return early if parsing the first option fails - if
305 * we are doing multiple arguments, we can allow the first one
306 * being empty.
307 */
308 r1 = __handle_option(o, ptr, data, 1, !!ptr2);
309
310 if (!ptr2)
311 return r1;
312
313 ptr2++;
314 r2 = __handle_option(o, ptr2, data, 0, 0);
315
316 return r1 && r2;
317}
318
319int parse_cmd_option(const char *opt, const char *val,
320 struct fio_option *options, void *data)
321{
322 struct fio_option *o;
323
324 o = find_option(options, opt);
325 if (!o) {
326 fprintf(stderr, "Bad option %s\n", opt);
327 return 1;
328 }
329
330 if (!handle_option(o, val, data))
331 return 0;
332
333 fprintf(stderr, "fio: failed parsing %s=%s\n", opt, val);
334 return 1;
335}
336
337int parse_option(const char *opt, struct fio_option *options, void *data)
338{
339 struct fio_option *o;
340 char *pre, *post;
341 char tmp[64];
342
343 strncpy(tmp, opt, sizeof(tmp) - 1);
344
345 pre = strchr(tmp, '=');
346 if (pre) {
347 post = pre;
348 *pre = '\0';
349 pre = tmp;
350 post++;
351 o = find_option(options, pre);
352 } else {
353 o = find_option(options, tmp);
354 post = NULL;
355 }
356
357 if (!o) {
358 fprintf(stderr, "Bad option %s\n", tmp);
359 return 1;
360 }
361
362 if (!handle_option(o, post, data))
363 return 0;
364
365 fprintf(stderr, "fio: failed parsing %s\n", opt);
366 return 1;
367}
368
369int show_cmd_help(struct fio_option *options, const char *name)
370{
371 int show_all = !strcmp(name, "all");
372 struct fio_option *o = &options[0];
373 const char *typehelp[] = {
374 "string (opt=bla)",
375 "string with possible k/m/g postfix (opt=4k)",
376 "string with range and postfix (opt=1k-4k)",
377 "string with time postfix (opt=10s)",
378 "string (opt=bla)",
379 "string with dual range (opt=1k-4k,4k-8k)",
380 "integer value (opt=100)",
381 "no argument (opt)",
382 };
383 int found = 0;
384
385 while (o->name) {
386 int match = !strcmp(name, o->name);
387
388 if (show_all || match) {
389 found = 1;
390 printf("%16s: %s\n", o->name, o->help);
391 if (match) {
392 printf("%16s: %s\n", "type", typehelp[o->type]);
393 printf("%16s: %s\n", "default", o->def ? o->def : "no default");
394 }
395 }
396
397 o++;
398 }
399
400 if (found)
401 return 0;
402
403 printf("No such command: %s\n", name);
404 return 1;
405}
406
407void fill_default_options(void *data, struct fio_option *options)
408{
409 struct fio_option *o = &options[0];
410
411 while (o->name) {
412 if (o->def)
413 handle_option(o, o->def, data);
414 o++;
415 }
416}