+
+/*
+ * Option match, levenshtein distance. Handy for not quite remembering what
+ * the option name is.
+ */
+static int string_distance(const char *s1, const char *s2)
+{
+ unsigned int s1_len = strlen(s1);
+ unsigned int s2_len = strlen(s2);
+ unsigned int *p, *q, *r;
+ unsigned int i, j;
+
+ p = malloc(sizeof(unsigned int) * (s2_len + 1));
+ q = malloc(sizeof(unsigned int) * (s2_len + 1));
+
+ p[0] = 0;
+ for (i = 1; i <= s2_len; i++)
+ p[i] = p[i - 1] + 1;
+
+ for (i = 1; i <= s1_len; i++) {
+ q[0] = p[0] + 1;
+ for (j = 1; j <= s2_len; j++) {
+ unsigned int sub = p[j - 1];
+
+ if (s1[i - 1] != s2[j - 1])
+ sub++;
+
+ q[j] = min(p[j] + 1, min(q[j - 1] + 1, sub));
+ }
+ r = p;
+ p = q;
+ q = r;
+ }
+
+ i = p[s2_len];
+ free(p);
+ free(q);
+ return i;
+}
+
+static void show_option_help(struct fio_option *o)
+{
+ const char *typehelp[] = {
+ "string (opt=bla)",
+ "string with possible k/m/g postfix (opt=4k)",
+ "string with range and postfix (opt=1k-4k)",
+ "string with time postfix (opt=10s)",
+ "string (opt=bla)",
+ "string with dual range (opt=1k-4k,4k-8k)",
+ "integer value (opt=100)",
+ "boolean value (opt=1)",
+ "no argument (opt)",
+ };
+
+ if (o->alias)
+ printf("%20s: %s\n", "alias", o->alias);
+
+ printf("%20s: %s\n", "type", typehelp[o->type]);
+ printf("%20s: %s\n", "default", o->def ? o->def : "no default");
+ show_option_range(o);
+ show_option_values(o);
+}
+
+static struct fio_option *find_child(struct fio_option *options,
+ struct fio_option *o)
+{
+ struct fio_option *__o;
+
+ for (__o = options + 1; __o->name; __o++)
+ if (__o->parent && !strcmp(__o->parent, o->name))
+ return __o;
+
+ return NULL;
+}
+
+static void __print_option(struct fio_option *o, struct fio_option *org,
+ int level)
+{
+ char name[256], *p;
+ int depth;
+
+ if (!o)
+ return;
+ if (!org)
+ org = o;
+
+ p = name;
+ depth = level;
+ while (depth--)
+ p += sprintf(p, "%s", " ");
+
+ sprintf(p, "%s", o->name);
+
+ printf("%-24s: %s\n", name, o->help);
+}
+
+static void print_option(struct fio_option *o)
+{
+ struct fio_option *parent;
+ struct fio_option *__o;
+ unsigned int printed;
+ unsigned int level;
+
+ __print_option(o, NULL, 0);
+ parent = o;
+ level = 0;
+ do {
+ level++;
+ printed = 0;
+
+ while ((__o = find_child(o, parent)) != NULL) {
+ __print_option(__o, o, level);
+ o = __o;
+ printed++;
+ }
+
+ parent = o;
+ } while (printed);
+}
+
+int show_cmd_help(struct fio_option *options, const char *name)
+{
+ struct fio_option *o, *closest;
+ unsigned int best_dist;
+ int found = 0;
+ int show_all = 0;
+
+ if (!name || !strcmp(name, "all"))
+ show_all = 1;
+
+ closest = NULL;
+ best_dist = -1;
+ for (o = &options[0]; o->name; o++) {
+ int match = 0;
+
+ if (o->type == FIO_OPT_DEPRECATED)
+ continue;
+
+ if (name) {
+ if (!strcmp(name, o->name) ||
+ (o->alias && !strcmp(name, o->alias)))
+ match = 1;
+ else {
+ unsigned int dist;
+
+ dist = string_distance(name, o->name);
+ if (dist < best_dist) {
+ best_dist = dist;
+ closest = o;
+ }
+ }
+ }
+
+ if (show_all || match) {
+ found = 1;
+ if (match)
+ printf("%24s: %s\n", o->name, o->help);
+ if (show_all) {
+ if (!o->parent)
+ print_option(o);
+ continue;
+ }
+ }
+
+ if (!match)
+ continue;
+
+ show_option_help(o);
+ }
+
+ if (found)
+ return 0;
+
+ printf("No such command: %s", name);
+ if (closest) {
+ printf(" - showing closest match\n");
+ printf("%20s: %s\n", closest->name, closest->help);
+ show_option_help(closest);
+ } else
+ printf("\n");
+
+ return 1;
+}
+
+/*
+ * Handle parsing of default parameters.
+ */
+void fill_default_options(void *data, struct fio_option *options)
+{
+ struct fio_option *o;
+
+ dprint(FD_PARSE, "filling default options\n");
+
+ for (o = &options[0]; o->name; o++)
+ if (o->def)
+ handle_option(o, o->def, data);
+}
+
+/*
+ * Sanitize the options structure. For now it just sets min/max for bool
+ * values and whether both callback and offsets are given.
+ */
+void options_init(struct fio_option *options)
+{
+ struct fio_option *o;
+
+ dprint(FD_PARSE, "init options\n");
+
+ for (o = &options[0]; o->name; o++) {
+ if (o->type == FIO_OPT_DEPRECATED)
+ continue;
+ if (o->type == FIO_OPT_BOOL) {
+ o->minval = 0;
+ o->maxval = 1;
+ }
+ if (o->type == FIO_OPT_STR_SET && o->def) {
+ fprintf(stderr, "Option %s: string set option with"
+ " default will always be true\n",
+ o->name);
+ }
+ if (!o->cb && !o->off1) {
+ fprintf(stderr, "Option %s: neither cb nor offset"
+ " given\n", o->name);
+ }
+ if (o->type == FIO_OPT_STR || o->type == FIO_OPT_STR_STORE)
+ continue;
+ if (o->cb && (o->off1 || o->off2 || o->off3 || o->off4)) {
+ fprintf(stderr, "Option %s: both cb and offset given\n",
+ o->name);
+ }
+ }
+}