config: fixes

This commit is contained in:
Přemysl Eric Janouch 2015-05-02 21:56:05 +02:00
parent cfc77159d8
commit cb2cbc9100
1 changed files with 31 additions and 28 deletions

View File

@ -675,7 +675,7 @@ config_item_string_array (const struct str *s)
static struct config_item_ * static struct config_item_ *
config_item_object (void) config_item_object (void)
{ {
struct config_item_ *self = config_item_new (CONFIG_ITEM_BOOLEAN); struct config_item_ *self = config_item_new (CONFIG_ITEM_OBJECT);
str_map_init (&self->value.object); str_map_init (&self->value.object);
self->value.object.free = (void (*)(void *)) config_item_destroy; self->value.object.free = (void (*)(void *)) config_item_destroy;
return self; return self;
@ -1150,26 +1150,6 @@ config_tokenizer_next (struct config_tokenizer *self, struct error **e)
} }
} }
bool is_word = false;
while (config_tokenizer_is_word_char (*self->p))
{
is_word = true;
str_reset (&self->string);
str_append_c (&self->string, config_tokenizer_advance (self));
}
if (is_word)
{
if (!strcmp (self->string.str, "null"))
return CONFIG_T_NULL;
bool boolean;
if (!set_boolean_if_valid (&boolean, self->string.str))
return CONFIG_T_WORD;
self->integer = boolean;
return CONFIG_T_BOOLEAN;
}
char *end; char *end;
errno = 0; errno = 0;
self->integer = strtoll (self->p, &end, 10); self->integer = strtoll (self->p, &end, 10);
@ -1185,8 +1165,26 @@ config_tokenizer_next (struct config_tokenizer *self, struct error **e)
return CONFIG_T_INTEGER; return CONFIG_T_INTEGER;
} }
if (!config_tokenizer_is_word_char (*self->p))
{
config_tokenizer_error (self, e, "invalid input"); config_tokenizer_error (self, e, "invalid input");
return CONFIG_T_ABORT; return CONFIG_T_ABORT;
}
str_reset (&self->string);
do
str_append_c (&self->string, config_tokenizer_advance (self));
while (config_tokenizer_is_word_char (*self->p));
if (!strcmp (self->string.str, "null"))
return CONFIG_T_NULL;
bool boolean;
if (!set_boolean_if_valid (&boolean, self->string.str))
return CONFIG_T_WORD;
self->integer = boolean;
return CONFIG_T_BOOLEAN;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1436,14 +1434,18 @@ config_schema_fix_value
if (replace) if (replace)
{ {
struct error *e = NULL; struct error *e = NULL;
struct config_item_ *default_ = config_item_parse if (schema->default_)
item = config_item_parse
(schema->default_, strlen (schema->default_), true, &e); (schema->default_, strlen (schema->default_), true, &e);
if (e || !config_item_validate_by_schema (default_, schema, &e)) else
item = config_item_null ();
if (e || !config_item_validate_by_schema (item, schema, &e))
exit_fatal ("invalid default for `%s': %s", exit_fatal ("invalid default for `%s': %s",
schema->name, e->message); schema->name, e->message);
config_item_move (item, default_); // This will free the old item if there was any
str_map_set (&object->value.object, schema->name, default_); str_map_set (&object->value.object, schema->name, item);
} }
// Make sure the string subtype fits the schema // Make sure the string subtype fits the schema
@ -1531,6 +1533,7 @@ static void
config_load (struct config *self, struct config_item_ *root) config_load (struct config *self, struct config_item_ *root)
{ {
hard_assert (root->type == CONFIG_ITEM_OBJECT); hard_assert (root->type == CONFIG_ITEM_OBJECT);
self->root = root;
struct str_map_iter iter; struct str_map_iter iter;
str_map_iter_init (&iter, &self->modules); str_map_iter_init (&iter, &self->modules);