plugin.c 5.8 KB

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