kscheme.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_find_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_find_sym(scheme, sym_ref);
  210. if (!sym) {
  211. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  212. sym_ref);
  213. retcode = BOOL_FALSE;
  214. continue;
  215. }
  216. kaction_set_sym(action, sym);
  217. }
  218. return retcode;
  219. }
  220. bool_t kscheme_prepare_param_list(kscheme_t *scheme, faux_list_t *param_list,
  221. faux_error_t *error) {
  222. faux_list_node_t *iter = NULL;
  223. kparam_t *param = NULL;
  224. bool_t retcode = BOOL_TRUE;
  225. assert(scheme);
  226. if (!scheme)
  227. return BOOL_FALSE;
  228. assert(param_list);
  229. if (!param_list)
  230. return BOOL_FALSE;
  231. if (faux_list_is_empty(param_list))
  232. return BOOL_TRUE;
  233. iter = faux_list_head(param_list);
  234. while ((param = (kparam_t *)faux_list_each(&iter))) {
  235. kptype_t *ptype = NULL;
  236. const char *ptype_ref = kparam_ptype_ref(param);
  237. ptype = kscheme_find_ptype(scheme, ptype_ref);
  238. if (!ptype) {
  239. faux_error_sprintf(error, "Can't find ptype \"%s\"",
  240. ptype_ref);
  241. retcode = BOOL_FALSE;
  242. continue;
  243. }
  244. kparam_set_ptype(param, ptype);
  245. // Nested PARAMs
  246. if (!kscheme_prepare_param_list(scheme, kparam_params(param),
  247. error))
  248. retcode = BOOL_FALSE;
  249. }
  250. return retcode;
  251. }
  252. /** @brief Prepares schema for execution.
  253. *
  254. * It loads plugins, link unresolved symbols, then iterates all the
  255. * objects and link them to each other, check access
  256. * permissions. Without this function the schema is not fully functional.
  257. */
  258. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  259. {
  260. kscheme_views_node_t *views_iter = NULL;
  261. kview_t *view = NULL;
  262. assert(scheme);
  263. if (!scheme)
  264. return BOOL_FALSE;
  265. if (!context)
  266. return BOOL_FALSE;
  267. if (!kscheme_load_plugins(scheme, context, error))
  268. return BOOL_FALSE;
  269. // Iterate VIEWs
  270. views_iter = kscheme_views_iter(scheme);
  271. while ((view = kscheme_views_each(&views_iter))) {
  272. kview_commands_node_t *commands_iter = NULL;
  273. kcommand_t *command = NULL;
  274. printf("VIEW: %s\n", kview_name(view));
  275. // Iterate COMMANDs
  276. commands_iter = kview_commands_iter(view);
  277. while ((command = kview_commands_each(&commands_iter))) {
  278. // kview_commands_node_t *commands_iter = NULL;
  279. // kcommand_t *command = NULL;
  280. printf("COMMAND: %s\n", kcommand_name(command));
  281. // ACTIONs
  282. if (!kscheme_prepare_action_list(scheme,
  283. kcommand_actions(command), error))
  284. return BOOL_FALSE;
  285. // PARAMs
  286. if (!kscheme_prepare_param_list(scheme,
  287. kcommand_params(command), error))
  288. return BOOL_FALSE;
  289. }
  290. }
  291. #if 0
  292. clish_command_t *cmd;
  293. clish_view_t *view;
  294. clish_nspace_t *nspace;
  295. lub_list_t *view_tree, *nspace_tree;
  296. lub_list_node_t *nspace_iter, *view_iter;
  297. lub_bintree_t *cmd_tree;
  298. lub_bintree_iterator_t cmd_iter;
  299. clish_hook_access_fn_t *access_fn = NULL;
  300. clish_paramv_t *paramv;
  301. int i = 0;
  302. /* Iterate the VIEWs */
  303. view_tree = this->view_tree;
  304. view_iter = lub_list_iterator_init(view_tree);
  305. while(view_iter) {
  306. lub_list_node_t *old_view_iter;
  307. view = (clish_view_t *)lub_list_node__get_data(view_iter);
  308. old_view_iter = view_iter;
  309. view_iter = lub_list_node__get_next(view_iter);
  310. /* Check access rights for the VIEW */
  311. if (access_fn && clish_view__get_access(view) &&
  312. access_fn(this, clish_view__get_access(view))) {
  313. #ifdef DEBUG
  314. fprintf(stderr, "Warning: Access denied. Remove VIEW \"%s\"\n",
  315. clish_view__get_name(view));
  316. #endif
  317. lub_list_del(view_tree, old_view_iter);
  318. lub_list_node_free(old_view_iter);
  319. clish_view_delete(view);
  320. continue;
  321. }
  322. /* Iterate the NAMESPACEs */
  323. nspace_tree = clish_view__get_nspaces(view);
  324. nspace_iter = lub_list__get_head(nspace_tree);
  325. while(nspace_iter) {
  326. clish_view_t *ref_view;
  327. lub_list_node_t *old_nspace_iter;
  328. nspace = (clish_nspace_t *)lub_list_node__get_data(nspace_iter);
  329. old_nspace_iter = nspace_iter;
  330. nspace_iter = lub_list_node__get_next(nspace_iter);
  331. /* Resolve NAMESPACEs and remove unresolved ones */
  332. ref_view = clish_shell_find_view(this, clish_nspace__get_view_name(nspace));
  333. if (!ref_view) {
  334. #ifdef DEBUG
  335. fprintf(stderr, "Warning: Remove unresolved NAMESPACE \"%s\" from \"%s\" VIEW\n",
  336. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  337. #endif
  338. lub_list_del(nspace_tree, old_nspace_iter);
  339. lub_list_node_free(old_nspace_iter);
  340. clish_nspace_delete(nspace);
  341. continue;
  342. }
  343. clish_nspace__set_view(nspace, ref_view);
  344. clish_nspace__set_view_name(nspace, NULL); /* Free some memory */
  345. /* Check access rights for the NAMESPACE */
  346. if (access_fn && (
  347. /* Check NAMESPASE owned access */
  348. (clish_nspace__get_access(nspace) && access_fn(this, clish_nspace__get_access(nspace)))
  349. ||
  350. /* Check referenced VIEW's access */
  351. (clish_view__get_access(ref_view) && access_fn(this, clish_view__get_access(ref_view)))
  352. )) {
  353. #ifdef DEBUG
  354. fprintf(stderr, "Warning: Access denied. Remove NAMESPACE \"%s\" from \"%s\" VIEW\n",
  355. clish_nspace__get_view_name(nspace), clish_view__get_name(view));
  356. #endif
  357. lub_list_del(nspace_tree, old_nspace_iter);
  358. lub_list_node_free(old_nspace_iter);
  359. clish_nspace_delete(nspace);
  360. continue;
  361. }
  362. }
  363. /* Iterate the COMMANDs */
  364. cmd_tree = clish_view__get_tree(view);
  365. cmd = lub_bintree_findfirst(cmd_tree);
  366. for (lub_bintree_iterator_init(&cmd_iter, cmd_tree, cmd);
  367. cmd; cmd = lub_bintree_iterator_next(&cmd_iter)) {
  368. int cmd_is_alias = clish_command__get_alias(cmd)?1:0;
  369. clish_param_t *args = NULL;
  370. /* Check access rights for the COMMAND */
  371. if (access_fn && clish_command__get_access(cmd) &&
  372. access_fn(this, clish_command__get_access(cmd))) {
  373. #ifdef DEBUG
  374. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  375. clish_command__get_name(cmd), clish_view__get_name(view));
  376. #endif
  377. lub_bintree_remove(cmd_tree, cmd);
  378. clish_command_delete(cmd);
  379. continue;
  380. }
  381. /* Resolve command aliases */
  382. if (cmd_is_alias) {
  383. clish_view_t *aview;
  384. clish_command_t *cmdref;
  385. const char *alias_view = clish_command__get_alias_view(cmd);
  386. if (!alias_view)
  387. aview = clish_command__get_pview(cmd);
  388. else
  389. aview = clish_shell_find_view(this, alias_view);
  390. if (!aview /* Removed or broken VIEW */
  391. ||
  392. /* Removed or broken referenced COMMAND */
  393. !(cmdref = clish_view_find_command(aview, clish_command__get_alias(cmd), BOOL_FALSE))
  394. ) {
  395. #ifdef DEBUG
  396. fprintf(stderr, "Warning: Remove unresolved link \"%s\" from \"%s\" VIEW\n",
  397. clish_command__get_name(cmd), clish_view__get_name(view));
  398. #endif
  399. lub_bintree_remove(cmd_tree, cmd);
  400. clish_command_delete(cmd);
  401. continue;
  402. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken VIEW for alias \"%s\"\n",
  403. clish_command__get_name(cmd));
  404. return -1; */
  405. /*fprintf(stderr, CLISH_XML_ERROR_STR"Broken alias \"%s\"\n",
  406. clish_command__get_name(cmd));
  407. return -1; */
  408. }
  409. if (!clish_command_alias_to_link(cmd, cmdref)) {
  410. fprintf(stderr, CLISH_XML_ERROR_STR"Something wrong with alias \"%s\"\n",
  411. clish_command__get_name(cmd));
  412. return -1;
  413. }
  414. /* Check access rights for newly constructed COMMAND.
  415. Now the link has access filed from referenced command.
  416. */
  417. if (access_fn && clish_command__get_access(cmd) &&
  418. access_fn(this, clish_command__get_access(cmd))) {
  419. #ifdef DEBUG
  420. fprintf(stderr, "Warning: Access denied. Remove COMMAND \"%s\" from VIEW \"%s\"\n",
  421. clish_command__get_name(cmd), clish_view__get_name(view));
  422. #endif
  423. lub_bintree_remove(cmd_tree, cmd);
  424. clish_command_delete(cmd);
  425. continue;
  426. }
  427. }
  428. if (cmd_is_alias) /* Don't duplicate paramv processing for aliases */
  429. continue;
  430. /* Iterate PARAMeters */
  431. paramv = clish_command__get_paramv(cmd);
  432. if (iterate_paramv(this, paramv, access_fn) < 0)
  433. return -1;
  434. /* Resolve PTYPE for args */
  435. if ((args = clish_command__get_args(cmd))) {
  436. if (!resolve_ptype(this, args))
  437. return -1;
  438. }
  439. }
  440. }
  441. #endif
  442. return BOOL_TRUE;
  443. }