shell_startup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. NULL,
  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. /* Resolve PTYPE for given PARAM.
  71. */
  72. static clish_ptype_t * resolve_ptype(clish_shell_t *this, clish_param_t *param)
  73. {
  74. clish_ptype_t *ptype = NULL;
  75. if (!this || !param)
  76. return NULL;
  77. /* Resolve PARAM's PTYPE */
  78. ptype = clish_shell_find_ptype(this, clish_param__get_ptype_name(param));
  79. if (!ptype) {
  80. fprintf(stderr, "Error: Unresolved PTYPE \"%s\" in PARAM \"%s\"\n",
  81. clish_param__get_ptype_name(param),
  82. clish_param__get_name(param));
  83. return NULL;
  84. }
  85. clish_param__set_ptype(param, ptype);
  86. clish_param__set_ptype_name(param, NULL); /* Free some memory */
  87. return ptype;
  88. }
  89. /*-------------------------------------------------------- */
  90. /* Static recursive function to iterate parameters. Logically it's the
  91. * part of clish_shell_prepare() function.
  92. */
  93. static int iterate_paramv(clish_shell_t *this, clish_paramv_t *paramv,
  94. clish_hook_access_fn_t *access_fn)
  95. {
  96. int i = 0;
  97. clish_param_t *param;
  98. while((param = clish_paramv__get_param(paramv, i))) {
  99. clish_paramv_t *nested_paramv;
  100. /* Resolve PARAM's PTYPE */
  101. if (!resolve_ptype(this, param))
  102. return -1;
  103. /* Check access for PARAM */
  104. if (access_fn && clish_param__get_access(param) &&
  105. access_fn(this, clish_param__get_access(param))) {
  106. #ifdef DEBUG
  107. fprintf(stderr, "Warning: Access denied. Remove PARAM \"%s\"\n",
  108. clish_param__get_name(param));
  109. #endif
  110. if (clish_paramv_remove(paramv, i) < 0) {
  111. fprintf(stderr, "Error: Some system problem\n");
  112. return -1;
  113. }
  114. clish_param_delete(param);
  115. continue; /* Don't increment index */
  116. }
  117. /* Recursive iterate nested PARAMs */
  118. nested_paramv = clish_param__get_paramv(param);
  119. if (iterate_paramv(this, nested_paramv, access_fn) < 0)
  120. return -1;
  121. i++;
  122. }
  123. return 0;
  124. }
  125. /*-------------------------------------------------------- */
  126. /* This function prepares schema for execution. It loads
  127. * plugins, link unresolved symbols, then iterates all the
  128. * objects and link them to each other, check access
  129. * permissions. Don't execute clish_shell_startup() without this
  130. * function.
  131. */
  132. int clish_shell_prepare(clish_shell_t *this)
  133. {
  134. clish_command_t *cmd;
  135. clish_view_t *view;
  136. clish_nspace_t *nspace;
  137. lub_bintree_t *view_tree, *cmd_tree;
  138. lub_list_t *nspace_tree;
  139. lub_bintree_iterator_t cmd_iter, view_iter;
  140. lub_list_node_t *nspace_iter;
  141. clish_hook_access_fn_t *access_fn = NULL;
  142. clish_paramv_t *paramv;
  143. int i = 0;
  144. /* Add statically linked plugins */
  145. while (clish_plugin_builtin_list[i].name) {
  146. clish_plugin_t *plugin;
  147. plugin = clish_shell_find_create_plugin(this,
  148. clish_plugin_builtin_list[i].name);
  149. clish_plugin__set_init(plugin,
  150. clish_plugin_builtin_list[i].init);
  151. clish_plugin__set_builtin_flag(plugin, BOOL_TRUE);
  152. i++;
  153. }
  154. /* Add default plugin to the list of plugins */
  155. if (this->default_plugin) {
  156. clish_shell_find_create_plugin(this, "clish");
  157. /* Default hooks */
  158. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  159. if (this->hooks_use[i])
  160. continue;
  161. if (!clish_plugin_default_hook[i])
  162. continue;
  163. clish_sym__set_name(this->hooks[i],
  164. clish_plugin_default_hook[i]);
  165. }
  166. }
  167. /* Add default syms to unresolved table */
  168. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  169. if (clish_sym__get_name(this->hooks[i]))
  170. lub_list_add(this->syms, this->hooks[i]);
  171. }
  172. /* Load plugins and link symbols */
  173. if (clish_shell_load_plugins(this) < 0)
  174. return -1;
  175. if (clish_shell_link_plugins(this) < 0)
  176. return -1;
  177. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  178. /* Iterate the VIEWs */
  179. view_tree = &this->view_tree;
  180. view = lub_bintree_findfirst(view_tree);
  181. for (lub_bintree_iterator_init(&view_iter, view_tree, view);
  182. view; view = lub_bintree_iterator_next(&view_iter)) {
  183. /* Check access rights for the VIEW */
  184. if (access_fn && clish_view__get_access(view) &&
  185. access_fn(this, clish_view__get_access(view))) {
  186. #ifdef DEBUG
  187. fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
  188. clish_view__get_name(view));
  189. #endif
  190. lub_bintree_remove(view_tree, view);
  191. clish_view_delete(view);
  192. continue;
  193. }
  194. /* Iterate the NAMESPACEs */
  195. nspace_tree = clish_view__get_nspace_tree(view);
  196. nspace_iter = lub_list__get_head(nspace_tree);
  197. while(nspace_iter) {
  198. clish_view_t *ref_view;
  199. lub_list_node_t *old_nspace_iter;
  200. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  201. old_nspace_iter = nspace_iter;
  202. nspace_iter = lub_list_node__get_next(nspace_iter);
  203. /* Resolve NAMESPACEs and remove unresolved ones */
  204. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  205. if (!ref_view) {
  206. #ifdef DEBUG
  207. fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
  208. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  209. #endif
  210. lub_list_del(nspace_tree, old_nspace_iter);
  211. lub_list_node_free(old_nspace_iter);
  212. clish_nspace_delete(nspace);
  213. continue;
  214. }
  215. clish_nspace__set_view(nspace, ref_view);
  216. clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
  217. /* Check access rights for the NAMESPACE */
  218. if (access_fn && (
  219. /* Check NAMESPASE owned access */
  220. (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
  221. ||
  222. /* Check referenced VIEW's access */
  223. (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
  224. )) {
  225. #ifdef DEBUG
  226. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
  227. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  228. #endif
  229. lub_list_del(nspace_tree, old_nspace_iter);
  230. lub_list_node_free(old_nspace_iter);
  231. clish_nspace_delete(nspace);
  232. continue;
  233. }
  234. }
  235. /* Iterate the COMMANDs */
  236. cmd_tree = clish_view__get_command_tree(view);
  237. cmd = lub_bintree_findfirst(cmd_tree);
  238. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  239. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  240. int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
  241. clish_param_t *args = NULL;
  242. /* Check access rights for the COMMAND */
  243. if (access_fn && clish_command__get_access(cmd) &&
  244. access_fn(this, clish_command__get_access(cmd))) {
  245. #ifdef DEBUG
  246. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  247. clish_command__get_name(cmd), clish_view__get_name(view));
  248. #endif
  249. lub_bintree_remove(cmd_tree, cmd);
  250. clish_command_delete(cmd);
  251. continue;
  252. }
  253. /* Resolve command aliases */
  254. if (cmd_is_alias) {
  255. clish_view_t *aview;
  256. clish_command_t *cmdref;
  257. const char *alias_view = clish_command__get_alias_view(cmd);
  258. if (!alias_view)
  259. aview = clish_command__get_pview(cmd);
  260. else
  261. aview = clish_shell_find_view(this, alias_view);
  262. if (!aview /* Removed or broken VIEW */
  263. ||
  264. /* Removed or broken referenced COMMAND */
  265. !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
  266. ) {
  267. #ifdef DEBUG
  268. fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
  269. clish_command__get_name(cmd), clish_view__get_name(view));
  270. #endif
  271. lub_bintree_remove(cmd_tree, cmd);
  272. clish_command_delete(cmd);
  273. continue;
  274. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
  275. clish_command__get_name(cmd));
  276. return -1; */
  277. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
  278. clish_command__get_name(cmd));
  279. return -1; */
  280. }
  281. if (!clish_command_alias_to_link(cmd, cmdref)) {
  282. fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
  283. clish_command__get_name(cmd));
  284. return -1;
  285. }
  286. /* Check access rights for newly constructed COMMAND.
  287. Now the link has access filed from referenced command.
  288. */
  289. if (access_fn && clish_command__get_access(cmd) &&
  290. access_fn(this, clish_command__get_access(cmd))) {
  291. #ifdef DEBUG
  292. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  293. clish_command__get_name(cmd), clish_view__get_name(view));
  294. #endif
  295. lub_bintree_remove(cmd_tree, cmd);
  296. clish_command_delete(cmd);
  297. continue;
  298. }
  299. }
  300. if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
  301. continue;
  302. /* Iterate PARAMeters */
  303. paramv = clish_command__get_paramv(cmd);
  304. if (iterate_paramv(this, paramv, access_fn) < 0)
  305. return -1;
  306. /* Resolve PTYPE for args */
  307. if ((args = clish_command__get_args(cmd))) {
  308. if (!resolve_ptype(this, args))
  309. return -1;
  310. }
  311. }
  312. }
  313. return 0;
  314. }
  315. /*----------------------------------------------------------- */