shell_plugin.c 5.5 KB

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