plugin.c 3.6 KB

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