shell_startup.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * shell_startup.c
  3. */
  4. #include "private.h"
  5. #include <assert.h>
  6. #include "lub/string.h"
  7. /* Default hooks */
  8. const char* clish_plugin_default_hook[] = {
  9. NULL,
  10. "clish_script@clish",
  11. "clish_hook_access@clish",
  12. "clish_hook_config@clish",
  13. "clish_hook_log@clish"
  14. };
  15. /*----------------------------------------------------------- */
  16. int clish_shell_startup(clish_shell_t *this)
  17. {
  18. const char *banner;
  19. clish_context_t context;
  20. if (!this->startup) {
  21. fprintf(stderr, "Error: Can't get valid STARTUP tag.\n");
  22. return -1;
  23. }
  24. /* Prepare context */
  25. clish_context_init(&context, this);
  26. clish_context__set_cmd(&context, this->startup);
  27. clish_context__set_action(&context,
  28. clish_command__get_action(this->startup));
  29. banner = clish_command__get_detail(this->startup);
  30. if (banner)
  31. tinyrl_printf(this->tinyrl, "%s\n", banner);
  32. /* Call log initialize */
  33. if (clish_shell__get_log(this))
  34. clish_shell_exec_log(&context, NULL, 0);
  35. /* Call startup script */
  36. return clish_shell_execute(&context, NULL);
  37. }
  38. /*----------------------------------------------------------- */
  39. void clish_shell__set_startup_view(clish_shell_t *this, const char *viewname)
  40. {
  41. clish_view_t *view;
  42. assert(this);
  43. assert(this->startup);
  44. /* Search for the view */
  45. view = clish_shell_find_view(this, viewname);
  46. assert(view);
  47. clish_command__force_viewname(this->startup, viewname);
  48. }
  49. /*----------------------------------------------------------- */
  50. void clish_shell__set_startup_viewid(clish_shell_t *this, const char *viewid)
  51. {
  52. assert(this);
  53. assert(this->startup);
  54. clish_command__force_viewid(this->startup, viewid);
  55. }
  56. /*----------------------------------------------------------- */
  57. void clish_shell__set_default_shebang(clish_shell_t *this, const char *shebang)
  58. {
  59. assert(this);
  60. lub_string_free(this->default_shebang);
  61. this->default_shebang = lub_string_dup(shebang);
  62. }
  63. /*----------------------------------------------------------- */
  64. const char * clish_shell__get_default_shebang(const clish_shell_t *this)
  65. {
  66. assert(this);
  67. return this->default_shebang;
  68. }
  69. /*-------------------------------------------------------- */
  70. /* Don't forget:
  71. * Global view
  72. * hooks
  73. * remove unresolved syms list
  74. */
  75. int clish_shell_prepare(clish_shell_t *this)
  76. {
  77. clish_command_t *cmd;
  78. clish_view_t *view;
  79. clish_nspace_t *nspace;
  80. lub_bintree_t *view_tree, *cmd_tree;
  81. lub_list_t *nspace_tree;
  82. lub_bintree_iterator_t cmd_iter, view_iter;
  83. lub_list_node_t *nspace_iter;
  84. clish_hook_access_fn_t *access_fn = NULL;
  85. int i;
  86. /* Add default plugin to the list of plugins */
  87. if (this->default_plugin) {
  88. clish_plugin_t *plugin;
  89. plugin = clish_plugin_new("clish");
  90. lub_list_add(this->plugins, plugin);
  91. /* Default hooks */
  92. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  93. if (this->hooks_use[i])
  94. continue;
  95. if (!clish_plugin_default_hook[i])
  96. continue;
  97. clish_sym__set_name(this->hooks[i],
  98. clish_plugin_default_hook[i]);
  99. }
  100. }
  101. /* Add default syms to unresolved table */
  102. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  103. if (clish_sym__get_name(this->hooks[i]))
  104. lub_list_add(this->syms, this->hooks[i]);
  105. }
  106. /* Load plugins and link symbols */
  107. if (clish_shell_load_plugins(this) < 0)
  108. return -1;
  109. if (clish_shell_link_plugins(this) < 0)
  110. return -1;
  111. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  112. /* Iterate the VIEWs */
  113. view_tree = &this->view_tree;
  114. view = lub_bintree_findfirst(view_tree);
  115. for (lub_bintree_iterator_init(&view_iter, view_tree, view);
  116. view; view = lub_bintree_iterator_next(&view_iter)) {
  117. /* Check access rights for the VIEW */
  118. if (access_fn && clish_view__get_access(view) &&
  119. access_fn(this, clish_view__get_access(view))) {
  120. fprintf(stderr, "Warning: Access denied. Remove VIEW %s\n",
  121. clish_view__get_name(view));
  122. lub_bintree_remove(view_tree, view);
  123. clish_view_delete(view);
  124. continue;
  125. }
  126. /* Iterate the NAMESPACEs */
  127. nspace_tree = clish_view__get_nspace_tree(view);
  128. nspace_iter = lub_list__get_head(nspace_tree);
  129. while(nspace_iter) {
  130. clish_view_t *ref_view;
  131. lub_list_node_t *old_nspace_iter;
  132. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  133. old_nspace_iter = nspace_iter;
  134. nspace_iter = lub_list_node__get_next(nspace_iter);
  135. /* Resolve NAMESPACEs and remove unresolved ones */
  136. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  137. if (!ref_view) {
  138. fprintf(stderr, "Warning: Remove unresolved NAMESPACE %s from %s VIEW\n",
  139. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  140. lub_list_del(nspace_tree, old_nspace_iter);
  141. lub_list_node_free(old_nspace_iter);
  142. clish_nspace_delete(nspace);
  143. continue;
  144. }
  145. clish_nspace__set_view(nspace, ref_view);
  146. /* Check access rights for the NAMESPACE */
  147. if (access_fn && clish_nspace__get_access(nspace) &&
  148. access_fn(this, clish_nspace__get_access(nspace))) {
  149. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE %s from %s VIEW\n",
  150. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  151. lub_list_del(nspace_tree, old_nspace_iter);
  152. lub_list_node_free(old_nspace_iter);
  153. clish_nspace_delete(nspace);
  154. continue;
  155. }
  156. }
  157. /* Iterate the COMMANDs */
  158. cmd_tree = clish_view__get_command_tree(view);
  159. cmd = lub_bintree_findfirst(cmd_tree);
  160. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  161. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  162. if (!clish_command_alias_to_link(cmd)) {
  163. fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias %s\n",
  164. clish_command__get_name(cmd));
  165. return -1;
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. /*----------------------------------------------------------- */