blob: 4d4fdddeae573f12e10270291785401029399d53 [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>
Yu-ju Hong83349192011-08-13 00:53:44 +020012#include <math.h>
Bruce Cranab9461e2013-02-02 14:31:24 +000013#include <float.h>
Jens Axboecb2c86f2006-10-26 13:48:34 +020014
15#include "parse.h"
Jens Axboea3d741f2008-02-27 18:32:33 +010016#include "debug.h"
Jens Axboe9f988e22010-03-04 10:42:38 +010017#include "options.h"
Elliott Hugheseda3a602017-05-19 18:53:02 -070018#include "optgroup.h"
Jens Axboee25839d2012-11-06 10:49:42 +010019#include "minmax.h"
Vincent Kang Fufd112d32013-02-02 09:28:55 +010020#include "lib/ieee754.h"
Elliott Hugheseda3a602017-05-19 18:53:02 -070021#include "lib/pow2.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020022
Stephen M. Cameron88b635b2014-09-29 12:10:49 -060023#ifdef CONFIG_ARITHMETIC
24#include "y.tab.h"
25#endif
26
Jens Axboe9af4a242012-03-16 10:13:49 +010027static struct fio_option *__fio_options;
Jens Axboe3b8b7132008-06-10 19:46:23 +020028
Jens Axboef0857372007-03-19 13:15:23 +010029static int vp_cmp(const void *p1, const void *p2)
30{
31 const struct value_pair *vp1 = p1;
32 const struct value_pair *vp2 = p2;
33
34 return strlen(vp2->ival) - strlen(vp1->ival);
35}
36
Jens Axboece952ab2011-08-31 14:29:22 -060037static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
Jens Axboef0857372007-03-19 13:15:23 +010038{
39 const struct value_pair *vp;
40 int entries;
41
42 memset(vpmap, 0, PARSE_MAX_VP * sizeof(struct value_pair));
43
44 for (entries = 0; entries < PARSE_MAX_VP; entries++) {
45 vp = &o->posval[entries];
46 if (!vp->ival || vp->ival[0] == '\0')
47 break;
48
49 memcpy(&vpmap[entries], vp, sizeof(*vp));
50 }
51
52 qsort(vpmap, entries, sizeof(struct value_pair), vp_cmp);
53}
54
Jens Axboe06b0be62011-10-04 10:31:10 +020055static void show_option_range(struct fio_option *o,
Elliott Hugheseda3a602017-05-19 18:53:02 -070056 size_t (*logger)(const char *format, ...))
Jens Axboeb1ec1da2007-02-23 10:29:16 +010057{
Jens Axboe3c037bc2013-04-10 11:21:33 +020058 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +000059 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
Yu-ju Hong83349192011-08-13 00:53:44 +020060 return;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010061
Jens Axboe06b0be62011-10-04 10:31:10 +020062 logger("%20s: min=%f", "range", o->minfp);
Bruce Cranab9461e2013-02-02 14:31:24 +000063 if (o->maxfp != DBL_MAX)
Jens Axboe06b0be62011-10-04 10:31:10 +020064 logger(", max=%f", o->maxfp);
65 logger("\n");
Jens Axboe3c037bc2013-04-10 11:21:33 +020066 } else if (!o->posval[0].ival) {
Yu-ju Hong83349192011-08-13 00:53:44 +020067 if (!o->minval && !o->maxval)
68 return;
69
Jens Axboe06b0be62011-10-04 10:31:10 +020070 logger("%20s: min=%d", "range", o->minval);
Yu-ju Hong83349192011-08-13 00:53:44 +020071 if (o->maxval)
Jens Axboe06b0be62011-10-04 10:31:10 +020072 logger(", max=%d", o->maxval);
73 logger("\n");
Yu-ju Hong83349192011-08-13 00:53:44 +020074 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010075}
76
77static void show_option_values(struct fio_option *o)
78{
Jens Axboea3073f42010-03-05 10:06:15 +010079 int i;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010080
Jens Axboea3073f42010-03-05 10:06:15 +010081 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboe78372132007-03-14 13:24:07 +010082 const struct value_pair *vp = &o->posval[i];
83
84 if (!vp->ival)
Jens Axboea3073f42010-03-05 10:06:15 +010085 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +010086
Jens Axboe06b0be62011-10-04 10:31:10 +020087 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
Jens Axboe78372132007-03-14 13:24:07 +010088 if (vp->help)
Jens Axboe06b0be62011-10-04 10:31:10 +020089 log_info(" %s", vp->help);
90 log_info("\n");
Jens Axboea3073f42010-03-05 10:06:15 +010091 }
Jens Axboeb1ec1da2007-02-23 10:29:16 +010092
93 if (i)
Jens Axboe06b0be62011-10-04 10:31:10 +020094 log_info("\n");
Jens Axboeb1ec1da2007-02-23 10:29:16 +010095}
96
Jens Axboe06b0be62011-10-04 10:31:10 +020097static void show_option_help(struct fio_option *o, int is_err)
Jens Axboed447a8c2009-07-17 22:26:15 +020098{
99 const char *typehelp[] = {
Jens Axboe07b32322010-03-05 09:48:44 +0100100 "invalid",
Jens Axboed447a8c2009-07-17 22:26:15 +0200101 "string (opt=bla)",
Jens Axboeae3fb6f2010-09-14 13:10:35 +0200102 "string (opt=bla)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200103 "string with possible k/m/g postfix (opt=4k)",
104 "string with time postfix (opt=10s)",
105 "string (opt=bla)",
106 "string with dual range (opt=1k-4k,4k-8k)",
107 "integer value (opt=100)",
108 "boolean value (opt=1)",
Yu-ju Hong83349192011-08-13 00:53:44 +0200109 "list of floating point values separated by ':' (opt=5.9:7.8)",
Jens Axboed447a8c2009-07-17 22:26:15 +0200110 "no argument (opt)",
Jens Axboe07b32322010-03-05 09:48:44 +0100111 "deprecated",
Elliott Hugheseda3a602017-05-19 18:53:02 -0700112 "unsupported",
Jens Axboed447a8c2009-07-17 22:26:15 +0200113 };
Elliott Hugheseda3a602017-05-19 18:53:02 -0700114 size_t (*logger)(const char *format, ...);
Jens Axboe06b0be62011-10-04 10:31:10 +0200115
116 if (is_err)
117 logger = log_err;
118 else
119 logger = log_info;
Jens Axboed447a8c2009-07-17 22:26:15 +0200120
121 if (o->alias)
Jens Axboe06b0be62011-10-04 10:31:10 +0200122 logger("%20s: %s\n", "alias", o->alias);
Jens Axboed447a8c2009-07-17 22:26:15 +0200123
Jens Axboe06b0be62011-10-04 10:31:10 +0200124 logger("%20s: %s\n", "type", typehelp[o->type]);
125 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
Jens Axboe07b32322010-03-05 09:48:44 +0100126 if (o->prof_name)
Jens Axboe06b0be62011-10-04 10:31:10 +0200127 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
128 show_option_range(o, logger);
Jens Axboed447a8c2009-07-17 22:26:15 +0200129 show_option_values(o);
130}
131
Jens Axboe0de5b262014-02-21 15:26:01 -0800132static unsigned long long get_mult_time(const char *str, int len,
133 int is_seconds)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200134{
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100135 const char *p = str;
136 char *c;
Jens Axboee4668262014-02-21 11:50:09 -0800137 unsigned long long mult = 1;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700138 int i;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100139
140 /*
141 * Go forward until we hit a non-digit, or +/- sign
142 */
143 while ((p - str) <= len) {
144 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
145 break;
146 p++;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200147 }
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100148
Jens Axboe0de5b262014-02-21 15:26:01 -0800149 if (!isalpha((int) *p)) {
150 if (is_seconds)
151 return 1000000UL;
152 else
153 return 1;
154 }
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100155
156 c = strdup(p);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700157 for (i = 0; i < strlen(c); i++)
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100158 c[i] = tolower(c[i]);
159
Jens Axboee4668262014-02-21 11:50:09 -0800160 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100161 mult = 1;
Jens Axboee4668262014-02-21 11:50:09 -0800162 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100163 mult = 1000;
Jens Axboee4668262014-02-21 11:50:09 -0800164 else if (!strcmp("s", c))
165 mult = 1000000;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100166 else if (!strcmp("m", c))
Jens Axboee4668262014-02-21 11:50:09 -0800167 mult = 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100168 else if (!strcmp("h", c))
Jens Axboee4668262014-02-21 11:50:09 -0800169 mult = 60 * 60 * 1000000UL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100170 else if (!strcmp("d", c))
Elliott Hugheseda3a602017-05-19 18:53:02 -0700171 mult = 24 * 60 * 60 * 1000000ULL;
Christian Ehrhardt74454ce2014-02-20 14:20:01 +0100172
173 free(c);
174 return mult;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200175}
176
Jens Axboea03fb652013-03-29 12:20:51 -0600177static int is_separator(char c)
178{
179 switch (c) {
180 case ':':
181 case '-':
182 case ',':
183 case '/':
184 return 1;
185 default:
186 return 0;
187 }
188}
189
Jens Axboe7bb59102011-07-12 19:47:03 +0200190static unsigned long long __get_mult_bytes(const char *p, void *data,
191 int *percent)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200192{
Jens Axboed6978a32009-07-18 21:04:09 +0200193 unsigned int kb_base = fio_get_kb_base(data);
Jens Axboea639f0b2009-07-18 08:25:35 +0200194 unsigned long long ret = 1;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200195 unsigned int i, pow = 0, mult = kb_base;
196 char *c;
Jens Axboea639f0b2009-07-18 08:25:35 +0200197
Jens Axboe57fc29f2010-06-23 22:24:07 +0200198 if (!p)
199 return 1;
Jens Axboea639f0b2009-07-18 08:25:35 +0200200
Jens Axboe57fc29f2010-06-23 22:24:07 +0200201 c = strdup(p);
202
Jens Axboea03fb652013-03-29 12:20:51 -0600203 for (i = 0; i < strlen(c); i++) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200204 c[i] = tolower(c[i]);
Jens Axboea03fb652013-03-29 12:20:51 -0600205 if (is_separator(c[i])) {
206 c[i] = '\0';
207 break;
208 }
209 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200210
Elliott Hugheseda3a602017-05-19 18:53:02 -0700211 /* If kb_base is 1000, use true units.
212 * If kb_base is 1024, use opposite units.
213 */
Jens Axboea04f1582012-03-07 10:53:29 +0100214 if (!strncmp("pib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200215 pow = 5;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700216 if (kb_base == 1000)
217 mult = 1024;
218 else if (kb_base == 1024)
219 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100220 } else if (!strncmp("tib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200221 pow = 4;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700222 if (kb_base == 1000)
223 mult = 1024;
224 else if (kb_base == 1024)
225 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100226 } else if (!strncmp("gib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200227 pow = 3;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700228 if (kb_base == 1000)
229 mult = 1024;
230 else if (kb_base == 1024)
231 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100232 } else if (!strncmp("mib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200233 pow = 2;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700234 if (kb_base == 1000)
235 mult = 1024;
236 else if (kb_base == 1024)
237 mult = 1000;
Jens Axboea04f1582012-03-07 10:53:29 +0100238 } else if (!strncmp("kib", c, 3)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200239 pow = 1;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700240 if (kb_base == 1000)
241 mult = 1024;
242 else if (kb_base == 1024)
243 mult = 1000;
244 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200245 pow = 5;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700246 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200247 pow = 4;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700248 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200249 pow = 3;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700250 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200251 pow = 2;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700252 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200253 pow = 1;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700254 } else if (!strncmp("%", c, 1)) {
Jens Axboe7bb59102011-07-12 19:47:03 +0200255 *percent = 1;
Jens Axboee721c572012-02-09 20:55:29 +0100256 free(c);
Jens Axboe7bb59102011-07-12 19:47:03 +0200257 return ret;
258 }
Jens Axboe57fc29f2010-06-23 22:24:07 +0200259
260 while (pow--)
261 ret *= (unsigned long long) mult;
262
263 free(c);
Jens Axboea639f0b2009-07-18 08:25:35 +0200264 return ret;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200265}
266
Jens Axboe7bb59102011-07-12 19:47:03 +0200267static unsigned long long get_mult_bytes(const char *str, int len, void *data,
Jens Axboe6925dd32011-09-07 22:10:11 +0200268 int *percent)
Jens Axboe57fc29f2010-06-23 22:24:07 +0200269{
Jens Axboe55ed9632011-03-27 20:55:09 +0200270 const char *p = str;
Jens Axboeba4ddd62011-09-07 22:11:00 +0200271 int digit_seen = 0;
Jens Axboe57fc29f2010-06-23 22:24:07 +0200272
Jens Axboe1d1c1872010-07-29 10:50:05 +0200273 if (len < 2)
Jens Axboe7bb59102011-07-12 19:47:03 +0200274 return __get_mult_bytes(str, data, percent);
Jens Axboe1d1c1872010-07-29 10:50:05 +0200275
Steven Langd0c814e2011-10-28 08:37:13 +0200276 /*
277 * Go forward until we hit a non-digit, or +/- sign
278 */
Jens Axboe55ed9632011-03-27 20:55:09 +0200279 while ((p - str) <= len) {
Jens Axboeba4ddd62011-09-07 22:11:00 +0200280 if (!isdigit((int) *p) &&
281 (((*p != '+') && (*p != '-')) || digit_seen))
Jens Axboe55ed9632011-03-27 20:55:09 +0200282 break;
Jens Axboe3c703d12011-09-21 09:27:24 +0200283 digit_seen |= isdigit((int) *p);
Jens Axboe55ed9632011-03-27 20:55:09 +0200284 p++;
285 }
286
Jens Axboe7bb59102011-07-12 19:47:03 +0200287 if (!isalpha((int) *p) && (*p != '%'))
Jens Axboe57fc29f2010-06-23 22:24:07 +0200288 p = NULL;
289
Jens Axboe7bb59102011-07-12 19:47:03 +0200290 return __get_mult_bytes(p, data, percent);
Jens Axboe57fc29f2010-06-23 22:24:07 +0200291}
292
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600293extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700294 double *dval, double implied_units,
295 int is_time);
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600296
Jens Axboecb2c86f2006-10-26 13:48:34 +0200297/*
Yu-ju Hong83349192011-08-13 00:53:44 +0200298 * Convert string into a floating number. Return 1 for success and 0 otherwise.
299 */
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700300int str_to_float(const char *str, double *val, int is_time)
Yu-ju Hong83349192011-08-13 00:53:44 +0200301{
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600302#ifdef CONFIG_ARITHMETIC
303 int rc;
304 long long ival;
305 double dval;
306
307 if (str[0] == '(') {
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700308 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600309 if (!rc) {
310 *val = dval;
311 return 1;
312 }
313 }
314#endif
315 return 1 == sscanf(str, "%lf", val);
Yu-ju Hong83349192011-08-13 00:53:44 +0200316}
317
318/*
Jens Axboee1f36502006-10-27 10:54:08 +0200319 * convert string into decimal value, noting any size suffix
Jens Axboecb2c86f2006-10-26 13:48:34 +0200320 */
Jens Axboe0de5b262014-02-21 15:26:01 -0800321int str_to_decimal(const char *str, long long *val, int kilo, void *data,
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700322 int is_seconds, int is_time)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200323{
Jens Axboeb347f9d2009-03-09 14:15:21 +0100324 int len, base;
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600325 int rc = 1;
326#ifdef CONFIG_ARITHMETIC
327 long long ival;
328 double dval;
Stephen M. Cameron64c9d602014-09-30 10:32:28 -0500329 double implied_units = 1.0;
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600330#endif
Jens Axboecb2c86f2006-10-26 13:48:34 +0200331
Jens Axboecb2c86f2006-10-26 13:48:34 +0200332 len = strlen(str);
Jens Axboef90eff52006-11-06 11:08:21 +0100333 if (!len)
334 return 1;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200335
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600336#ifdef CONFIG_ARITHMETIC
Stephen M. Cameron64c9d602014-09-30 10:32:28 -0500337 if (is_seconds)
338 implied_units = 1000000.0;
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600339 if (str[0] == '(')
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700340 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
Stephen M. Cameronc3805eb2014-09-29 12:15:35 -0600341 if (str[0] == '(' && !rc) {
342 if (!kilo && is_seconds)
343 *val = ival / 1000000LL;
344 else
345 *val = ival;
346 }
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600347#endif
Jens Axboeb347f9d2009-03-09 14:15:21 +0100348
Stephen M. Cameron88b635b2014-09-29 12:10:49 -0600349 if (rc == 1) {
350 if (strstr(str, "0x") || strstr(str, "0X"))
351 base = 16;
352 else
353 base = 10;
354
355 *val = strtoll(str, NULL, base);
356 if (*val == LONG_MAX && errno == ERANGE)
357 return 1;
358 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200359
Jens Axboe7bb59102011-07-12 19:47:03 +0200360 if (kilo) {
361 unsigned long long mult;
362 int perc = 0;
363
Jens Axboe6925dd32011-09-07 22:10:11 +0200364 mult = get_mult_bytes(str, len, data, &perc);
Jens Axboe7bb59102011-07-12 19:47:03 +0200365 if (perc)
366 *val = -1ULL - *val;
367 else
368 *val *= mult;
369 } else
Jens Axboe0de5b262014-02-21 15:26:01 -0800370 *val *= get_mult_time(str, len, is_seconds);
Jens Axboe7126d862014-12-17 13:27:32 -0700371
Jens Axboecb2c86f2006-10-26 13:48:34 +0200372 return 0;
373}
374
Jens Axboe9af4a242012-03-16 10:13:49 +0100375int check_str_bytes(const char *p, long long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200376{
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700377 return str_to_decimal(p, val, 1, data, 0, 0);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200378}
379
Jens Axboe0de5b262014-02-21 15:26:01 -0800380int check_str_time(const char *p, long long *val, int is_seconds)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200381{
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700382 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
Jens Axboecb2c86f2006-10-26 13:48:34 +0200383}
384
385void strip_blank_front(char **p)
386{
387 char *s = *p;
388
Jens Axboe4c8e9642011-10-15 14:37:38 +0200389 if (!strlen(s))
390 return;
Jens Axboe76cd9372011-07-08 21:14:57 +0200391 while (isspace((int) *s))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200392 s++;
Jens Axboe4d651da2007-03-28 19:34:53 +0200393
394 *p = s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200395}
396
397void strip_blank_end(char *p)
398{
Jens Axboe853ee7f2009-03-06 20:29:29 +0100399 char *start = p, *s;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200400
Jens Axboe4c8e9642011-10-15 14:37:38 +0200401 if (!strlen(p))
402 return;
403
Jens Axboe523bfad2007-04-04 11:04:06 +0200404 s = strchr(p, ';');
405 if (s)
406 *s = '\0';
407 s = strchr(p, '#');
408 if (s)
409 *s = '\0';
410 if (s)
411 p = s;
412
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200413 s = p + strlen(p);
Jens Axboe76cd9372011-07-08 21:14:57 +0200414 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
Jens Axboecb2c86f2006-10-26 13:48:34 +0200415 s--;
416
417 *(s + 1) = '\0';
418}
419
Jens Axboed6978a32009-07-18 21:04:09 +0200420static int check_range_bytes(const char *str, long *val, void *data)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200421{
Jens Axboe57fc29f2010-06-23 22:24:07 +0200422 long long __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200423
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700424 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
Jens Axboe57fc29f2010-06-23 22:24:07 +0200425 *val = __val;
Jens Axboecb2c86f2006-10-26 13:48:34 +0200426 return 0;
427 }
428
Jens Axboecb2c86f2006-10-26 13:48:34 +0200429 return 1;
430}
431
Jens Axboe63f29372007-01-10 13:20:09 +0100432static int check_int(const char *p, int *val)
Jens Axboecb2c86f2006-10-26 13:48:34 +0200433{
Jens Axboe787f7e92006-11-06 13:26:29 +0100434 if (!strlen(p))
435 return 1;
Jens Axboed78ee462007-08-10 14:03:20 +0200436 if (strstr(p, "0x") || strstr(p, "0X")) {
Jens Axboea61bdfd2007-07-30 09:07:04 +0200437 if (sscanf(p, "%x", val) == 1)
438 return 0;
439 } else {
440 if (sscanf(p, "%u", val) == 1)
441 return 0;
442 }
Jens Axboecb2c86f2006-10-26 13:48:34 +0200443
444 return 1;
445}
446
Jens Axboe8adb4522014-09-23 10:38:54 -0600447static size_t opt_len(const char *str)
Jens Axboe808def72010-07-14 16:27:02 -0600448{
449 char *postfix;
450
451 postfix = strchr(str, ':');
452 if (!postfix)
453 return strlen(str);
454
455 return (int)(postfix - str);
456}
457
Jens Axboe119cd932012-12-06 17:34:57 +0100458static int str_match_len(const struct value_pair *vp, const char *str)
459{
460 return max(strlen(vp->ival), opt_len(str));
461}
462
Jens Axboef0fdbca2014-02-11 15:44:50 -0700463#define val_store(ptr, val, off, or, data, o) \
464 do { \
465 ptr = td_var((data), (o), (off)); \
466 if ((or)) \
467 *ptr |= (val); \
468 else \
469 *ptr = (val); \
Jens Axboe17abbe82006-11-06 11:23:10 +0100470 } while (0)
471
Jens Axboef90eff52006-11-06 11:08:21 +0100472static int __handle_option(struct fio_option *o, const char *ptr, void *data,
Yu-ju Hong83349192011-08-13 00:53:44 +0200473 int first, int more, int curr)
Jens Axboee1f36502006-10-27 10:54:08 +0200474{
Erwan Velu2fdbefd2013-07-22 23:48:48 +0200475 int il=0, *ilp;
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100476 fio_fp64_t *flp;
Jens Axboe63f29372007-01-10 13:20:09 +0100477 long long ull, *ullp;
478 long ul1, ul2;
Yu-ju Hong83349192011-08-13 00:53:44 +0200479 double uf;
Jens Axboebfe1d592012-11-29 21:33:03 +0100480 char **cp = NULL;
Jens Axboee42b01e2011-05-05 12:05:10 -0600481 int ret = 0, is_time = 0;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600482 const struct value_pair *vp;
483 struct value_pair posval[PARSE_MAX_VP];
484 int i, all_skipped = 1;
Jens Axboee1f36502006-10-27 10:54:08 +0200485
Jens Axboea3d741f2008-02-27 18:32:33 +0100486 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
487 o->type, ptr);
488
Jens Axboee3cedca2008-11-19 19:57:52 +0100489 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200490 log_err("Option %s requires an argument\n", o->name);
Jens Axboe08e26e32006-11-21 13:15:10 +0100491 return 1;
492 }
493
Jens Axboee1f36502006-10-27 10:54:08 +0200494 switch (o->type) {
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100495 case FIO_OPT_STR:
496 case FIO_OPT_STR_MULTI: {
Jens Axboee1f36502006-10-27 10:54:08 +0200497 fio_opt_str_fn *fn = o->cb;
498
Jens Axboef0857372007-03-19 13:15:23 +0100499 posval_sort(o, posval);
500
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100501 ret = 1;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100502 for (i = 0; i < PARSE_MAX_VP; i++) {
Jens Axboef0857372007-03-19 13:15:23 +0100503 vp = &posval[i];
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100504 if (!vp->ival || vp->ival[0] == '\0')
Jens Axboea3073f42010-03-05 10:06:15 +0100505 continue;
Jens Axboeae2ddba2010-03-10 12:49:03 +0100506 all_skipped = 0;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700507 if (!ptr)
508 break;
Jens Axboe119cd932012-12-06 17:34:57 +0100509 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100510 ret = 0;
Jens Axboe7b504ed2014-02-11 14:19:38 -0700511 if (o->off1)
Daniel Gollubebadc0c2014-02-12 08:24:57 -0700512 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
Jens Axboe5f6ddf12010-03-09 20:40:44 +0100513 continue;
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100514 }
515 }
516
Jens Axboeae2ddba2010-03-10 12:49:03 +0100517 if (ret && !all_skipped)
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100518 show_option_values(o);
519 else if (fn)
520 ret = fn(data, ptr);
Jens Axboee1f36502006-10-27 10:54:08 +0200521 break;
522 }
Jens Axboee42b01e2011-05-05 12:05:10 -0600523 case FIO_OPT_STR_VAL_TIME:
524 is_time = 1;
Jens Axboef7fa2652009-03-09 14:20:20 +0100525 case FIO_OPT_INT:
Jens Axboee42b01e2011-05-05 12:05:10 -0600526 case FIO_OPT_STR_VAL: {
527 fio_opt_str_val_fn *fn = o->cb;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200528 char tmp[128], *p;
529
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700530 if (!is_time && o->is_time)
531 is_time = o->is_time;
532
Jens Axboeeaa89f52015-01-22 14:51:56 -0700533 tmp[sizeof(tmp) - 1] = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200534 strncpy(tmp, ptr, sizeof(tmp) - 1);
535 p = strchr(tmp, ',');
536 if (p)
537 *p = '\0';
Jens Axboee1f36502006-10-27 10:54:08 +0200538
Jens Axboee42b01e2011-05-05 12:05:10 -0600539 if (is_time)
Jens Axboe0de5b262014-02-21 15:26:01 -0800540 ret = check_str_time(tmp, &ull, o->is_seconds);
Jens Axboee42b01e2011-05-05 12:05:10 -0600541 else
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200542 ret = check_str_bytes(tmp, &ull, data);
Jens Axboee42b01e2011-05-05 12:05:10 -0600543
Jens Axboeae3fcb52013-04-25 10:06:32 -0600544 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
545
Jens Axboee1f36502006-10-27 10:54:08 +0200546 if (ret)
547 break;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700548 if (o->pow2 && !is_power_of_2(ull)) {
549 log_err("%s: must be a power-of-2\n", o->name);
550 return 1;
551 }
Jens Axboee1f36502006-10-27 10:54:08 +0200552
Jens Axboedb8e0162007-01-10 13:41:26 +0100553 if (o->maxval && ull > o->maxval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200554 log_err("max value out of range: %llu"
555 " (%u max)\n", ull, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100556 return 1;
557 }
558 if (o->minval && ull < o->minval) {
Jens Axboed4fc2f02012-09-24 14:32:26 +0200559 log_err("min value out of range: %llu"
560 " (%u min)\n", ull, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100561 return 1;
562 }
Jens Axboed926d532013-04-09 21:02:15 +0200563 if (o->posval[0].ival) {
564 posval_sort(o, posval);
565
566 ret = 1;
567 for (i = 0; i < PARSE_MAX_VP; i++) {
568 vp = &posval[i];
569 if (!vp->ival || vp->ival[0] == '\0')
570 continue;
571 if (vp->oval == ull) {
572 ret = 0;
573 break;
574 }
575 }
576 if (ret) {
Jens Axboe128e4ca2013-04-10 11:06:34 +0200577 log_err("fio: value %llu not allowed:\n", ull);
578 show_option_values(o);
Jens Axboed926d532013-04-09 21:02:15 +0200579 return 1;
580 }
581 }
Jens Axboee1f36502006-10-27 10:54:08 +0200582
583 if (fn)
584 ret = fn(data, &ull);
585 else {
Jens Axboee01b22b2009-06-09 15:43:25 +0200586 if (o->type == FIO_OPT_INT) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700587 if (first)
588 val_store(ilp, ull, o->off1, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200589 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700590 if (o->off2)
591 val_store(ilp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100592 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200593 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700594 if (o->off3)
595 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200596 }
597 if (!more) {
598 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700599 if (o->off2)
600 val_store(ilp, ull, o->off2, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200601 }
602 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700603 if (o->off3)
604 val_store(ilp, ull, o->off3, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200605 }
606 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100607 } else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700608 if (first)
609 val_store(ullp, ull, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100610 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700611 if (o->off2)
612 val_store(ullp, ull, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100613 }
Jens Axboe75e6f362006-11-03 08:17:09 +0100614 }
Jens Axboee1f36502006-10-27 10:54:08 +0200615 }
616 break;
617 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200618 case FIO_OPT_FLOAT_LIST: {
Jens Axboeeef02442013-02-06 08:51:57 +0100619 char *cp2;
620
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100621 if (first) {
622 /*
623 ** Initialize precision to 0 and zero out list
624 ** in case specified list is shorter than default
625 */
Jens Axboe3e260a42013-12-09 12:38:53 -0700626 if (o->off2) {
627 ul2 = 0;
Jens Axboef0fdbca2014-02-11 15:44:50 -0700628 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700629 *ilp = ul2;
630 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100631
Jens Axboef0fdbca2014-02-11 15:44:50 -0700632 flp = td_var(data, o, o->off1);
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100633 for(i = 0; i < o->maxlen; i++)
634 flp[i].u.f = 0.0;
635 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200636 if (curr >= o->maxlen) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200637 log_err("the list exceeding max length %d\n",
Yu-ju Hong83349192011-08-13 00:53:44 +0200638 o->maxlen);
639 return 1;
640 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -0700641 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
Jens Axboe28d7c362011-10-05 20:30:24 +0200642 log_err("not a floating point value: %s\n", ptr);
Yu-ju Hong83349192011-08-13 00:53:44 +0200643 return 1;
644 }
Jens Axboe26650692013-02-02 09:56:23 +0100645 if (uf > o->maxfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200646 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200647 " (range max: %f)\n", uf, o->maxfp);
648 return 1;
649 }
Jens Axboe26650692013-02-02 09:56:23 +0100650 if (uf < o->minfp) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200651 log_err("value out of range: %f"
Yu-ju Hong83349192011-08-13 00:53:44 +0200652 " (range min: %f)\n", uf, o->minfp);
653 return 1;
654 }
655
Jens Axboef0fdbca2014-02-11 15:44:50 -0700656 flp = td_var(data, o, o->off1);
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100657 flp[curr].u.f = uf;
Yu-ju Hong83349192011-08-13 00:53:44 +0200658
Jens Axboeae3fcb52013-04-25 10:06:32 -0600659 dprint(FD_PARSE, " out=%f\n", uf);
660
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100661 /*
662 ** Calculate precision for output by counting
663 ** number of digits after period. Find first
664 ** period in entire remaining list each time
665 */
666 cp2 = strchr(ptr, '.');
667 if (cp2 != NULL) {
Jens Axboeeef02442013-02-06 08:51:57 +0100668 int len = 0;
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100669
670 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
671 len++;
672
Jens Axboe3e260a42013-12-09 12:38:53 -0700673 if (o->off2) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700674 ilp = td_var(data, o, o->off2);
Jens Axboe3e260a42013-12-09 12:38:53 -0700675 if (len > *ilp)
676 *ilp = len;
677 }
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100678 }
679
Yu-ju Hong83349192011-08-13 00:53:44 +0200680 break;
681 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100682 case FIO_OPT_STR_STORE: {
683 fio_opt_str_fn *fn = o->cb;
684
Jens Axboef75c69a2014-04-03 08:18:07 -0600685 if (!strlen(ptr))
686 return 1;
687
Jens Axboe7b504ed2014-02-11 14:19:38 -0700688 if (o->off1) {
Jens Axboef0fdbca2014-02-11 15:44:50 -0700689 cp = td_var(data, o, o->off1);
Steven Lang184b4092011-11-16 10:33:51 +0100690 *cp = strdup(ptr);
Jens Axboe1c964ce2011-08-31 16:45:03 -0600691 }
Jens Axboe02c6aad2010-03-04 13:49:11 +0100692
Steven Lang184b4092011-11-16 10:33:51 +0100693 if (fn)
694 ret = fn(data, ptr);
695 else if (o->posval[0].ival) {
696 posval_sort(o, posval);
Jens Axboec44b1ff2011-08-30 20:43:53 -0600697
Steven Lang184b4092011-11-16 10:33:51 +0100698 ret = 1;
699 for (i = 0; i < PARSE_MAX_VP; i++) {
700 vp = &posval[i];
Jens Axboeab508172014-04-14 13:16:10 -0600701 if (!vp->ival || vp->ival[0] == '\0' || !cp)
Steven Lang184b4092011-11-16 10:33:51 +0100702 continue;
703 all_skipped = 0;
Jens Axboe119cd932012-12-06 17:34:57 +0100704 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
Steven Lang184b4092011-11-16 10:33:51 +0100705 char *rest;
706
707 ret = 0;
708 if (vp->cb)
709 fn = vp->cb;
710 rest = strstr(*cp ?: ptr, ":");
711 if (rest) {
712 if (*cp)
713 *rest = '\0';
714 ptr = rest + 1;
715 } else
716 ptr = NULL;
717 break;
718 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100719 }
720 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600721
Steven Lang184b4092011-11-16 10:33:51 +0100722 if (!all_skipped) {
723 if (ret && !*cp)
724 show_option_values(o);
725 else if (ret && *cp)
726 ret = 0;
727 else if (fn && ptr)
728 ret = fn(data, ptr);
729 }
Jens Axboec44b1ff2011-08-30 20:43:53 -0600730
Jens Axboee1f36502006-10-27 10:54:08 +0200731 break;
Jens Axboeaf52b342007-03-13 10:07:47 +0100732 }
Jens Axboee1f36502006-10-27 10:54:08 +0200733 case FIO_OPT_RANGE: {
Jens Axboeb765a372006-10-27 15:00:16 +0200734 char tmp[128];
Jens Axboee1f36502006-10-27 10:54:08 +0200735 char *p1, *p2;
736
Jens Axboeeaa89f52015-01-22 14:51:56 -0700737 tmp[sizeof(tmp) - 1] = '\0';
Jens Axboe0bbab0e2006-11-02 09:18:36 +0100738 strncpy(tmp, ptr, sizeof(tmp) - 1);
Jens Axboeb765a372006-10-27 15:00:16 +0200739
Dave Engberg31d23f42011-07-23 21:07:13 +0200740 /* Handle bsrange with separate read,write values: */
741 p1 = strchr(tmp, ',');
742 if (p1)
743 *p1 = '\0';
744
Jens Axboeb765a372006-10-27 15:00:16 +0200745 p1 = strchr(tmp, '-');
Jens Axboee1f36502006-10-27 10:54:08 +0200746 if (!p1) {
Jens Axboe0c9baf92007-01-11 15:59:26 +0100747 p1 = strchr(tmp, ':');
748 if (!p1) {
749 ret = 1;
750 break;
751 }
Jens Axboee1f36502006-10-27 10:54:08 +0200752 }
753
754 p2 = p1 + 1;
755 *p1 = '\0';
Jens Axboeb765a372006-10-27 15:00:16 +0200756 p1 = tmp;
Jens Axboee1f36502006-10-27 10:54:08 +0200757
758 ret = 1;
Jens Axboed6978a32009-07-18 21:04:09 +0200759 if (!check_range_bytes(p1, &ul1, data) &&
760 !check_range_bytes(p2, &ul2, data)) {
Jens Axboee1f36502006-10-27 10:54:08 +0200761 ret = 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200762 if (ul1 > ul2) {
Jens Axboef90eff52006-11-06 11:08:21 +0100763 unsigned long foo = ul1;
764
765 ul1 = ul2;
766 ul2 = foo;
767 }
768
769 if (first) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700770 val_store(ilp, ul1, o->off1, 0, data, o);
771 val_store(ilp, ul2, o->off2, 0, data, o);
Jens Axboee1f36502006-10-27 10:54:08 +0200772 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200773 if (curr == 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700774 if (o->off3 && o->off4) {
775 val_store(ilp, ul1, o->off3, 0, data, o);
776 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200777 }
778 }
779 if (curr == 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700780 if (o->off5 && o->off6) {
781 val_store(ilp, ul1, o->off5, 0, data, o);
782 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200783 }
784 }
785 if (!more) {
786 if (curr < 1) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700787 if (o->off3 && o->off4) {
788 val_store(ilp, ul1, o->off3, 0, data, o);
789 val_store(ilp, ul2, o->off4, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200790 }
791 }
792 if (curr < 2) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700793 if (o->off5 && o->off6) {
794 val_store(ilp, ul1, o->off5, 0, data, o);
795 val_store(ilp, ul2, o->off6, 0, data, o);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200796 }
797 }
Jens Axboe17abbe82006-11-06 11:23:10 +0100798 }
799 }
800
Jens Axboee1f36502006-10-27 10:54:08 +0200801 break;
802 }
Jens Axboe74ba1802011-07-15 09:09:15 +0200803 case FIO_OPT_BOOL:
804 case FIO_OPT_STR_SET: {
Jens Axboee1f36502006-10-27 10:54:08 +0200805 fio_opt_int_fn *fn = o->cb;
806
Jens Axboe74ba1802011-07-15 09:09:15 +0200807 if (ptr)
808 ret = check_int(ptr, &il);
809 else if (o->type == FIO_OPT_BOOL)
810 ret = 1;
811 else
812 il = 1;
813
Jens Axboeae3fcb52013-04-25 10:06:32 -0600814 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
815
Jens Axboee1f36502006-10-27 10:54:08 +0200816 if (ret)
817 break;
818
Jens Axboedb8e0162007-01-10 13:41:26 +0100819 if (o->maxval && il > (int) o->maxval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200820 log_err("max value out of range: %d (%d max)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100821 il, o->maxval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100822 return 1;
823 }
824 if (o->minval && il < o->minval) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200825 log_err("min value out of range: %d (%d min)\n",
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100826 il, o->minval);
Jens Axboedb8e0162007-01-10 13:41:26 +0100827 return 1;
828 }
Jens Axboee1f36502006-10-27 10:54:08 +0200829
Jens Axboe76a43db2007-01-11 13:24:44 +0100830 if (o->neg)
831 il = !il;
832
Jens Axboee1f36502006-10-27 10:54:08 +0200833 if (fn)
834 ret = fn(data, &il);
835 else {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700836 if (first)
837 val_store(ilp, il, o->off1, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100838 if (!more) {
Jens Axboe7b504ed2014-02-11 14:19:38 -0700839 if (o->off2)
840 val_store(ilp, il, o->off2, 0, data, o);
Jens Axboe02c6aad2010-03-04 13:49:11 +0100841 }
Jens Axboee1f36502006-10-27 10:54:08 +0200842 }
843 break;
844 }
Jens Axboe15ca1502008-04-07 09:26:02 +0200845 case FIO_OPT_DEPRECATED:
Jens Axboe06b0be62011-10-04 10:31:10 +0200846 log_info("Option %s is deprecated\n", o->name);
Jens Axboed9472272013-07-25 10:20:45 -0600847 ret = 1;
Jens Axboe15ca1502008-04-07 09:26:02 +0200848 break;
Jens Axboee1f36502006-10-27 10:54:08 +0200849 default:
Jens Axboe06b0be62011-10-04 10:31:10 +0200850 log_err("Bad option type %u\n", o->type);
Jens Axboee1f36502006-10-27 10:54:08 +0200851 ret = 1;
852 }
853
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200854 if (ret)
855 return ret;
856
Jens Axboed447a8c2009-07-17 22:26:15 +0200857 if (o->verify) {
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200858 ret = o->verify(o, data);
Jens Axboed447a8c2009-07-17 22:26:15 +0200859 if (ret) {
Jens Axboe28d7c362011-10-05 20:30:24 +0200860 log_err("Correct format for offending option\n");
861 log_err("%20s: %s\n", o->name, o->help);
Jens Axboe06b0be62011-10-04 10:31:10 +0200862 show_option_help(o, 1);
Jens Axboed447a8c2009-07-17 22:26:15 +0200863 }
864 }
Jens Axboe70a4c0c2009-07-01 09:24:05 +0200865
Jens Axboee1f36502006-10-27 10:54:08 +0200866 return ret;
867}
868
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200869static int handle_option(struct fio_option *o, const char *__ptr, void *data)
Jens Axboef90eff52006-11-06 11:08:21 +0100870{
Jens Axboeb62bdf22010-03-10 12:36:17 +0100871 char *o_ptr, *ptr, *ptr2;
872 int ret, done;
Jens Axboef90eff52006-11-06 11:08:21 +0100873
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200874 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
875
Jens Axboeb62bdf22010-03-10 12:36:17 +0100876 o_ptr = ptr = NULL;
Jens Axboe7c8f1a52009-06-09 12:28:57 +0200877 if (__ptr)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100878 o_ptr = ptr = strdup(__ptr);
Jens Axboea3d741f2008-02-27 18:32:33 +0100879
Jens Axboef90eff52006-11-06 11:08:21 +0100880 /*
Jens Axboeb62bdf22010-03-10 12:36:17 +0100881 * See if we have another set of parameters, hidden after a comma.
882 * Do this before parsing this round, to check if we should
Jens Axboe787f7e92006-11-06 13:26:29 +0100883 * copy set 1 options to set 2.
Jens Axboef90eff52006-11-06 11:08:21 +0100884 */
Jens Axboeb62bdf22010-03-10 12:36:17 +0100885 done = 0;
886 ret = 1;
887 do {
888 int __ret;
889
890 ptr2 = NULL;
891 if (ptr &&
892 (o->type != FIO_OPT_STR_STORE) &&
Yu-ju Hong83349192011-08-13 00:53:44 +0200893 (o->type != FIO_OPT_STR) &&
894 (o->type != FIO_OPT_FLOAT_LIST)) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100895 ptr2 = strchr(ptr, ',');
896 if (ptr2 && *(ptr2 + 1) == '\0')
897 *ptr2 = '\0';
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200898 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
Jens Axboeb62bdf22010-03-10 12:36:17 +0100899 if (!ptr2)
900 ptr2 = strchr(ptr, ':');
901 if (!ptr2)
902 ptr2 = strchr(ptr, '-');
903 }
Yu-ju Hong83349192011-08-13 00:53:44 +0200904 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
905 ptr2 = strchr(ptr, ':');
Jens Axboeb62bdf22010-03-10 12:36:17 +0100906 }
907
908 /*
909 * Don't return early if parsing the first option fails - if
910 * we are doing multiple arguments, we can allow the first one
911 * being empty.
912 */
Yu-ju Hong83349192011-08-13 00:53:44 +0200913 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
Jens Axboeb62bdf22010-03-10 12:36:17 +0100914 if (ret)
915 ret = __ret;
916
Jens Axboe0c9baf92007-01-11 15:59:26 +0100917 if (!ptr2)
Jens Axboeb62bdf22010-03-10 12:36:17 +0100918 break;
Jens Axboe787f7e92006-11-06 13:26:29 +0100919
Jens Axboeb62bdf22010-03-10 12:36:17 +0100920 ptr = ptr2 + 1;
921 done++;
922 } while (1);
Jens Axboe787f7e92006-11-06 13:26:29 +0100923
Jens Axboeb62bdf22010-03-10 12:36:17 +0100924 if (o_ptr)
925 free(o_ptr);
926 return ret;
Jens Axboef90eff52006-11-06 11:08:21 +0100927}
928
Elliott Hugheseda3a602017-05-19 18:53:02 -0700929struct fio_option *find_option(struct fio_option *options, const char *opt)
930{
931 struct fio_option *o;
932
933 for (o = &options[0]; o->name; o++) {
934 if (!o_match(o, opt))
935 continue;
936 if (o->type == FIO_OPT_UNSUPPORTED) {
937 log_err("Option <%s>: %s\n", o->name, o->help);
938 continue;
939 }
940
941 return o;
942 }
943
944 return NULL;
945}
946
947
Steven Langde890a12011-11-09 14:03:34 +0100948static struct fio_option *get_option(char *opt,
Jens Axboe07b32322010-03-05 09:48:44 +0100949 struct fio_option *options, char **post)
Jens Axboe3b8b7132008-06-10 19:46:23 +0200950{
951 struct fio_option *o;
952 char *ret;
953
954 ret = strchr(opt, '=');
955 if (ret) {
956 *post = ret;
957 *ret = '\0';
Steven Langde890a12011-11-09 14:03:34 +0100958 ret = opt;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200959 (*post)++;
Jens Axboe43c129b2008-12-08 14:06:42 +0100960 strip_blank_end(ret);
Jens Axboe07b32322010-03-05 09:48:44 +0100961 o = find_option(options, ret);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200962 } else {
Jens Axboe07b32322010-03-05 09:48:44 +0100963 o = find_option(options, opt);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200964 *post = NULL;
965 }
966
967 return o;
968}
969
970static int opt_cmp(const void *p1, const void *p2)
971{
Steven Langde890a12011-11-09 14:03:34 +0100972 struct fio_option *o;
973 char *s, *foo;
Jens Axboe8cdabc12008-11-19 12:31:43 +0100974 int prio1, prio2;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200975
Jens Axboe8cdabc12008-11-19 12:31:43 +0100976 prio1 = prio2 = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200977
Steven Langde890a12011-11-09 14:03:34 +0100978 if (*(char **)p1) {
979 s = strdup(*((char **) p1));
Jens Axboe9af4a242012-03-16 10:13:49 +0100980 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100981 if (o)
982 prio1 = o->prio;
983 free(s);
984 }
985 if (*(char **)p2) {
986 s = strdup(*((char **) p2));
Jens Axboe9af4a242012-03-16 10:13:49 +0100987 o = get_option(s, __fio_options, &foo);
Steven Langde890a12011-11-09 14:03:34 +0100988 if (o)
989 prio2 = o->prio;
990 free(s);
991 }
992
Jens Axboe8cdabc12008-11-19 12:31:43 +0100993 return prio2 - prio1;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200994}
995
996void sort_options(char **opts, struct fio_option *options, int num_opts)
997{
Jens Axboe9af4a242012-03-16 10:13:49 +0100998 __fio_options = options;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200999 qsort(opts, num_opts, sizeof(char *), opt_cmp);
Jens Axboe9af4a242012-03-16 10:13:49 +01001000 __fio_options = NULL;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001001}
1002
Elliott Hugheseda3a602017-05-19 18:53:02 -07001003static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1004 const char *post)
1005{
1006 struct print_option *p;
1007
1008 if (!dump_list)
1009 return;
1010
1011 p = malloc(sizeof(*p));
1012 p->name = strdup(o->name);
1013 if (post)
1014 p->value = strdup(post);
1015 else
1016 p->value = NULL;
1017
1018 flist_add_tail(&p->list, dump_list);
1019}
1020
Jens Axboeb4692822006-10-27 13:43:22 +02001021int parse_cmd_option(const char *opt, const char *val,
Elliott Hugheseda3a602017-05-19 18:53:02 -07001022 struct fio_option *options, void *data,
1023 struct flist_head *dump_list)
Jens Axboeb4692822006-10-27 13:43:22 +02001024{
1025 struct fio_option *o;
1026
Jens Axboe07b32322010-03-05 09:48:44 +01001027 o = find_option(options, opt);
Jens Axboeb4692822006-10-27 13:43:22 +02001028 if (!o) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001029 log_err("Bad option <%s>\n", opt);
Jens Axboeb4692822006-10-27 13:43:22 +02001030 return 1;
1031 }
1032
Elliott Hugheseda3a602017-05-19 18:53:02 -07001033 if (handle_option(o, val, data)) {
1034 log_err("fio: failed parsing %s=%s\n", opt, val);
1035 return 1;
1036 }
Jens Axboeb1508cf2006-11-02 09:12:40 +01001037
Elliott Hugheseda3a602017-05-19 18:53:02 -07001038 add_to_dump_list(o, dump_list, val);
1039 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02001040}
1041
Steven Langde890a12011-11-09 14:03:34 +01001042int parse_option(char *opt, const char *input,
Jens Axboe292cc472013-11-26 20:39:35 -07001043 struct fio_option *options, struct fio_option **o, void *data,
Elliott Hugheseda3a602017-05-19 18:53:02 -07001044 struct flist_head *dump_list)
Jens Axboee1f36502006-10-27 10:54:08 +02001045{
Steven Langd0c814e2011-10-28 08:37:13 +02001046 char *post;
Jens Axboee1f36502006-10-27 10:54:08 +02001047
Steven Langd0c814e2011-10-28 08:37:13 +02001048 if (!opt) {
1049 log_err("fio: failed parsing %s\n", input);
Steven Langde890a12011-11-09 14:03:34 +01001050 *o = NULL;
Jens Axboe38789b52010-03-03 09:23:20 +01001051 return 1;
Steven Langd0c814e2011-10-28 08:37:13 +02001052 }
Jens Axboee1f36502006-10-27 10:54:08 +02001053
Steven Langde890a12011-11-09 14:03:34 +01001054 *o = get_option(opt, options, &post);
1055 if (!*o) {
1056 if (post) {
1057 int len = strlen(opt);
1058 if (opt + len + 1 != post)
1059 memmove(opt + len + 1, post, strlen(post));
1060 opt[len] = '=';
1061 }
Jens Axboee1f36502006-10-27 10:54:08 +02001062 return 1;
1063 }
1064
Jens Axboe292cc472013-11-26 20:39:35 -07001065 if (handle_option(*o, post, data)) {
1066 log_err("fio: failed parsing %s\n", input);
1067 return 1;
1068 }
Jens Axboeb1508cf2006-11-02 09:12:40 +01001069
Elliott Hugheseda3a602017-05-19 18:53:02 -07001070 add_to_dump_list(*o, dump_list, post);
Jens Axboe292cc472013-11-26 20:39:35 -07001071 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +02001072}
Jens Axboefd28ca42007-01-09 21:20:13 +01001073
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001074/*
1075 * Option match, levenshtein distance. Handy for not quite remembering what
1076 * the option name is.
1077 */
Jens Axboe786715e2014-12-05 09:35:40 -07001078int string_distance(const char *s1, const char *s2)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001079{
1080 unsigned int s1_len = strlen(s1);
1081 unsigned int s2_len = strlen(s2);
1082 unsigned int *p, *q, *r;
1083 unsigned int i, j;
1084
1085 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1086 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1087
1088 p[0] = 0;
1089 for (i = 1; i <= s2_len; i++)
1090 p[i] = p[i - 1] + 1;
1091
1092 for (i = 1; i <= s1_len; i++) {
1093 q[0] = p[0] + 1;
1094 for (j = 1; j <= s2_len; j++) {
1095 unsigned int sub = p[j - 1];
Jens Axboe0f7f9a92014-11-06 09:21:10 -07001096 unsigned int pmin;
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001097
1098 if (s1[i - 1] != s2[j - 1])
1099 sub++;
1100
Jens Axboe0f7f9a92014-11-06 09:21:10 -07001101 pmin = min(q[j - 1] + 1, sub);
1102 q[j] = min(p[j] + 1, pmin);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001103 }
1104 r = p;
1105 p = q;
1106 q = r;
1107 }
1108
1109 i = p[s2_len];
1110 free(p);
1111 free(q);
1112 return i;
1113}
1114
Elliott Hugheseda3a602017-05-19 18:53:02 -07001115/*
1116 * Make a guess of whether the distance from 's1' is significant enough
1117 * to warrant printing the guess. We set this to a 1/2 match.
1118 */
1119int string_distance_ok(const char *opt, int distance)
1120{
1121 size_t len;
1122
1123 len = strlen(opt);
1124 len = (len + 1) / 2;
1125 return distance <= len;
1126}
1127
Jens Axboeafdf9352007-07-31 16:14:34 +02001128static struct fio_option *find_child(struct fio_option *options,
1129 struct fio_option *o)
1130{
1131 struct fio_option *__o;
1132
Jens Axboefdf28742007-07-31 23:06:09 +02001133 for (__o = options + 1; __o->name; __o++)
1134 if (__o->parent && !strcmp(__o->parent, o->name))
Jens Axboeafdf9352007-07-31 16:14:34 +02001135 return __o;
1136
1137 return NULL;
1138}
1139
Jens Axboe323d9112008-03-01 15:12:14 +01001140static void __print_option(struct fio_option *o, struct fio_option *org,
1141 int level)
Jens Axboeafdf9352007-07-31 16:14:34 +02001142{
1143 char name[256], *p;
Jens Axboe323d9112008-03-01 15:12:14 +01001144 int depth;
Jens Axboeafdf9352007-07-31 16:14:34 +02001145
1146 if (!o)
1147 return;
Jens Axboeef9aff52007-07-31 22:56:53 +02001148 if (!org)
1149 org = o;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001150
Jens Axboeafdf9352007-07-31 16:14:34 +02001151 p = name;
Jens Axboe323d9112008-03-01 15:12:14 +01001152 depth = level;
1153 while (depth--)
1154 p += sprintf(p, "%s", " ");
Jens Axboeafdf9352007-07-31 16:14:34 +02001155
1156 sprintf(p, "%s", o->name);
1157
Jens Axboe28d7c362011-10-05 20:30:24 +02001158 log_info("%-24s: %s\n", name, o->help);
Jens Axboe323d9112008-03-01 15:12:14 +01001159}
1160
1161static void print_option(struct fio_option *o)
1162{
1163 struct fio_option *parent;
1164 struct fio_option *__o;
1165 unsigned int printed;
1166 unsigned int level;
1167
1168 __print_option(o, NULL, 0);
1169 parent = o;
1170 level = 0;
1171 do {
1172 level++;
1173 printed = 0;
1174
1175 while ((__o = find_child(o, parent)) != NULL) {
1176 __print_option(__o, o, level);
1177 o = __o;
1178 printed++;
1179 }
1180
1181 parent = o;
1182 } while (printed);
Jens Axboeafdf9352007-07-31 16:14:34 +02001183}
1184
Jens Axboe07b32322010-03-05 09:48:44 +01001185int show_cmd_help(struct fio_option *options, const char *name)
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001186{
1187 struct fio_option *o, *closest;
Jens Axboed091d092010-04-01 19:56:23 +02001188 unsigned int best_dist = -1U;
Jens Axboe29fc6af2007-01-09 21:22:02 +01001189 int found = 0;
Jens Axboe320beef2007-03-01 10:44:12 +01001190 int show_all = 0;
1191
1192 if (!name || !strcmp(name, "all"))
1193 show_all = 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001194
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001195 closest = NULL;
1196 best_dist = -1;
Jens Axboe4945ba12007-01-11 14:36:56 +01001197 for (o = &options[0]; o->name; o++) {
Jens Axboe320beef2007-03-01 10:44:12 +01001198 int match = 0;
1199
Jens Axboe15ca1502008-04-07 09:26:02 +02001200 if (o->type == FIO_OPT_DEPRECATED)
1201 continue;
Jens Axboe07b32322010-03-05 09:48:44 +01001202 if (!exec_profile && o->prof_name)
1203 continue;
Jens Axboe4ac23d22013-05-23 21:14:03 +02001204 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1205 continue;
Jens Axboe15ca1502008-04-07 09:26:02 +02001206
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001207 if (name) {
Jens Axboe7f9348f2007-03-15 14:09:28 +01001208 if (!strcmp(name, o->name) ||
1209 (o->alias && !strcmp(name, o->alias)))
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001210 match = 1;
1211 else {
1212 unsigned int dist;
1213
1214 dist = string_distance(name, o->name);
1215 if (dist < best_dist) {
1216 best_dist = dist;
1217 closest = o;
1218 }
1219 }
1220 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001221
1222 if (show_all || match) {
Jens Axboe29fc6af2007-01-09 21:22:02 +01001223 found = 1;
Jens Axboec167ded2007-03-14 13:02:53 +01001224 if (match)
Jens Axboe28d7c362011-10-05 20:30:24 +02001225 log_info("%20s: %s\n", o->name, o->help);
Jens Axboec167ded2007-03-14 13:02:53 +01001226 if (show_all) {
Jens Axboeafdf9352007-07-31 16:14:34 +02001227 if (!o->parent)
Jens Axboe323d9112008-03-01 15:12:14 +01001228 print_option(o);
Jens Axboe4945ba12007-01-11 14:36:56 +01001229 continue;
Jens Axboec167ded2007-03-14 13:02:53 +01001230 }
Jens Axboefd28ca42007-01-09 21:20:13 +01001231 }
1232
Jens Axboe70df2f12007-01-11 14:04:47 +01001233 if (!match)
1234 continue;
1235
Jens Axboe06b0be62011-10-04 10:31:10 +02001236 show_option_help(o, 0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001237 }
1238
Jens Axboe29fc6af2007-01-09 21:22:02 +01001239 if (found)
1240 return 0;
Jens Axboefd28ca42007-01-09 21:20:13 +01001241
Jens Axboe28d7c362011-10-05 20:30:24 +02001242 log_err("No such command: %s", name);
Jens Axboed091d092010-04-01 19:56:23 +02001243
1244 /*
1245 * Only print an appropriately close option, one where the edit
1246 * distance isn't too big. Otherwise we get crazy matches.
1247 */
1248 if (closest && best_dist < 3) {
Jens Axboe28d7c362011-10-05 20:30:24 +02001249 log_info(" - showing closest match\n");
1250 log_info("%20s: %s\n", closest->name, closest->help);
Jens Axboe06b0be62011-10-04 10:31:10 +02001251 show_option_help(closest, 0);
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001252 } else
Jens Axboe28d7c362011-10-05 20:30:24 +02001253 log_info("\n");
Jens Axboe0e9f7fa2007-03-01 14:05:30 +01001254
Jens Axboe29fc6af2007-01-09 21:22:02 +01001255 return 1;
Jens Axboefd28ca42007-01-09 21:20:13 +01001256}
Jens Axboeee738492007-01-10 11:23:16 +01001257
Jens Axboe13335dd2007-01-10 13:14:02 +01001258/*
1259 * Handle parsing of default parameters.
1260 */
Jens Axboeee738492007-01-10 11:23:16 +01001261void fill_default_options(void *data, struct fio_option *options)
1262{
Jens Axboe4945ba12007-01-11 14:36:56 +01001263 struct fio_option *o;
Jens Axboeee738492007-01-10 11:23:16 +01001264
Jens Axboea3d741f2008-02-27 18:32:33 +01001265 dprint(FD_PARSE, "filling default options\n");
1266
Jens Axboe4945ba12007-01-11 14:36:56 +01001267 for (o = &options[0]; o->name; o++)
Jens Axboeee738492007-01-10 11:23:16 +01001268 if (o->def)
1269 handle_option(o, o->def, data);
Jens Axboeee738492007-01-10 11:23:16 +01001270}
Jens Axboe13335dd2007-01-10 13:14:02 +01001271
Elliott Hugheseda3a602017-05-19 18:53:02 -07001272static void option_init(struct fio_option *o)
Jens Axboe9f988e22010-03-04 10:42:38 +01001273{
Elliott Hugheseda3a602017-05-19 18:53:02 -07001274 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
Jens Axboe9f988e22010-03-04 10:42:38 +01001275 return;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001276 if (o->name && !o->lname)
1277 log_err("Option %s: missing long option name\n", o->name);
Jens Axboe9f988e22010-03-04 10:42:38 +01001278 if (o->type == FIO_OPT_BOOL) {
1279 o->minval = 0;
1280 o->maxval = 1;
1281 }
Jens Axboed4fc2f02012-09-24 14:32:26 +02001282 if (o->type == FIO_OPT_INT) {
1283 if (!o->maxval)
1284 o->maxval = UINT_MAX;
1285 }
Yu-ju Hong83349192011-08-13 00:53:44 +02001286 if (o->type == FIO_OPT_FLOAT_LIST) {
Bruce Cranab9461e2013-02-02 14:31:24 +00001287 o->minfp = DBL_MIN;
1288 o->maxfp = DBL_MAX;
Yu-ju Hong83349192011-08-13 00:53:44 +02001289 }
Jens Axboeef7035a2014-06-18 15:30:09 -07001290 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
Jens Axboe06b0be62011-10-04 10:31:10 +02001291 log_err("Option %s: string set option with"
Jens Axboe9f988e22010-03-04 10:42:38 +01001292 " default will always be true\n", o->name);
1293 }
Jens Axboe7b504ed2014-02-11 14:19:38 -07001294 if (!o->cb && !o->off1)
Jens Axboe06b0be62011-10-04 10:31:10 +02001295 log_err("Option %s: neither cb nor offset given\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001296 if (!o->category) {
Jens Axboecca5b5b2013-04-10 19:35:25 +02001297 log_info("Option %s: no category defined. Setting to misc\n", o->name);
Jens Axboe22754742013-04-10 13:05:23 +02001298 o->category = FIO_OPT_C_GENERAL;
Jens Axboeffd78212013-04-10 13:07:43 +02001299 o->group = FIO_OPT_G_INVALID;
Jens Axboe22754742013-04-10 13:05:23 +02001300 }
Jens Axboe5f6ddf12010-03-09 20:40:44 +01001301 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1302 o->type == FIO_OPT_STR_MULTI)
Jens Axboe9f988e22010-03-04 10:42:38 +01001303 return;
Jens Axboe9f988e22010-03-04 10:42:38 +01001304}
1305
Jens Axboe13335dd2007-01-10 13:14:02 +01001306/*
1307 * Sanitize the options structure. For now it just sets min/max for bool
Jens Axboe5b0a8882007-01-11 14:40:27 +01001308 * values and whether both callback and offsets are given.
Jens Axboe13335dd2007-01-10 13:14:02 +01001309 */
1310void options_init(struct fio_option *options)
1311{
Jens Axboe4945ba12007-01-11 14:36:56 +01001312 struct fio_option *o;
Jens Axboe13335dd2007-01-10 13:14:02 +01001313
Jens Axboea3d741f2008-02-27 18:32:33 +01001314 dprint(FD_PARSE, "init options\n");
1315
Jens Axboe90265352012-03-19 20:29:44 +01001316 for (o = &options[0]; o->name; o++) {
Jens Axboe9f988e22010-03-04 10:42:38 +01001317 option_init(o);
Jens Axboe90265352012-03-19 20:29:44 +01001318 if (o->inverse)
1319 o->inv_opt = find_option(options, o->inverse);
1320 }
Jens Axboe13335dd2007-01-10 13:14:02 +01001321}
Jens Axboe7e356b22011-10-14 10:55:16 +02001322
Elliott Hugheseda3a602017-05-19 18:53:02 -07001323void options_mem_dupe(struct fio_option *options, void *data)
1324{
1325 struct fio_option *o;
1326 char **ptr;
1327
1328 dprint(FD_PARSE, "dup options\n");
1329
1330 for (o = &options[0]; o->name; o++) {
1331 if (o->type != FIO_OPT_STR_STORE)
1332 continue;
1333
1334 ptr = td_var(data, o, o->off1);
1335 if (*ptr)
1336 *ptr = strdup(*ptr);
1337 }
1338}
1339
Jens Axboe7e356b22011-10-14 10:55:16 +02001340void options_free(struct fio_option *options, void *data)
1341{
1342 struct fio_option *o;
1343 char **ptr;
1344
1345 dprint(FD_PARSE, "free options\n");
1346
1347 for (o = &options[0]; o->name; o++) {
Steven Langde890a12011-11-09 14:03:34 +01001348 if (o->type != FIO_OPT_STR_STORE || !o->off1)
Jens Axboe7e356b22011-10-14 10:55:16 +02001349 continue;
1350
Jens Axboef0fdbca2014-02-11 15:44:50 -07001351 ptr = td_var(data, o, o->off1);
Jens Axboe7e356b22011-10-14 10:55:16 +02001352 if (*ptr) {
1353 free(*ptr);
1354 *ptr = NULL;
1355 }
1356 }
1357}