1
0

plugin.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. return this;
  30. }
  31. /*--------------------------------------------------------- */
  32. void clish_sym_free(clish_sym_t *this)
  33. {
  34. if (!this)
  35. return;
  36. lub_string_free(this->name);
  37. free(this);
  38. }
  39. /*--------------------------------------------------------- */
  40. void clish_sym__set_func(clish_sym_t *this, clish_plugin_fn_t *func)
  41. {
  42. this->func = func;
  43. }
  44. /*--------------------------------------------------------- */
  45. clish_plugin_fn_t *clish_sym__get_func(clish_sym_t *this)
  46. {
  47. return this->func;
  48. }
  49. /*--------------------------------------------------------- */
  50. void clish_sym__set_name(clish_sym_t *this, const char *name)
  51. {
  52. lub_string_free(this->name);
  53. this->name = lub_string_dup(name);
  54. }
  55. /*--------------------------------------------------------- */
  56. char *clish_sym__get_name(clish_sym_t *this)
  57. {
  58. return this->name;
  59. }
  60. /**********************************************************
  61. * PLUGIN functions *
  62. **********************************************************/
  63. /*--------------------------------------------------------- */
  64. clish_plugin_t *clish_plugin_new(const char *name, const char *file)
  65. {
  66. clish_plugin_t *this;
  67. this = malloc(sizeof(*this));
  68. if (file)
  69. this->file = lub_string_dup(file);
  70. else
  71. this->file = NULL; /* For main program binary */
  72. if (name)
  73. this->name = lub_string_dup(name);
  74. else
  75. this->name = NULL;
  76. this->dlhan = NULL;
  77. /* Initialise the list of symbols */
  78. this->syms = lub_list_new(clish_sym_compare);
  79. return this;
  80. }
  81. /*--------------------------------------------------------- */
  82. void clish_plugin_free(clish_plugin_t *this)
  83. {
  84. lub_list_node_t *iter;
  85. if (!this)
  86. return;
  87. lub_string_free(this->file);
  88. lub_string_free(this->name);
  89. /* Free symbol list */
  90. while ((iter = lub_list__get_head(this->syms))) {
  91. /* Remove the symbol from the list */
  92. lub_list_del(this->syms, iter);
  93. /* Free the instance */
  94. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  95. lub_list_node_free(iter);
  96. }
  97. lub_list_free(this->syms);
  98. if (this->dlhan)
  99. dlclose(this->dlhan);
  100. free(this);
  101. }
  102. /*--------------------------------------------------------- */
  103. int clish_plugin_add_sym(clish_plugin_t *this,
  104. clish_plugin_fn_t *func, const char *name)
  105. {
  106. clish_sym_t *sym;
  107. if (!name || !func)
  108. return -1;
  109. if (!(sym = clish_sym_new(name, func)))
  110. return -1;
  111. lub_list_add(this->syms, sym);
  112. return 0;
  113. }
  114. /*--------------------------------------------------------- */
  115. clish_plugin_fn_t *clish_plugin_get_sym(clish_plugin_t *this, const char *name)
  116. {
  117. lub_list_node_t *iter;
  118. clish_sym_t *sym;
  119. /* Iterate elements */
  120. for(iter = lub_list__get_head(this->syms);
  121. iter; iter = lub_list_node__get_next(iter)) {
  122. int res;
  123. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  124. res = strcmp(clish_sym__get_name(sym), name);
  125. if (!res)
  126. return clish_sym__get_func(sym);
  127. if (res > 0) /* No chances to find name */
  128. break;
  129. }
  130. return NULL;
  131. }
  132. /*--------------------------------------------------------- */
  133. void *clish_plugin_load(clish_plugin_t *this)
  134. {
  135. clish_plugin_init_t *plugin_init;
  136. if (!this)
  137. return NULL;
  138. if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_GLOBAL)))
  139. return NULL;
  140. plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
  141. if (!plugin_init) {
  142. dlclose(this->dlhan);
  143. this->dlhan = NULL;
  144. return NULL;
  145. }
  146. plugin_init(this);
  147. return this->dlhan;
  148. }
  149. /*--------------------------------------------------------- */
  150. char *clish_plugin__get_name(const clish_plugin_t *this)
  151. {
  152. return this->name;
  153. }
  154. /*--------------------------------------------------------- */
  155. char *clish_plugin__get_file(const clish_plugin_t *this)
  156. {
  157. return this->file;
  158. }
  159. /*--------------------------------------------------------- */