shell_startup.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. static int iterate_paramv(clish_shell_t *this, clish_paramv_t *paramv,
  71. clish_hook_access_fn_t *access_fn)
  72. {
  73. int i = 0;
  74. clish_param_t *param;
  75. while((param = clish_paramv__get_param(paramv, i))) {
  76. clish_paramv_t *nested_paramv;
  77. clish_ptype_t *ptype = NULL;
  78. /* Resolve PARAM's PTYPE */
  79. ptype = clish_shell_find_ptype(this, clish_param__get_ptype_name(param));
  80. if (!ptype) {
  81. fprintf(stderr, "Error: Unresolved PTYPE \"%s\" in PARAM \"%s\"\n",
  82. clish_param__get_ptype_name(param),
  83. clish_param__get_name(param));
  84. return -1;
  85. }
  86. clish_param__set_ptype(param, ptype);
  87. clish_param__set_ptype_name(param, NULL); /* Free some memory */
  88. /* Check access for PARAM */
  89. if (access_fn && clish_param__get_access(param) &&
  90. access_fn(this, clish_param__get_access(param))) {
  91. #ifdef DEBUG
  92. fprintf(stderr, "Warning: Access denied. Remove PARAM \"%s\"\n",
  93. clish_param__get_name(param));
  94. #endif
  95. if (clish_paramv_remove(paramv, i) < 0) {
  96. fprintf(stderr, "Error: Some system problem\n");
  97. return -1;
  98. }
  99. clish_param_delete(param);
  100. continue; /* Don't increment index */
  101. }
  102. /* Recursive iterate nested PARAMs */
  103. nested_paramv = clish_param__get_paramv(param);
  104. if (iterate_paramv(this, nested_paramv, access_fn) < 0)
  105. return -1;
  106. i++;
  107. }
  108. return 0;
  109. }
  110. /*-------------------------------------------------------- */
  111. int clish_shell_prepare(clish_shell_t *this)
  112. {
  113. clish_command_t *cmd;
  114. clish_view_t *view;
  115. clish_nspace_t *nspace;
  116. lub_bintree_t *view_tree, *cmd_tree;
  117. lub_list_t *nspace_tree;
  118. lub_bintree_iterator_t cmd_iter, view_iter;
  119. lub_list_node_t *nspace_iter;
  120. clish_hook_access_fn_t *access_fn = NULL;
  121. int i;
  122. clish_paramv_t *paramv;
  123. /* Add default plugin to the list of plugins */
  124. if (this->default_plugin) {
  125. clish_plugin_t *plugin;
  126. plugin = clish_plugin_new("clish");
  127. lub_list_add(this->plugins, plugin);
  128. /* Default hooks */
  129. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  130. if (this->hooks_use[i])
  131. continue;
  132. if (!clish_plugin_default_hook[i])
  133. continue;
  134. clish_sym__set_name(this->hooks[i],
  135. clish_plugin_default_hook[i]);
  136. }
  137. }
  138. /* Add default syms to unresolved table */
  139. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  140. if (clish_sym__get_name(this->hooks[i]))
  141. lub_list_add(this->syms, this->hooks[i]);
  142. }
  143. /* Load plugins and link symbols */
  144. if (clish_shell_load_plugins(this) < 0)
  145. return -1;
  146. if (clish_shell_link_plugins(this) < 0)
  147. return -1;
  148. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  149. /* Iterate the VIEWs */
  150. view_tree = &this->view_tree;
  151. view = lub_bintree_findfirst(view_tree);
  152. for (lub_bintree_iterator_init(&view_iter, view_tree, view);
  153. view; view = lub_bintree_iterator_next(&view_iter)) {
  154. /* Check access rights for the VIEW */
  155. if (access_fn && clish_view__get_access(view) &&
  156. access_fn(this, clish_view__get_access(view))) {
  157. #ifdef DEBUG
  158. fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
  159. clish_view__get_name(view));
  160. #endif
  161. lub_bintree_remove(view_tree, view);
  162. clish_view_delete(view);
  163. continue;
  164. }
  165. /* Iterate the NAMESPACEs */
  166. nspace_tree = clish_view__get_nspace_tree(view);
  167. nspace_iter = lub_list__get_head(nspace_tree);
  168. while(nspace_iter) {
  169. clish_view_t *ref_view;
  170. lub_list_node_t *old_nspace_iter;
  171. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  172. old_nspace_iter = nspace_iter;
  173. nspace_iter = lub_list_node__get_next(nspace_iter);
  174. /* Resolve NAMESPACEs and remove unresolved ones */
  175. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  176. if (!ref_view) {
  177. #ifdef DEBUG
  178. fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
  179. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  180. #endif
  181. lub_list_del(nspace_tree, old_nspace_iter);
  182. lub_list_node_free(old_nspace_iter);
  183. clish_nspace_delete(nspace);
  184. continue;
  185. }
  186. clish_nspace__set_view(nspace, ref_view);
  187. clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
  188. /* Check access rights for the NAMESPACE */
  189. if (access_fn && clish_nspace__get_access(nspace) &&
  190. access_fn(this, clish_nspace__get_access(nspace))) {
  191. #ifdef DEBUG
  192. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
  193. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  194. #endif
  195. lub_list_del(nspace_tree, old_nspace_iter);
  196. lub_list_node_free(old_nspace_iter);
  197. clish_nspace_delete(nspace);
  198. continue;
  199. }
  200. }
  201. /* Iterate the COMMANDs */
  202. cmd_tree = clish_view__get_command_tree(view);
  203. cmd = lub_bintree_findfirst(cmd_tree);
  204. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  205. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  206. int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
  207. /* Resolve command aliases */
  208. if (cmd_is_alias) {
  209. clish_view_t *aview;
  210. clish_command_t *cmdref;
  211. const char *alias_view = clish_command__get_alias_view(cmd);
  212. if (!alias_view)
  213. aview = clish_command__get_pview(cmd);
  214. else
  215. aview = clish_shell_find_view(this, alias_view);
  216. if (!aview) {
  217. fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
  218. clish_command__get_name(cmd));
  219. return -1;
  220. }
  221. cmdref = clish_view_find_command(aview,
  222. clish_command__get_alias(cmd), BOOL_FALSE);
  223. if (!cmdref) {
  224. fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
  225. clish_command__get_name(cmd));
  226. return -1;
  227. }
  228. if (!clish_command_alias_to_link(cmd, cmdref)) {
  229. fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
  230. clish_command__get_name(cmd));
  231. return -1;
  232. }
  233. }
  234. /* Check access rights for the COMMAND */
  235. if (access_fn && clish_command__get_access(cmd) &&
  236. access_fn(this, clish_command__get_access(cmd))) {
  237. #ifdef DEBUG
  238. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  239. clish_command__get_name(cmd), clish_view__get_name(view));
  240. #endif
  241. lub_bintree_remove(cmd_tree, cmd);
  242. clish_command_delete(cmd);
  243. continue;
  244. }
  245. if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
  246. continue;
  247. paramv = clish_command__get_paramv(cmd);
  248. if (iterate_paramv(this, paramv, access_fn) < 0)
  249. return -1;
  250. }
  251. }
  252. return 0;
  253. }
  254. /*----------------------------------------------------------- */