This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository legacy-imlib2.

View the commit online.

commit c035f8d19a0d357e97ef104665391cba4990aa47
Author: Kim Woelders <[email protected]>
AuthorDate: Fri May 13 21:33:58 2022 +0200

    modules: Enable setting multiple loader/filter paths
    
    IMLIB2_LOADER_PATH and IMLIB2_FILTER_PATH may now contain multiple colon
    separated paths.
---
 src/lib/file.h    |   8 ++--
 src/lib/modules.c | 138 +++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 100 insertions(+), 46 deletions(-)

diff --git a/src/lib/file.h b/src/lib/file.h
index 0061194..2de9e16 100644
--- a/src/lib/file.h
+++ b/src/lib/file.h
@@ -44,9 +44,9 @@ void                __imlib_FileDel(const char *s);
 char               *__imlib_FileHomeDir(int uid);
 int                 __imlib_ItemInList(char **list, int size, char *item);
 
-const char         *__imlib_PathToFilters(void);
-const char         *__imlib_PathToLoaders(void);
-char              **__imlib_ModulesList(const char *path, int *num_ret);
-char               *__imlib_ModuleFind(const char *path, const char *name);
+char              **__imlib_PathToFilters(void);
+char              **__imlib_PathToLoaders(void);
+char              **__imlib_ModulesList(char **path, int *num_ret);
+char               *__imlib_ModuleFind(char **path, const char *name);
 
 #endif
diff --git a/src/lib/modules.c b/src/lib/modules.c
index 14d56b5..d6dfb3f 100644
--- a/src/lib/modules.c
+++ b/src/lib/modules.c
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #include "file.h"
+#include "strutils.h"
 
 static const char  *
 __imlib_PathToModules(void)
@@ -23,42 +24,70 @@ __imlib_PathToModules(void)
    return PACKAGE_LIB_DIR "/imlib2";
 }
 
-const char         *
-__imlib_PathToFilters(void)
+static char       **
+_module_paths(const char *env, const char *mdir)
 {
-   static char        *path = NULL;
+   char              **ppaths, **pp;
+   const char         *penv;
    char                buf[1024];
 
-   if (path)
-      return path;
+   penv = getenv(env);
+   if (penv)
+     {
+        ppaths = __imlib_StrSplit(penv, ':');
+        if (!ppaths)
+           goto done;
+        for (pp = ppaths; *pp; pp++)
+          {
+             if (strcmp(*pp, "*") == 0)
+               {
+                  /* Substitute default path */
+                  free(*pp);
+                  snprintf(buf, sizeof(buf), "%s/%s",
+                           __imlib_PathToModules(), mdir);
+                  *pp = strdup(buf);
+               }
+          }
+     }
+   else
+     {
+        /* Use default path */
+        ppaths = malloc(2 * sizeof(char *));
+        if (!ppaths)
+           goto done;
+        snprintf(buf, sizeof(buf), "%s/%s", __imlib_PathToModules(), mdir);
+        ppaths[0] = strdup(buf);
+        ppaths[1] = NULL;
+     }
 
-   path = getenv("IMLIB2_FILTER_PATH");
-   if (path && __imlib_FileIsDir(path))
-      return path;
+ done:
+   return ppaths;
+}
+
+char              **
+__imlib_PathToFilters(void)
+{
+   static char       **ppaths = NULL;
+
+   if (ppaths)
+      return ppaths;
 
-   snprintf(buf, sizeof(buf), "%s/%s", __imlib_PathToModules(), "filters");
-   path = strdup(buf);
+   ppaths = _module_paths("IMLIB2_FILTER_PATH", "filters");
 
-   return path;
+   return ppaths;
 }
 
-const char         *
+char              **
 __imlib_PathToLoaders(void)
 {
-   static char        *path = NULL;
-   char                buf[1024];
+   static char       **ppaths = NULL;
 
-   if (path)
-      return path;
+   if (ppaths)
+      return ppaths;
 
-   path = getenv("IMLIB2_LOADER_PATH");
-   if (path && __imlib_FileIsDir(path))
-      return path;
-
-   snprintf(buf, sizeof(buf), "%s/%s", __imlib_PathToModules(), "loaders");
-   path = strdup(buf);
+   ppaths = _module_paths("IMLIB2_LOADER_PATH", "loaders");
 
-   return path;
+   return ppaths;
 }
 
 static bool
@@ -81,45 +110,70 @@ _file_is_module(const char *name)
 }
 
 char              **
-__imlib_ModulesList(const char *path, int *num_ret)
+__imlib_ModulesList(char **ppath, int *num_ret)
 {
-   char              **list = NULL, **l;
+   char              **pp, **list, **l;
    char                file[1024], *p;
    int                 num, i, ntot;
 
    *num_ret = 0;
-   ntot = 0;
+   list = NULL;
 
-   l = __imlib_FileDir(path, &num);
-   if (num <= 0)
-      return NULL;
+   if (!ppath)
+      goto done;
 
-   list = malloc(num * sizeof(char *));
-   if (list)
+   ntot = 0;
+
+   for (pp = ppath; *pp; pp++)
      {
-        for (i = 0; i < num; i++)
+        l = __imlib_FileDir(*pp, &num);
+        if (!l)
+           continue;
+        if (num <= 0)
+           continue;
+
+        list = realloc(list, (ntot + num) * sizeof(char *));
+        if (list)
           {
-             if (!_file_is_module(l[i]))
-                continue;
-             snprintf(file, sizeof(file), "%s/%s", path, l[i]);
-             p = strdup(file);
-             if (p)
-                list[ntot++] = p;
+             for (i = 0; i < num; i++)
+               {
+                  if (!_file_is_module(l[i]))
+                     continue;
+                  snprintf(file, sizeof(file), "%s/%s", *pp, l[i]);
+                  p = strdup(file);
+                  if (p)
+                     list[ntot++] = p;
+               }
           }
+        __imlib_FileFreeDirList(l, num);
+        if (!list)
+           goto done;
      }
-   __imlib_FileFreeDirList(l, num);
 
    *num_ret = ntot;
 
+ done:
    return list;
 }
 
 char               *
-__imlib_ModuleFind(const char *path, const char *name)
+__imlib_ModuleFind(char **ppath, const char *name)
 {
+   char              **pp;
    char                nbuf[4096];
 
-   snprintf(nbuf, sizeof(nbuf), "%s/%s.so", path, name);
+   if (!ppath)
+      return NULL;
+
+   for (pp = ppath; *pp; pp++)
+     {
+        snprintf(nbuf, sizeof(nbuf), "%s/%s.so", *pp, name);
+
+        if (!__imlib_FileIsFile(nbuf))
+           continue;
+
+        return strdup(nbuf);
+     }
 
-   return strdup(nbuf);
+   return NULL;
 }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to