1
0

plugin.c 5.2 KB

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