plugin.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**********************************************************
  12. * SYM functions *
  13. **********************************************************/
  14. /*--------------------------------------------------------- */
  15. static int clish_sym_compare(const void *first, const void *second)
  16. {
  17. const clish_sym_t *f = (const clish_sym_t *)first;
  18. const clish_sym_t *s = (const clish_sym_t *)second;
  19. return strcmp(f->name, s->name);
  20. }
  21. /*--------------------------------------------------------- */
  22. static clish_sym_t *clish_sym_new(const char *name, clish_plugin_fn_t *func)
  23. {
  24. clish_sym_t *this;
  25. this = malloc(sizeof(*this));
  26. this->name = lub_string_dup(name);
  27. this->func = func;
  28. return this;
  29. }
  30. /*--------------------------------------------------------- */
  31. static void clish_sym_free(clish_sym_t *this)
  32. {
  33. if (!this)
  34. return;
  35. lub_string_free(this->name);
  36. free(this);
  37. }
  38. /**********************************************************
  39. * PLUGIN functions *
  40. **********************************************************/
  41. /*--------------------------------------------------------- */
  42. clish_plugin_t *clish_plugin_new(const char *name, const char *file)
  43. {
  44. clish_plugin_t *this;
  45. if (!file)
  46. return NULL;
  47. this = malloc(sizeof(*this));
  48. this->file = lub_string_dup(file);
  49. if (name)
  50. this->name = lub_string_dup(name);
  51. else
  52. this->name = NULL;
  53. /* Initialise the list of symbols */
  54. this->syms = lub_list_new(clish_sym_compare);
  55. return this;
  56. }
  57. /*--------------------------------------------------------- */
  58. void clish_plugin_free(clish_plugin_t *this)
  59. {
  60. lub_list_node_t *iter;
  61. if (!this)
  62. return;
  63. lub_string_free(this->file);
  64. lub_string_free(this->name);
  65. /* Free symbol list */
  66. while ((iter = lub_list__get_head(this->syms))) {
  67. /* Remove the symbol from the list */
  68. lub_list_del(this->syms, iter);
  69. /* Free the instance */
  70. clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
  71. lub_list_node_free(iter);
  72. }
  73. lub_list_free(this->syms);
  74. free(this);
  75. }
  76. /*--------------------------------------------------------- */
  77. int clish_plugin_sym(clish_plugin_t *this,
  78. clish_plugin_fn_t *func, const char *name)
  79. {
  80. clish_sym_t *sym;
  81. if (!name || !func)
  82. return -1;
  83. if (!(sym = clish_sym_new(name, func)))
  84. return -1;
  85. lub_list_add(this->syms, sym);
  86. return 0;
  87. }
  88. /*--------------------------------------------------------- */
  89. clish_plugin_fn_t *clish_plugin_dlsym(clish_plugin_t *this, const char *name)
  90. {
  91. lub_list_node_t *iter;
  92. clish_sym_t *sym;
  93. /* Iterate elements */
  94. for(iter = lub_list__get_head(this->syms);
  95. iter; iter = lub_list_node__get_next(iter)) {
  96. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  97. }
  98. return NULL;
  99. }
  100. /*--------------------------------------------------------- */
  101. int clish_plugin_load(clish_plugin_t *this)
  102. {
  103. return 0;
  104. }
  105. /*--------------------------------------------------------- */