Allow to replace/extend previously defined values/sections in strongswan.conf.

This commit is contained in:
Tobias Brunner
2010-12-03 17:38:37 +01:00
parent 3eb23f3864
commit 5e3be75829
+50 -9
View File
@@ -473,6 +473,22 @@ static void section_destroy(section_t *this)
free(this); free(this);
} }
/**
* callback to find a section by name
*/
static bool section_find(section_t *this, char *name)
{
return streq(this->name, name);
}
/**
* callback to find a kv pair by key
*/
static bool kv_find(kv_t *this, char *key)
{
return streq(this->key, key);
}
/** /**
* parse text, truncate "skip" chars, delimited by term respecting brackets. * parse text, truncate "skip" chars, delimited by term respecting brackets.
* *
@@ -562,13 +578,27 @@ static bool parse_section(char **text, section_t *section)
case '{': case '{':
if (parse(text, "\t ", "}", "{", &inner)) if (parse(text, "\t ", "}", "{", &inner))
{ {
section_t *sub = section_create(key); section_t *sub;
if (parse_section(&inner, sub)) if (section->sections->find_first(section->sections,
(linked_list_match_t)section_find,
(void**)&sub, key) != SUCCESS)
{ {
section->sections->insert_last(section->sections, sub); sub = section_create(key);
continue; if (parse_section(&inner, sub))
{
section->sections->insert_last(section->sections,
sub);
continue;
}
section_destroy(sub);
}
else
{ /* extend the existing section */
if (parse_section(&inner, sub))
{
continue;
}
} }
section_destroy(sub);
DBG1(DBG_LIB, "parsing subsection '%s' failed", key); DBG1(DBG_LIB, "parsing subsection '%s' failed", key);
break; break;
} }
@@ -577,10 +607,21 @@ static bool parse_section(char **text, section_t *section)
case '=': case '=':
if (parse(text, "\t ", "\n", NULL, &value)) if (parse(text, "\t ", "\n", NULL, &value))
{ {
kv_t *kv = malloc_thing(kv_t); kv_t *kv;
kv->key = key; if (section->kv->find_first(section->kv,
kv->value = value; (linked_list_match_t)kv_find,
section->kv->insert_last(section->kv, kv); (void**)&kv, key) != SUCCESS)
{
INIT(kv,
.key = key,
.value = value,
);
section->kv->insert_last(section->kv, kv);
}
else
{ /* replace with the most recently read value */
kv->value = value;
}
continue; continue;
} }
DBG1(DBG_LIB, "parsing value failed near %s", *text); DBG1(DBG_LIB, "parsing value failed near %s", *text);