Revision: 77533
http://sourceforge.net/p/brlcad/code/77533
Author: brlcad
Date: 2020-10-22 04:26:45 +0000 (Thu, 22 Oct 2020)
Log Message:
-----------
having every potential access point call init is super brittle. void pointer
is no good too. instead, wrap the command map in a function and make it do the
init check for all. that said, we shouldn't be initializing like this.
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 03:59:47 UTC (rev 77532)
+++ brlcad/trunk/src/libged/exec.cpp 2020-10-22 04:26:45 UTC (rev 77533)
@@ -33,6 +33,7 @@
#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
@@ -48,30 +49,13 @@
start = bu_gettime();
}
- // 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;
+ // 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();
- // 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 03:59:47 UTC (rev
77532)
+++ brlcad/trunk/src/libged/ged_init.cpp 2020-10-22 04:26:45 UTC (rev
77533)
@@ -43,23 +43,44 @@
#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()
+ged_init_msgs(void)
{
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. */
@@ -71,19 +92,12 @@
}
int cmd_invalid = 1;
- // 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();
- }
+ /* make sure plugins are loaded */
+ std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
std::string scmd(cmd);
- std::map<std::string, const struct ged_cmd *>::iterator cmd_it =
cmd_map.find(scmd);
- if (cmd_it != cmd_map.end()) {
+ std::map<std::string, const struct ged_cmd *>::iterator cmd_it =
cmap.find(scmd);
+ if (cmd_it != cmap.end()) {
cmd_invalid = 0;
}
if (cmd_invalid) {
@@ -92,8 +106,8 @@
if (func) {
ged_func_ptr c1 = cmd_it->second->i->cmd;
- std::map<std::string, const struct ged_cmd *>::iterator func_it =
cmd_map.find(std::string(func));
- if (func_it == cmd_map.end()) {
+ std::map<std::string, const struct ged_cmd *>::iterator func_it =
cmap.find(std::string(func));
+ if (func_it == cmap.end()) {
// func not in table, nothing to validate against - return invalid
return 1;
}
@@ -120,20 +134,13 @@
}
unsigned long min_dist = LONG_MAX;
- // 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();
- }
+ /* make sure plugins are loaded */
+ std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
const char *ccmd = NULL;
std::string scmd(cmd);
std::map<std::string, const struct ged_cmd *>::iterator cmd_it;
- for (cmd_it = cmd_map.begin(); cmd_it != cmd_map.end(); cmd_it++) {
+ for (cmd_it = cmap.begin(); cmd_it != cmap.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();
@@ -153,9 +160,12 @@
bu_argv_free(cmd_list_len, (char **)cmd_list);
cmd_list_len = 0;
}
- cmd_list = (char **)bu_calloc(cmd_map.size(), sizeof(char *), "ged cmd
argv");
+
+ std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
+
+ cmd_list = (char **)bu_calloc(cmap.size(), sizeof(char *), "ged cmd argv");
std::map<std::string, const struct ged_cmd *>::iterator m_it;
- for (m_it = cmd_map.begin(); m_it != cmd_map.end(); m_it++) {
+ for (m_it = cmap.begin(); m_it != cmap.end(); m_it++) {
const char *str = m_it->first.c_str();
cmd_list[cmd_list_len] = bu_strdup(str);
cmd_list_len++;
@@ -228,25 +238,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 (cmd_map.find(key) != cmd_map.end()) {
+ if (cmap.find(key) != cmap.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;
}
- cmd_map[key] = cmd;
+ cmap[key] = cmd;
// MGED calls many of these commands with an _mged_ prefix -
allow for that
std::string mged_key = std::string("_mged_") + key;
- cmd_map[mged_key] = cmd;
+ cmap[mged_key] = cmd;
}
cmd_funcs.insert(dl_handle);
}
}
-
- ged_cmds = (void *)&cmd_map;
}
@@ -253,7 +263,9 @@
static void
libged_clear(void)
{
- cmd_map.clear();
+ std::map<std::string, const struct ged_cmd *>& cmap = ged_cmd_map();
+ cmap.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 03:59:47 UTC (rev
77532)
+++ brlcad/trunk/src/libged/include/plugin.h 2020-10-22 04:26:45 UTC (rev
77533)
@@ -28,7 +28,6 @@
#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