kscheme.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #include <klish/kcontext.h>
  14. struct kscheme_s {
  15. faux_list_t *plugins;
  16. faux_list_t *ptypes;
  17. faux_list_t *views;
  18. };
  19. // Simple methods
  20. // PLUGIN list
  21. KGET(scheme, faux_list_t *, plugins);
  22. KCMP_NESTED(scheme, plugin, name);
  23. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  24. KADD_NESTED(scheme, plugin);
  25. KFIND_NESTED(scheme, plugin);
  26. KNESTED_LEN(scheme, plugin);
  27. KNESTED_ITER(scheme, plugin);
  28. KNESTED_EACH(scheme, plugin);
  29. // PTYPE list
  30. KGET(scheme, faux_list_t *, ptypes);
  31. KCMP_NESTED(scheme, ptype, name);
  32. KCMP_NESTED_BY_KEY(scheme, ptype, name);
  33. KADD_NESTED(scheme, ptype);
  34. KFIND_NESTED(scheme, ptype);
  35. KNESTED_LEN(scheme, ptype);
  36. KNESTED_ITER(scheme, ptype);
  37. KNESTED_EACH(scheme, ptype);
  38. // VIEW list
  39. KGET(scheme, faux_list_t *, views);
  40. KCMP_NESTED(scheme, view, name);
  41. KCMP_NESTED_BY_KEY(scheme, view, name);
  42. KADD_NESTED(scheme, view);
  43. KFIND_NESTED(scheme, view);
  44. KNESTED_LEN(scheme, view);
  45. KNESTED_ITER(scheme, view);
  46. KNESTED_EACH(scheme, view);
  47. kscheme_t *kscheme_new(void)
  48. {
  49. kscheme_t *scheme = NULL;
  50. scheme = faux_zmalloc(sizeof(*scheme));
  51. assert(scheme);
  52. if (!scheme)
  53. return NULL;
  54. // PLUGIN list
  55. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  56. kscheme_plugin_compare, kscheme_plugin_kcompare,
  57. (void (*)(void *))kplugin_free);
  58. assert(scheme->plugins);
  59. // PTYPE list
  60. scheme->ptypes = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  61. kscheme_ptype_compare, kscheme_ptype_kcompare,
  62. (void (*)(void *))kptype_free);
  63. assert(scheme->ptypes);
  64. // VIEW list
  65. scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  66. kscheme_view_compare, kscheme_view_kcompare,
  67. (void (*)(void *))kview_free);
  68. assert(scheme->views);
  69. return scheme;
  70. }
  71. void kscheme_free(kscheme_t *scheme)
  72. {
  73. if (!scheme)
  74. return;
  75. faux_list_free(scheme->plugins);
  76. faux_list_free(scheme->ptypes);
  77. faux_list_free(scheme->views);
  78. faux_free(scheme);
  79. }
  80. #define TAG "PLUGIN"
  81. bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  82. faux_error_t *error)
  83. {
  84. bool_t retcode = BOOL_TRUE;
  85. kscheme_plugins_node_t *iter = NULL;
  86. kplugin_t *plugin = NULL;
  87. assert(scheme);
  88. if (!scheme)
  89. return BOOL_FALSE;
  90. assert(scheme->plugins);
  91. if (!context)
  92. return BOOL_FALSE;
  93. iter = kscheme_plugins_iter(scheme);
  94. while ((plugin = kscheme_plugins_each(&iter))) {
  95. int init_retcode = 0;
  96. if (!kplugin_load(plugin)) {
  97. faux_error_sprintf(error,
  98. TAG ": Can't load plugin \"%s\"",
  99. kplugin_name(plugin));
  100. retcode = BOOL_FALSE;
  101. continue; // Try to load all plugins
  102. }
  103. kcontext_set_type(context, KCONTEXT_PLUGIN_INIT);
  104. kcontext_set_plugin(context, plugin);
  105. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  106. faux_error_sprintf(error,
  107. TAG ": Can't init plugin \"%s\" (%d)",
  108. kplugin_name(plugin), init_retcode);
  109. retcode = BOOL_FALSE;
  110. continue;
  111. }
  112. }
  113. return retcode;
  114. }
  115. bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  116. faux_error_t *error)
  117. {
  118. kscheme_plugins_node_t *iter = NULL;
  119. kplugin_t *plugin = NULL;
  120. assert(scheme);
  121. if (!scheme)
  122. return BOOL_FALSE;
  123. assert(scheme->plugins);
  124. if (!context)
  125. return BOOL_FALSE;
  126. iter = kscheme_plugins_iter(scheme);
  127. while ((plugin = kscheme_plugins_each(&iter))) {
  128. int fini_retcode = -1;
  129. kcontext_set_type(context, KCONTEXT_PLUGIN_FINI);
  130. kcontext_set_plugin(context, plugin);
  131. if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
  132. faux_error_sprintf(error,
  133. TAG ": Can't fini plugin \"%s\" (%d)",
  134. kplugin_name(plugin), fini_retcode);
  135. }
  136. }
  137. return BOOL_TRUE;
  138. }
  139. bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  140. {
  141. assert(scheme);
  142. if (!scheme)
  143. return BOOL_FALSE;
  144. if (!context)
  145. return BOOL_FALSE;
  146. if (!kscheme_fini_plugins(scheme, context, error))
  147. return BOOL_FALSE;
  148. return BOOL_TRUE;
  149. }
  150. ksym_t *kscheme_resolve_sym(const kscheme_t *scheme, const char *name)
  151. {
  152. char *saveptr = NULL;
  153. const char *delim = "@";
  154. char *plugin_name = NULL;
  155. char *cmd_name = NULL;
  156. char *full_name = NULL;
  157. ksym_t *sym = NULL;
  158. assert(scheme);
  159. if (!scheme)
  160. return NULL;
  161. assert(name);
  162. if (!name)
  163. return NULL;
  164. // Parse full name to get sym name and optional plugin name
  165. full_name = faux_str_dup(name);
  166. cmd_name = strtok_r(full_name, delim, &saveptr);
  167. if (!cmd_name) {
  168. faux_str_free(full_name);
  169. return NULL;
  170. }
  171. plugin_name = strtok_r(NULL, delim, &saveptr);
  172. // Search for symbol within specified PLUGIN only
  173. if (plugin_name) {
  174. kplugin_t *plugin = NULL;
  175. plugin = kscheme_find_plugin(scheme, plugin_name);
  176. if (!plugin)
  177. return NULL;
  178. sym = kplugin_find_sym(plugin, cmd_name);
  179. // Search for symbol within all PLUGINs
  180. } else {
  181. kscheme_plugins_node_t *iter = NULL;
  182. kplugin_t *plugin = NULL;
  183. iter = kscheme_plugins_iter(scheme);
  184. while ((plugin = kscheme_plugins_each(&iter))) {
  185. sym = kplugin_find_sym(plugin, cmd_name);
  186. if (sym)
  187. break;
  188. }
  189. }
  190. return sym;
  191. }
  192. bool_t kscheme_prepare_action_list(kscheme_t *scheme, faux_list_t *action_list,
  193. faux_error_t *error) {
  194. faux_list_node_t *iter = NULL;
  195. kaction_t *action = NULL;
  196. bool_t retcode = BOOL_TRUE;
  197. assert(scheme);
  198. if (!scheme)
  199. return BOOL_FALSE;
  200. assert(action_list);
  201. if (!action_list)
  202. return BOOL_FALSE;
  203. if (faux_list_is_empty(action_list))
  204. return BOOL_TRUE;
  205. iter = faux_list_head(action_list);
  206. while ((action = (kaction_t *)faux_list_each(&iter))) {
  207. ksym_t *sym = NULL;
  208. const char *sym_ref = kaction_sym_ref(action);
  209. sym = kscheme_resolve_sym(scheme, sym_ref);
  210. if (!sym) {
  211. faux_error_sprintf(error, "Can't resolve symbol \"%s\"",
  212. sym_ref);
  213. retcode = BOOL_FALSE;
  214. continue;
  215. }
  216. kaction_set_sym(action, sym);
  217. }
  218. return retcode;
  219. }
  220. /** @brief Prepares schema for execution.
  221. *
  222. * It loads plugins, link unresolved symbols, then iterates all the
  223. * objects and link them to each other, check access
  224. * permissions. Without this function the schema is not fully functional.
  225. */
  226. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  227. {
  228. kscheme_views_node_t *views_iter = NULL;
  229. kview_t *view = NULL;
  230. assert(scheme);
  231. if (!scheme)
  232. return BOOL_FALSE;
  233. if (!context)
  234. return BOOL_FALSE;
  235. if (!kscheme_load_plugins(scheme, context, error))
  236. return BOOL_FALSE;
  237. // Iterate VIEWs
  238. views_iter = kscheme_views_iter(scheme);
  239. while ((view = kscheme_views_each(&views_iter))) {
  240. kview_commands_node_t *commands_iter = NULL;
  241. kcommand_t *command = NULL;
  242. printf("VIEW: %s\n", kview_name(view));
  243. // Iterate COMMANDs
  244. commands_iter = kview_commands_iter(view);
  245. while ((command = kview_commands_each(&commands_iter))) {
  246. // kview_commands_node_t *commands_iter = NULL;
  247. // kcommand_t *command = NULL;
  248. printf("COMMAND: %s\n", kcommand_name(command));
  249. if (!kscheme_prepare_action_list(scheme,
  250. kcommand_actions(command), error))
  251. return BOOL_FALSE;
  252. }
  253. }
  254. #if 0
  255. clish_command_t *cmd;
  256. clish_view_t *view;
  257. clish_nspace_t *nspace;
  258. lub_list_t *view_tree, *nspace_tree;
  259. lub_list_node_t *nspace_iter, *view_iter;
  260. lub_bintree_t *cmd_tree;
  261. lub_bintree_iterator_t cmd_iter;
  262. clish_hook_access_fn_t *access_fn = NULL;
  263. clish_paramv_t *paramv;
  264. int i = 0;
  265. /* Iterate the VIEWs */
  266. view_tree = this->view_tree;
  267. view_iter = lub_list_iterator_init(view_tree);
  268. while(view_iter) {
  269. lub_list_node_t *old_view_iter;
  270. view = (clish_view_t *)lub_list_node__get_data(view_iter);
  271. old_view_iter = view_iter;
  272. view_iter = lub_list_node__get_next(view_iter);
  273. /* Check access rights for the VIEW */
  274. if (access_fn && clish_view__get_access(view) &&
  275. access_fn(this, clish_view__get_access(view))) {
  276. #ifdef DEBUG
  277. fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
  278. clish_view__get_name(view));
  279. #endif
  280. lub_list_del(view_tree, old_view_iter);
  281. lub_list_node_free(old_view_iter);
  282. clish_view_delete(view);
  283. continue;
  284. }
  285. /* Iterate the NAMESPACEs */
  286. nspace_tree = clish_view__get_nspaces(view);
  287. nspace_iter = lub_list__get_head(nspace_tree);
  288. while(nspace_iter) {
  289. clish_view_t *ref_view;
  290. lub_list_node_t *old_nspace_iter;
  291. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  292. old_nspace_iter = nspace_iter;
  293. nspace_iter = lub_list_node__get_next(nspace_iter);
  294. /* Resolve NAMESPACEs and remove unresolved ones */
  295. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  296. if (!ref_view) {
  297. #ifdef DEBUG
  298. fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
  299. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  300. #endif
  301. lub_list_del(nspace_tree, old_nspace_iter);
  302. lub_list_node_free(old_nspace_iter);
  303. clish_nspace_delete(nspace);
  304. continue;
  305. }
  306. clish_nspace__set_view(nspace, ref_view);
  307. clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
  308. /* Check access rights for the NAMESPACE */
  309. if (access_fn && (
  310. /* Check NAMESPASE owned access */
  311. (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
  312. ||
  313. /* Check referenced VIEW's access */
  314. (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
  315. )) {
  316. #ifdef DEBUG
  317. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
  318. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  319. #endif
  320. lub_list_del(nspace_tree, old_nspace_iter);
  321. lub_list_node_free(old_nspace_iter);
  322. clish_nspace_delete(nspace);
  323. continue;
  324. }
  325. }
  326. /* Iterate the COMMANDs */
  327. cmd_tree = clish_view__get_tree(view);
  328. cmd = lub_bintree_findfirst(cmd_tree);
  329. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  330. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  331. int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
  332. clish_param_t *args = NULL;
  333. /* Check access rights for the COMMAND */
  334. if (access_fn && clish_command__get_access(cmd) &&
  335. access_fn(this, clish_command__get_access(cmd))) {
  336. #ifdef DEBUG
  337. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  338. clish_command__get_name(cmd), clish_view__get_name(view));
  339. #endif
  340. lub_bintree_remove(cmd_tree, cmd);
  341. clish_command_delete(cmd);
  342. continue;
  343. }
  344. /* Resolve command aliases */
  345. if (cmd_is_alias) {
  346. clish_view_t *aview;
  347. clish_command_t *cmdref;
  348. const char *alias_view = clish_command__get_alias_view(cmd);
  349. if (!alias_view)
  350. aview = clish_command__get_pview(cmd);
  351. else
  352. aview = clish_shell_find_view(this, alias_view);
  353. if (!aview /* Removed or broken VIEW */
  354. ||
  355. /* Removed or broken referenced COMMAND */
  356. !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
  357. ) {
  358. #ifdef DEBUG
  359. fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
  360. clish_command__get_name(cmd), clish_view__get_name(view));
  361. #endif
  362. lub_bintree_remove(cmd_tree, cmd);
  363. clish_command_delete(cmd);
  364. continue;
  365. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
  366. clish_command__get_name(cmd));
  367. return -1; */
  368. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
  369. clish_command__get_name(cmd));
  370. return -1; */
  371. }
  372. if (!clish_command_alias_to_link(cmd, cmdref)) {
  373. fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
  374. clish_command__get_name(cmd));
  375. return -1;
  376. }
  377. /* Check access rights for newly constructed COMMAND.
  378. Now the link has access filed from referenced command.
  379. */
  380. if (access_fn && clish_command__get_access(cmd) &&
  381. access_fn(this, clish_command__get_access(cmd))) {
  382. #ifdef DEBUG
  383. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  384. clish_command__get_name(cmd), clish_view__get_name(view));
  385. #endif
  386. lub_bintree_remove(cmd_tree, cmd);
  387. clish_command_delete(cmd);
  388. continue;
  389. }
  390. }
  391. if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
  392. continue;
  393. /* Iterate PARAMeters */
  394. paramv = clish_command__get_paramv(cmd);
  395. if (iterate_paramv(this, paramv, access_fn) < 0)
  396. return -1;
  397. /* Resolve PTYPE for args */
  398. if ((args = clish_command__get_args(cmd))) {
  399. if (!resolve_ptype(this, args))
  400. return -1;
  401. }
  402. }
  403. }
  404. #endif
  405. return BOOL_TRUE;
  406. }