Revision: 77333
          http://sourceforge.net/p/brlcad/code/77333
Author:   brlcad
Date:     2020-10-05 16:19:45 +0000 (Mon, 05 Oct 2020)
Log Message:
-----------
rework the dynamic library lookup to go through bu_dir() for all lookups, so 
there are no longer hardcoded assumptions about the library suffix.  eliminates 
the need for a direct cwd call as well.

Modified Paths:
--------------
    brlcad/trunk/src/liboptical/material.c

Modified: brlcad/trunk/src/liboptical/material.c
===================================================================
--- brlcad/trunk/src/liboptical/material.c      2020-10-05 14:32:19 UTC (rev 
77332)
+++ brlcad/trunk/src/liboptical/material.c      2020-10-05 16:19:45 UTC (rev 
77333)
@@ -72,13 +72,13 @@
  * up the shader named "material" in the table.
  */
 static struct mfuncs *
-try_load(const char *path, const char *material, const char *shader_name)
+try_load(const char *path, const char *material)
 {
+    char sym[MAXPATHLEN];
+    const char *dl_error_str;
+    struct mfuncs *mfp;
+    struct mfuncs *shader_mfuncs;
     void *handle;
-    struct mfuncs *shader_mfuncs;
-    struct mfuncs *mfp;
-    const char *dl_error_str;
-    char sym[MAXPATHLEN];
 
     if (! (handle = bu_dlopen(path, BU_RTLD_NOW))) {
        if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL)
@@ -89,7 +89,7 @@
     }
 
     /* Find the {shader}_mfuncs symbol in the library */
-    snprintf(sym, MAXPATHLEN, "%s_mfuncs", shader_name);
+    snprintf(sym, MAXPATHLEN, "%s_mfuncs", material);
     shader_mfuncs = (struct mfuncs *)bu_dlsym((struct mfuncs *)handle, sym);
 
     dl_error_str=bu_dlerror();
@@ -108,19 +108,19 @@
     }
 
     if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL)
-       bu_log("%s_mfuncs table found\n", shader_name);
+       bu_log("%s_mfuncs table found\n", material);
 
     /* make sure the shader we were looking for is in the mfuncs table */
     for (mfp = shader_mfuncs; mfp && mfp->mf_name != (char *)NULL; mfp++) {
        RT_CK_MF(mfp);
 
-       if (BU_STR_EQUAL(mfp->mf_name, shader_name)) {
+       if (BU_STR_EQUAL(mfp->mf_name, material)) {
            bu_dlclose(handle);
            return shader_mfuncs; /* found it! */
        }
     }
 
-    if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL) bu_log("shader '%s' not found in 
library\n", shader_name);
+    if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL) bu_log("shader '%s' not found in 
library\n", material);
 
     /* found the library, but not the shader */
     bu_dlclose(handle);
@@ -131,80 +131,72 @@
 struct mfuncs *
 load_dynamic_shader(const char *material)
 {
+    char libpath[MAXPATHLEN]; /* path to shader lib */
+    char mat_fname[MAXPATHLEN]; /* {material}.{EXT}, no path */
+    char mat_libfname[MAXPATHLEN]; /* lib{material}.{EXT}, no path */
     struct mfuncs *shader_mfuncs = (struct mfuncs *)NULL;
-    char libname[MAXPATHLEN];
-    char libpath[MAXPATHLEN];
-    char *cwd = (char *)NULL;
-    int old_optical_debug = OPTICAL_DEBUG;
-    char sh_name[128]; /* XXX constants are bogus */
 
-    if (strlen(material) < sizeof(sh_name)) {
-       bu_strlcpy(sh_name, material, sizeof(sh_name));
-    } else {
+    if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL)
+       bu_log("load_dynamic_shader(\"%s\")\n", material);
+
+    if (sizeof(mat_fname) <= strlen(material)) {
        bu_log("shader name too long \"%s\" %zu > %lu\n",
-              material, strlen(material), sizeof(sh_name));
+              material, strlen(material), sizeof(mat_fname));
        return (struct mfuncs *)NULL;
     }
 
-    if (OPTICAL_DEBUG&OPTICAL_DEBUG_MATERIAL)
-       bu_log("load_dynamic_shader(\"%s\")\n", sh_name);
+    /* precalculate the potential file names, with+without lib prefix */
+    bu_dir(mat_fname, sizeof(mat_fname), material, BU_DIR_LIBEXT, NULL);
+    bu_strlcat(mat_libfname, "lib", sizeof(mat_libfname));
+    bu_strlcat(mat_libfname, mat_fname, sizeof(mat_libfname));
 
-    cwd = bu_getcwd((char *)NULL, (size_t)MAXPATHLEN);
-
-    /* Look in the current working directory for {sh_name}.so */
-    snprintf(libname, sizeof(libname), "%s/%s.so", cwd, sh_name);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    /* Look in current working directory for {material}.so */
+    bu_dir(libpath, sizeof(libpath), BU_DIR_CURR, mat_fname, NULL);
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
-    /* Look in the current working directory for lib{sh_name}.so */
-    snprintf(libname, sizeof(libname), "%s/lib%s.so", cwd, sh_name);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    /* Look in current working directory for lib{material}.so */
+    bu_dir(libpath, sizeof(libpath), BU_DIR_CURR, mat_libfname, NULL);
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
-    /* Don't specify a path in order to look in the location indicated
-     * by ld (e.g., $LD_LIBRARY_PATH) for {sh_name}.so
+    /* Don't specify a path in order to look in system locations
+     * indicated by ld (e.g., $LD_LIBRARY_PATH) for {material}.so
      */
-    snprintf(libname, sizeof(libname), "%s.so", sh_name);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    bu_strlcpy(libpath, mat_fname, sizeof(libpath));
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
-    /* Don't specify a path in order to look in the location indicated
-     * by ld (e.g., $LD_LIBRARY_PATH) for lib{sh_name}.so
+    /* Don't specify a path in order to look in the system locations
+     * indicated by ld (e.g., $LD_LIBRARY_PATH) for lib{material}.so
      */
-    snprintf(libname, sizeof(libname), "lib%s.so", sh_name);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    bu_strlcpy(libpath, mat_libfname, sizeof(libpath));
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
-    /* Look in BRL-CAD install dir under lib dir for {sh_name}.so */
-    bu_dir(libpath, sizeof(libpath), BU_DIR_LIB, sh_name, BU_DIR_LIBEXT, NULL);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    /* Look in installation library path for {material}.so */
+    bu_dir(libpath, sizeof(libpath), BU_DIR_LIB, mat_fname, NULL);
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
-    /* Look in BRL-CAD install dir under lib dir for lib{sh_name}.so */
-    snprintf(libpath, sizeof(libpath), "lib%s.so", sh_name);
-    bu_dir(libpath, sizeof(libpath), BU_DIR_LIB, libpath, BU_DIR_LIBEXT, NULL);
-    shader_mfuncs = try_load(libname, material, sh_name);
+    /* Look in installation library path for lib{material}.so */
+    bu_dir(libpath, sizeof(libpath), BU_DIR_LIB, mat_libfname, NULL);
+    shader_mfuncs = try_load(libpath, material);
     if (shader_mfuncs)
        goto done;
 
 done:
-    /* clean up memory allocated */
-    if (cwd)
-       free(cwd);
-
     /* print appropriate log messages */
     if (shader_mfuncs)
-       bu_log("loaded from %s\n", libname);
+       bu_log("loaded shader [%s] from %s\n", material, libpath);
     else
-       bu_log("WARNING: shader [%s] not found\n", sh_name);
+       bu_log("WARNING: shader [%s] not found\n", material);
 
-    optical_debug = old_optical_debug;
-
     return shader_mfuncs;
 }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to