conf: rename snd_conf_load1() to _snd_config_load_with_include()

Always free the include path which must be mallocated by the caller.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-01-07 13:15:32 +01:00
parent f664a7aec9
commit 1e5ecbc806
3 changed files with 29 additions and 41 deletions

View file

@ -355,7 +355,7 @@ int snd_config_search_alias_hooks(snd_config_t *config,
int _snd_conf_generic_id(const char *id); int _snd_conf_generic_id(const char *id);
int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in, int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
const char *default_include_path); int override, char *default_include_path);
/* convenience macros */ /* convenience macros */
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

View file

@ -1832,16 +1832,19 @@ int snd_config_top(snd_config_t **config)
return _snd_config_make(config, 0, SND_CONFIG_TYPE_COMPOUND); return _snd_config_make(config, 0, SND_CONFIG_TYPE_COMPOUND);
} }
static int snd_config_load1(snd_config_t *config, snd_input_t *in, int override, #ifndef DOC_HIDDEN
char *default_include_path) int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
int override, char *default_include_path)
{ {
int err; int err;
input_t input; input_t input;
struct filedesc *fd, *fd_next; struct filedesc *fd, *fd_next;
assert(config && in); assert(config && in);
fd = malloc(sizeof(*fd)); fd = malloc(sizeof(*fd));
if (!fd) if (!fd) {
return -ENOMEM; err = -ENOMEM;
goto _end_inc;
}
fd->name = NULL; fd->name = NULL;
fd->in = in; fd->in = in;
fd->line = 1; fd->line = 1;
@ -1852,6 +1855,7 @@ static int snd_config_load1(snd_config_t *config, snd_input_t *in, int override,
err = add_include_path(fd, default_include_path); err = add_include_path(fd, default_include_path);
if (err < 0) if (err < 0)
goto _end; goto _end;
default_include_path = NULL;
} }
input.current = fd; input.current = fd;
input.unget = 0; input.unget = 0;
@ -1900,8 +1904,11 @@ static int snd_config_load1(snd_config_t *config, snd_input_t *in, int override,
free_include_paths(fd); free_include_paths(fd);
free(fd); free(fd);
_end_inc:
free(default_include_path);
return err; return err;
} }
#endif
/** /**
* \brief Loads a configuration tree. * \brief Loads a configuration tree.
@ -1921,29 +1928,9 @@ static int snd_config_load1(snd_config_t *config, snd_input_t *in, int override,
*/ */
int snd_config_load(snd_config_t *config, snd_input_t *in) int snd_config_load(snd_config_t *config, snd_input_t *in)
{ {
return snd_config_load1(config, in, 0, NULL); return _snd_config_load_with_include(config, in, 0, NULL);
} }
#ifndef DOC_HIDDEN
/* load config with the default include path; used internally for UCM parser */
int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
const char *default_include_path)
{
int err;
char *s = NULL;
if (default_include_path) {
s = strdup(default_include_path);
if (!s)
return -ENOMEM;
}
err = snd_config_load1(config, in, 0, s);
if (err < 0)
free(s);
return err;
}
#endif
/** /**
* \brief Loads a configuration tree and overrides existing configuration nodes. * \brief Loads a configuration tree and overrides existing configuration nodes.
* \param config Handle to a top level configuration node. * \param config Handle to a top level configuration node.
@ -1956,7 +1943,7 @@ int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
*/ */
int snd_config_load_override(snd_config_t *config, snd_input_t *in) int snd_config_load_override(snd_config_t *config, snd_input_t *in)
{ {
return snd_config_load1(config, in, 1, NULL); return _snd_config_load_with_include(config, in, 1, NULL);
} }
/** /**

View file

@ -54,17 +54,12 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
FILE *fp; FILE *fp;
snd_input_t *in; snd_input_t *in;
snd_config_t *top; snd_config_t *top;
const char *default_path; char *default_path;
int err; int err;
fp = fopen(file, "r"); fp = fopen(file, "r");
if (fp == NULL) { err = fp == NULL ? -errno : snd_input_stdio_attach(&in, fp, 1);
err = -errno;
goto __err;
}
err = snd_input_stdio_attach(&in, fp, 1);
if (err < 0) { if (err < 0) {
__err:
uc_error("could not open configuration file %s", file); uc_error("could not open configuration file %s", file);
return err; return err;
} }
@ -75,19 +70,25 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
default_path = getenv(ALSA_CONFIG_UCM_VAR); default_path = getenv(ALSA_CONFIG_UCM_VAR);
if (!default_path || !*default_path) if (!default_path || !*default_path)
default_path = ALSA_CONFIG_DIR "/ucm"; default_path = ALSA_CONFIG_DIR "/ucm";
err = _snd_config_load_with_include(top, in, default_path); default_path = strdup(default_path);
if (!default_path) {
err = -ENOMEM;
goto __err2;
}
err = _snd_config_load_with_include(top, in, 0, default_path);
if (err < 0) { if (err < 0) {
uc_error("could not load configuration file %s", file); uc_error("could not load configuration file %s", file);
snd_config_delete(top); goto __err2;
return err;
} }
err = snd_input_close(in); err = snd_input_close(in);
if (err < 0) { if (err < 0)
snd_config_delete(top); goto __err2;
return err;
}
*cfg = top; *cfg = top;
return 0; return 0;
__err2:
snd_config_delete(top);
return err;
} }
void uc_mgr_free_value(struct list_head *base) void uc_mgr_free_value(struct list_head *base)