2019-05-09 12:06:58 +00:00
|
|
|
/*
|
|
|
|
KVMGFX Client - A KVM Client for VGA Passthrough
|
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
|
|
|
Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/option.h"
|
|
|
|
#include "common/debug.h"
|
2019-05-21 01:31:31 +00:00
|
|
|
#include "common/stringutils.h"
|
2019-05-09 12:06:58 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
2019-05-11 08:22:01 +00:00
|
|
|
#include <stdio.h>
|
2019-05-09 12:06:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
struct OptionGroup
|
|
|
|
{
|
|
|
|
const char * module;
|
|
|
|
struct Option ** options;
|
|
|
|
int count;
|
|
|
|
int pad;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct State
|
|
|
|
{
|
2019-05-11 01:50:26 +00:00
|
|
|
bool doHelp;
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option ** options;
|
2019-05-09 12:06:58 +00:00
|
|
|
int oCount;
|
|
|
|
struct OptionGroup * groups;
|
|
|
|
int gCount;
|
|
|
|
};
|
|
|
|
|
2019-05-11 09:07:10 +00:00
|
|
|
static struct State state =
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-11 01:50:26 +00:00
|
|
|
.doHelp = false,
|
2019-05-09 12:06:58 +00:00
|
|
|
.options = NULL,
|
|
|
|
.oCount = 0,
|
|
|
|
.groups = NULL,
|
|
|
|
.gCount = 0
|
|
|
|
};
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static bool int_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
opt->value.x_int = atol(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool bool_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
opt->value.x_bool =
|
|
|
|
strcmp(str, "1" ) == 0 ||
|
|
|
|
strcmp(str, "on" ) == 0 ||
|
|
|
|
strcmp(str, "yes" ) == 0 ||
|
|
|
|
strcmp(str, "true") == 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool string_parser(struct Option * opt, const char * str)
|
|
|
|
{
|
|
|
|
free(opt->value.x_string);
|
|
|
|
opt->value.x_string = strdup(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * int_toString(struct Option * opt)
|
|
|
|
{
|
|
|
|
int len = snprintf(NULL, 0, "%d", opt->value.x_int);
|
|
|
|
char * ret = malloc(len + 1);
|
|
|
|
sprintf(ret, "%d", opt->value.x_int);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * bool_toString(struct Option * opt)
|
|
|
|
{
|
|
|
|
return strdup(opt->value.x_bool ? "yes" : "no");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * string_toString(struct Option * opt)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!opt->value.x_string)
|
|
|
|
return NULL;
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
return strdup(opt->value.x_string);
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
bool option_register(struct Option options[])
|
|
|
|
{
|
|
|
|
int new = 0;
|
2019-05-11 10:58:49 +00:00
|
|
|
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
|
2019-05-09 12:06:58 +00:00
|
|
|
++new;
|
|
|
|
|
|
|
|
state.options = realloc(
|
|
|
|
state.options,
|
2019-05-16 23:25:57 +00:00
|
|
|
sizeof(struct Option *) * (state.oCount + new)
|
2019-05-09 12:06:58 +00:00
|
|
|
);
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
for(int i = 0; options[i].type != OPTION_TYPE_NONE; ++i)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
state.options[state.oCount + i] = (struct Option *)malloc(sizeof(struct Option));
|
|
|
|
struct Option * o = state.options[state.oCount + i];
|
2019-05-09 12:06:58 +00:00
|
|
|
memcpy(o, &options[i], sizeof(struct Option));
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
if (!o->parser)
|
|
|
|
{
|
|
|
|
switch(o->type)
|
|
|
|
{
|
|
|
|
case OPTION_TYPE_INT:
|
|
|
|
o->parser = int_parser;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_STRING:
|
|
|
|
o->parser = string_parser;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_BOOL:
|
|
|
|
o->parser = bool_parser;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("BUG: Non int/string/bool option types must have a parser");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!o->toString)
|
|
|
|
{
|
|
|
|
switch(o->type)
|
|
|
|
{
|
|
|
|
case OPTION_TYPE_INT:
|
|
|
|
o->toString = int_toString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_STRING:
|
|
|
|
o->toString = string_toString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPTION_TYPE_BOOL:
|
|
|
|
o->toString = bool_toString;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_ERROR("BUG: Non int/string/bool option types must implement toString");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
// ensure the string is locally allocated
|
2019-05-11 10:58:49 +00:00
|
|
|
if (o->type == OPTION_TYPE_STRING)
|
2019-05-21 01:31:31 +00:00
|
|
|
{
|
|
|
|
if (o->value.x_string)
|
|
|
|
o->value.x_string = strdup(o->value.x_string);
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
|
|
|
// add the option to the correct group for help printout
|
|
|
|
bool found = false;
|
|
|
|
for(int g = 0; g < state.gCount; ++g)
|
|
|
|
{
|
|
|
|
struct OptionGroup * group = &state.groups[g];
|
|
|
|
if (strcmp(group->module, o->module) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
group->options = realloc(
|
|
|
|
group->options,
|
|
|
|
sizeof(struct Option *) * (group->count + 1)
|
|
|
|
);
|
|
|
|
group->options[group->count] = o;
|
|
|
|
|
|
|
|
int len = strlen(o->name);
|
|
|
|
if (len > group->pad)
|
|
|
|
group->pad = len;
|
|
|
|
|
|
|
|
++group->count;
|
2019-05-16 23:25:57 +00:00
|
|
|
break;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
state.groups = realloc(
|
|
|
|
state.groups,
|
|
|
|
sizeof(struct OptionGroup) * (state.gCount + 1)
|
|
|
|
);
|
|
|
|
|
|
|
|
struct OptionGroup * group = &state.groups[state.gCount];
|
|
|
|
++state.gCount;
|
|
|
|
|
|
|
|
group->module = o->module;
|
|
|
|
group->options = malloc(sizeof(struct Option *));
|
|
|
|
group->options[0] = o;
|
|
|
|
group->count = 1;
|
|
|
|
group->pad = strlen(o->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.oCount += new;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
void option_free()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2019-05-11 10:58:49 +00:00
|
|
|
if (o->type == OPTION_TYPE_STRING)
|
|
|
|
free(o->value.x_string);
|
2019-05-16 23:25:57 +00:00
|
|
|
free(o);
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
free(state.options);
|
|
|
|
state.options = NULL;
|
|
|
|
state.oCount = 0;
|
|
|
|
|
|
|
|
free(state.groups);
|
|
|
|
state.groups = NULL;
|
|
|
|
state.gCount = 0;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
static bool option_set(struct Option * opt, const char * value)
|
2019-05-11 08:22:01 +00:00
|
|
|
{
|
2019-05-12 06:51:37 +00:00
|
|
|
if (!opt->parser(opt, value))
|
|
|
|
{
|
|
|
|
opt->failed_set = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt->failed_set = false;
|
|
|
|
return true;
|
2019-05-11 08:22:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
bool option_parse(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
for(int a = 1; a < argc; ++a)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
struct Option * o = NULL;
|
|
|
|
char * value = NULL;
|
|
|
|
|
|
|
|
// emulate getopt for backwards compatability
|
|
|
|
if (argv[a][0] == '-')
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
if (strcmp(argv[a], "-h") == 0 || strcmp(argv[a], "--help") == 0)
|
|
|
|
{
|
|
|
|
state.doHelp = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
if (strlen(argv[a]) != 2)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored invalid argvument: %s", argv[a]);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
|
|
|
if (state.options[i]->shortopt == argv[a][1])
|
|
|
|
{
|
|
|
|
o = state.options[i];
|
2019-05-21 01:34:50 +00:00
|
|
|
if (o->type != OPTION_TYPE_BOOL && a < argc - 1)
|
|
|
|
{
|
|
|
|
++a;
|
|
|
|
value = strdup(argv[a]);
|
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
char * arg = strdup(argv[a]);
|
|
|
|
char * module = strtok(arg , ":");
|
|
|
|
char * name = strtok(NULL, "=");
|
|
|
|
value = strtok(NULL, "" );
|
|
|
|
|
|
|
|
if (!module || !name)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored invalid argument: %s", argv[a]);
|
|
|
|
free(arg);
|
2019-05-09 12:06:58 +00:00
|
|
|
continue;
|
2019-05-21 01:31:31 +00:00
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
o = option_get(module, name);
|
|
|
|
if (value)
|
|
|
|
value = strdup(value);
|
|
|
|
|
|
|
|
free(arg);
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!value)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
if (o->type == OPTION_TYPE_BOOL)
|
|
|
|
{
|
2019-05-21 02:58:53 +00:00
|
|
|
o->value.x_bool = !o->value.x_bool;
|
2019-05-21 01:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (o->type != OPTION_TYPE_CUSTOM)
|
|
|
|
{
|
|
|
|
DEBUG_WARN("Ignored invalid argument, missing value: %s", argv[a]);
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!o)
|
2019-05-11 08:22:01 +00:00
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
DEBUG_WARN("Ignored unknown argument: %s", argv[a]);
|
|
|
|
free(value);
|
2019-05-11 08:22:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-21 01:31:31 +00:00
|
|
|
option_set(o, value);
|
|
|
|
free(value);
|
2019-05-11 08:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char * file_parse_module(FILE * fp)
|
|
|
|
{
|
|
|
|
char * module = NULL;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for(int c = fgetc(fp); !feof(fp); c = fgetc(fp))
|
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case ']':
|
|
|
|
if (module)
|
|
|
|
module[len] = '\0';
|
|
|
|
return module;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
free(module);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (len % 32 == 0)
|
|
|
|
module = realloc(module, len + 32 + 1);
|
|
|
|
module[len++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (module)
|
|
|
|
free(module);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool option_load(const char * filename)
|
|
|
|
{
|
|
|
|
FILE * fp = fopen(filename, "r");
|
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool result = true;
|
|
|
|
int lineno = 1;
|
|
|
|
char * module = NULL;
|
|
|
|
bool line = true;
|
|
|
|
bool expectLine = false;
|
|
|
|
bool expectValue = false;
|
|
|
|
char * name = NULL;
|
|
|
|
int nameLen = 0;
|
|
|
|
char * value = NULL;
|
|
|
|
int valueLen = 0;
|
|
|
|
|
|
|
|
char ** p = &name;
|
|
|
|
int * len = &nameLen;
|
|
|
|
|
|
|
|
for(int c = fgetc(fp); !feof(fp); c = fgetc(fp))
|
|
|
|
{
|
|
|
|
switch(c)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
2019-05-11 08:22:01 +00:00
|
|
|
case '[':
|
|
|
|
if (expectLine)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected new line", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
free(module);
|
|
|
|
module = file_parse_module(fp);
|
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, failed to parse the module", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
line = false;
|
|
|
|
expectLine = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*len % 32 == 0)
|
|
|
|
*p = realloc(*p, *len + 32 + 1);
|
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
case '\r':
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
if (name)
|
|
|
|
{
|
2019-05-21 02:28:13 +00:00
|
|
|
if (!module)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, module not specified for option", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-11 08:22:01 +00:00
|
|
|
if (!o)
|
|
|
|
DEBUG_WARN("Ignored unknown option %s:%s", module, name);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!option_set(o, value))
|
|
|
|
DEBUG_ERROR("Failed to set the option value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
line = true;
|
|
|
|
expectLine = false;
|
|
|
|
expectValue = false;
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
p = &name;
|
|
|
|
len = &nameLen;
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
name = NULL;
|
|
|
|
nameLen = 0;
|
|
|
|
free(value);
|
|
|
|
value = NULL;
|
|
|
|
valueLen = 0;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
case '=':
|
|
|
|
if (!expectValue)
|
|
|
|
{
|
|
|
|
if (!name)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected option name", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-05-21 02:34:41 +00:00
|
|
|
//rtrim
|
|
|
|
while(nameLen > 1 && (name[nameLen-1] == ' ' || name[nameLen-1] == '\t'))
|
|
|
|
--nameLen;
|
2019-05-11 08:22:01 +00:00
|
|
|
name[nameLen] = '\0';
|
|
|
|
expectValue = true;
|
|
|
|
|
|
|
|
p = &value;
|
|
|
|
len = &valueLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*len % 32 == 0)
|
|
|
|
*p = realloc(*p, *len + 32 + 1);
|
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-05-11 08:22:01 +00:00
|
|
|
if (expectLine)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Syntax error on line %d, expected new line", lineno);
|
|
|
|
result = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
line = false;
|
|
|
|
|
2019-05-21 02:34:41 +00:00
|
|
|
//ltrim
|
|
|
|
if (*len == 0 && (c == ' ' || c == '\t'))
|
|
|
|
break;
|
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
if (*len % 32 == 0)
|
|
|
|
*p = realloc(*p, *len + 32 + 1);
|
|
|
|
(*p)[(*len)++] = c;
|
2019-05-09 12:06:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-11 01:21:00 +00:00
|
|
|
}
|
2019-05-09 13:05:33 +00:00
|
|
|
|
2019-05-11 08:22:01 +00:00
|
|
|
exit:
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
free(module);
|
|
|
|
free(name );
|
|
|
|
free(value );
|
|
|
|
|
|
|
|
return result;
|
2019-05-11 01:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool option_validate()
|
|
|
|
{
|
2019-05-11 01:50:26 +00:00
|
|
|
if (state.doHelp)
|
|
|
|
{
|
|
|
|
option_print();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-11 01:21:00 +00:00
|
|
|
// validate the option values
|
|
|
|
bool ok = true;
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2019-05-11 01:35:17 +00:00
|
|
|
const char * error = NULL;
|
2019-05-12 06:51:37 +00:00
|
|
|
bool invalid = o->failed_set;
|
2019-05-11 01:35:17 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (!invalid && o->validator)
|
|
|
|
invalid = !o->validator(o, &error);
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (invalid)
|
|
|
|
{
|
|
|
|
printf("\nInvalid value provided to the option: %s:%s\n", o->module, o->name);
|
2019-05-12 06:13:50 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (error)
|
|
|
|
printf("\n Error: %s\n", error);
|
|
|
|
|
|
|
|
if (o->getValues)
|
|
|
|
{
|
|
|
|
StringList values = o->getValues(o);
|
|
|
|
printf("\nValid values are:\n\n");
|
|
|
|
for(unsigned int v = 0; v < stringlist_count(values); ++v)
|
|
|
|
printf(" * %s\n", stringlist_at(values, v));
|
|
|
|
stringlist_free(&values);
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
|
2019-05-12 06:51:37 +00:00
|
|
|
if (o->printHelp)
|
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
o->printHelp();
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
2019-05-12 06:51:37 +00:00
|
|
|
|
|
|
|
ok = false;
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 01:35:17 +00:00
|
|
|
if (!ok)
|
|
|
|
printf("\n");
|
|
|
|
|
2019-05-11 01:21:00 +00:00
|
|
|
return ok;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void option_print()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"The following is a complete list of options accepted by this application\n\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
for(int g = 0; g < state.gCount; ++g)
|
|
|
|
{
|
2019-05-21 01:31:31 +00:00
|
|
|
StringList lines = stringlist_new(true);
|
|
|
|
StringList values = stringlist_new(true);
|
|
|
|
int len;
|
|
|
|
int maxLen;
|
|
|
|
int valueLen = 5;
|
|
|
|
char * line;
|
|
|
|
|
|
|
|
// ensure the pad length is atleast as wide as the heading
|
|
|
|
if (state.groups[g].pad < 4)
|
|
|
|
state.groups[g].pad = 4;
|
|
|
|
|
|
|
|
// get the values and the max value length
|
2019-05-09 12:06:58 +00:00
|
|
|
for(int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
{
|
|
|
|
struct Option * o = state.groups[g].options[i];
|
2019-05-11 10:58:49 +00:00
|
|
|
char * value = o->toString(o);
|
2019-05-21 01:31:31 +00:00
|
|
|
if (!value)
|
|
|
|
{
|
|
|
|
value = strdup("NULL");
|
|
|
|
len = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
len = strlen(value);
|
|
|
|
|
|
|
|
if (len > valueLen)
|
|
|
|
valueLen = len;
|
|
|
|
|
|
|
|
stringlist_push(values, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the heading
|
|
|
|
maxLen = alloc_sprintf(
|
|
|
|
&line,
|
|
|
|
"%-*s | Short | %-*s | Description",
|
2019-05-21 01:38:40 +00:00
|
|
|
strlen(state.groups[g].module) + state.groups[g].pad + 1,
|
2019-05-21 01:31:31 +00:00
|
|
|
"Long",
|
|
|
|
valueLen,
|
|
|
|
"Value"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(maxLen > 0);
|
|
|
|
stringlist_push(lines, line);
|
|
|
|
|
|
|
|
for(int i = 0; i < state.groups[g].count; ++i)
|
|
|
|
{
|
|
|
|
struct Option * o = state.groups[g].options[i];
|
|
|
|
char * value = stringlist_at(values, i);
|
|
|
|
|
|
|
|
len = alloc_sprintf(
|
|
|
|
&line,
|
|
|
|
"%s:%-*s | %c%c | %-*s | %s",
|
|
|
|
o->module,
|
|
|
|
state.groups[g].pad,
|
|
|
|
o->name,
|
|
|
|
o->shortopt ? '-' : ' ',
|
|
|
|
o->shortopt ? o->shortopt : ' ',
|
|
|
|
valueLen,
|
|
|
|
value,
|
|
|
|
o->description
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(len > 0);
|
|
|
|
stringlist_push(lines, line);
|
|
|
|
if (len > maxLen)
|
|
|
|
maxLen = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringlist_free(&values);
|
|
|
|
|
|
|
|
// print out the lines
|
|
|
|
for(int i = 0; i < stringlist_count(lines); ++i)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
// print a horizontal rule
|
|
|
|
printf(" |");
|
|
|
|
for(int i = 0; i < maxLen + 2; ++i)
|
|
|
|
putc('-', stdout);
|
|
|
|
printf("|\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
char * line = stringlist_at(lines, i);
|
|
|
|
printf(" | %-*s |\n", maxLen, line);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
// print a horizontal rule
|
|
|
|
printf(" |");
|
|
|
|
for(int i = 0; i < maxLen + 2; ++i)
|
|
|
|
putc('-', stdout);
|
|
|
|
printf("|\n");
|
|
|
|
}
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
2019-05-21 01:31:31 +00:00
|
|
|
|
|
|
|
// print a horizontal rule
|
|
|
|
printf(" |");
|
|
|
|
for(int i = 0; i < maxLen + 2; ++i)
|
|
|
|
putc('-', stdout);
|
|
|
|
printf("|\n");
|
|
|
|
|
|
|
|
stringlist_free(&lines);
|
|
|
|
|
2019-05-09 12:06:58 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * option_get(const char * module, const char * name)
|
2019-05-09 12:06:58 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < state.oCount; ++i)
|
|
|
|
{
|
2019-05-16 23:25:57 +00:00
|
|
|
struct Option * o = state.options[i];
|
2019-05-09 12:47:48 +00:00
|
|
|
if ((strcmp(o->module, module) == 0) && (strcmp(o->name, name) == 0))
|
2019-05-11 10:58:49 +00:00
|
|
|
return o;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int option_get_int(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
|
|
|
return -1;
|
|
|
|
assert(o->type == OPTION_TYPE_INT);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_int;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * option_get_string(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
|
|
|
return NULL;
|
|
|
|
assert(o->type == OPTION_TYPE_STRING);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_string;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool option_get_bool(const char * module, const char * name)
|
|
|
|
{
|
2019-05-11 10:58:49 +00:00
|
|
|
struct Option * o = option_get(module, name);
|
2019-05-09 12:06:58 +00:00
|
|
|
if (!o)
|
|
|
|
return false;
|
|
|
|
assert(o->type == OPTION_TYPE_BOOL);
|
2019-05-11 10:58:49 +00:00
|
|
|
return o->value.x_bool;
|
2019-05-09 12:06:58 +00:00
|
|
|
}
|