plugin.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. void *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 *file, const char *alias)
  108. {
  109. clish_plugin_t *this;
  110. this = malloc(sizeof(*this));
  111. this->file = lub_string_dup(file);
  112. this->name = NULL;
  113. this->conf = NULL;
  114. this->alias = lub_string_dup(alias);
  115. this->dlhan = NULL;
  116. /* Initialise the list of symbols */
  117. this->syms = lub_list_new(clish_sym_compare);
  118. /* Constructor and destructor */
  119. this->init = NULL;
  120. this->fini = NULL;
  121. return this;
  122. }
  123. /*--------------------------------------------------------- */
  124. void clish_plugin_free(clish_plugin_t *this, void *userdata)
  125. {
  126. lub_list_node_t *iter;
  127. if (!this)
  128. return;
  129. /* Execute destructor */
  130. if (this->fini)
  131. this->fini(userdata, this);
  132. lub_string_free(this->file);
  133. lub_string_free(this->name);
  134. lub_string_free(this->alias);
  135. lub_string_free(this->conf);
  136. /* Free symbol list */
  137. while ((iter = lub_list__get_head(this->syms))) {
  138. /* Remove the symbol from the list */
  139. lub_list_del(this->syms, iter);
  140. /* Free the instance */
  141. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  142. lub_list_node_free(iter);
  143. }
  144. lub_list_free(this->syms);
  145. if (this->dlhan)
  146. dlclose(this->dlhan);
  147. free(this);
  148. }
  149. /*--------------------------------------------------------- */
  150. clish_sym_t *clish_plugin_add_generic(clish_plugin_t *this,
  151. void *func, const char *name, int type, bool_t permanent)
  152. {
  153. clish_sym_t *sym;
  154. if (!name || !func)
  155. return NULL;
  156. if (!(sym = clish_sym_new(name, func, type)))
  157. return NULL;
  158. clish_sym__set_plugin(sym, this);
  159. clish_sym__set_permanent(sym, permanent);
  160. lub_list_add(this->syms, sym);
  161. return sym;
  162. }
  163. /*--------------------------------------------------------- */
  164. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  165. clish_hook_action_fn_t *func, const char *name)
  166. {
  167. return clish_plugin_add_generic(this, func,
  168. name, CLISH_SYM_TYPE_ACTION, BOOL_FALSE);
  169. }
  170. /*--------------------------------------------------------- */
  171. /* Add permanent symbol (can't be turned off by dry-run) */
  172. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  173. clish_hook_action_fn_t *func, const char *name)
  174. {
  175. return clish_plugin_add_generic(this, func,
  176. name, CLISH_SYM_TYPE_ACTION, BOOL_TRUE);
  177. }
  178. /*--------------------------------------------------------- */
  179. clish_sym_t *clish_plugin_add_hook(clish_plugin_t *this,
  180. void *func, const char *name, int type)
  181. {
  182. return clish_plugin_add_generic(this, func,
  183. name, type, BOOL_FALSE);
  184. }
  185. /*--------------------------------------------------------- */
  186. clish_sym_t *clish_plugin_add_phook(clish_plugin_t *this,
  187. void *func, const char *name, int type)
  188. {
  189. return clish_plugin_add_generic(this, func,
  190. name, type, BOOL_TRUE);
  191. }
  192. /*--------------------------------------------------------- */
  193. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name, int type)
  194. {
  195. lub_list_node_t *iter;
  196. clish_sym_t *sym;
  197. /* Iterate elements */
  198. for(iter = lub_list__get_head(this->syms);
  199. iter; iter = lub_list_node__get_next(iter)) {
  200. int res;
  201. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  202. res = strcmp(clish_sym__get_name(sym), name);
  203. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  204. return sym;
  205. if (res > 0) /* No chances to find name */
  206. break;
  207. }
  208. return NULL;
  209. }
  210. /*--------------------------------------------------------- */
  211. int clish_plugin_load(clish_plugin_t *this, void *userdata)
  212. {
  213. int res;
  214. if (!this)
  215. return -1;
  216. /* Open dynamic library */
  217. if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_LOCAL))) {
  218. fprintf(stderr, "Error: Can't open plugin %s: %s\n",
  219. this->file, dlerror());
  220. return -1;
  221. }
  222. /* Get plugin init function */
  223. this->init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
  224. if (!this->init) {
  225. fprintf(stderr, "Error: Can't get plugin %s init function: %s\n",
  226. this->file, dlerror());
  227. return -1;
  228. }
  229. /* Get plugin fini function */
  230. this->fini = (clish_plugin_fini_t *)dlsym(this->dlhan, CLISH_PLUGIN_FINI_NAME);
  231. /* Execute init function */
  232. if ((res = this->init(userdata, this)))
  233. fprintf(stderr, "Error: Plugin %s init retcode: %d\n",
  234. this->file, res);
  235. return res;
  236. }
  237. /*--------------------------------------------------------- */
  238. void clish_plugin__set_name(clish_plugin_t *this, const char *name)
  239. {
  240. lub_string_free(this->name);
  241. this->name = lub_string_dup(name);
  242. }
  243. /*--------------------------------------------------------- */
  244. char *clish_plugin__get_name(const clish_plugin_t *this)
  245. {
  246. return this->name;
  247. }
  248. /*--------------------------------------------------------- */
  249. void clish_plugin__set_alias(clish_plugin_t *this, const char *alias)
  250. {
  251. lub_string_free(this->alias);
  252. this->alias = lub_string_dup(alias);
  253. }
  254. /*--------------------------------------------------------- */
  255. char *clish_plugin__get_alias(const clish_plugin_t *this)
  256. {
  257. return this->alias;
  258. }
  259. /*--------------------------------------------------------- */
  260. char *clish_plugin__get_pubname(const clish_plugin_t *this)
  261. {
  262. return (this->alias ? this->alias : this->name);
  263. }
  264. /*--------------------------------------------------------- */
  265. char *clish_plugin__get_file(const clish_plugin_t *this)
  266. {
  267. return this->file;
  268. }
  269. /*--------------------------------------------------------- */
  270. void clish_plugin__set_conf(clish_plugin_t *this, const char *conf)
  271. {
  272. lub_string_free(this->conf);
  273. this->conf = lub_string_dup(conf);
  274. }
  275. /*--------------------------------------------------------- */
  276. char *clish_plugin__get_conf(const clish_plugin_t *this)
  277. {
  278. return this->conf;
  279. }
  280. /*--------------------------------------------------------- */