Refactor fio_show_ioengine_help()
[fio.git] / parse.c
CommitLineData
cb2c86fd
JA
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>
88b5a391 11#include <stdlib.h>
83349190 12#include <math.h>
ab9461ea 13#include <float.h>
cb2c86fd
JA
14
15#include "parse.h"
a3d741fa 16#include "debug.h"
9f988e2e 17#include "options.h"
d220c761 18#include "optgroup.h"
e25839d4 19#include "minmax.h"
fd112d34 20#include "lib/ieee754.h"
0f38bbef 21#include "lib/pow2.h"
cb2c86fd 22
b470a02c
SC
23#ifdef CONFIG_ARITHMETIC
24#include "y.tab.h"
25#endif
26
9af4a244 27static struct fio_option *__fio_options;
3b8b7135 28
f085737f
JA
29static 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
ce952ab6 37static void posval_sort(struct fio_option *o, struct value_pair *vpmap)
f085737f
JA
38{
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
06b0be6e 55static void show_option_range(struct fio_option *o,
5768cc2b 56 size_t (*logger)(const char *format, ...))
b1ec1da6 57{
3c037bcf 58 if (o->type == FIO_OPT_FLOAT_LIST) {
ab9461ea 59 if (o->minfp == DBL_MIN && o->maxfp == DBL_MAX)
83349190
YH
60 return;
61
06b0be6e 62 logger("%20s: min=%f", "range", o->minfp);
ab9461ea 63 if (o->maxfp != DBL_MAX)
06b0be6e
JA
64 logger(", max=%f", o->maxfp);
65 logger("\n");
3c037bcf 66 } else if (!o->posval[0].ival) {
83349190
YH
67 if (!o->minval && !o->maxval)
68 return;
b1ec1da6 69
06b0be6e 70 logger("%20s: min=%d", "range", o->minval);
83349190 71 if (o->maxval)
06b0be6e
JA
72 logger(", max=%d", o->maxval);
73 logger("\n");
83349190 74 }
b1ec1da6
JA
75}
76
77static void show_option_values(struct fio_option *o)
78{
a3073f4a 79 int i;
b1ec1da6 80
a3073f4a 81 for (i = 0; i < PARSE_MAX_VP; i++) {
7837213b 82 const struct value_pair *vp = &o->posval[i];
b1ec1da6 83
7837213b 84 if (!vp->ival)
a3073f4a 85 continue;
b1ec1da6 86
06b0be6e 87 log_info("%20s: %-10s", i == 0 ? "valid values" : "", vp->ival);
7837213b 88 if (vp->help)
06b0be6e
JA
89 log_info(" %s", vp->help);
90 log_info("\n");
a3073f4a 91 }
b1ec1da6
JA
92
93 if (i)
06b0be6e 94 log_info("\n");
b1ec1da6
JA
95}
96
06b0be6e 97static void show_option_help(struct fio_option *o, int is_err)
d447a8c2
JA
98{
99 const char *typehelp[] = {
07b3232d 100 "invalid",
d447a8c2 101 "string (opt=bla)",
ae3fb6fb 102 "string (opt=bla)",
d447a8c2
JA
103 "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)",
83349190 109 "list of floating point values separated by ':' (opt=5.9:7.8)",
d447a8c2 110 "no argument (opt)",
07b3232d 111 "deprecated",
675d2d65 112 "unsupported",
d447a8c2 113 };
5768cc2b 114 size_t (*logger)(const char *format, ...);
06b0be6e
JA
115
116 if (is_err)
117 logger = log_err;
118 else
119 logger = log_info;
d447a8c2
JA
120
121 if (o->alias)
06b0be6e 122 logger("%20s: %s\n", "alias", o->alias);
d447a8c2 123
06b0be6e
JA
124 logger("%20s: %s\n", "type", typehelp[o->type]);
125 logger("%20s: %s\n", "default", o->def ? o->def : "no default");
07b3232d 126 if (o->prof_name)
06b0be6e
JA
127 logger("%20s: only for profile '%s'\n", "valid", o->prof_name);
128 show_option_range(o, logger);
d447a8c2
JA
129 show_option_values(o);
130}
131
0de5b26f
JA
132static unsigned long long get_mult_time(const char *str, int len,
133 int is_seconds)
cb2c86fd 134{
74454ce4
CE
135 const char *p = str;
136 char *c;
e4668264 137 unsigned long long mult = 1;
74454ce4
CE
138
139 /*
140 * Go forward until we hit a non-digit, or +/- sign
141 */
142 while ((p - str) <= len) {
143 if (!isdigit((int) *p) && (*p != '+') && (*p != '-'))
144 break;
145 p++;
cb2c86fd 146 }
74454ce4 147
0de5b26f
JA
148 if (!isalpha((int) *p)) {
149 if (is_seconds)
150 return 1000000UL;
151 else
152 return 1;
153 }
74454ce4
CE
154
155 c = strdup(p);
156 for (int i = 0; i < strlen(c); i++)
157 c[i] = tolower(c[i]);
158
e4668264 159 if (!strncmp("us", c, 2) || !strncmp("usec", c, 4))
74454ce4 160 mult = 1;
e4668264 161 else if (!strncmp("ms", c, 2) || !strncmp("msec", c, 4))
74454ce4 162 mult = 1000;
e4668264
JA
163 else if (!strcmp("s", c))
164 mult = 1000000;
74454ce4 165 else if (!strcmp("m", c))
e4668264 166 mult = 60 * 1000000UL;
74454ce4 167 else if (!strcmp("h", c))
e4668264 168 mult = 60 * 60 * 1000000UL;
74454ce4 169 else if (!strcmp("d", c))
e4668264 170 mult = 24 * 60 * 60 * 1000000UL;
74454ce4
CE
171
172 free(c);
173 return mult;
cb2c86fd
JA
174}
175
a03fb65f
JA
176static int is_separator(char c)
177{
178 switch (c) {
179 case ':':
180 case '-':
181 case ',':
182 case '/':
183 return 1;
184 default:
185 return 0;
186 }
187}
188
7bb59102
JA
189static unsigned long long __get_mult_bytes(const char *p, void *data,
190 int *percent)
cb2c86fd 191{
d6978a32 192 unsigned int kb_base = fio_get_kb_base(data);
a639f0bb 193 unsigned long long ret = 1;
57fc29fa
JA
194 unsigned int i, pow = 0, mult = kb_base;
195 char *c;
a639f0bb 196
57fc29fa
JA
197 if (!p)
198 return 1;
a639f0bb 199
57fc29fa
JA
200 c = strdup(p);
201
a03fb65f 202 for (i = 0; i < strlen(c); i++) {
57fc29fa 203 c[i] = tolower(c[i]);
a03fb65f
JA
204 if (is_separator(c[i])) {
205 c[i] = '\0';
206 break;
207 }
208 }
57fc29fa 209
d694a6a7
RE
210 /* If kb_base is 1000, use true units.
211 * If kb_base is 1024, use opposite units.
212 */
a04f158d 213 if (!strncmp("pib", c, 3)) {
57fc29fa 214 pow = 5;
d694a6a7
RE
215 if (kb_base == 1000)
216 mult = 1024;
217 else if (kb_base == 1024)
218 mult = 1000;
a04f158d 219 } else if (!strncmp("tib", c, 3)) {
57fc29fa 220 pow = 4;
d694a6a7
RE
221 if (kb_base == 1000)
222 mult = 1024;
223 else if (kb_base == 1024)
224 mult = 1000;
a04f158d 225 } else if (!strncmp("gib", c, 3)) {
57fc29fa 226 pow = 3;
d694a6a7
RE
227 if (kb_base == 1000)
228 mult = 1024;
229 else if (kb_base == 1024)
230 mult = 1000;
a04f158d 231 } else if (!strncmp("mib", c, 3)) {
57fc29fa 232 pow = 2;
d694a6a7
RE
233 if (kb_base == 1000)
234 mult = 1024;
235 else if (kb_base == 1024)
236 mult = 1000;
a04f158d 237 } else if (!strncmp("kib", c, 3)) {
57fc29fa 238 pow = 1;
d694a6a7
RE
239 if (kb_base == 1000)
240 mult = 1024;
241 else if (kb_base == 1024)
242 mult = 1000;
243 } else if (!strncmp("p", c, 1) || !strncmp("pb", c, 2)) {
57fc29fa 244 pow = 5;
d694a6a7 245 } else if (!strncmp("t", c, 1) || !strncmp("tb", c, 2)) {
57fc29fa 246 pow = 4;
d694a6a7 247 } else if (!strncmp("g", c, 1) || !strncmp("gb", c, 2)) {
57fc29fa 248 pow = 3;
d694a6a7 249 } else if (!strncmp("m", c, 1) || !strncmp("mb", c, 2)) {
57fc29fa 250 pow = 2;
d694a6a7 251 } else if (!strncmp("k", c, 1) || !strncmp("kb", c, 2)) {
57fc29fa 252 pow = 1;
d694a6a7 253 } else if (!strncmp("%", c, 1)) {
7bb59102 254 *percent = 1;
e721c57f 255 free(c);
7bb59102
JA
256 return ret;
257 }
57fc29fa
JA
258
259 while (pow--)
260 ret *= (unsigned long long) mult;
261
262 free(c);
a639f0bb 263 return ret;
cb2c86fd
JA
264}
265
7bb59102 266static unsigned long long get_mult_bytes(const char *str, int len, void *data,
6925dd35 267 int *percent)
57fc29fa 268{
55ed9636 269 const char *p = str;
ba4ddd69 270 int digit_seen = 0;
57fc29fa 271
1d1c187b 272 if (len < 2)
7bb59102 273 return __get_mult_bytes(str, data, percent);
1d1c187b 274
d0c814ec
SL
275 /*
276 * Go forward until we hit a non-digit, or +/- sign
277 */
55ed9636 278 while ((p - str) <= len) {
ba4ddd69
JA
279 if (!isdigit((int) *p) &&
280 (((*p != '+') && (*p != '-')) || digit_seen))
55ed9636 281 break;
3c703d13 282 digit_seen |= isdigit((int) *p);
55ed9636
JA
283 p++;
284 }
285
7bb59102 286 if (!isalpha((int) *p) && (*p != '%'))
57fc29fa
JA
287 p = NULL;
288
7bb59102 289 return __get_mult_bytes(p, data, percent);
57fc29fa
JA
290}
291
b470a02c 292extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
88038bc7
SC
293 double *dval, double implied_units,
294 int is_time);
b470a02c 295
83349190
YH
296/*
297 * Convert string into a floating number. Return 1 for success and 0 otherwise.
298 */
88038bc7 299int str_to_float(const char *str, double *val, int is_time)
83349190 300{
b470a02c
SC
301#ifdef CONFIG_ARITHMETIC
302 int rc;
303 long long ival;
304 double dval;
305
306 if (str[0] == '(') {
88038bc7 307 rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0, is_time);
b470a02c
SC
308 if (!rc) {
309 *val = dval;
310 return 1;
311 }
312 }
313#endif
314 return 1 == sscanf(str, "%lf", val);
83349190
YH
315}
316
cb2c86fd 317/*
e1f36503 318 * convert string into decimal value, noting any size suffix
cb2c86fd 319 */
0de5b26f 320int str_to_decimal(const char *str, long long *val, int kilo, void *data,
88038bc7 321 int is_seconds, int is_time)
cb2c86fd 322{
b347f9da 323 int len, base;
b470a02c
SC
324 int rc = 1;
325#ifdef CONFIG_ARITHMETIC
326 long long ival;
327 double dval;
8e2c678f 328 double implied_units = 1.0;
b470a02c 329#endif
cb2c86fd 330
cb2c86fd 331 len = strlen(str);
f90eff5a
JA
332 if (!len)
333 return 1;
cb2c86fd 334
b470a02c 335#ifdef CONFIG_ARITHMETIC
8e2c678f
SC
336 if (is_seconds)
337 implied_units = 1000000.0;
b470a02c 338 if (str[0] == '(')
88038bc7 339 rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units, is_time);
18722a18
SC
340 if (str[0] == '(' && !rc) {
341 if (!kilo && is_seconds)
342 *val = ival / 1000000LL;
343 else
344 *val = ival;
345 }
b470a02c 346#endif
b347f9da 347
b470a02c
SC
348 if (rc == 1) {
349 if (strstr(str, "0x") || strstr(str, "0X"))
350 base = 16;
351 else
352 base = 10;
353
354 *val = strtoll(str, NULL, base);
355 if (*val == LONG_MAX && errno == ERANGE)
356 return 1;
357 }
cb2c86fd 358
7bb59102
JA
359 if (kilo) {
360 unsigned long long mult;
361 int perc = 0;
362
6925dd35 363 mult = get_mult_bytes(str, len, data, &perc);
7bb59102
JA
364 if (perc)
365 *val = -1ULL - *val;
366 else
367 *val *= mult;
368 } else
0de5b26f 369 *val *= get_mult_time(str, len, is_seconds);
1d31d1bc 370
cb2c86fd
JA
371 return 0;
372}
373
9af4a244 374int check_str_bytes(const char *p, long long *val, void *data)
cb2c86fd 375{
88038bc7 376 return str_to_decimal(p, val, 1, data, 0, 0);
cb2c86fd
JA
377}
378
0de5b26f 379int check_str_time(const char *p, long long *val, int is_seconds)
cb2c86fd 380{
88038bc7 381 return str_to_decimal(p, val, 0, NULL, is_seconds, 1);
cb2c86fd
JA
382}
383
384void strip_blank_front(char **p)
385{
386 char *s = *p;
387
4c8e9640
JA
388 if (!strlen(s))
389 return;
76cd9378 390 while (isspace((int) *s))
cb2c86fd 391 s++;
4d651dad
JA
392
393 *p = s;
cb2c86fd
JA
394}
395
396void strip_blank_end(char *p)
397{
853ee7fc 398 char *start = p, *s;
523bfadb 399
4c8e9640
JA
400 if (!strlen(p))
401 return;
402
523bfadb
JA
403 s = strchr(p, ';');
404 if (s)
405 *s = '\0';
406 s = strchr(p, '#');
407 if (s)
408 *s = '\0';
409 if (s)
410 p = s;
411
7f7e6e59 412 s = p + strlen(p);
76cd9378 413 while ((isspace((int) *s) || iscntrl((int) *s)) && (s > start))
cb2c86fd
JA
414 s--;
415
416 *(s + 1) = '\0';
417}
418
d6978a32 419static int check_range_bytes(const char *str, long *val, void *data)
cb2c86fd 420{
57fc29fa 421 long long __val;
cb2c86fd 422
88038bc7 423 if (!str_to_decimal(str, &__val, 1, data, 0, 0)) {
57fc29fa 424 *val = __val;
cb2c86fd
JA
425 return 0;
426 }
427
cb2c86fd
JA
428 return 1;
429}
430
63f29372 431static int check_int(const char *p, int *val)
cb2c86fd 432{
787f7e95
JA
433 if (!strlen(p))
434 return 1;
d78ee463 435 if (strstr(p, "0x") || strstr(p, "0X")) {
a61bdfd8
JA
436 if (sscanf(p, "%x", val) == 1)
437 return 0;
438 } else {
439 if (sscanf(p, "%u", val) == 1)
440 return 0;
441 }
cb2c86fd 442
e1f36503
JA
443 return 1;
444}
cb2c86fd 445
e6f735f0 446static size_t opt_len(const char *str)
808def70
JA
447{
448 char *postfix;
449
450 postfix = strchr(str, ':');
451 if (!postfix)
452 return strlen(str);
453
454 return (int)(postfix - str);
455}
456
119cd939
JA
457static int str_match_len(const struct value_pair *vp, const char *str)
458{
459 return max(strlen(vp->ival), opt_len(str));
460}
461
f0fdbcaf
JA
462#define val_store(ptr, val, off, or, data, o) \
463 do { \
464 ptr = td_var((data), (o), (off)); \
465 if ((or)) \
466 *ptr |= (val); \
467 else \
468 *ptr = (val); \
17abbe89
JA
469 } while (0)
470
f90eff5a 471static int __handle_option(struct fio_option *o, const char *ptr, void *data,
83349190 472 int first, int more, int curr)
cb2c86fd 473{
2fdbefdd 474 int il=0, *ilp;
fd112d34 475 fio_fp64_t *flp;
63f29372
JA
476 long long ull, *ullp;
477 long ul1, ul2;
83349190 478 double uf;
bfe1d592 479 char **cp = NULL;
e42b01eb 480 int ret = 0, is_time = 0;
c44b1ff5
JA
481 const struct value_pair *vp;
482 struct value_pair posval[PARSE_MAX_VP];
483 int i, all_skipped = 1;
cb2c86fd 484
a3d741fa
JA
485 dprint(FD_PARSE, "__handle_option=%s, type=%d, ptr=%s\n", o->name,
486 o->type, ptr);
487
e3cedca7 488 if (!ptr && o->type != FIO_OPT_STR_SET && o->type != FIO_OPT_STR) {
28d7c363 489 log_err("Option %s requires an argument\n", o->name);
08e26e35
JA
490 return 1;
491 }
492
e1f36503 493 switch (o->type) {
5f6ddf1e
JA
494 case FIO_OPT_STR:
495 case FIO_OPT_STR_MULTI: {
e1f36503 496 fio_opt_str_fn *fn = o->cb;
b1ec1da6 497
f085737f
JA
498 posval_sort(o, posval);
499
5f6ddf1e 500 ret = 1;
b1ec1da6 501 for (i = 0; i < PARSE_MAX_VP; i++) {
f085737f 502 vp = &posval[i];
b1ec1da6 503 if (!vp->ival || vp->ival[0] == '\0')
a3073f4a 504 continue;
ae2ddba4 505 all_skipped = 0;
078b46d1
JA
506 if (!ptr)
507 break;
119cd939 508 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
b1ec1da6 509 ret = 0;
7b504edd 510 if (o->off1)
ebadc0ce 511 val_store(ilp, vp->oval, o->off1, vp->orval, data, o);
5f6ddf1e 512 continue;
b1ec1da6
JA
513 }
514 }
cb2c86fd 515
ae2ddba4 516 if (ret && !all_skipped)
b1ec1da6
JA
517 show_option_values(o);
518 else if (fn)
519 ret = fn(data, ptr);
e1f36503
JA
520 break;
521 }
e42b01eb
JA
522 case FIO_OPT_STR_VAL_TIME:
523 is_time = 1;
1a1137d9 524 case FIO_OPT_INT:
e42b01eb
JA
525 case FIO_OPT_STR_VAL: {
526 fio_opt_str_val_fn *fn = o->cb;
6eaf09d6
SL
527 char tmp[128], *p;
528
88038bc7
SC
529 if (!is_time && o->is_time)
530 is_time = o->is_time;
531
209e1037 532 tmp[sizeof(tmp) - 1] = '\0';
6eaf09d6
SL
533 strncpy(tmp, ptr, sizeof(tmp) - 1);
534 p = strchr(tmp, ',');
535 if (p)
536 *p = '\0';
e42b01eb
JA
537
538 if (is_time)
0de5b26f 539 ret = check_str_time(tmp, &ull, o->is_seconds);
e42b01eb 540 else
6eaf09d6 541 ret = check_str_bytes(tmp, &ull, data);
e1f36503 542
ae3fcb5a
JA
543 dprint(FD_PARSE, " ret=%d, out=%llu\n", ret, ull);
544
e1f36503
JA
545 if (ret)
546 break;
0f38bbef
JA
547 if (o->pow2 && !is_power_of_2(ull)) {
548 log_err("%s: must be a power-of-2\n", o->name);
549 return 1;
550 }
e1f36503 551
db8e0165 552 if (o->maxval && ull > o->maxval) {
d4fc2f04
JA
553 log_err("max value out of range: %llu"
554 " (%u max)\n", ull, o->maxval);
db8e0165
JA
555 return 1;
556 }
557 if (o->minval && ull < o->minval) {
d4fc2f04
JA
558 log_err("min value out of range: %llu"
559 " (%u min)\n", ull, o->minval);
db8e0165
JA
560 return 1;
561 }
d926d535
JA
562 if (o->posval[0].ival) {
563 posval_sort(o, posval);
564
565 ret = 1;
566 for (i = 0; i < PARSE_MAX_VP; i++) {
567 vp = &posval[i];
568 if (!vp->ival || vp->ival[0] == '\0')
569 continue;
570 if (vp->oval == ull) {
571 ret = 0;
572 break;
573 }
574 }
575 if (ret) {
128e4caf
JA
576 log_err("fio: value %llu not allowed:\n", ull);
577 show_option_values(o);
d926d535
JA
578 return 1;
579 }
580 }
e1f36503
JA
581
582 if (fn)
583 ret = fn(data, &ull);
584 else {
e01b22b8 585 if (o->type == FIO_OPT_INT) {
7b504edd
JA
586 if (first)
587 val_store(ilp, ull, o->off1, 0, data, o);
6eaf09d6 588 if (curr == 1) {
7b504edd
JA
589 if (o->off2)
590 val_store(ilp, ull, o->off2, 0, data, o);
02c6aad5 591 }
6eaf09d6 592 if (curr == 2) {
7b504edd
JA
593 if (o->off3)
594 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
595 }
596 if (!more) {
597 if (curr < 1) {
7b504edd
JA
598 if (o->off2)
599 val_store(ilp, ull, o->off2, 0, data, o);
6eaf09d6
SL
600 }
601 if (curr < 2) {
7b504edd
JA
602 if (o->off3)
603 val_store(ilp, ull, o->off3, 0, data, o);
6eaf09d6
SL
604 }
605 }
75e6f36f 606 } else {
7b504edd
JA
607 if (first)
608 val_store(ullp, ull, o->off1, 0, data, o);
02c6aad5 609 if (!more) {
7b504edd
JA
610 if (o->off2)
611 val_store(ullp, ull, o->off2, 0, data, o);
02c6aad5 612 }
75e6f36f 613 }
e1f36503
JA
614 }
615 break;
616 }
83349190 617 case FIO_OPT_FLOAT_LIST: {
eef02441
JA
618 char *cp2;
619
435d195a
VKF
620 if (first) {
621 /*
622 ** Initialize precision to 0 and zero out list
623 ** in case specified list is shorter than default
624 */
3e260a46
JA
625 if (o->off2) {
626 ul2 = 0;
f0fdbcaf 627 ilp = td_var(data, o, o->off2);
3e260a46
JA
628 *ilp = ul2;
629 }
435d195a 630
f0fdbcaf 631 flp = td_var(data, o, o->off1);
435d195a
VKF
632 for(i = 0; i < o->maxlen; i++)
633 flp[i].u.f = 0.0;
634 }
83349190 635 if (curr >= o->maxlen) {
28d7c363 636 log_err("the list exceeding max length %d\n",
83349190
YH
637 o->maxlen);
638 return 1;
639 }
88038bc7 640 if (!str_to_float(ptr, &uf, 0)) { /* this breaks if we ever have lists of times */
28d7c363 641 log_err("not a floating point value: %s\n", ptr);
83349190
YH
642 return 1;
643 }
26650695 644 if (uf > o->maxfp) {
28d7c363 645 log_err("value out of range: %f"
83349190
YH
646 " (range max: %f)\n", uf, o->maxfp);
647 return 1;
648 }
26650695 649 if (uf < o->minfp) {
28d7c363 650 log_err("value out of range: %f"
83349190
YH
651 " (range min: %f)\n", uf, o->minfp);
652 return 1;
653 }
654
f0fdbcaf 655 flp = td_var(data, o, o->off1);
fd112d34 656 flp[curr].u.f = uf;
83349190 657
ae3fcb5a
JA
658 dprint(FD_PARSE, " out=%f\n", uf);
659
435d195a
VKF
660 /*
661 ** Calculate precision for output by counting
662 ** number of digits after period. Find first
663 ** period in entire remaining list each time
664 */
665 cp2 = strchr(ptr, '.');
666 if (cp2 != NULL) {
eef02441 667 int len = 0;
435d195a
VKF
668
669 while (*++cp2 != '\0' && *cp2 >= '0' && *cp2 <= '9')
670 len++;
671
3e260a46 672 if (o->off2) {
f0fdbcaf 673 ilp = td_var(data, o, o->off2);
3e260a46
JA
674 if (len > *ilp)
675 *ilp = len;
676 }
435d195a
VKF
677 }
678
83349190
YH
679 break;
680 }
af52b345
JA
681 case FIO_OPT_STR_STORE: {
682 fio_opt_str_fn *fn = o->cb;
683
f75c69a1
JA
684 if (!strlen(ptr))
685 return 1;
686
7b504edd 687 if (o->off1) {
f0fdbcaf 688 cp = td_var(data, o, o->off1);
184b4098 689 *cp = strdup(ptr);
1c964ce5 690 }
02c6aad5 691
184b4098
SL
692 if (fn)
693 ret = fn(data, ptr);
694 else if (o->posval[0].ival) {
695 posval_sort(o, posval);
696
697 ret = 1;
698 for (i = 0; i < PARSE_MAX_VP; i++) {
699 vp = &posval[i];
ab50817f 700 if (!vp->ival || vp->ival[0] == '\0' || !cp)
184b4098
SL
701 continue;
702 all_skipped = 0;
119cd939 703 if (!strncmp(vp->ival, ptr, str_match_len(vp, ptr))) {
184b4098
SL
704 char *rest;
705
706 ret = 0;
707 if (vp->cb)
708 fn = vp->cb;
709 rest = strstr(*cp ?: ptr, ":");
710 if (rest) {
711 if (*cp)
712 *rest = '\0';
713 ptr = rest + 1;
714 } else
715 ptr = NULL;
716 break;
717 }
af52b345
JA
718 }
719 }
c44b1ff5 720
184b4098
SL
721 if (!all_skipped) {
722 if (ret && !*cp)
723 show_option_values(o);
724 else if (ret && *cp)
725 ret = 0;
726 else if (fn && ptr)
727 ret = fn(data, ptr);
728 }
c44b1ff5 729
e1f36503 730 break;
af52b345 731 }
e1f36503 732 case FIO_OPT_RANGE: {
b765a372 733 char tmp[128];
e1f36503
JA
734 char *p1, *p2;
735
209e1037 736 tmp[sizeof(tmp) - 1] = '\0';
0bbab0e7 737 strncpy(tmp, ptr, sizeof(tmp) - 1);
b765a372 738
31d23f47
DE
739 /* Handle bsrange with separate read,write values: */
740 p1 = strchr(tmp, ',');
741 if (p1)
742 *p1 = '\0';
743
b765a372 744 p1 = strchr(tmp, '-');
e1f36503 745 if (!p1) {
0c9baf91
JA
746 p1 = strchr(tmp, ':');
747 if (!p1) {
748 ret = 1;
749 break;
750 }
e1f36503
JA
751 }
752
753 p2 = p1 + 1;
754 *p1 = '\0';
b765a372 755 p1 = tmp;
e1f36503
JA
756
757 ret = 1;
d6978a32
JA
758 if (!check_range_bytes(p1, &ul1, data) &&
759 !check_range_bytes(p2, &ul2, data)) {
e1f36503 760 ret = 0;
e1f36503 761 if (ul1 > ul2) {
f90eff5a
JA
762 unsigned long foo = ul1;
763
764 ul1 = ul2;
765 ul2 = foo;
766 }
767
768 if (first) {
7b504edd
JA
769 val_store(ilp, ul1, o->off1, 0, data, o);
770 val_store(ilp, ul2, o->off2, 0, data, o);
17abbe89 771 }
6eaf09d6 772 if (curr == 1) {
7b504edd
JA
773 if (o->off3 && o->off4) {
774 val_store(ilp, ul1, o->off3, 0, data, o);
775 val_store(ilp, ul2, o->off4, 0, data, o);
6eaf09d6
SL
776 }
777 }
778 if (curr == 2) {
7b504edd
JA
779 if (o->off5 && o->off6) {
780 val_store(ilp, ul1, o->off5, 0, data, o);
781 val_store(ilp, ul2, o->off6, 0, data, o);
6eaf09d6
SL
782 }
783 }
784 if (!more) {
785 if (curr < 1) {
7b504edd
JA
786 if (o->off3 && o->off4) {
787 val_store(ilp, ul1, o->off3, 0, data, o);
788 val_store(ilp, ul2, o->off4, 0, data, o);
6eaf09d6
SL
789 }
790 }
791 if (curr < 2) {
7b504edd
JA
792 if (o->off5 && o->off6) {
793 val_store(ilp, ul1, o->off5, 0, data, o);
794 val_store(ilp, ul2, o->off6, 0, data, o);
6eaf09d6
SL
795 }
796 }
e1f36503 797 }
17abbe89
JA
798 }
799
e1f36503
JA
800 break;
801 }
74ba1808
JA
802 case FIO_OPT_BOOL:
803 case FIO_OPT_STR_SET: {
e1f36503
JA
804 fio_opt_int_fn *fn = o->cb;
805
74ba1808
JA
806 if (ptr)
807 ret = check_int(ptr, &il);
808 else if (o->type == FIO_OPT_BOOL)
809 ret = 1;
810 else
811 il = 1;
812
ae3fcb5a
JA
813 dprint(FD_PARSE, " ret=%d, out=%d\n", ret, il);
814
e1f36503
JA
815 if (ret)
816 break;
817
db8e0165 818 if (o->maxval && il > (int) o->maxval) {
28d7c363 819 log_err("max value out of range: %d (%d max)\n",
5ec10eaa 820 il, o->maxval);
db8e0165
JA
821 return 1;
822 }
823 if (o->minval && il < o->minval) {
28d7c363 824 log_err("min value out of range: %d (%d min)\n",
5ec10eaa 825 il, o->minval);
db8e0165
JA
826 return 1;
827 }
e1f36503 828
76a43db4
JA
829 if (o->neg)
830 il = !il;
831
e1f36503
JA
832 if (fn)
833 ret = fn(data, &il);
834 else {
7b504edd
JA
835 if (first)
836 val_store(ilp, il, o->off1, 0, data, o);
02c6aad5 837 if (!more) {
7b504edd
JA
838 if (o->off2)
839 val_store(ilp, il, o->off2, 0, data, o);
02c6aad5 840 }
e1f36503
JA
841 }
842 break;
843 }
15ca150e 844 case FIO_OPT_DEPRECATED:
06b0be6e 845 log_info("Option %s is deprecated\n", o->name);
d9472271 846 ret = 1;
15ca150e 847 break;
e1f36503 848 default:
06b0be6e 849 log_err("Bad option type %u\n", o->type);
e1f36503
JA
850 ret = 1;
851 }
cb2c86fd 852
70a4c0c8
JA
853 if (ret)
854 return ret;
855
d447a8c2 856 if (o->verify) {
70a4c0c8 857 ret = o->verify(o, data);
d447a8c2 858 if (ret) {
28d7c363
JA
859 log_err("Correct format for offending option\n");
860 log_err("%20s: %s\n", o->name, o->help);
06b0be6e 861 show_option_help(o, 1);
d447a8c2
JA
862 }
863 }
70a4c0c8 864
e1f36503 865 return ret;
cb2c86fd
JA
866}
867
7c8f1a5c 868static int handle_option(struct fio_option *o, const char *__ptr, void *data)
f90eff5a 869{
b62bdf2c
JA
870 char *o_ptr, *ptr, *ptr2;
871 int ret, done;
f90eff5a 872
7c8f1a5c
JA
873 dprint(FD_PARSE, "handle_option=%s, ptr=%s\n", o->name, __ptr);
874
b62bdf2c 875 o_ptr = ptr = NULL;
7c8f1a5c 876 if (__ptr)
b62bdf2c 877 o_ptr = ptr = strdup(__ptr);
a3d741fa 878
f90eff5a 879 /*
b62bdf2c
JA
880 * See if we have another set of parameters, hidden after a comma.
881 * Do this before parsing this round, to check if we should
787f7e95 882 * copy set 1 options to set 2.
f90eff5a 883 */
b62bdf2c
JA
884 done = 0;
885 ret = 1;
886 do {
887 int __ret;
888
889 ptr2 = NULL;
890 if (ptr &&
891 (o->type != FIO_OPT_STR_STORE) &&
83349190
YH
892 (o->type != FIO_OPT_STR) &&
893 (o->type != FIO_OPT_FLOAT_LIST)) {
b62bdf2c
JA
894 ptr2 = strchr(ptr, ',');
895 if (ptr2 && *(ptr2 + 1) == '\0')
896 *ptr2 = '\0';
6eaf09d6 897 if (o->type != FIO_OPT_STR_MULTI && o->type != FIO_OPT_RANGE) {
b62bdf2c
JA
898 if (!ptr2)
899 ptr2 = strchr(ptr, ':');
900 if (!ptr2)
901 ptr2 = strchr(ptr, '-');
902 }
83349190
YH
903 } else if (ptr && o->type == FIO_OPT_FLOAT_LIST) {
904 ptr2 = strchr(ptr, ':');
b62bdf2c 905 }
787f7e95 906
b62bdf2c
JA
907 /*
908 * Don't return early if parsing the first option fails - if
909 * we are doing multiple arguments, we can allow the first one
910 * being empty.
911 */
83349190 912 __ret = __handle_option(o, ptr, data, !done, !!ptr2, done);
b62bdf2c
JA
913 if (ret)
914 ret = __ret;
787f7e95 915
b62bdf2c
JA
916 if (!ptr2)
917 break;
f90eff5a 918
b62bdf2c
JA
919 ptr = ptr2 + 1;
920 done++;
921 } while (1);
787f7e95 922
b62bdf2c
JA
923 if (o_ptr)
924 free(o_ptr);
925 return ret;
f90eff5a
JA
926}
927
75e6bcba
JA
928struct fio_option *find_option(struct fio_option *options, const char *opt)
929{
930 struct fio_option *o;
931
932 for (o = &options[0]; o->name; o++) {
933 if (!o_match(o, opt))
934 continue;
935 if (o->type == FIO_OPT_UNSUPPORTED) {
936 log_err("Option <%s>: %s\n", o->name, o->help);
937 continue;
938 }
939
940 return o;
941 }
942
943 return NULL;
944}
945
946
de890a1e 947static struct fio_option *get_option(char *opt,
07b3232d 948 struct fio_option *options, char **post)
3b8b7135
JA
949{
950 struct fio_option *o;
951 char *ret;
952
953 ret = strchr(opt, '=');
954 if (ret) {
955 *post = ret;
956 *ret = '\0';
de890a1e 957 ret = opt;
3b8b7135 958 (*post)++;
43c129b4 959 strip_blank_end(ret);
07b3232d 960 o = find_option(options, ret);
3b8b7135 961 } else {
07b3232d 962 o = find_option(options, opt);
3b8b7135
JA
963 *post = NULL;
964 }
965
966 return o;
967}
968
969static int opt_cmp(const void *p1, const void *p2)
970{
de890a1e
SL
971 struct fio_option *o;
972 char *s, *foo;
8cdabc1d 973 int prio1, prio2;
3b8b7135 974
8cdabc1d 975 prio1 = prio2 = 0;
3b8b7135 976
de890a1e
SL
977 if (*(char **)p1) {
978 s = strdup(*((char **) p1));
9af4a244 979 o = get_option(s, __fio_options, &foo);
de890a1e
SL
980 if (o)
981 prio1 = o->prio;
982 free(s);
983 }
984 if (*(char **)p2) {
985 s = strdup(*((char **) p2));
9af4a244 986 o = get_option(s, __fio_options, &foo);
de890a1e
SL
987 if (o)
988 prio2 = o->prio;
989 free(s);
990 }
991
8cdabc1d 992 return prio2 - prio1;
3b8b7135
JA
993}
994
995void sort_options(char **opts, struct fio_option *options, int num_opts)
996{
9af4a244 997 __fio_options = options;
3b8b7135 998 qsort(opts, num_opts, sizeof(char *), opt_cmp);
9af4a244 999 __fio_options = NULL;
3b8b7135
JA
1000}
1001
d8b4f395
JA
1002static void add_to_dump_list(struct fio_option *o, struct flist_head *dump_list,
1003 const char *post)
1004{
1005 struct print_option *p;
1006
1007 if (!dump_list)
1008 return;
1009
1010 p = malloc(sizeof(*p));
1011 p->name = strdup(o->name);
1012 if (post)
1013 p->value = strdup(post);
1014 else
1015 p->value = NULL;
1016
1017 flist_add_tail(&p->list, dump_list);
1018}
1019
b4692828 1020int parse_cmd_option(const char *opt, const char *val,
d8b4f395
JA
1021 struct fio_option *options, void *data,
1022 struct flist_head *dump_list)
b4692828
JA
1023{
1024 struct fio_option *o;
1025
07b3232d 1026 o = find_option(options, opt);
b4692828 1027 if (!o) {
06b0be6e 1028 log_err("Bad option <%s>\n", opt);
b4692828
JA
1029 return 1;
1030 }
1031
d8b4f395
JA
1032 if (handle_option(o, val, data)) {
1033 log_err("fio: failed parsing %s=%s\n", opt, val);
1034 return 1;
1035 }
b1508cf9 1036
d8b4f395
JA
1037 add_to_dump_list(o, dump_list, val);
1038 return 0;
b4692828
JA
1039}
1040
de890a1e 1041int parse_option(char *opt, const char *input,
292cc475 1042 struct fio_option *options, struct fio_option **o, void *data,
c2292325 1043 struct flist_head *dump_list)
cb2c86fd 1044{
d0c814ec 1045 char *post;
e1f36503 1046
d0c814ec
SL
1047 if (!opt) {
1048 log_err("fio: failed parsing %s\n", input);
de890a1e 1049 *o = NULL;
38789b58 1050 return 1;
d0c814ec 1051 }
e1f36503 1052
de890a1e
SL
1053 *o = get_option(opt, options, &post);
1054 if (!*o) {
1055 if (post) {
1056 int len = strlen(opt);
1057 if (opt + len + 1 != post)
1058 memmove(opt + len + 1, post, strlen(post));
1059 opt[len] = '=';
1060 }
e1f36503
JA
1061 return 1;
1062 }
1063
292cc475
JA
1064 if (handle_option(*o, post, data)) {
1065 log_err("fio: failed parsing %s\n", input);
1066 return 1;
1067 }
b1508cf9 1068
d8b4f395 1069 add_to_dump_list(*o, dump_list, post);
292cc475 1070 return 0;
e1f36503 1071}
fd28ca49 1072
0e9f7fac
JA
1073/*
1074 * Option match, levenshtein distance. Handy for not quite remembering what
1075 * the option name is.
1076 */
a893c261 1077int string_distance(const char *s1, const char *s2)
0e9f7fac
JA
1078{
1079 unsigned int s1_len = strlen(s1);
1080 unsigned int s2_len = strlen(s2);
1081 unsigned int *p, *q, *r;
1082 unsigned int i, j;
1083
1084 p = malloc(sizeof(unsigned int) * (s2_len + 1));
1085 q = malloc(sizeof(unsigned int) * (s2_len + 1));
1086
1087 p[0] = 0;
1088 for (i = 1; i <= s2_len; i++)
1089 p[i] = p[i - 1] + 1;
1090
1091 for (i = 1; i <= s1_len; i++) {
1092 q[0] = p[0] + 1;
1093 for (j = 1; j <= s2_len; j++) {
1094 unsigned int sub = p[j - 1];
8a1db9a1 1095 unsigned int pmin;
0e9f7fac
JA
1096
1097 if (s1[i - 1] != s2[j - 1])
1098 sub++;
1099
8a1db9a1
JA
1100 pmin = min(q[j - 1] + 1, sub);
1101 q[j] = min(p[j] + 1, pmin);
0e9f7fac
JA
1102 }
1103 r = p;
1104 p = q;
1105 q = r;
1106 }
1107
1108 i = p[s2_len];
1109 free(p);
1110 free(q);
1111 return i;
1112}
1113
3701636d
JA
1114/*
1115 * Make a guess of whether the distance from 's1' is significant enough
1116 * to warrant printing the guess. We set this to a 1/2 match.
1117 */
1118int string_distance_ok(const char *opt, int distance)
1119{
1120 size_t len;
1121
1122 len = strlen(opt);
1123 len = (len + 1) / 2;
1124 return distance <= len;
1125}
1126
afdf9352
JA
1127static struct fio_option *find_child(struct fio_option *options,
1128 struct fio_option *o)
1129{
1130 struct fio_option *__o;
1131
fdf28744
JA
1132 for (__o = options + 1; __o->name; __o++)
1133 if (__o->parent && !strcmp(__o->parent, o->name))
afdf9352
JA
1134 return __o;
1135
1136 return NULL;
1137}
1138
323d9113
JA
1139static void __print_option(struct fio_option *o, struct fio_option *org,
1140 int level)
afdf9352
JA
1141{
1142 char name[256], *p;
323d9113 1143 int depth;
afdf9352
JA
1144
1145 if (!o)
1146 return;
ef9aff52
JA
1147 if (!org)
1148 org = o;
5ec10eaa 1149
afdf9352 1150 p = name;
323d9113
JA
1151 depth = level;
1152 while (depth--)
1153 p += sprintf(p, "%s", " ");
afdf9352
JA
1154
1155 sprintf(p, "%s", o->name);
1156
28d7c363 1157 log_info("%-24s: %s\n", name, o->help);
323d9113
JA
1158}
1159
1160static void print_option(struct fio_option *o)
1161{
1162 struct fio_option *parent;
1163 struct fio_option *__o;
1164 unsigned int printed;
1165 unsigned int level;
1166
1167 __print_option(o, NULL, 0);
1168 parent = o;
1169 level = 0;
1170 do {
1171 level++;
1172 printed = 0;
1173
1174 while ((__o = find_child(o, parent)) != NULL) {
1175 __print_option(__o, o, level);
1176 o = __o;
1177 printed++;
1178 }
1179
1180 parent = o;
1181 } while (printed);
afdf9352
JA
1182}
1183
07b3232d 1184int show_cmd_help(struct fio_option *options, const char *name)
0e9f7fac
JA
1185{
1186 struct fio_option *o, *closest;
d091d099 1187 unsigned int best_dist = -1U;
29fc6afe 1188 int found = 0;
320beefe
JA
1189 int show_all = 0;
1190
1191 if (!name || !strcmp(name, "all"))
1192 show_all = 1;
fd28ca49 1193
0e9f7fac
JA
1194 closest = NULL;
1195 best_dist = -1;
4945ba12 1196 for (o = &options[0]; o->name; o++) {
320beefe
JA
1197 int match = 0;
1198
15ca150e
JA
1199 if (o->type == FIO_OPT_DEPRECATED)
1200 continue;
07b3232d
JA
1201 if (!exec_profile && o->prof_name)
1202 continue;
4ac23d27
JA
1203 if (exec_profile && !(o->prof_name && !strcmp(exec_profile, o->prof_name)))
1204 continue;
15ca150e 1205
0e9f7fac 1206 if (name) {
7f9348f8
JA
1207 if (!strcmp(name, o->name) ||
1208 (o->alias && !strcmp(name, o->alias)))
0e9f7fac
JA
1209 match = 1;
1210 else {
1211 unsigned int dist;
1212
1213 dist = string_distance(name, o->name);
1214 if (dist < best_dist) {
1215 best_dist = dist;
1216 closest = o;
1217 }
1218 }
1219 }
fd28ca49
JA
1220
1221 if (show_all || match) {
29fc6afe 1222 found = 1;
c167dedc 1223 if (match)
28d7c363 1224 log_info("%20s: %s\n", o->name, o->help);
c167dedc 1225 if (show_all) {
afdf9352 1226 if (!o->parent)
323d9113 1227 print_option(o);
4945ba12 1228 continue;
c167dedc 1229 }
fd28ca49
JA
1230 }
1231
70df2f19
JA
1232 if (!match)
1233 continue;
1234
06b0be6e 1235 show_option_help(o, 0);
fd28ca49
JA
1236 }
1237
29fc6afe
JA
1238 if (found)
1239 return 0;
fd28ca49 1240
28d7c363 1241 log_err("No such command: %s", name);
d091d099
JA
1242
1243 /*
1244 * Only print an appropriately close option, one where the edit
1245 * distance isn't too big. Otherwise we get crazy matches.
1246 */
1247 if (closest && best_dist < 3) {
28d7c363
JA
1248 log_info(" - showing closest match\n");
1249 log_info("%20s: %s\n", closest->name, closest->help);
06b0be6e 1250 show_option_help(closest, 0);
0e9f7fac 1251 } else
28d7c363 1252 log_info("\n");
0e9f7fac 1253
29fc6afe 1254 return 1;
fd28ca49 1255}
ee738499 1256
13335ddb
JA
1257/*
1258 * Handle parsing of default parameters.
1259 */
ee738499
JA
1260void fill_default_options(void *data, struct fio_option *options)
1261{
4945ba12 1262 struct fio_option *o;
ee738499 1263
a3d741fa
JA
1264 dprint(FD_PARSE, "filling default options\n");
1265
4945ba12 1266 for (o = &options[0]; o->name; o++)
ee738499
JA
1267 if (o->def)
1268 handle_option(o, o->def, data);
ee738499 1269}
13335ddb 1270
8aa89d70 1271static void option_init(struct fio_option *o)
9f988e2e 1272{
75e6bcba 1273 if (o->type == FIO_OPT_DEPRECATED || o->type == FIO_OPT_UNSUPPORTED)
9f988e2e 1274 return;
bbcacb72
JA
1275 if (o->name && !o->lname)
1276 log_err("Option %s: missing long option name\n", o->name);
9f988e2e
JA
1277 if (o->type == FIO_OPT_BOOL) {
1278 o->minval = 0;
1279 o->maxval = 1;
1280 }
d4fc2f04
JA
1281 if (o->type == FIO_OPT_INT) {
1282 if (!o->maxval)
1283 o->maxval = UINT_MAX;
1284 }
83349190 1285 if (o->type == FIO_OPT_FLOAT_LIST) {
ab9461ea
BC
1286 o->minfp = DBL_MIN;
1287 o->maxfp = DBL_MAX;
83349190 1288 }
c8931876 1289 if (o->type == FIO_OPT_STR_SET && o->def && !o->no_warn_def) {
06b0be6e 1290 log_err("Option %s: string set option with"
9f988e2e
JA
1291 " default will always be true\n", o->name);
1292 }
7b504edd 1293 if (!o->cb && !o->off1)
06b0be6e 1294 log_err("Option %s: neither cb nor offset given\n", o->name);
22754746 1295 if (!o->category) {
cca5b5bc 1296 log_info("Option %s: no category defined. Setting to misc\n", o->name);
22754746 1297 o->category = FIO_OPT_C_GENERAL;
ffd7821f 1298 o->group = FIO_OPT_G_INVALID;
22754746 1299 }
5f6ddf1e
JA
1300 if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE ||
1301 o->type == FIO_OPT_STR_MULTI)
9f988e2e 1302 return;
9f988e2e
JA
1303}
1304
13335ddb
JA
1305/*
1306 * Sanitize the options structure. For now it just sets min/max for bool
5b0a8880 1307 * values and whether both callback and offsets are given.
13335ddb
JA
1308 */
1309void options_init(struct fio_option *options)
1310{
4945ba12 1311 struct fio_option *o;
13335ddb 1312
a3d741fa
JA
1313 dprint(FD_PARSE, "init options\n");
1314
90265353 1315 for (o = &options[0]; o->name; o++) {
9f988e2e 1316 option_init(o);
90265353
JA
1317 if (o->inverse)
1318 o->inv_opt = find_option(options, o->inverse);
1319 }
13335ddb 1320}
7e356b2d
JA
1321
1322void options_free(struct fio_option *options, void *data)
1323{
1324 struct fio_option *o;
1325 char **ptr;
1326
1327 dprint(FD_PARSE, "free options\n");
1328
1329 for (o = &options[0]; o->name; o++) {
de890a1e 1330 if (o->type != FIO_OPT_STR_STORE || !o->off1)
7e356b2d
JA
1331 continue;
1332
f0fdbcaf 1333 ptr = td_var(data, o, o->off1);
7e356b2d
JA
1334 if (*ptr) {
1335 free(*ptr);
1336 *ptr = NULL;
1337 }
1338 }
1339}