plugin.c 4.1 KB

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