plugin.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * plugin.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/list.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <assert.h>
  11. #include <dlfcn.h>
  12. /**********************************************************
  13. * SYM functions *
  14. **********************************************************/
  15. /*--------------------------------------------------------- */
  16. int clish_sym_compare(const void *first, const void *second)
  17. {
  18. const clish_sym_t *f = (const clish_sym_t *)first;
  19. const clish_sym_t *s = (const clish_sym_t *)second;
  20. return strcmp(f->name, s->name);
  21. }
  22. /*--------------------------------------------------------- */
  23. clish_sym_t *clish_sym_new(const char *name, void *func, int type)
  24. {
  25. clish_sym_t *this;
  26. this = malloc(sizeof(*this));
  27. this->name = lub_string_dup(name);
  28. this->func = func;
  29. this->type = type;
  30. this->permanent = BOOL_FALSE;
  31. return this;
  32. }
  33. /*--------------------------------------------------------- */
  34. void clish_sym_free(clish_sym_t *this)
  35. {
  36. if (!this)
  37. return;
  38. lub_string_free(this->name);
  39. free(this);
  40. }
  41. /*--------------------------------------------------------- */
  42. void clish_sym__set_func(clish_sym_t *this, void *func)
  43. {
  44. this->func = func;
  45. }
  46. /*--------------------------------------------------------- */
  47. clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *this)
  48. {
  49. return this->func;
  50. }
  51. /*--------------------------------------------------------- */
  52. void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
  53. {
  54. this->permanent = permanent;
  55. }
  56. /*--------------------------------------------------------- */
  57. bool_t clish_sym__get_permanent(clish_sym_t *this)
  58. {
  59. return this->permanent;
  60. }
  61. /*--------------------------------------------------------- */
  62. void clish_sym__set_name(clish_sym_t *this, const char *name)
  63. {
  64. lub_string_free(this->name);
  65. this->name = lub_string_dup(name);
  66. }
  67. /*--------------------------------------------------------- */
  68. char *clish_sym__get_name(clish_sym_t *this)
  69. {
  70. return this->name;
  71. }
  72. /*--------------------------------------------------------- */
  73. void clish_sym__set_plugin(clish_sym_t *this, clish_plugin_t *plugin)
  74. {
  75. this->plugin = plugin;
  76. }
  77. /*--------------------------------------------------------- */
  78. clish_plugin_t *clish_sym__get_plugin(clish_sym_t *this)
  79. {
  80. return this->plugin;
  81. }
  82. /*--------------------------------------------------------- */
  83. void clish_sym__set_type(clish_sym_t *this, int type)
  84. {
  85. this->type = type;
  86. }
  87. /*--------------------------------------------------------- */
  88. int clish_sym__get_type(clish_sym_t *this)
  89. {
  90. return this->type;
  91. }
  92. /*--------------------------------------------------------- */
  93. int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
  94. {
  95. char *name;
  96. if (!dst || !src)
  97. return -1;
  98. name = dst->name;
  99. *dst = *src;
  100. dst->name = name;
  101. return 0;
  102. }
  103. /**********************************************************
  104. * PLUGIN functions *
  105. **********************************************************/
  106. /*--------------------------------------------------------- */
  107. clish_plugin_t *clish_plugin_new(const char *name, const char *file)
  108. {
  109. clish_plugin_t *this;
  110. this = malloc(sizeof(*this));
  111. if (file)
  112. this->file = lub_string_dup(file);
  113. else
  114. this->file = NULL; /* For main program binary */
  115. if (name)
  116. this->name = lub_string_dup(name);
  117. else
  118. this->name = NULL;
  119. this->dlhan = NULL;
  120. /* Initialise the list of symbols */
  121. this->syms = lub_list_new(clish_sym_compare);
  122. return this;
  123. }
  124. /*--------------------------------------------------------- */
  125. void clish_plugin_free(clish_plugin_t *this)
  126. {
  127. lub_list_node_t *iter;
  128. if (!this)
  129. return;
  130. lub_string_free(this->file);
  131. lub_string_free(this->name);
  132. /* Free symbol list */
  133. while ((iter = lub_list__get_head(this->syms))) {
  134. /* Remove the symbol from the list */
  135. lub_list_del(this->syms, iter);
  136. /* Free the instance */
  137. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  138. lub_list_node_free(iter);
  139. }
  140. lub_list_free(this->syms);
  141. if (this->dlhan)
  142. dlclose(this->dlhan);
  143. free(this);
  144. }
  145. /*--------------------------------------------------------- */
  146. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  147. void *func, const char *name, int type)
  148. {
  149. clish_sym_t *sym;
  150. if (!name || !func)
  151. return NULL;
  152. if (!(sym = clish_sym_new(name, func, type)))
  153. return NULL;
  154. clish_sym__set_plugin(sym, this);
  155. lub_list_add(this->syms, sym);
  156. return sym;
  157. }
  158. /*--------------------------------------------------------- */
  159. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  160. clish_plugin_fn_t *func, const char *name)
  161. {
  162. return clish_plugin_add_generic(this, func,
  163. name, CLISH_SYM_TYPE_FN);
  164. }
  165. /*--------------------------------------------------------- */
  166. /* Add permanent symbol (can't be turned off by dry-run) */
  167. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  168. clish_plugin_fn_t *func, const char *name)
  169. {
  170. clish_sym_t *sym;
  171. if (!(sym = clish_plugin_add_sym(this, func, name)))
  172. return NULL;
  173. clish_sym__set_permanent(sym, BOOL_TRUE);
  174. return sym;
  175. }
  176. /*--------------------------------------------------------- */
  177. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  178. {
  179. lub_list_node_t *iter;
  180. clish_sym_t *sym;
  181. /* Iterate elements */
  182. for(iter = lub_list__get_head(this->syms);
  183. iter; iter = lub_list_node__get_next(iter)) {
  184. int res;
  185. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  186. res = strcmp(clish_sym__get_name(sym), name);
  187. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  188. return sym;
  189. if (res > 0) /* No chances to find name */
  190. break;
  191. }
  192. return NULL;
  193. }
  194. /*--------------------------------------------------------- */
  195. void *clish_plugin_load(clish_plugin_t *this)
  196. {
  197. clish_plugin_init_t *plugin_init;
  198. if (!this)
  199. return NULL;
  200. if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_GLOBAL)))
  201. return NULL;
  202. plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
  203. if (!plugin_init) {
  204. dlclose(this->dlhan);
  205. this->dlhan = NULL;
  206. return NULL;
  207. }
  208. plugin_init(this);
  209. return this->dlhan;
  210. }
  211. /*--------------------------------------------------------- */
  212. char *clish_plugin__get_name(const clish_plugin_t *this)
  213. {
  214. return this->name;
  215. }
  216. /*--------------------------------------------------------- */
  217. char *clish_plugin__get_file(const clish_plugin_t *this)
  218. {
  219. return this->file;
  220. }
  221. /*--------------------------------------------------------- */