shell_startup.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /* Don't forget:
  63. * Global view
  64. * startup command
  65. * hooks
  66. * remove unresolved syms
  67. */
  68. int clish_shell_prepare(clish_shell_t *this)
  69. {
  70. clish_command_t *cmd;
  71. clish_view_t *view;
  72. clish_nspace_t *nspace;
  73. lub_bintree_t *view_tree, *cmd_tree;
  74. lub_list_t *nspace_tree;
  75. lub_bintree_iterator_t cmd_iter, view_iter;
  76. lub_list_node_t *nspace_iter;
  77. clish_hook_access_fn_t *access_fn = NULL;
  78. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  79. /* Iterate the VIEWs */
  80. view_tree = &this->view_tree;
  81. view = lub_bintree_findfirst(view_tree);
  82. for (lub_bintree_iterator_init(&view_iter, view_tree, view);
  83. view; view = lub_bintree_iterator_next(&view_iter)) {
  84. /* Check access rights for the VIEW */
  85. if (access_fn && clish_view__get_access(view) &&
  86. (access_fn(this, clish_view__get_access(view)) < 0)) {
  87. lub_bintree_remove(view_tree, view);
  88. clish_view_delete(view);
  89. continue;
  90. }
  91. /* Iterate the NAMESPACEs */
  92. nspace_tree = clish_view__get_nspace_tree(view);
  93. nspace_iter = lub_list__get_head(nspace_tree);
  94. while(nspace_iter) {
  95. clish_view_t *ref_view;
  96. lub_list_node_t *old_nspace_iter;
  97. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  98. old_nspace_iter = nspace_iter;
  99. nspace_iter = lub_list_node__get_next(nspace_iter);
  100. /* Resolve NAMESPACEs and remove unresolved ones */
  101. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  102. if (!ref_view) {
  103. fprintf(stderr, "Warning: Remove unresolved NAMESPACE %s from %s VIEW\n",
  104. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  105. lub_list_del(nspace_tree, old_nspace_iter);
  106. lub_list_node_free(old_nspace_iter);
  107. clish_nspace_delete(nspace);
  108. continue;
  109. }
  110. clish_nspace__set_view(nspace, ref_view);
  111. /* Check access rights for the NAMESPACE */
  112. if (access_fn && clish_nspace__get_access(nspace) &&
  113. (access_fn(this, clish_nspace__get_access(nspace)) < 0)) {
  114. lub_list_del(nspace_tree, old_nspace_iter);
  115. lub_list_node_free(old_nspace_iter);
  116. clish_nspace_delete(nspace);
  117. continue;
  118. }
  119. }
  120. /* Iterate the COMMANDs */
  121. cmd_tree = clish_view__get_command_tree(view);
  122. cmd = lub_bintree_findfirst(cmd_tree);
  123. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  124. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  125. if (!clish_command_alias_to_link(cmd)) {
  126. fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
  127. clish_command__get_name(cmd));
  128. return -1;
  129. }
  130. }
  131. }
  132. return 0;
  133. }
  134. /*----------------------------------------------------------- */