1
0

plugin.c 4.3 KB

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