shell_startup.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * shell_startup.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include "lub/string.h"
  7. /*----------------------------------------------------------- */
  8. int clish_shell_startup(clish_shell_t *this)
  9. {
  10. const char *banner;
  11. clish_context_t context;
  12. if (!this->startup) {
  13. fprintf(stderr, "Error: Can't get valid STARTUP tag.\n");
  14. return -1;
  15. }
  16. /* Prepare context */
  17. clish_context_init(&context, this);
  18. clish_context__set_cmd(&context, this->startup);
  19. clish_context__set_action(&context,
  20. clish_command__get_action(this->startup));
  21. banner = clish_command__get_detail(this->startup);
  22. if (banner)
  23. tinyrl_printf(this->tinyrl, "%s\n", banner);
  24. /* Call log initialize */
  25. if (clish_shell__get_log(this))
  26. clish_shell_exec_log(&context, NULL, 0);
  27. /* Call startup script */
  28. return clish_shell_execute(&context, NULL);
  29. }
  30. /*----------------------------------------------------------- */
  31. void clish_shell__set_startup_view(clish_shell_t *this, const char *viewname)
  32. {
  33. clish_view_t *view;
  34. assert(this);
  35. assert(this->startup);
  36. /* Search for the view */
  37. view = clish_shell_find_view(this, viewname);
  38. assert(view);
  39. clish_command__force_viewname(this->startup, viewname);
  40. }
  41. /*----------------------------------------------------------- */
  42. void clish_shell__set_startup_viewid(clish_shell_t *this, const char *viewid)
  43. {
  44. assert(this);
  45. assert(this->startup);
  46. clish_command__force_viewid(this->startup, viewid);
  47. }
  48. /*----------------------------------------------------------- */
  49. void clish_shell__set_default_shebang(clish_shell_t *this, const char *shebang)
  50. {
  51. assert(this);
  52. lub_string_free(this->default_shebang);
  53. this->default_shebang = lub_string_dup(shebang);
  54. }
  55. /*----------------------------------------------------------- */
  56. const char * clish_shell__get_default_shebang(const clish_shell_t *this)
  57. {
  58. assert(this);
  59. return this->default_shebang;
  60. }
  61. /*-------------------------------------------------------- */
  62. int clish_shell_prepare(clish_shell_t *this)
  63. {
  64. clish_command_t *cmd;
  65. clish_view_t *view;
  66. clish_nspace_t *nspace;
  67. lub_bintree_t *view_tree, *cmd_tree;
  68. lub_list_t *nspace_tree;
  69. lub_bintree_iterator_t cmd_iter, view_iter;
  70. lub_list_node_t *nspace_iter;
  71. clish_hook_access_fn_t *access_fn = NULL;
  72. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  73. /* Iterate the VIEWs */
  74. view_tree = &this->view_tree;
  75. view = lub_bintree_findfirst(view_tree);
  76. for (lub_bintree_iterator_init(&view_iter, view_tree, view);
  77. view; view = lub_bintree_iterator_next(&view_iter)) {
  78. /* Check access rights for the VIEW */
  79. if (access_fn && clish_view__get_access(view) &&
  80. (access_fn(this, clish_view__get_access(view)) < 0)) {
  81. lub_bintree_remove(view_tree, view);
  82. clish_view_delete(view);
  83. continue;
  84. }
  85. /* Iterate the NAMESPACEs */
  86. nspace_tree = clish_view__get_nspace_tree(view);
  87. nspace_iter = lub_list__get_head(nspace_tree);
  88. while(nspace_iter) {
  89. lub_list_node_t *old_nspace_iter;
  90. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  91. old_nspace_iter = nspace_iter;
  92. nspace_iter = lub_list_node__get_next(nspace_iter);
  93. /* Resolve NAMESPACEs and remove unresolved ones */
  94. /* Check access rights for the NAMESPACE */
  95. if (access_fn && clish_nspace__get_access(nspace) &&
  96. (access_fn(this, clish_nspace__get_access(nspace)) < 0)) {
  97. lub_list_del(nspace_tree, old_nspace_iter);
  98. lub_list_node_free(old_nspace_iter);
  99. clish_nspace_delete(nspace);
  100. continue;
  101. }
  102. }
  103. /* Iterate the COMMANDs */
  104. cmd_tree = clish_view__get_command_tree(view);
  105. cmd = lub_bintree_findfirst(cmd_tree);
  106. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  107. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  108. if (!clish_command_alias_to_link(cmd)) {
  109. fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
  110. clish_command__get_name(cmd));
  111. return -1;
  112. }
  113. }
  114. }
  115. return 0;
  116. }
  117. /*----------------------------------------------------------- */