]> git.alsa-project.org Git - alsa-lib.git/commitdiff
conf: improve the include paths code
authorJaroslav Kysela <perex@perex.cz>
Sun, 10 Nov 2019 12:00:59 +0000 (13:00 +0100)
committerJaroslav Kysela <perex@perex.cz>
Sun, 10 Nov 2019 12:05:53 +0000 (13:05 +0100)
- various cleanups (more straight code and allocations)
- do not add the base config path /usr/share/alsa to the
  explicit include list of directories
  (it's not wanted for ucm configs)

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/local.h
src/conf.c
src/ucm/utils.c

index bf19f0f8413144b145a4160ee66ffb64179d39f3..e1e4aa21224b397bb138c7785aaa24d46bda01bb 100644 (file)
@@ -359,7 +359,7 @@ int snd_config_search_alias_hooks(snd_config_t *config,
 int _snd_conf_generic_id(const char *id);
 
 int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
-                                 int override, char *default_include_path);
+                                 int override, const char * const *default_include_path);
 
 /* convenience macros */
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
index 77fd0da506f67f188f4a343aabf8b05743d7a40c..558114ad5a38f8e0b688a5dfcfb92d8ab06ae20d 100644 (file)
@@ -525,7 +525,7 @@ static inline void snd_config_unlock(void) { }
  * The direcotry should be a subdiretory of top configuration directory
  * "/usr/share/alsa/".
  */
-static int add_include_path(struct filedesc *fd, char *dir)
+static int add_include_path(struct filedesc *fd, const char *dir)
 {
        struct include_path *path;
 
@@ -533,7 +533,12 @@ static int add_include_path(struct filedesc *fd, char *dir)
        if (!path)
                return -ENOMEM;
 
-       path->dir = dir;
+       path->dir = strdup(dir);
+       if (path->dir == NULL) {
+               free(path);
+               return -ENOMEM;
+       }
+
        list_add_tail(&path->list, &fd->include_paths);
        return 0;
 }
@@ -598,53 +603,37 @@ static char *_snd_config_path(const char *name)
  *
  * This function will search and open the file in the following order
  * of priority:
- * 1. directly open the file by its name;
- * 2. search for the file name in top configuration directory
- *     "/usr/share/alsa/";
- * 3. search for the file name in in additional configuration directories
- *     specified by users, via alsaconf syntax
- *     <searchdir:relative-path/to/user/share/alsa>;
- *     These directories should be subdirectories of /usr/share/alsa.
+ * 1. directly open the file by its name (only if absolute)
+ * 2. search for the file name in in additional configuration directories
+ *    specified by users, via alsaconf syntax
+ *    <searchdir:relative-path/to/user/share/alsa>;
+ *    These directories should be subdirectories of /usr/share/alsa.
  */
 static int input_stdio_open(snd_input_t **inputp, const char *file,
                            struct list_head *include_paths)
 {
-       struct list_head *pos, *base;
+       struct list_head *pos;
        struct include_path *path;
-       char full_path[PATH_MAX + 1];
+       char full_path[PATH_MAX];
        int err = 0;
 
-       err = snd_input_stdio_open(inputp, file, "r");
-       if (err == 0)
-               goto out;
-
-       if (file[0] == '/') /* not search file with absolute path */
-               return err;
-
-       /* search file in top configuration directory /usr/share/alsa */
-       snprintf(full_path, PATH_MAX, "%s/%s", snd_config_topdir(), file);
-       err = snd_input_stdio_open(inputp, full_path, "r");
-       if (err == 0)
-               goto out;
+       if (file[0] == '/')
+               return snd_input_stdio_open(inputp, file, "r");
 
        /* search file in user specified include paths. These directories
         * are subdirectories of /usr/share/alsa.
         */
-       if (include_paths) {
-               base = include_paths;
-               list_for_each(pos, base) {
-                       path = list_entry(pos, struct include_path, list);
-                       if (!path->dir)
-                               continue;
+       list_for_each(pos, include_paths) {
+               path = list_entry(pos, struct include_path, list);
+               if (!path->dir)
+                       continue;
 
-                       snprintf(full_path, PATH_MAX, "%s/%s", path->dir, file);
-                       err = snd_input_stdio_open(inputp, full_path, "r");
-                       if (err == 0)
-                               goto out;
-               }
+               snprintf(full_path, PATH_MAX, "%s/%s", path->dir, file);
+               err = snd_input_stdio_open(inputp, full_path, "r");
+               if (err == 0)
+                       return 0;
        }
 
-out:
        return err;
 }
 
@@ -798,9 +787,9 @@ static int get_char_skip_comments(input_t *input)
                                closedir(dirp);
 
                                err = add_include_path(input->current, str);
+                               free(str);
                                if (err < 0) {
                                        SNDERR("Cannot add search dir %s", str);
-                                       free(str);
                                        return err;
                                }
                                continue;
@@ -1835,28 +1824,32 @@ int snd_config_top(snd_config_t **config)
 
 #ifndef DOC_HIDDEN
 int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
-                                  int override, char *default_include_path)
+                                 int override, const char * const *include_paths)
 {
        int err;
        input_t input;
        struct filedesc *fd, *fd_next;
+
        assert(config && in);
        fd = malloc(sizeof(*fd));
-       if (!fd) {
-               err = -ENOMEM;
-               goto _end_inc;
-       }
+       if (!fd)
+               return -ENOMEM;
        fd->name = NULL;
        fd->in = in;
        fd->line = 1;
        fd->column = 0;
        fd->next = NULL;
        INIT_LIST_HEAD(&fd->include_paths);
-       if (default_include_path) {
-               err = add_include_path(fd, default_include_path);
+       if (include_paths) {
+               for (; *include_paths; include_paths++) {
+                       err = add_include_path(fd, *include_paths);
+                       if (err < 0)
+                               goto _end;
+               }
+       } else {
+               err = add_include_path(fd, snd_config_topdir());
                if (err < 0)
                        goto _end;
-               default_include_path = NULL;
        }
        input.current = fd;
        input.unget = 0;
@@ -1905,8 +1898,6 @@ int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
 
        free_include_paths(fd);
        free(fd);
- _end_inc:
-       free(default_include_path);
        return err;
 }
 #endif
index 5607304e64269028edaeab2f6396d5296d527f4c..d6ed50a1f0cdca69e78b07ff388b40fe69dc6691 100644 (file)
@@ -54,7 +54,7 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
        FILE *fp;
        snd_input_t *in;
        snd_config_t *top;
-       char *default_path;
+       const char *path, *default_paths[2];
        int err;
 
        fp = fopen(file, "r");
@@ -71,15 +71,13 @@ int uc_mgr_config_load(const char *file, snd_config_t **cfg)
        if (err < 0)
                goto __err1;
 
-       default_path = getenv(ALSA_CONFIG_UCM_VAR);
-       if (!default_path || !*default_path)
-               default_path = ALSA_CONFIG_DIR "/ucm";
-       default_path = strdup(default_path);
-       if (!default_path) {
-               err = -ENOMEM;
-               goto __err2;
-       }
-       err = _snd_config_load_with_include(top, in, 0, default_path);
+       path = getenv(ALSA_CONFIG_UCM_VAR);
+       if (!path || path[0] == '\0')
+               path = ALSA_CONFIG_DIR "/ucm";
+
+       default_paths[0] = path;
+       default_paths[1] = NULL;
+       err = _snd_config_load_with_include(top, in, 0, default_paths);
        if (err < 0) {
                uc_error("could not load configuration file %s", file);
                goto __err2;