kscheme.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/error.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kptype.h>
  11. #include <klish/kview.h>
  12. #include <klish/kscheme.h>
  13. struct kscheme_s {
  14. faux_list_t *plugins;
  15. faux_list_t *ptypes;
  16. faux_list_t *views;
  17. };
  18. // Simple methods
  19. // PLUGIN list
  20. KCMP_NESTED(scheme, plugin, name);
  21. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  22. KADD_NESTED(scheme, plugin);
  23. KFIND_NESTED(scheme, plugin);
  24. KNESTED_LEN(scheme, plugin);
  25. KNESTED_ITER(scheme, plugin);
  26. KNESTED_EACH(scheme, plugin);
  27. // PTYPE list
  28. KCMP_NESTED(scheme, ptype, name);
  29. KCMP_NESTED_BY_KEY(scheme, ptype, name);
  30. KADD_NESTED(scheme, ptype);
  31. KFIND_NESTED(scheme, ptype);
  32. KNESTED_LEN(scheme, ptype);
  33. KNESTED_ITER(scheme, ptype);
  34. KNESTED_EACH(scheme, ptype);
  35. // VIEW list
  36. KCMP_NESTED(scheme, view, name);
  37. KCMP_NESTED_BY_KEY(scheme, view, name);
  38. KADD_NESTED(scheme, view);
  39. KFIND_NESTED(scheme, view);
  40. KNESTED_LEN(scheme, view);
  41. KNESTED_ITER(scheme, view);
  42. KNESTED_EACH(scheme, view);
  43. kscheme_t *kscheme_new(void)
  44. {
  45. kscheme_t *scheme = NULL;
  46. scheme = faux_zmalloc(sizeof(*scheme));
  47. assert(scheme);
  48. if (!scheme)
  49. return NULL;
  50. // PLUGIN list
  51. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  52. kscheme_plugin_compare, kscheme_plugin_kcompare,
  53. (void (*)(void *))kplugin_free);
  54. assert(scheme->plugins);
  55. // PTYPE list
  56. scheme->ptypes = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  57. kscheme_ptype_compare, kscheme_ptype_kcompare,
  58. (void (*)(void *))kptype_free);
  59. assert(scheme->ptypes);
  60. // VIEW list
  61. scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  62. kscheme_view_compare, kscheme_view_kcompare,
  63. (void (*)(void *))kview_free);
  64. assert(scheme->views);
  65. return scheme;
  66. }
  67. void kscheme_free(kscheme_t *scheme)
  68. {
  69. if (!scheme)
  70. return;
  71. faux_list_free(scheme->plugins);
  72. faux_list_free(scheme->ptypes);
  73. faux_list_free(scheme->views);
  74. faux_free(scheme);
  75. }
  76. #define TAG "PLUGIN"
  77. bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  78. faux_error_t *error)
  79. {
  80. bool_t retcode = BOOL_TRUE;
  81. kscheme_plugins_node_t *iter = NULL;
  82. kplugin_t *plugin = NULL;
  83. assert(scheme);
  84. if (!scheme)
  85. return BOOL_FALSE;
  86. assert(scheme->plugins);
  87. iter = kscheme_plugins_iter(scheme);
  88. while ((plugin = kscheme_plugins_each(&iter))) {
  89. int init_retcode = 0;
  90. if (!kplugin_load(plugin)) {
  91. faux_error_sprintf(error,
  92. TAG ": Can't load plugin \"%s\"",
  93. kplugin_name(plugin));
  94. retcode = BOOL_FALSE;
  95. continue; // Try to load all plugins
  96. }
  97. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  98. faux_error_sprintf(error,
  99. TAG ": Can't init plugin \"%s\" (%d)",
  100. kplugin_name(plugin), init_retcode);
  101. retcode = BOOL_FALSE;
  102. continue;
  103. }
  104. }
  105. return retcode;
  106. }
  107. bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  108. faux_error_t *error)
  109. {
  110. bool_t retcode = BOOL_TRUE;
  111. kscheme_plugins_node_t *iter = NULL;
  112. kplugin_t *plugin = NULL;
  113. assert(scheme);
  114. if (!scheme)
  115. return BOOL_FALSE;
  116. assert(scheme->plugins);
  117. iter = kscheme_plugins_iter(scheme);
  118. while ((plugin = kscheme_plugins_each(&iter))) {
  119. int init_retcode = 0;
  120. if (!kplugin_load(plugin)) {
  121. faux_error_sprintf(error,
  122. TAG ": Can't load plugin \"%s\"",
  123. kplugin_name(plugin));
  124. retcode = BOOL_FALSE;
  125. continue; // Try to load all plugins
  126. }
  127. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  128. faux_error_sprintf(error,
  129. TAG ": Can't init plugin \"%s\" (%d)",
  130. kplugin_name(plugin), init_retcode);
  131. retcode = BOOL_FALSE;
  132. continue;
  133. }
  134. }
  135. return retcode;
  136. }
  137. #if 0
  138. /** @brief Prepares schema for execution.
  139. *
  140. * It loads plugins, link unresolved symbols, then iterates all the
  141. * objects and link them to each other, check access
  142. * permissions. Without this function the schema is not fully functional.
  143. */
  144. int kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  145. {
  146. clish_command_t *cmd;
  147. clish_view_t *view;
  148. clish_nspace_t *nspace;
  149. lub_list_t *view_tree, *nspace_tree;
  150. lub_list_node_t *nspace_iter, *view_iter;
  151. lub_bintree_t *cmd_tree;
  152. lub_bintree_iterator_t cmd_iter;
  153. clish_hook_access_fn_t *access_fn = NULL;
  154. clish_paramv_t *paramv;
  155. int i = 0;
  156. /* Add statically linked plugins */
  157. while (clish_plugin_builtin_list[i].name) {
  158. clish_plugin_t *plugin;
  159. plugin = clish_shell_find_create_plugin(this,
  160. clish_plugin_builtin_list[i].name);
  161. clish_plugin__set_init(plugin,
  162. clish_plugin_builtin_list[i].init);
  163. clish_plugin__set_builtin_flag(plugin, BOOL_TRUE);
  164. i++;
  165. }
  166. /* Add default plugin to the list of plugins */
  167. if (this->default_plugin) {
  168. clish_shell_find_create_plugin(this, "clish");
  169. /* Default hooks */
  170. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  171. if (this->hooks_use[i])
  172. continue;
  173. if (!clish_plugin_default_hook[i])
  174. continue;
  175. clish_sym__set_name(this->hooks[i],
  176. clish_plugin_default_hook[i]);
  177. }
  178. }
  179. /* Add default syms to unresolved table */
  180. for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
  181. if (clish_sym__get_name(this->hooks[i]))
  182. lub_list_add(this->syms, this->hooks[i]);
  183. }
  184. /* Load plugins and link symbols */
  185. if (clish_shell_load_plugins(this) < 0)
  186. return -1;
  187. if (clish_shell_link_plugins(this) < 0)
  188. return -1;
  189. access_fn = clish_sym__get_func(clish_shell_get_hook(this, CLISH_SYM_TYPE_ACCESS));
  190. /* Iterate the VIEWs */
  191. view_tree = this->view_tree;
  192. view_iter = lub_list_iterator_init(view_tree);
  193. while(view_iter) {
  194. lub_list_node_t *old_view_iter;
  195. view = (clish_view_t *)lub_list_node__get_data(view_iter);
  196. old_view_iter = view_iter;
  197. view_iter = lub_list_node__get_next(view_iter);
  198. /* Check access rights for the VIEW */
  199. if (access_fn && clish_view__get_access(view) &&
  200. access_fn(this, clish_view__get_access(view))) {
  201. #ifdef DEBUG
  202. fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
  203. clish_view__get_name(view));
  204. #endif
  205. lub_list_del(view_tree, old_view_iter);
  206. lub_list_node_free(old_view_iter);
  207. clish_view_delete(view);
  208. continue;
  209. }
  210. /* Iterate the NAMESPACEs */
  211. nspace_tree = clish_view__get_nspaces(view);
  212. nspace_iter = lub_list__get_head(nspace_tree);
  213. while(nspace_iter) {
  214. clish_view_t *ref_view;
  215. lub_list_node_t *old_nspace_iter;
  216. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  217. old_nspace_iter = nspace_iter;
  218. nspace_iter = lub_list_node__get_next(nspace_iter);
  219. /* Resolve NAMESPACEs and remove unresolved ones */
  220. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  221. if (!ref_view) {
  222. #ifdef DEBUG
  223. fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
  224. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  225. #endif
  226. lub_list_del(nspace_tree, old_nspace_iter);
  227. lub_list_node_free(old_nspace_iter);
  228. clish_nspace_delete(nspace);
  229. continue;
  230. }
  231. clish_nspace__set_view(nspace, ref_view);
  232. clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
  233. /* Check access rights for the NAMESPACE */
  234. if (access_fn && (
  235. /* Check NAMESPASE owned access */
  236. (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
  237. ||
  238. /* Check referenced VIEW's access */
  239. (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
  240. )) {
  241. #ifdef DEBUG
  242. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
  243. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  244. #endif
  245. lub_list_del(nspace_tree, old_nspace_iter);
  246. lub_list_node_free(old_nspace_iter);
  247. clish_nspace_delete(nspace);
  248. continue;
  249. }
  250. }
  251. /* Iterate the COMMANDs */
  252. cmd_tree = clish_view__get_tree(view);
  253. cmd = lub_bintree_findfirst(cmd_tree);
  254. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  255. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  256. int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
  257. clish_param_t *args = NULL;
  258. /* Check access rights for the COMMAND */
  259. if (access_fn && clish_command__get_access(cmd) &&
  260. access_fn(this, clish_command__get_access(cmd))) {
  261. #ifdef DEBUG
  262. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  263. clish_command__get_name(cmd), clish_view__get_name(view));
  264. #endif
  265. lub_bintree_remove(cmd_tree, cmd);
  266. clish_command_delete(cmd);
  267. continue;
  268. }
  269. /* Resolve command aliases */
  270. if (cmd_is_alias) {
  271. clish_view_t *aview;
  272. clish_command_t *cmdref;
  273. const char *alias_view = clish_command__get_alias_view(cmd);
  274. if (!alias_view)
  275. aview = clish_command__get_pview(cmd);
  276. else
  277. aview = clish_shell_find_view(this, alias_view);
  278. if (!aview /* Removed or broken VIEW */
  279. ||
  280. /* Removed or broken referenced COMMAND */
  281. !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
  282. ) {
  283. #ifdef DEBUG
  284. fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
  285. clish_command__get_name(cmd), clish_view__get_name(view));
  286. #endif
  287. lub_bintree_remove(cmd_tree, cmd);
  288. clish_command_delete(cmd);
  289. continue;
  290. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
  291. clish_command__get_name(cmd));
  292. return -1; */
  293. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
  294. clish_command__get_name(cmd));
  295. return -1; */
  296. }
  297. if (!clish_command_alias_to_link(cmd, cmdref)) {
  298. fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
  299. clish_command__get_name(cmd));
  300. return -1;
  301. }
  302. /* Check access rights for newly constructed COMMAND.
  303. Now the link has access filed from referenced command.
  304. */
  305. if (access_fn && clish_command__get_access(cmd) &&
  306. access_fn(this, clish_command__get_access(cmd))) {
  307. #ifdef DEBUG
  308. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  309. clish_command__get_name(cmd), clish_view__get_name(view));
  310. #endif
  311. lub_bintree_remove(cmd_tree, cmd);
  312. clish_command_delete(cmd);
  313. continue;
  314. }
  315. }
  316. if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
  317. continue;
  318. /* Iterate PARAMeters */
  319. paramv = clish_command__get_paramv(cmd);
  320. if (iterate_paramv(this, paramv, access_fn) < 0)
  321. return -1;
  322. /* Resolve PTYPE for args */
  323. if ((args = clish_command__get_args(cmd))) {
  324. if (!resolve_ptype(this, args))
  325. return -1;
  326. }
  327. }
  328. }
  329. return 0;
  330. }
  331. #endif