topology: change include path

Add support for relative (based on the path from the parsed
configuration file) and absolute include path without
ALSA_CONFIG_DIR environment variable usage. The dependency
on the alsa-lib config tree may be added on demand.

Link: https://github.com/alsa-project/alsa-utils/pull/117
Link: https://github.com/alsa-project/alsa-utils/issues/118
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2021-12-06 10:13:22 +01:00
parent ef3a96367e
commit 66693b1a77
3 changed files with 42 additions and 25 deletions

View file

@ -310,11 +310,11 @@ static int pre_process_variables_expand_fcn(snd_config_t **dst, const char *str,
} }
static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top, static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top,
const char *pre_processor_defs); const char *pre_processor_defs, const char *inc_path);
static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_config_t *config, static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_config_t *config,
const char *pre_processor_defs, snd_config_t **new, const char *pre_processor_defs, snd_config_t **new,
snd_config_t *variable) snd_config_t *variable, const char *inc_path)
{ {
snd_config_iterator_t i, next; snd_config_iterator_t i, next;
const char *variable_name; const char *variable_name;
@ -371,7 +371,7 @@ static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_conf
regex_t regex; regex_t regex;
const char *filename; const char *filename;
const char *id; const char *id;
char *full_path, *alsa_dir = getenv("ALSA_CONFIG_DIR"); char *full_path;
n = snd_config_iterator_entry(i); n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0) if (snd_config_get_id(n, &id) < 0)
@ -389,22 +389,26 @@ static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_conf
continue; continue;
/* regex matched. now include the conf file */ /* regex matched. now include the conf file */
snd_config_get_string(n, &filename); ret = snd_config_get_string(n, &filename);
if (ret < 0)
goto err;
if (alsa_dir) if (filename && filename[0] != '/')
full_path = tplg_snprintf("%s/%s", alsa_dir, filename); full_path = tplg_snprintf("%s/%s", inc_path, filename);
else else
full_path = tplg_snprintf("%s", alsa_dir); full_path = tplg_snprintf("%s", filename);
ret = snd_input_stdio_open(&in, full_path, "r"); ret = snd_input_stdio_open(&in, full_path, "r");
free(full_path);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Unable to open included conf file %s\n", filename); fprintf(stderr, "Unable to open included conf file %s\n", full_path);
free(full_path);
goto err; goto err;
} }
free(full_path);
/* load config */ /* load config */
ret = snd_config_load(*new, in); ret = snd_config_load(*new, in);
snd_input_close(in);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Unable to load included configuration\n"); fprintf(stderr, "Unable to load included configuration\n");
goto err; goto err;
@ -418,7 +422,7 @@ static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_conf
} }
/* recursively process any nested includes */ /* recursively process any nested includes */
return pre_process_includes(tplg_pp, *new, pre_processor_defs); return pre_process_includes(tplg_pp, *new, pre_processor_defs, inc_path);
} }
err: err:
@ -427,7 +431,7 @@ err:
} }
static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top, static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top,
const char *pre_processor_defs) const char *pre_processor_defs, const char *inc_path)
{ {
snd_config_iterator_t i, next; snd_config_iterator_t i, next;
snd_config_t *includes, *conf_defines; snd_config_t *includes, *conf_defines;
@ -460,7 +464,7 @@ static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t
} }
/* create conf node from included file */ /* create conf node from included file */
ret = pre_process_include_conf(tplg_pp, n, pre_processor_defs, &new, define); ret = pre_process_include_conf(tplg_pp, n, pre_processor_defs, &new, define, inc_path);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Unable to process include file \n"); fprintf(stderr, "Unable to process include file \n");
return ret; return ret;
@ -481,7 +485,7 @@ static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t
} }
static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_config_t *top, static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_config_t *top,
const char *pre_processor_defs) const char *pre_processor_defs, const char *inc_path)
{ {
snd_config_iterator_t i, next; snd_config_iterator_t i, next;
int ret; int ret;
@ -490,7 +494,7 @@ static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_conf
return 0; return 0;
/* process includes at this node */ /* process includes at this node */
ret = pre_process_includes(tplg_pp, top, pre_processor_defs); ret = pre_process_includes(tplg_pp, top, pre_processor_defs, inc_path);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Failed to process includes\n"); fprintf(stderr, "Failed to process includes\n");
return ret; return ret;
@ -502,7 +506,7 @@ static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_conf
n = snd_config_iterator_entry(i); n = snd_config_iterator_entry(i);
ret = pre_process_includes_all(tplg_pp, n, pre_processor_defs); ret = pre_process_includes_all(tplg_pp, n, pre_processor_defs, inc_path);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
@ -511,7 +515,7 @@ static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_conf
} }
int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size, int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size,
const char *pre_processor_defs) const char *pre_processor_defs, const char *inc_path)
{ {
snd_input_t *in; snd_input_t *in;
snd_config_t *top; snd_config_t *top;
@ -546,7 +550,7 @@ int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_
} }
/* include conditional conf files */ /* include conditional conf files */
err = pre_process_includes_all(tplg_pp, tplg_pp->input_cfg, pre_processor_defs); err = pre_process_includes_all(tplg_pp, tplg_pp->input_cfg, pre_processor_defs, inc_path);
if (err < 0) { if (err < 0) {
fprintf(stderr, "Failed to process conditional includes in input config\n"); fprintf(stderr, "Failed to process conditional includes in input config\n");
goto err; goto err;

View file

@ -233,13 +233,22 @@ static int dump(const char *source_file, const char *output_file, int cflags, in
return err; return err;
} }
static char *get_inc_path(const char *filename)
{
const char *s = strrchr(filename, '/');
char *r = strdup(filename);
if (r && s)
r[s - filename] = '\0';
return r;
}
/* Convert Topology2.0 conf to the existing conf syntax */ /* Convert Topology2.0 conf to the existing conf syntax */
static int pre_process_conf(const char *source_file, const char *output_file, static int pre_process_conf(const char *source_file, const char *output_file,
const char *pre_processor_defs) const char *pre_processor_defs)
{ {
struct tplg_pre_processor *tplg_pp; struct tplg_pre_processor *tplg_pp;
size_t config_size; size_t config_size;
char *config; char *config, *inc_path;
int err; int err;
err = load(source_file, (void **)&config, &config_size); err = load(source_file, (void **)&config, &config_size);
@ -247,7 +256,7 @@ static int pre_process_conf(const char *source_file, const char *output_file,
return err; return err;
/* init pre-processor */ /* init pre-processor */
err = init_pre_precessor(&tplg_pp, SND_OUTPUT_STDIO, output_file); err = init_pre_processor(&tplg_pp, SND_OUTPUT_STDIO, output_file);
if (err < 0) { if (err < 0) {
fprintf(stderr, _("failed to init pre-processor for Topology2.0\n")); fprintf(stderr, _("failed to init pre-processor for Topology2.0\n"));
free(config); free(config);
@ -255,7 +264,9 @@ static int pre_process_conf(const char *source_file, const char *output_file,
} }
/* pre-process conf file */ /* pre-process conf file */
err = pre_process(tplg_pp, config, config_size, pre_processor_defs); inc_path = get_inc_path(source_file);
err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
free(inc_path);
/* free pre-processor */ /* free pre-processor */
free_pre_preprocessor(tplg_pp); free_pre_preprocessor(tplg_pp);
@ -268,7 +279,7 @@ static int compile(const char *source_file, const char *output_file, int cflags,
{ {
struct tplg_pre_processor *tplg_pp = NULL; struct tplg_pre_processor *tplg_pp = NULL;
snd_tplg_t *tplg; snd_tplg_t *tplg;
char *config; char *config, *inc_path;
void *bin; void *bin;
size_t config_size, size; size_t config_size, size;
int err; int err;
@ -283,10 +294,12 @@ static int compile(const char *source_file, const char *output_file, int cflags,
size_t size; size_t size;
/* init pre-processor */ /* init pre-processor */
init_pre_precessor(&tplg_pp, SND_OUTPUT_BUFFER, NULL); init_pre_processor(&tplg_pp, SND_OUTPUT_BUFFER, NULL);
/* pre-process conf file */ /* pre-process conf file */
err = pre_process(tplg_pp, config, config_size, pre_processor_defs); inc_path = get_inc_path(source_file);
err = pre_process(tplg_pp, config, config_size, pre_processor_defs, inc_path);
free(inc_path);
if (err) { if (err) {
free_pre_preprocessor(tplg_pp); free_pre_preprocessor(tplg_pp);
free(config); free(config);

View file

@ -28,8 +28,8 @@ struct tplg_pre_processor {
}; };
int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size, int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size,
const char *pre_processor_defs); const char *pre_processor_defs, const char *inc_path);
int init_pre_precessor(struct tplg_pre_processor **tplg_pp, snd_output_type_t type, int init_pre_processor(struct tplg_pre_processor **tplg_pp, snd_output_type_t type,
const char *output_file); const char *output_file);
void free_pre_preprocessor(struct tplg_pre_processor *tplg_pp); void free_pre_preprocessor(struct tplg_pre_processor *tplg_pp);
#endif #endif