From 66693b1a77016626d6b860f0d11597c0564194ac Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Mon, 6 Dec 2021 10:13:22 +0100 Subject: [PATCH] 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 --- topology/pre-processor.c | 38 +++++++++++++++++++++----------------- topology/topology.c | 25 +++++++++++++++++++------ topology/topology.h | 4 ++-- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/topology/pre-processor.c b/topology/pre-processor.c index fce37b7..de44ac5 100644 --- a/topology/pre-processor.c +++ b/topology/pre-processor.c @@ -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, - 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, 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; 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; const char *filename; const char *id; - char *full_path, *alsa_dir = getenv("ALSA_CONFIG_DIR"); + char *full_path; n = snd_config_iterator_entry(i); 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; /* 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) - full_path = tplg_snprintf("%s/%s", alsa_dir, filename); + if (filename && filename[0] != '/') + full_path = tplg_snprintf("%s/%s", inc_path, filename); else - full_path = tplg_snprintf("%s", alsa_dir); + full_path = tplg_snprintf("%s", filename); ret = snd_input_stdio_open(&in, full_path, "r"); - free(full_path); 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; } + free(full_path); /* load config */ ret = snd_config_load(*new, in); + snd_input_close(in); if (ret < 0) { fprintf(stderr, "Unable to load included configuration\n"); 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 */ - return pre_process_includes(tplg_pp, *new, pre_processor_defs); + return pre_process_includes(tplg_pp, *new, pre_processor_defs, inc_path); } err: @@ -427,7 +431,7 @@ err: } 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_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 */ - 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) { fprintf(stderr, "Unable to process include file \n"); 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, - const char *pre_processor_defs) + const char *pre_processor_defs, const char *inc_path) { snd_config_iterator_t i, next; int ret; @@ -490,7 +494,7 @@ static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_conf return 0; /* 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) { fprintf(stderr, "Failed to process includes\n"); 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); - 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) 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, - const char *pre_processor_defs) + const char *pre_processor_defs, const char *inc_path) { snd_input_t *in; 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 */ - 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) { fprintf(stderr, "Failed to process conditional includes in input config\n"); goto err; diff --git a/topology/topology.c b/topology/topology.c index 4a0750f..0c36e7c 100644 --- a/topology/topology.c +++ b/topology/topology.c @@ -233,13 +233,22 @@ static int dump(const char *source_file, const char *output_file, int cflags, in 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 */ static int pre_process_conf(const char *source_file, const char *output_file, const char *pre_processor_defs) { struct tplg_pre_processor *tplg_pp; size_t config_size; - char *config; + char *config, *inc_path; int err; 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; /* 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) { fprintf(stderr, _("failed to init pre-processor for Topology2.0\n")); free(config); @@ -255,7 +264,9 @@ static int pre_process_conf(const char *source_file, const char *output_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_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; snd_tplg_t *tplg; - char *config; + char *config, *inc_path; void *bin; size_t config_size, size; int err; @@ -283,10 +294,12 @@ static int compile(const char *source_file, const char *output_file, int cflags, size_t size; /* 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 */ - 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) { free_pre_preprocessor(tplg_pp); free(config); diff --git a/topology/topology.h b/topology/topology.h index 5dd9b1f..11ee64e 100644 --- a/topology/topology.h +++ b/topology/topology.h @@ -28,8 +28,8 @@ struct tplg_pre_processor { }; int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size, - const char *pre_processor_defs); -int init_pre_precessor(struct tplg_pre_processor **tplg_pp, snd_output_type_t type, + const char *pre_processor_defs, const char *inc_path); +int init_pre_processor(struct tplg_pre_processor **tplg_pp, snd_output_type_t type, const char *output_file); void free_pre_preprocessor(struct tplg_pre_processor *tplg_pp); #endif