Fix: Support multiple parameters in config file

This commit is contained in:
Andreas Eversberg 2020-09-19 11:23:26 +02:00
parent 8929b332cb
commit 51204b55a9
1 changed files with 22 additions and 11 deletions

View File

@ -62,16 +62,16 @@ void option_add(int short_option, const char *long_option, int parameter_count)
option_tailp = &(option->next); option_tailp = &(option->next);
} }
// FIXME: support more than one option */
int options_config_file(const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[])) int options_config_file(const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]))
{ {
static const char *home; static const char *home;
char config[256]; char config[256];
FILE *fp; FILE *fp;
char buffer[256], opt[256], param[256], *p, *argv[1]; char buffer[256], opt[256], param[256], *p, *argv[16];
char params[1024];
int line; int line;
int rc = 1; int rc = 1;
int i; int i, j;
option_t *option; option_t *option;
/* open config file */ /* open config file */
@ -113,18 +113,29 @@ int options_config_file(const char *config_file, int (*handle_options)(int short
while (*p > '\0' && *p <= ' ') while (*p > '\0' && *p <= ' ')
p++; p++;
/* get param from line */ /* get param from line */
params[0] = '\0';
i = 0; i = 0;
while (*p > ' ') while (*p) {
param[i++] = *p++; /* copy parameter */
param[i] = '\0'; j = 0;
while (*p > ' ')
param[j++] = *p++;
param[j] = '\0';
argv[i] = strdup(param);
sprintf(strchr(params, '\0'), " '%s'", param);
/* skip white spaces behind option */
while (*p > '\0' && *p <= ' ')
p++;
i++;
}
/* search option */ /* search option */
for (option = option_head; option; option = option->next) { for (option = option_head; option; option = option->next) {
if (opt[0] == option->short_option && opt[1] == '\0') { if (opt[0] == option->short_option && opt[1] == '\0') {
PDEBUG(DOPTIONS, DEBUG_INFO, "Config file option '%s' ('%s'), parameter '%s'\n", opt, option->long_option, param); PDEBUG(DOPTIONS, DEBUG_INFO, "Config file option '%s' ('%s'), parameter%s\n", opt, option->long_option, params);
break; break;
} }
if (!strcmp(opt, option->long_option)) { if (!strcmp(opt, option->long_option)) {
PDEBUG(DOPTIONS, DEBUG_INFO, "Config file option '%s', parameter '%s'\n", opt, param); PDEBUG(DOPTIONS, DEBUG_INFO, "Config file option '%s', parameter%s\n", opt, params);
break; break;
} }
} }
@ -133,11 +144,11 @@ int options_config_file(const char *config_file, int (*handle_options)(int short
rc = -EINVAL; rc = -EINVAL;
goto done; goto done;
} }
if (option->parameter_count && !param[0]) { if (option->parameter_count != i) {
PDEBUG(DOPTIONS, DEBUG_ERROR, "Given option '%s' in config file '%s' at line %d requires %d parameter(s), use '-h' for help!\n", opt, config_file, line, option->parameter_count); PDEBUG(DOPTIONS, DEBUG_ERROR, "Given option '%s' in config file '%s' at line %d requires %d parameter(s), use '-h' for help!\n", opt, config_file, line, option->parameter_count);
return -EINVAL; return -EINVAL;
} }
argv[0] = strdup(param); for (j = 0; j < i; j++)
rc = handle_options(option->short_option, 0, argv); rc = handle_options(option->short_option, 0, argv);
if (rc <= 0) if (rc <= 0)
goto done; goto done;
@ -154,7 +165,7 @@ done:
int options_command_line(int argc, char *argv[], int (*handle_options)(int short_option, int argi, char *argv[])) int options_command_line(int argc, char *argv[], int (*handle_options)(int short_option, int argi, char *argv[]))
{ {
option_t *option; option_t *option;
char params[256]; char params[1024];
int argi, i; int argi, i;
int rc; int rc;