Browse Source

Allow invalid values to be in the configuration file, just skipping over them provided the rest of the file is valid JSON.

This will allow older configuration files with options no longer existing to still be portable.
nfactor-troky
Con Kolivas 13 years ago
parent
commit
15672b52a6
  1. 13
      main.c

13
main.c

@ -1746,7 +1746,7 @@ static struct opt_table opt_config_table[] = {
OPT_ENDTABLE OPT_ENDTABLE
}; };
static char *parse_config(json_t *config) static char *parse_config(json_t *config, bool fileconf)
{ {
static char err_buf[200]; static char err_buf[200];
json_t *val; json_t *val;
@ -1779,7 +1779,7 @@ static char *parse_config(json_t *config)
if (json_is_string(json_array_get(val, n))) if (json_is_string(json_array_get(val, n)))
err = opt->cb_arg(json_string_value(json_array_get(val, n)), opt->u.arg); err = opt->cb_arg(json_string_value(json_array_get(val, n)), opt->u.arg);
else if (json_is_object(json_array_get(val, n))) else if (json_is_object(json_array_get(val, n)))
err = parse_config(json_array_get(val, n)); err = parse_config(json_array_get(val, n), false);
} }
} else if ((opt->type&OPT_NOARG) && json_is_true(val)) { } else if ((opt->type&OPT_NOARG) && json_is_true(val)) {
err = opt->cb(opt->u.arg); err = opt->cb(opt->u.arg);
@ -1787,11 +1787,18 @@ static char *parse_config(json_t *config)
err = "Invalid value"; err = "Invalid value";
} }
if (err) { if (err) {
/* Allow invalid values to be in configuration
* file, just skipping over them provided the
* JSON is still valid after that. */
if (fileconf)
applog(LOG_ERR, "Invalid config option %s: %s", p, err);
else {
sprintf(err_buf, "Parsing JSON option %s: %s", sprintf(err_buf, "Parsing JSON option %s: %s",
p, err); p, err);
return err_buf; return err_buf;
} }
} }
}
free(name); free(name);
} }
return NULL; return NULL;
@ -1809,7 +1816,7 @@ static char *load_config(const char *arg, void *unused)
config_loaded = true; config_loaded = true;
/* Parse the config now, so we can override it. That can keep pointers /* Parse the config now, so we can override it. That can keep pointers
* so don't free config object. */ * so don't free config object. */
return parse_config(config); return parse_config(config, true);
} }
static void load_default_config(void) static void load_default_config(void)

Loading…
Cancel
Save