1
0

shell_plugin.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * shell_plugin.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include "lub/list.h"
  7. #include "lub/bintree.h"
  8. #include "clish/plugin.h"
  9. #include "clish/view.h"
  10. /*----------------------------------------------------------------------- */
  11. int clish_shell_load_plugins(clish_shell_t *this)
  12. {
  13. lub_list_node_t *iter;
  14. clish_plugin_t *plugin;
  15. assert(this);
  16. /* Iterate elements */
  17. for(iter = lub_list__get_head(this->plugins);
  18. iter; iter = lub_list_node__get_next(iter)) {
  19. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  20. if (!clish_plugin_load(plugin))
  21. return -1;
  22. #ifdef DEBUG
  23. clish_plugin_dump(plugin);
  24. #endif
  25. }
  26. return 0;
  27. }
  28. /*----------------------------------------------------------------------- */
  29. static clish_plugin_fn_t *plugins_find_sym(clish_shell_t *this, const char *name)
  30. {
  31. lub_list_node_t *iter;
  32. clish_plugin_t *plugin;
  33. clish_plugin_fn_t *func = NULL;
  34. assert(this);
  35. /* Iterate elements */
  36. for(iter = lub_list__get_head(this->plugins);
  37. iter; iter = lub_list_node__get_next(iter)) {
  38. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  39. if ((func = clish_plugin_get_sym(plugin, name)))
  40. break;
  41. }
  42. return func;
  43. }
  44. /*----------------------------------------------------------------------- */
  45. static int plugins_link_view(clish_shell_t *this, clish_view_t *view)
  46. {
  47. clish_command_t *c;
  48. lub_bintree_iterator_t iter;
  49. lub_bintree_t *tree;
  50. tree = clish_view__cmd_tree(view);
  51. /* Iterate the tree of commands */
  52. c = lub_bintree_findfirst(tree);
  53. for (lub_bintree_iterator_init(&iter, tree, c);
  54. c; c = lub_bintree_iterator_next(&iter)) {
  55. plugins_find_sym(this, "jjj");
  56. printf("command: %s\n", clish_command__get_name(c));
  57. }
  58. return 0;
  59. }
  60. /*----------------------------------------------------------------------- */
  61. int clish_shell_link_plugins(clish_shell_t *this)
  62. {
  63. clish_view_t *v;
  64. lub_bintree_iterator_t iter;
  65. v = lub_bintree_findfirst(&this->view_tree);
  66. /* Iterate the tree of views */
  67. for (lub_bintree_iterator_init(&iter, &this->view_tree, v);
  68. v; v = lub_bintree_iterator_next(&iter)) {
  69. if (plugins_link_view(this, v))
  70. return -1;
  71. }
  72. return 0;
  73. }