123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- /*
- * plugin.c
- */
- #include "private.h"
- #include "lub/string.h"
- #include "lub/list.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
- #include <dlfcn.h>
- /**********************************************************
- * SYM functions *
- **********************************************************/
- /*--------------------------------------------------------- */
- int clish_sym_compare(const void *first, const void *second)
- {
- const clish_sym_t *f = (const clish_sym_t *)first;
- const clish_sym_t *s = (const clish_sym_t *)second;
- return strcmp(f->name, s->name);
- }
- /*--------------------------------------------------------- */
- clish_sym_t *clish_sym_new(const char *name, void *func, int type)
- {
- clish_sym_t *this;
- this = malloc(sizeof(*this));
- this->name = lub_string_dup(name);
- this->func = func;
- this->type = type;
- this->permanent = BOOL_FALSE;
- return this;
- }
- /*--------------------------------------------------------- */
- void clish_sym_free(clish_sym_t *this)
- {
- if (!this)
- return;
- lub_string_free(this->name);
- free(this);
- }
- /*--------------------------------------------------------- */
- void clish_sym__set_func(clish_sym_t *this, void *func)
- {
- this->func = func;
- }
- /*--------------------------------------------------------- */
- clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *this)
- {
- return this->func;
- }
- /*--------------------------------------------------------- */
- void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
- {
- this->permanent = permanent;
- }
- /*--------------------------------------------------------- */
- bool_t clish_sym__get_permanent(clish_sym_t *this)
- {
- return this->permanent;
- }
- /*--------------------------------------------------------- */
- void clish_sym__set_name(clish_sym_t *this, const char *name)
- {
- lub_string_free(this->name);
- this->name = lub_string_dup(name);
- }
- /*--------------------------------------------------------- */
- char *clish_sym__get_name(clish_sym_t *this)
- {
- return this->name;
- }
- /*--------------------------------------------------------- */
- void clish_sym__set_plugin(clish_sym_t *this, clish_plugin_t *plugin)
- {
- this->plugin = plugin;
- }
- /*--------------------------------------------------------- */
- clish_plugin_t *clish_sym__get_plugin(clish_sym_t *this)
- {
- return this->plugin;
- }
- /*--------------------------------------------------------- */
- void clish_sym__set_type(clish_sym_t *this, int type)
- {
- this->type = type;
- }
- /*--------------------------------------------------------- */
- int clish_sym__get_type(clish_sym_t *this)
- {
- return this->type;
- }
- /*--------------------------------------------------------- */
- int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
- {
- char *name;
- if (!dst || !src)
- return -1;
- name = dst->name;
- *dst = *src;
- dst->name = name;
- return 0;
- }
- /**********************************************************
- * PLUGIN functions *
- **********************************************************/
- /*--------------------------------------------------------- */
- clish_plugin_t *clish_plugin_new(const char *name, const char *file)
- {
- clish_plugin_t *this;
- this = malloc(sizeof(*this));
- if (file)
- this->file = lub_string_dup(file);
- else
- this->file = NULL; /* For main program binary */
- if (name)
- this->name = lub_string_dup(name);
- else
- this->name = NULL;
- this->dlhan = NULL;
- /* Initialise the list of symbols */
- this->syms = lub_list_new(clish_sym_compare);
- return this;
- }
- /*--------------------------------------------------------- */
- void clish_plugin_free(clish_plugin_t *this)
- {
- lub_list_node_t *iter;
- if (!this)
- return;
- lub_string_free(this->file);
- lub_string_free(this->name);
- /* Free symbol list */
- while ((iter = lub_list__get_head(this->syms))) {
- /* Remove the symbol from the list */
- lub_list_del(this->syms, iter);
- /* Free the instance */
- clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
- lub_list_node_free(iter);
- }
- lub_list_free(this->syms);
- if (this->dlhan)
- dlclose(this->dlhan);
- free(this);
- }
- /*--------------------------------------------------------- */
- clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
- void *func, const char *name, int type)
- {
- clish_sym_t *sym;
- if (!name || !func)
- return NULL;
- if (!(sym = clish_sym_new(name, func, type)))
- return NULL;
- clish_sym__set_plugin(sym, this);
- lub_list_add(this->syms, sym);
- return sym;
- }
- /*--------------------------------------------------------- */
- clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
- clish_plugin_fn_t *func, const char *name)
- {
- return clish_plugin_add_generic(this, func,
- name, CLISH_SYM_TYPE_FN);
- }
- /*--------------------------------------------------------- */
- /* Add permanent symbol (can't be turned off by dry-run) */
- clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
- clish_plugin_fn_t *func, const char *name)
- {
- clish_sym_t *sym;
- if (!(sym = clish_plugin_add_sym(this, func, name)))
- return NULL;
- clish_sym__set_permanent(sym, BOOL_TRUE);
- return sym;
- }
- /*--------------------------------------------------------- */
- clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
- {
- lub_list_node_t *iter;
- clish_sym_t *sym;
- /* Iterate elements */
- for(iter = lub_list__get_head(this->syms);
- iter; iter = lub_list_node__get_next(iter)) {
- int res;
- sym = (clish_sym_t *)lub_list_node__get_data(iter);
- res = strcmp(clish_sym__get_name(sym), name);
- if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
- return sym;
- if (res > 0) /* No chances to find name */
- break;
- }
- return NULL;
- }
- /*--------------------------------------------------------- */
- void *clish_plugin_load(clish_plugin_t *this)
- {
- clish_plugin_init_t *plugin_init;
- if (!this)
- return NULL;
- if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_GLOBAL)))
- return NULL;
- plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
- if (!plugin_init) {
- dlclose(this->dlhan);
- this->dlhan = NULL;
- return NULL;
- }
- plugin_init(this);
- return this->dlhan;
- }
- /*--------------------------------------------------------- */
- char *clish_plugin__get_name(const clish_plugin_t *this)
- {
- return this->name;
- }
- /*--------------------------------------------------------- */
- char *clish_plugin__get_file(const clish_plugin_t *this)
- {
- return this->file;
- }
- /*--------------------------------------------------------- */
|