Hello,

I'm writing an application that needs to be highly customizable and modified
on the fly (ie the application continues to run while you can unload/reload modules)

The modules should be able to attach themselves to various events and commands that will be triggered at some point by the main application..

I'm going to have separate interfaces for dynamic C libs, perl scripts and lua scripts...

As are as the code to handle each interface, that is not a problem...really I am just asking for advice as to a good way to abstract the layer above the individual module interfaces..

My current attempt is this, each of the above have simple handle objects used to access them so I came up with this simple code that uses a hash table (GNU hsearch)
to store a "module" object with the information needed to link commands (via a simple string for the key):
Code:
struct module_t {
    short int m_type;      /*REQ M_TYPE_PERL M_TYPE_LUA or M_TYPE_C */
    void * m_handle;       /*REQ handle we use to interface with the module */
    char * m_name;         /*OPT name of the module (for display purposes) */
    char * m_path;         /*REQ filename of module (mainly for loading purposes) */
};

typedef struct module_t MODULE;

static MODULE C_MODULE_INIT = {
    M_TYPE_C,
    NULL
};
static MODULE PERL_MODULE_INIT = {
    M_TYPE_PERL,
    NULL
};

...........clipped hash table functions...................


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>  /* for debugging purposes */
#include <dlfcn.h> /* for dlopen, dlsym, etc.. (C modules) */
#include <search.h> /* for GNU hash table functions */

#define M_TYPE_PERL         0x1
#define M_TYPE_C            0x2
#define M_TYPE_LUA          0x4
#define M_CMD_HASH_SIZE     1007
/**********************************************************************
 * TEST DRIVER
 **********************************************************************/
int main(void)
{
    ENTRY * ep;
    int i = 0;
    MODULE * cmod = malloc(sizeof *cmod);

    if (!cmod)
        return 1;

    *cmod = C_MODULE_INIT;

    i = m_init_cmd_lookup();
    if (i == 0 && fprintf(stderr,"%s\n",strerror(errno)));
    ep = m_cmd_insert("blah",cmod);

    ep = m_cmd_lookup("blah");
    if (ep) {
        MODULE * cast = ep->data;
        fprintf(stdout,"found %s type: %d\n",ep->key,cast->m_type);
    }

    free(cmod);

    m_destroy_cmd_lookup();
    return 0;
}
Do you think this is a good way to do it?





Another question:
Each module needs to have a simple way to attach itself to multiple commands
or events...what is a good way on the module side to do this?

Something like in the module client code: (for a C module)
Code:
Mod.bind({ .cmd = "speak", .func = cmd_speak});
I know I can accomplish this sort of thing via macros ... but it seems like a lot of potential for bugs...what other methods could I use?