1
0

shell_plugin.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * shell_plugin.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include <string.h>
  7. #include "lub/string.h"
  8. #include "lub/list.h"
  9. #include "lub/bintree.h"
  10. #include "clish/plugin.h"
  11. #include "clish/view.h"
  12. /*----------------------------------------------------------------------- */
  13. /* For all plugins:
  14. * * dlopen(plugin)
  15. * * dlsym(initialize function)
  16. * * exec init functions to get all plugin syms
  17. */
  18. int clish_shell_load_plugins(clish_shell_t *this)
  19. {
  20. lub_list_node_t *iter;
  21. clish_plugin_t *plugin;
  22. assert(this);
  23. /* Iterate elements */
  24. for(iter = lub_list__get_head(this->plugins);
  25. iter; iter = lub_list_node__get_next(iter)) {
  26. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  27. if (!clish_plugin_load(plugin)) {
  28. fprintf(stderr, "Error: Can't load plugin %s.\n",
  29. clish_plugin__get_file(plugin));
  30. return -1;
  31. }
  32. #ifdef DEBUG
  33. clish_plugin_dump(plugin);
  34. #endif
  35. }
  36. return 0;
  37. }
  38. /*----------------------------------------------------------------------- */
  39. /* Iterate plugins to find symbol by name.
  40. * The symbol name can be simple or with namespace:
  41. * mysym@plugin1
  42. * The symbols with prefix will be resolved using specified plugin only.
  43. */
  44. static clish_sym_t *plugins_find_sym(clish_shell_t *this, const char *name)
  45. {
  46. lub_list_node_t *iter;
  47. clish_plugin_t *plugin;
  48. clish_sym_t *sym = NULL;
  49. /* To parse command name */
  50. char *saveptr;
  51. const char *delim = "@";
  52. char *plugin_name = NULL;
  53. char *cmdn = NULL;
  54. char *str = lub_string_dup(name);
  55. assert(this);
  56. /* Parse name to get sym name and optional plugin name */
  57. cmdn = strtok_r(str, delim, &saveptr);
  58. if (!cmdn) {
  59. lub_string_free(str);
  60. return NULL;
  61. }
  62. plugin_name = strtok_r(NULL, delim, &saveptr);
  63. if (plugin_name) {
  64. /* Search for symbol in specified namespace */
  65. /* Iterate elements */
  66. for(iter = lub_list__get_head(this->plugins);
  67. iter; iter = lub_list_node__get_next(iter)) {
  68. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  69. if (strcmp(clish_plugin__get_name(plugin), plugin_name))
  70. continue;
  71. if ((sym = clish_plugin_get_sym(plugin, cmdn)))
  72. break;
  73. }
  74. } else {
  75. /* Iterate all plugins */
  76. for(iter = lub_list__get_head(this->plugins);
  77. iter; iter = lub_list_node__get_next(iter)) {
  78. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  79. if ((sym = clish_plugin_get_sym(plugin, cmdn)))
  80. break;
  81. }
  82. }
  83. lub_string_free(str);
  84. return sym;
  85. }
  86. /*--------------------------------------------------------- */
  87. /* Find symbol by name in the list of unresolved symbols */
  88. clish_sym_t *clish_shell_find_sym(clish_shell_t *this, const char *name)
  89. {
  90. lub_list_node_t *iter;
  91. clish_sym_t *sym;
  92. /* Iterate elements */
  93. for(iter = lub_list__get_head(this->syms);
  94. iter; iter = lub_list_node__get_next(iter)) {
  95. int res;
  96. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  97. res = strcmp(clish_sym__get_name(sym), name);
  98. if (!res)
  99. return sym;
  100. if (res > 0) /* No chances to find name */
  101. break;
  102. }
  103. return NULL;
  104. }
  105. /*----------------------------------------------------------------------- */
  106. /* Add symbol to the table of unresolved symbols */
  107. clish_sym_t *clish_shell_add_sym(clish_shell_t *this,
  108. clish_plugin_fn_t *func, const char *name)
  109. {
  110. clish_sym_t *sym = NULL;
  111. if (!name)
  112. return NULL;
  113. if ((sym = clish_shell_find_sym(this, name)))
  114. return sym;
  115. if (!(sym = clish_sym_new(name, func)))
  116. return NULL;
  117. lub_list_add(this->syms, sym);
  118. return sym;
  119. }
  120. /*----------------------------------------------------------------------- */
  121. clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *this,
  122. const char *name)
  123. {
  124. return clish_shell_add_sym(this, NULL, name);
  125. }
  126. /*----------------------------------------------------------------------- */
  127. /* Link unresolved symbols */
  128. int clish_shell_link_plugins(clish_shell_t *this)
  129. {
  130. clish_sym_t *sym, *plugin_sym;
  131. lub_list_node_t *iter;
  132. char *sym_name = NULL;
  133. /* Iterate elements */
  134. for(iter = lub_list__get_head(this->syms);
  135. iter; iter = lub_list_node__get_next(iter)) {
  136. sym = (clish_sym_t *)lub_list_node__get_data(iter);
  137. sym_name = clish_sym__get_name(sym);
  138. plugin_sym = plugins_find_sym(this, sym_name);
  139. if (!plugin_sym) {
  140. fprintf(stderr, "Error: Can't resolve symbol %s.\n",
  141. sym_name);
  142. return -1;
  143. }
  144. /* Copy symbol attributes */
  145. clish_sym_clone(sym, plugin_sym);
  146. }
  147. return 0;
  148. }