Parse: return sections in real order

This commit is contained in:
Philippe Pepiot
2010-04-15 01:57:35 +02:00
parent 7f7bbd1f09
commit 18b702c112
3 changed files with 41 additions and 37 deletions

View File

@@ -78,7 +78,7 @@ static char * get_kw_name(enum conf_type);
static TAILQ_HEAD(, conf_keyword) keywords;
static TAILQ_HEAD(, conf_stack) stack;
static SLIST_HEAD(, conf_sec) config;
static TAILQ_HEAD(, conf_sec) config;
static struct conf_keyword *curk; /* current keyword */
static struct conf_stack *curw; /* current word */
static const struct opt_type opt_type_null = { 0, 0, False, NULL };
@@ -255,13 +255,13 @@ get_conf(const char *name)
curk = TAILQ_FIRST(&keywords);
curw = TAILQ_FIRST(&stack);
SLIST_INIT(&config);
TAILQ_INIT(&config);
while (!TAILQ_EMPTY(&keywords)) {
switch (curk->type) {
case SEC_START:
s = get_section();
SLIST_INSERT_HEAD(&config, s, entry);
TAILQ_INSERT_TAIL(&config, s, entry);
break;
default:
errx(1, "%s:%d: near '%s', config out of any section",
@@ -281,6 +281,9 @@ get_section(void)
s = emalloc(1, sizeof(*s));
s->name = strdup(curw->name);
TAILQ_INIT(&s->sub);
SLIST_INIT(&s->optlist);
pop_stack();
pop_keyword();
@@ -298,7 +301,7 @@ get_section(void)
break;
case SEC_START:
sub = get_section();
SLIST_INSERT_HEAD(&s->sub, sub, entry);
TAILQ_INSERT_TAIL(&s->sub, sub, entry);
s->nsub++;
case SEC_END:
break;
@@ -403,7 +406,7 @@ print_unused(struct conf_sec *sec)
if (!sec)
{
SLIST_FOREACH(s, &config, entry)
TAILQ_FOREACH(s, &config, entry)
print_unused(s);
return;
}
@@ -413,8 +416,8 @@ print_unused(struct conf_sec *sec)
warnx("%s:%d, unused param %s",
file.name, o->line, o->name);
SLIST_FOREACH(s, &sec->sub, entry)
if (!SLIST_EMPTY(&s->sub))
TAILQ_FOREACH(s, &sec->sub, entry)
if (!TAILQ_EMPTY(&s->sub))
print_unused(s);
}
@@ -427,7 +430,7 @@ free_conf(struct conf_sec *sec)
if (!sec)
{
SLIST_FOREACH(s, &config, entry)
TAILQ_FOREACH(s, &config, entry)
{
free(s->name);
free_conf(s);
@@ -448,10 +451,10 @@ free_conf(struct conf_sec *sec)
free(o);
}
while (!SLIST_EMPTY(&sec->sub))
while (!TAILQ_EMPTY(&sec->sub))
{
s = SLIST_FIRST(&sec->sub);
SLIST_REMOVE_HEAD(&sec->sub, entry);
s = TAILQ_FIRST(&sec->sub);
TAILQ_REMOVE(&sec->sub, s, entry);
free_conf(s);
}
@@ -469,7 +472,7 @@ fetch_section(struct conf_sec *s, char *name)
if (!s) {
ret = emalloc(2, sizeof(struct conf_sec *));
SLIST_FOREACH(sec, &config, entry)
TAILQ_FOREACH(sec, &config, entry)
if (!strcmp(sec->name, name)) {
ret[0] = sec;
ret[1] = NULL;
@@ -478,7 +481,7 @@ fetch_section(struct conf_sec *s, char *name)
}
else {
ret = emalloc(s->nsub+1, sizeof(struct conf_sec *));
SLIST_FOREACH(sec, &s->sub, entry) {
TAILQ_FOREACH(sec, &s->sub, entry) {
if (!strcmp(sec->name, name) && i < s->nsub)
ret[i++] = sec;
}
@@ -496,11 +499,11 @@ fetch_section_first(struct conf_sec *s, char *name)
return NULL;
if (!s)
SLIST_FOREACH(sec, &config, entry)
TAILQ_FOREACH(sec, &config, entry)
if (!strcmp(sec->name, name))
return sec;
SLIST_FOREACH(sec, &s->sub, entry)
TAILQ_FOREACH(sec, &s->sub, entry)
if (!strcmp(sec->name, name))
return sec;

View File

@@ -31,10 +31,10 @@ struct conf_opt {
struct conf_sec {
char *name;
SLIST_HEAD(, conf_opt) optlist;
SLIST_HEAD(, conf_sec) sub;
TAILQ_HEAD(, conf_sec) sub;
size_t nopt;
size_t nsub;
SLIST_ENTRY(conf_sec) entry;
TAILQ_ENTRY(conf_sec) entry;
};
struct opt_type {
@@ -68,13 +68,14 @@ void free_conf(struct conf_sec *s);
* section.
* If section == NULL, return subsections from root section.
* Return a NULL terminated array.
* Subsections are returned in order as they are in config file
* WARNING : This MUST be free() after use.
*/
struct conf_sec **fetch_section(struct conf_sec *, char *);
/*
* Get first (last in config file) subsection matching the given name
* on the given section.
* Get first subsection matching the given name
* on the given section. (first found on the file)
*/
struct conf_sec *fetch_section_first(struct conf_sec *, char *);
@@ -89,6 +90,8 @@ size_t fetch_section_count(struct conf_sec **);
* given default param.
* WARNING: This MUST be free() after use.
* WARNING: The string member is directly taken from the config struct.
* WARNING: Returned in reverse order as they are in config file.
* (I think the last option MUST overwrite all others)
*/
struct opt_type fetch_opt_first(struct conf_sec *, char *, char *);