shell_plugin.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. fprintf(stderr, "Error: Can't load plugin %s.\n",
  22. clish_plugin__get_file(plugin));
  23. return -1;
  24. }
  25. #ifdef DEBUG
  26. clish_plugin_dump(plugin);
  27. #endif
  28. }
  29. return 0;
  30. }
  31. /*----------------------------------------------------------------------- */
  32. static clish_plugin_fn_t *plugins_find_sym(clish_shell_t *this, const char *name)
  33. {
  34. lub_list_node_t *iter;
  35. clish_plugin_t *plugin;
  36. clish_plugin_fn_t *func = NULL;
  37. assert(this);
  38. /* Iterate elements */
  39. for(iter = lub_list__get_head(this->plugins);
  40. iter; iter = lub_list_node__get_next(iter)) {
  41. plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
  42. if ((func = clish_plugin_get_sym(plugin, name)))
  43. break;
  44. }
  45. return func;
  46. }
  47. /*----------------------------------------------------------------------- */
  48. static int plugins_link_view(clish_shell_t *this, clish_view_t *view)
  49. {
  50. clish_command_t *c;
  51. lub_bintree_iterator_t iter;
  52. lub_bintree_t *tree;
  53. tree = clish_view__cmd_tree(view);
  54. /* Iterate the tree of commands */
  55. c = lub_bintree_findfirst(tree);
  56. for (lub_bintree_iterator_init(&iter, tree, c);
  57. c; c = lub_bintree_iterator_next(&iter)) {
  58. plugins_find_sym(this, "jjj");
  59. printf("command: %s\n", clish_command__get_name(c));
  60. }
  61. return 0;
  62. }
  63. /*----------------------------------------------------------------------- */
  64. int clish_shell_link_plugins(clish_shell_t *this)
  65. {
  66. clish_view_t *v;
  67. lub_bintree_iterator_t iter;
  68. v = lub_bintree_findfirst(&this->view_tree);
  69. /* Iterate the tree of views */
  70. for (lub_bintree_iterator_init(&iter, &this->view_tree, v);
  71. v; v = lub_bintree_iterator_next(&iter)) {
  72. if (plugins_link_view(this, v))
  73. return -1;
  74. }
  75. return 0;
  76. }