plugin.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * PLUGIN functions *
  83. **********************************************************/
  84. /*--------------------------------------------------------- */
  85. clish_plugin_t *clish_plugin_new(const char *name, const char *file)
  86. {
  87. clish_plugin_t *this;
  88. this = malloc(sizeof(*this));
  89. if (file)
  90. this->file = lub_string_dup(file);
  91. else
  92. this->file = NULL; /* For main program binary */
  93. if (name)
  94. this->name = lub_string_dup(name);
  95. else
  96. this->name = NULL;
  97. this->dlhan = NULL;
  98. /* Initialise the list of symbols */
  99. this->syms = lub_list_new(clish_sym_compare);
  100. return this;
  101. }
  102. /*--------------------------------------------------------- */
  103. void clish_plugin_free(clish_plugin_t *this)
  104. {
  105. lub_list_node_t *iter;
  106. if (!this)
  107. return;
  108. lub_string_free(this->file);
  109. lub_string_free(this->name);
  110. /* Free symbol list */
  111. while ((iter = lub_list__get_head(this->syms))) {
  112. /* Remove the symbol from the list */
  113. lub_list_del(this->syms, iter);
  114. /* Free the instance */
  115. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  116. lub_list_node_free(iter);
  117. }
  118. lub_list_free(this->syms);
  119. if (this->dlhan)
  120. dlclose(this->dlhan);
  121. free(this);
  122. }
  123. /*--------------------------------------------------------- */
  124. clish_sym_t *clish_plugin_add_sym(clish_plugin_t *this,
  125. clish_plugin_fn_t *func, const char *name)
  126. {
  127. clish_sym_t *sym;
  128. if (!name || !func)
  129. return NULL;
  130. if (!(sym = clish_sym_new(name, func)))
  131. return NULL;
  132. clish_sym__set_plugin(sym, this);
  133. lub_list_add(this->syms, sym);
  134. return sym;
  135. }
  136. /*--------------------------------------------------------- */
  137. /* Add permanent symbol (can't be turned off by dry-run) */
  138. clish_sym_t *clish_plugin_add_psym(clish_plugin_t *this,
  139. clish_plugin_fn_t *func, const char *name)
  140. {
  141. clish_sym_t *sym;
  142. if (!(sym = clish_plugin_add_sym(this, func, name)))
  143. return NULL;
  144. clish_sym__set_permanent(sym, BOOL_TRUE);
  145. return sym;
  146. }
  147. /*--------------------------------------------------------- */
  148. clish_sym_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name)
  149. {
  150. lub_list_node_t *iter;
  151. clish_sym_t *sym;
  152. /* Iterate elements */
  153. for(iter = lub_list__get_head(this->syms);
  154. iter; iter = lub_list_node__get_next(iter)) {
  155. int res;
  156. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  157. res = strcmp(clish_sym__get_name(sym), name);
  158. if (!res)
  159. return sym;
  160. if (res > 0) /* No chances to find name */
  161. break;
  162. }
  163. return NULL;
  164. }
  165. /*--------------------------------------------------------- */
  166. void *clish_plugin_load(clish_plugin_t *this)
  167. {
  168. clish_plugin_init_t *plugin_init;
  169. if (!this)
  170. return NULL;
  171. if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_GLOBAL)))
  172. return NULL;
  173. plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
  174. if (!plugin_init) {
  175. dlclose(this->dlhan);
  176. this->dlhan = NULL;
  177. return NULL;
  178. }
  179. plugin_init(this);
  180. return this->dlhan;
  181. }
  182. /*--------------------------------------------------------- */
  183. char *clish_plugin__get_name(const clish_plugin_t *this)
  184. {
  185. return this->name;
  186. }
  187. /*--------------------------------------------------------- */
  188. char *clish_plugin__get_file(const clish_plugin_t *this)
  189. {
  190. return this->file;
  191. }
  192. /*--------------------------------------------------------- */