shell_startup.c 10 KB

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