shell_plugin.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * shell_plugin.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <dlfcn.h>
  8. #include "lub/string.h"
  9. #include "lub/list.h"
  10. #include "lub/bintree.h"
  11. #include "clish/plugin.h"
  12. #include "clish/view.h"
  13. /*----------------------------------------------------------------------- */
  14. clish_plugin_t * clish_shell_find_plugin(clish_shell_t *this, const char *name)
  15. {
  16. lub_list_node_t *iter;
  17. clish_plugin_t *plugin;
  18. assert(this);
  19. if (!name || !name[0])
  20. return NULL;
  21. /* Iterate elements */
  22. for(iter = lub_list__get_head(this->plugins);
  23. iter; iter = lub_list_node__get_next(iter)) {
  24. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  25. if (!strcmp(name, clish_plugin__get_name(plugin)))
  26. return plugin;
  27. }
  28. return NULL;
  29. }
  30. /*----------------------------------------------------------------------- */
  31. clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *this,
  32. const char *name)
  33. {
  34. clish_plugin_t *plugin;
  35. assert(this);
  36. if (!name || !name[0])
  37. return NULL;
  38. plugin = clish_shell_find_plugin(this, name);
  39. if (plugin)
  40. return plugin;
  41. plugin = clish_plugin_new(name);
  42. lub_list_add(this->plugins, plugin);
  43. return plugin;
  44. }
  45. /*----------------------------------------------------------------------- */
  46. /* For all plugins:
  47. * * dlopen(plugin)
  48. * * dlsym(initialize function)
  49. * * exec init functions to get all plugin syms
  50. */
  51. int clish_shell_load_plugins(clish_shell_t *this)
  52. {
  53. lub_list_node_t *iter;
  54. clish_plugin_t *plugin;
  55. assert(this);
  56. /* Iterate elements */
  57. for(iter = lub_list__get_head(this->plugins);
  58. iter; iter = lub_list_node__get_next(iter)) {
  59. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  60. if (clish_plugin_load(plugin, (void *)this))
  61. return -1;
  62. #ifdef DEBUG
  63. clish_plugin_dump(plugin);
  64. #endif
  65. }
  66. return 0;
  67. }
  68. /*----------------------------------------------------------------------- */
  69. /* Iterate plugins to find symbol by name.
  70. * The symbol name can be simple or with namespace:
  71. * mysym@plugin1
  72. * The symbols with suffix will be resolved using specified plugin only.
  73. */
  74. static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name, int type)
  75. {
  76. lub_list_node_t *iter;
  77. clish_plugin_t *plugin;
  78. clish_sym_t *sym = NULL;
  79. /* To parse command name */
  80. char *saveptr = NULL;
  81. const char *delim = "@";
  82. char *plugin_name = NULL;
  83. char *cmdn = NULL;
  84. char *str = lub_string_dup(name);
  85. assert(this);
  86. /* Parse name to get sym name and optional plugin name */
  87. cmdn = strtok_r(str, delim, &saveptr);
  88. if (!cmdn) {
  89. lub_string_free(str);
  90. return NULL;
  91. }
  92. plugin_name = strtok_r(NULL, delim, &saveptr);
  93. if (plugin_name) {
  94. /* Search for symbol in specified namespace */
  95. /* Iterate elements */
  96. for(iter = lub_list__get_head(this->plugins);
  97. iter; iter = lub_list_node__get_next(iter)) {
  98. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  99. if (strcmp(clish_plugin__get_pubname(plugin), plugin_name))
  100. continue;
  101. if ((sym = clish_plugin_get_sym(plugin, cmdn, type)))
  102. break;
  103. }
  104. } else {
  105. /* Iterate all plugins */
  106. for(iter = lub_list__get_head(this->plugins);
  107. iter; iter = lub_list_node__get_next(iter)) {
  108. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  109. if ((sym = clish_plugin_get_sym(plugin, cmdn, type)))
  110. break;
  111. }
  112. }
  113. lub_string_free(str);
  114. return sym;
  115. }
  116. /*--------------------------------------------------------- */
  117. /* Find symbol by name in the list of unresolved symbols */
  118. clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name, int type)
  119. {
  120. lub_list_node_t *iter;
  121. clish_sym_t *sym;
  122. /* Iterate elements */
  123. for(iter = lub_list__get_head(this->syms);
  124. iter; iter = lub_list_node__get_next(iter)) {
  125. int res;
  126. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  127. res = strcmp(clish_sym__get_name(sym), name);
  128. if (!res && ((CLISH_SYM_TYPE_NONE == type) || (clish_sym__get_type(sym) == type)))
  129. return sym;
  130. if (res > 0) /* No chance to find name */
  131. break;
  132. }
  133. return NULL;
  134. }
  135. /*----------------------------------------------------------------------- */
  136. /* Add symbol to the table of unresolved symbols */
  137. clish_sym_t *clish_shell_add_sym(clish_shell_t *this,
  138. void *func, const char *name, int type)
  139. {
  140. clish_sym_t *sym = NULL;
  141. if (!name)
  142. return NULL;
  143. if ((sym = clish_shell_find_sym(this, name, type)))
  144. return sym;
  145. if (!(sym = clish_sym_new(name, func, type)))
  146. return NULL;
  147. lub_list_add(this->syms, sym);
  148. return sym;
  149. }
  150. /*----------------------------------------------------------------------- */
  151. clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *this,
  152. const char *name, int type)
  153. {
  154. return clish_shell_add_sym(this, NULL, name, type);
  155. }
  156. /*----------------------------------------------------------------------- */
  157. /* Link one unresolved symbol.
  158. * sym - unresolved symbol
  159. * Returns 0 if the symbol was succesfully resolved
  160. */
  161. static int link_unresolved_sym(clish_shell_t *this, clish_sym_t *sym)
  162. {
  163. clish_sym_t *plugin_sym;
  164. char *sym_name = NULL;
  165. int sym_type;
  166. if (clish_sym__get_func(sym)) /* Don't relink non-null fn */
  167. return 0;
  168. sym_name = clish_sym__get_name(sym);
  169. sym_type = clish_sym__get_type(sym);
  170. plugin_sym = plugins_find_sym(this, sym_name, sym_type);
  171. if (!plugin_sym) {
  172. fprintf(stderr, "Error: Can't resolve symbol %s.\n",
  173. sym_name);
  174. return -1;
  175. }
  176. /* Copy symbol attributes */
  177. clish_sym_clone(sym, plugin_sym);
  178. return 0;
  179. }
  180. /*----------------------------------------------------------------------- */
  181. /* Link unresolved symbols */
  182. int clish_shell_link_plugins(clish_shell_t *this)
  183. {
  184. clish_sym_t *sym;
  185. lub_list_node_t *iter;
  186. /* Iterate elements */
  187. for(iter = lub_list__get_head(this->syms);
  188. iter; iter = lub_list_node__get_next(iter)) {
  189. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  190. if (link_unresolved_sym(this, sym) < 0)
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. /*----------------------------------------------------------------------- */
  196. /* Get hook sym */
  197. clish_sym_t *clish_shell_get_hook(const clish_shell_t *this, int type)
  198. {
  199. return this->hooks[type];
  200. }