Revision: 77536
          http://sourceforge.net/p/brlcad/code/77536
Author:   starseeker
Date:     2020-10-22 14:23:10 +0000 (Thu, 22 Oct 2020)
Log Message:
-----------
Revert r77533 - breaks the build on Linux. asc2g segfaults trying to build the 
.g examples.

Modified Paths:
--------------
    brlcad/trunk/src/libged/exec.cpp
    brlcad/trunk/src/libged/ged_init.cpp
    brlcad/trunk/src/libged/include/plugin.h

Modified: brlcad/trunk/src/libged/exec.cpp
===================================================================
--- brlcad/trunk/src/libged/exec.cpp    2020-10-22 12:26:42 UTC (rev 77535)
+++ brlcad/trunk/src/libged/exec.cpp    2020-10-22 14:23:10 UTC (rev 77536)
@@ -33,7 +33,6 @@
 #include "./include/plugin.h"
 
 extern "C" void libged_init(void);
-extern std::map<std::string, const struct ged_cmd *>& ged_cmd_map(void); /* 
from ged_init.cpp */
 
 
 extern "C" int
@@ -49,13 +48,30 @@
        start = bu_gettime();
     }
 
-    // TODO - right now this is the map from the libged load, which we
-    // shouldn't be directly accessing.
-    std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
+    // TODO - right now this is the map from the libged load - should
+    // probably use this to initialize a struct ged copy when ged_init
+    // is called, so client codes can add their own commands to their
+    // gedp...
+    //
+    // The ged_cmds map should always reflect the original, vanilla
+    // state of libged's command set so we have a clean fallback
+    // available if we ever need it to fall back on/recover with.
+    std::map<std::string, const struct ged_cmd *> *cmap = 
(std::map<std::string, const struct ged_cmd *> *)ged_cmds;
 
+    // On OpenBSD, if the executable was launched in a way that
+    // requires bu_setprogname to find the BRL-CAD root directory the
+    // initial libged initialization would have failed.  If we have no
+    // ged_cmds at all this is probably what happened, so call
+    // libged_init again here.  By the time we are calling ged_exec
+    // bu_setprogname should be set and we should be ready to actually
+    // find the commands.
+    if (!cmap->size()) {
+       libged_init();
+    }
+
     std::string key(argv[0]);
-    std::map<std::string, const struct ged_cmd *>::iterator c_it = 
cmap.find(key);
-    if (c_it == cmap.end()) {
+    std::map<std::string, const struct ged_cmd *>::iterator c_it = 
cmap->find(key);
+    if (c_it == cmap->end()) {
        bu_vls_printf(gedp->ged_result_str, "unknown command: %s", argv[0]);
        return (GED_ERROR | GED_UNKNOWN);
     }

Modified: brlcad/trunk/src/libged/ged_init.cpp
===================================================================
--- brlcad/trunk/src/libged/ged_init.cpp        2020-10-22 12:26:42 UTC (rev 
77535)
+++ brlcad/trunk/src/libged/ged_init.cpp        2020-10-22 14:23:10 UTC (rev 
77536)
@@ -43,44 +43,23 @@
 
 #include "./include/plugin.h"
 
-
 static char **cmd_list = NULL;
 static size_t cmd_list_len = 0;
+static std::map<std::string, const struct ged_cmd *> cmd_map;
 static std::set<void *> cmd_funcs;
 static struct bu_vls init_msgs = BU_VLS_INIT_ZERO;
+void *ged_cmds;
 
 extern "C" void libged_init(void);
 
 
 const char *
-ged_init_msgs(void)
+ged_init_msgs()
 {
     return bu_vls_cstr(&init_msgs);
 }
 
 
-/* TODO: we shouldn't be loading all commands, otherwise there's no
- * point for this being dynamic.  ged_exec() should go through a
- * lookup interface and we just stash the function pointer.
- */
-std::map<std::string, const struct ged_cmd *>&
-ged_cmd_map(void)
-{
-    static std::map<std::string, const struct ged_cmd *> cmd_map;
-
-    // On OpenBSD, if the executable was launched in a way that requires
-    // bu_setprogname to find the BRL-CAD root directory the iniital libged
-    // initialization would have failed.  If we have no commands at all this is
-    // probably what happened, so call libged_init again here.  By the time we
-    // are calling ged_cmd_valid bu_setprogname should be set and we should be
-    // ready to actually find the commands.
-    if (!cmd_map.size()) {
-       libged_init();
-    }
-    return cmd_map;
-}
-
-
 /* If func is NULL, just see if the string has a cmd_map entry.
  * If func is defined, see if a) func and cmd have cmd_map entries and
  * b) if they both do, whether they map to the same function. */
@@ -92,12 +71,19 @@
     }
     int cmd_invalid = 1;
 
-    /* make sure plugins are loaded */
-    std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
+    // On OpenBSD, if the executable was launched in a way that requires
+    // bu_setprogname to find the BRL-CAD root directory the iniital libged
+    // initialization would have failed.  If we have no ged_cmds at all this is
+    // probably what happened, so call libged_init again here.  By the time we
+    // are calling ged_cmd_valid bu_setprogname should be set and we should be
+    // ready to actually find the commands.
+    if (!cmd_map.size()) {
+       libged_init();
+    }
 
     std::string scmd(cmd);
-    std::map<std::string, const struct ged_cmd *>::iterator cmd_it = 
cmap.find(scmd);
-    if (cmd_it != cmap.end()) {
+    std::map<std::string, const struct ged_cmd *>::iterator cmd_it = 
cmd_map.find(scmd);
+    if (cmd_it != cmd_map.end()) {
        cmd_invalid = 0;
     }
     if (cmd_invalid) {
@@ -106,8 +92,8 @@
 
     if (func) {
        ged_func_ptr c1 = cmd_it->second->i->cmd;
-       std::map<std::string, const struct ged_cmd *>::iterator func_it = 
cmap.find(std::string(func));
-       if (func_it == cmap.end()) {
+       std::map<std::string, const struct ged_cmd *>::iterator func_it = 
cmd_map.find(std::string(func));
+       if (func_it == cmd_map.end()) {
            // func not in table, nothing to validate against - return invalid
            return 1;
        }
@@ -134,13 +120,20 @@
     }
     unsigned long min_dist = LONG_MAX;
 
-    /* make sure plugins are loaded */
-    std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
+    // On OpenBSD, if the executable was launched in a way that requires
+    // bu_setprogname to find the BRL-CAD root directory the iniital libged
+    // initialization would have failed.  If we have no ged_cmds at all this is
+    // probably what happened, so call libged_init again here.  By the time we
+    // are calling ged_cmd_valid bu_setprogname should be set and we should be
+    // ready to actually find the commands.
+    if (!cmd_map.size()) {
+       libged_init();
+    }
 
     const char *ccmd = NULL;
     std::string scmd(cmd);
     std::map<std::string, const struct ged_cmd *>::iterator cmd_it;
-    for (cmd_it = cmap.begin(); cmd_it != cmap.end(); cmd_it++) {
+    for (cmd_it = cmd_map.begin(); cmd_it != cmd_map.end(); cmd_it++) {
        unsigned long edist = bu_editdist(cmd, cmd_it->first.c_str(), 0);
        if (edist < min_dist) {
            ccmd = (*cmd_it).first.c_str();
@@ -160,12 +153,9 @@
        bu_argv_free(cmd_list_len, (char **)cmd_list);
        cmd_list_len = 0;
     }
-
-    std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
-
-    cmd_list = (char **)bu_calloc(cmap.size(), sizeof(char *), "ged cmd argv");
+    cmd_list = (char **)bu_calloc(cmd_map.size(), sizeof(char *), "ged cmd 
argv");
     std::map<std::string, const struct ged_cmd *>::iterator m_it;
-    for (m_it = cmap.begin(); m_it != cmap.end(); m_it++) {
+    for (m_it = cmd_map.begin(); m_it != cmd_map.end(); m_it++) {
        const char *str = m_it->first.c_str();
        cmd_list[cmd_list_len] = bu_strdup(str);
        cmd_list_len++;
@@ -238,25 +228,25 @@
                continue;
            }
 
-           std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
-
            const struct ged_cmd **cmds = plugin->cmds;
            for (int c = 0; c < plugin->cmd_cnt; c++) {
                const struct ged_cmd *cmd = cmds[c];
                std::string key(cmd->i->cname);
-               if (cmap.find(key) != cmap.end()) {
+               if (cmd_map.find(key) != cmd_map.end()) {
                    bu_vls_printf(&init_msgs, "Warning - plugin '%s' provides 
command '%s' but that command has already been loaded, skipping\n", pfile, 
cmd->i->cname);
                    continue;
                }
-               cmap[key] = cmd;
+               cmd_map[key] = cmd;
 
                // MGED calls many of these commands with an _mged_ prefix - 
allow for that
                std::string mged_key = std::string("_mged_") + key;
-               cmap[mged_key] = cmd;
+               cmd_map[mged_key] = cmd;
            }
            cmd_funcs.insert(dl_handle);
        }
     }
+
+    ged_cmds = (void *)&cmd_map;
 }
 
 
@@ -263,9 +253,7 @@
 static void
 libged_clear(void)
 {
-    std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
-    cmap.clear();
-
+    cmd_map.clear();
     std::set<void *>::iterator h_it;
     for (h_it = cmd_funcs.begin(); h_it != cmd_funcs.end(); h_it++) {
        void *handle = *h_it;

Modified: brlcad/trunk/src/libged/include/plugin.h
===================================================================
--- brlcad/trunk/src/libged/include/plugin.h    2020-10-22 12:26:42 UTC (rev 
77535)
+++ brlcad/trunk/src/libged/include/plugin.h    2020-10-22 14:23:10 UTC (rev 
77536)
@@ -28,6 +28,7 @@
 
 #define GED_API (2*1000000 + (BRLCAD_VERSION_MAJOR*10000) + 
(BRLCAD_VERSION_MINOR*100) + BRLCAD_VERSION_PATCH)
 
+extern void *ged_cmds;
 
 /* Default command behaviors when it comes to impacts on calling applications.
  * Need callback hooks in gedp so the application can tell the command what it

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