#include <dlfcn.h>
#define ACMD(name) \
void (name)(struct char_data *ch, char *argument, int cmd, int subcmd)
typedef void (*acmd)(struct char_data *ch, char *argument, int cmd, int subcmd);
acmd command = (acmd) dlsym(0, "do_look");
if(command)
(*command) (ch, line, cmd, cmd_info[cmd].subcmd);
[Makefile]
$(BINDIR)/circle : $(OBJFILES)
$(CC) -o $(BINDIR)/circle $(PROFILE) $(OBJFILES) $(LIBS)
$(CC) -shared -o $(BINDIR)/circle.so $(PROFILE) $(OBJFILES) $(LIBS)
[Binding]
circle_lib = dlopen("circle.so", RTLD_LAZY);
[Lookup]
acmd command = (acmd) dlsym(circle_lib, "do_look");
/* Definition: */
typedef void* (*voidy)();
#define CALL(name, …)\
{ voidy ff;\
*(void**)(&ff) = dlsym(circle_lib, name);\
if (!ff) *(void**)(&ff) = dlsym(0, name);\
if (ff) ff(__VA_ARGS__); \
else log_printf("ERROR: Function %s not found in either lib or code.", name); }\
/* Examples: */
if (d->connected != CON_PLAYING) {
/* nanny(d, comm) */
CALL("nanny", d, comm);
}
…
if (!(pulse % PULSE_MOBILE)) {
/* mobile_activity() */
CALL("mobile_activity");
}
/* Definition: */
typedef int (*inty)();
#define RCALL(ret, name, …)\
{ inty ff;\
*(void**)(&ff) = dlsym(circle_lib, name);\
if (!ff) *(void**)(&ff) = dlsym(0, name);\
if (ff) ret = ff(__VA_ARGS__); \
else log_printf("ERROR: Function %s not found in either lib or code.", name); }\
/* Example: */
int r = 0;
if (!IS_NPC(ch)) {
if (GET_MOB_SHOP(k))
RCALL(r, "shop_keeper", ch, k, cmd, arg, SPEC_COMMAND);
if (!r && GET_MOB_QUEST(k))
RCALL(r, "questmaster", ch, k, cmd, arg, SPEC_COMMAND);
}
if (r) return r;
Has anyone used the symbol table in order to find and execute a function by its name? Reason I'm asking is I'd really like to house as many of the tables as I can inside a MySQL database, including stuff like the command interpreter structure and the special procedures assignment instructions, which link to functions.
In my research, using the symbol table seems to come up as the easiest (maybe only) way to get a function pointer via name, but I haven't come across a specific example of the kind I'll need. Was just curious if someone has gone down this path already and can let me know if it's worth pursuing further. Even better if you have some prt–porter code to share :)
http://www.gnu.org/software/libc/manual/...