conf.c: move section handling out of _parseconfig

_parseconfig now tracks the current section name directly so that the
name stored in the section struct is just a pointer to the one stored by
_parseconfig.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2013-07-22 02:46:48 -04:00 committed by Allan McRae
parent 8287312c29
commit 26da037fd5

View file

@ -744,7 +744,7 @@ static int setup_libalpm(void)
*/ */
struct section_t { struct section_t {
/* useful for all sections */ /* useful for all sections */
char *name; const char *name;
int is_options; int is_options;
int parse_options; int parse_options;
/* db section option gathering */ /* db section option gathering */
@ -836,14 +836,25 @@ cleanup:
alpm_list_free(section->servers); alpm_list_free(section->servers);
section->servers = NULL; section->servers = NULL;
section->siglevel = ALPM_SIG_USE_DEFAULT; section->siglevel = ALPM_SIG_USE_DEFAULT;
free(section->name);
section->name = NULL; section->name = NULL;
return ret; return ret;
} }
static int _parse_directive(const char *file, int linenum, static int _parse_directive(const char *file, int linenum, const char *name,
char *key, char *value, struct section_t *section) char *key, char *value, struct section_t *section)
{ {
if(!key && !value) {
int ret = finish_section(section);
pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
section->name = name;
if(name && strcmp(name, "options") == 0) {
section->is_options = 1;
} else {
section->is_options = 0;
}
return ret;
}
if(section->name == NULL) { if(section->name == NULL) {
pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"), pm_printf(ALPM_LOG_ERROR, _("config file %s, line %d: All directives must belong to a section.\n"),
file, linenum); file, linenum);
@ -857,6 +868,7 @@ static int _parse_directive(const char *file, int linenum,
/* ... or in a repo section */ /* ... or in a repo section */
return _parse_repo(key, value, file, linenum, section); return _parse_repo(key, value, file, linenum, section);
} }
return 0; return 0;
} }
@ -869,7 +881,8 @@ static int _parse_directive(const char *file, int linenum,
* @param depth the current recursion depth * @param depth the current recursion depth
* @return 0 on success, 1 on failure * @return 0 on success, 1 on failure
*/ */
static int _parseconfig(const char *file, struct section_t *section, int depth) static int _parseconfig(const char *file, struct section_t *section,
char **section_name, int depth)
{ {
FILE *fp = NULL; FILE *fp = NULL;
char line[PATH_MAX]; char line[PATH_MAX];
@ -922,14 +935,14 @@ static int _parseconfig(const char *file, struct section_t *section, int depth)
/* new config section, skip the '[' */ /* new config section, skip the '[' */
name = strdup(line + 1); name = strdup(line + 1);
name[line_len - 2] = '\0'; name[line_len - 2] = '\0';
/* we're at a new section; perform any post-actions for the prior */
if(finish_section(section)) { ret = _parse_directive(file, linenum, name, NULL, NULL, section);
ret = 1; free(*section_name);
*section_name = name;
if(ret) {
goto cleanup; goto cleanup;
} }
pm_printf(ALPM_LOG_DEBUG, "config: new section '%s'\n", name);
section->name = name;
section->is_options = (strcmp(name, "options") == 0);
continue; continue;
} }
@ -981,26 +994,31 @@ static int _parseconfig(const char *file, struct section_t *section, int depth)
for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) { for(gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n", pm_printf(ALPM_LOG_DEBUG, "config file %s, line %d: including %s\n",
file, linenum, globbuf.gl_pathv[gindex]); file, linenum, globbuf.gl_pathv[gindex]);
_parseconfig(globbuf.gl_pathv[gindex], section, depth + 1); _parseconfig(globbuf.gl_pathv[gindex], section,
section_name, depth + 1);
} }
break; break;
} }
globfree(&globbuf); globfree(&globbuf);
continue; continue;
} }
if((ret = _parse_directive(file, linenum, key, value, section)) != 0) { if((ret = _parse_directive(file, linenum, *section_name, key, value, section)) != 0) {
goto cleanup; goto cleanup;
} }
} }
if(depth == 0) { if(depth == 0) {
ret = finish_section(section); ret = _parse_directive(NULL, 0, NULL, NULL, NULL, section);
} }
cleanup: cleanup:
if(fp) { if(fp) {
fclose(fp); fclose(fp);
} }
if(depth == 0) {
free(*section_name);
*section_name = NULL;
}
pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file); pm_printf(ALPM_LOG_DEBUG, "config: finished parsing %s\n", file);
return ret; return ret;
} }
@ -1013,6 +1031,7 @@ int parseconfig(const char *file)
{ {
int ret; int ret;
struct section_t section; struct section_t section;
char *section_name = NULL;
memset(&section, 0, sizeof(struct section_t)); memset(&section, 0, sizeof(struct section_t));
section.siglevel = ALPM_SIG_USE_DEFAULT; section.siglevel = ALPM_SIG_USE_DEFAULT;
/* the config parse is a two-pass affair. We first parse the entire thing for /* the config parse is a two-pass affair. We first parse the entire thing for
@ -1022,7 +1041,7 @@ int parseconfig(const char *file)
/* call the real parseconfig function with a null section & db argument */ /* call the real parseconfig function with a null section & db argument */
pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n"); pm_printf(ALPM_LOG_DEBUG, "parseconfig: options pass\n");
section.parse_options = 1; section.parse_options = 1;
if((ret = _parseconfig(file, &section, 0))) { if((ret = _parseconfig(file, &section, &section_name, 0))) {
return ret; return ret;
} }
if((ret = setup_libalpm())) { if((ret = setup_libalpm())) {
@ -1031,7 +1050,7 @@ int parseconfig(const char *file)
/* second pass, repo section parsing */ /* second pass, repo section parsing */
pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n"); pm_printf(ALPM_LOG_DEBUG, "parseconfig: repo pass\n");
section.parse_options = 0; section.parse_options = 0;
return _parseconfig(file, &section, 0); return _parseconfig(file, &section, &section_name, 0);
} }
/* vim: set ts=2 sw=2 noet: */ /* vim: set ts=2 sw=2 noet: */