kscheme.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/knspace.h>
  13. #include <klish/kentry.h>
  14. #include <klish/kscheme.h>
  15. #include <klish/kcontext.h>
  16. struct kscheme_s {
  17. faux_list_t *plugins;
  18. faux_list_t *ptypes;
  19. faux_list_t *views;
  20. faux_list_t *entrys;
  21. };
  22. // Simple methods
  23. // PLUGIN list
  24. KGET(scheme, faux_list_t *, plugins);
  25. KCMP_NESTED(scheme, plugin, name);
  26. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  27. KADD_NESTED(scheme, kplugin_t *, plugins);
  28. KFIND_NESTED(scheme, plugin);
  29. KNESTED_LEN(scheme, plugins);
  30. KNESTED_ITER(scheme, plugins);
  31. KNESTED_EACH(scheme, kplugin_t *, plugins);
  32. // PTYPE list
  33. KGET(scheme, faux_list_t *, ptypes);
  34. KCMP_NESTED(scheme, ptype, name);
  35. KCMP_NESTED_BY_KEY(scheme, ptype, name);
  36. KADD_NESTED(scheme, kptype_t *, ptypes);
  37. KFIND_NESTED(scheme, ptype);
  38. KNESTED_LEN(scheme, ptypes);
  39. KNESTED_ITER(scheme, ptypes);
  40. KNESTED_EACH(scheme, kptype_t *, ptypes);
  41. // VIEW list
  42. KGET(scheme, faux_list_t *, views);
  43. KCMP_NESTED(scheme, view, name);
  44. KCMP_NESTED_BY_KEY(scheme, view, name);
  45. KADD_NESTED(scheme, kview_t *, views);
  46. KFIND_NESTED(scheme, view);
  47. KNESTED_LEN(scheme, views);
  48. KNESTED_ITER(scheme, views);
  49. KNESTED_EACH(scheme, kview_t *, views);
  50. // ENTRY list
  51. KGET(scheme, faux_list_t *, entrys);
  52. KCMP_NESTED(scheme, entry, name);
  53. KCMP_NESTED_BY_KEY(scheme, entry, name);
  54. KADD_NESTED(scheme, kentry_t *, entrys);
  55. KFIND_NESTED(scheme, entry);
  56. KNESTED_LEN(scheme, entrys);
  57. KNESTED_ITER(scheme, entrys);
  58. KNESTED_EACH(scheme, kentry_t *, entrys);
  59. kscheme_t *kscheme_new(void)
  60. {
  61. kscheme_t *scheme = NULL;
  62. scheme = faux_zmalloc(sizeof(*scheme));
  63. assert(scheme);
  64. if (!scheme)
  65. return NULL;
  66. // PLUGIN list
  67. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  68. kscheme_plugin_compare, kscheme_plugin_kcompare,
  69. (void (*)(void *))kplugin_free);
  70. assert(scheme->plugins);
  71. // PTYPE list
  72. scheme->ptypes = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  73. kscheme_ptype_compare, kscheme_ptype_kcompare,
  74. (void (*)(void *))kptype_free);
  75. assert(scheme->ptypes);
  76. // VIEW list
  77. scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  78. kscheme_view_compare, kscheme_view_kcompare,
  79. (void (*)(void *))kview_free);
  80. assert(scheme->views);
  81. // ENTRY list
  82. // Must be unsorted because order is important
  83. scheme->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  84. kscheme_entry_compare, kscheme_entry_kcompare,
  85. (void (*)(void *))kentry_free);
  86. assert(scheme->entrys);
  87. return scheme;
  88. }
  89. void kscheme_free(kscheme_t *scheme)
  90. {
  91. if (!scheme)
  92. return;
  93. faux_list_free(scheme->plugins);
  94. faux_list_free(scheme->ptypes);
  95. faux_list_free(scheme->views);
  96. faux_list_free(scheme->entrys);
  97. faux_free(scheme);
  98. }
  99. #define TAG "PLUGIN"
  100. bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  101. faux_error_t *error)
  102. {
  103. bool_t retcode = BOOL_TRUE;
  104. kscheme_plugins_node_t *iter = NULL;
  105. kplugin_t *plugin = NULL;
  106. assert(scheme);
  107. if (!scheme)
  108. return BOOL_FALSE;
  109. assert(scheme->plugins);
  110. if (!context)
  111. return BOOL_FALSE;
  112. iter = kscheme_plugins_iter(scheme);
  113. while ((plugin = kscheme_plugins_each(&iter))) {
  114. int init_retcode = 0;
  115. if (!kplugin_load(plugin)) {
  116. faux_error_sprintf(error,
  117. TAG ": Can't load plugin \"%s\"",
  118. kplugin_name(plugin));
  119. retcode = BOOL_FALSE;
  120. continue; // Try to load all plugins
  121. }
  122. kcontext_set_type(context, KCONTEXT_PLUGIN_INIT);
  123. kcontext_set_plugin(context, plugin);
  124. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  125. faux_error_sprintf(error,
  126. TAG ": Can't init plugin \"%s\" (%d)",
  127. kplugin_name(plugin), init_retcode);
  128. retcode = BOOL_FALSE;
  129. continue;
  130. }
  131. }
  132. return retcode;
  133. }
  134. bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  135. faux_error_t *error)
  136. {
  137. kscheme_plugins_node_t *iter = NULL;
  138. kplugin_t *plugin = NULL;
  139. assert(scheme);
  140. if (!scheme)
  141. return BOOL_FALSE;
  142. assert(scheme->plugins);
  143. if (!context)
  144. return BOOL_FALSE;
  145. iter = kscheme_plugins_iter(scheme);
  146. while ((plugin = kscheme_plugins_each(&iter))) {
  147. int fini_retcode = -1;
  148. kcontext_set_type(context, KCONTEXT_PLUGIN_FINI);
  149. kcontext_set_plugin(context, plugin);
  150. if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
  151. faux_error_sprintf(error,
  152. TAG ": Can't fini plugin \"%s\" (%d)",
  153. kplugin_name(plugin), fini_retcode);
  154. }
  155. }
  156. return BOOL_TRUE;
  157. }
  158. bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  159. {
  160. assert(scheme);
  161. if (!scheme)
  162. return BOOL_FALSE;
  163. if (!context)
  164. return BOOL_FALSE;
  165. if (!kscheme_fini_plugins(scheme, context, error))
  166. return BOOL_FALSE;
  167. return BOOL_TRUE;
  168. }
  169. ksym_t *kscheme_find_sym(const kscheme_t *scheme, const char *name)
  170. {
  171. char *saveptr = NULL;
  172. const char *delim = "@";
  173. char *plugin_name = NULL;
  174. char *cmd_name = NULL;
  175. char *full_name = NULL;
  176. ksym_t *sym = NULL;
  177. assert(scheme);
  178. if (!scheme)
  179. return NULL;
  180. assert(name);
  181. if (!name)
  182. return NULL;
  183. // Parse full name to get sym name and optional plugin name
  184. full_name = faux_str_dup(name);
  185. cmd_name = strtok_r(full_name, delim, &saveptr);
  186. if (!cmd_name) {
  187. faux_str_free(full_name);
  188. return NULL;
  189. }
  190. plugin_name = strtok_r(NULL, delim, &saveptr);
  191. // Search for symbol within specified PLUGIN only
  192. if (plugin_name) {
  193. kplugin_t *plugin = NULL;
  194. plugin = kscheme_find_plugin(scheme, plugin_name);
  195. if (!plugin) {
  196. faux_str_free(full_name);
  197. return NULL;
  198. }
  199. sym = kplugin_find_sym(plugin, cmd_name);
  200. // Search for symbol within all PLUGINs
  201. } else {
  202. kscheme_plugins_node_t *iter = NULL;
  203. kplugin_t *plugin = NULL;
  204. iter = kscheme_plugins_iter(scheme);
  205. while ((plugin = kscheme_plugins_each(&iter))) {
  206. sym = kplugin_find_sym(plugin, cmd_name);
  207. if (sym)
  208. break;
  209. }
  210. }
  211. faux_str_free(full_name);
  212. return sym;
  213. }
  214. bool_t kscheme_prepare_action_list(kscheme_t *scheme, faux_list_t *action_list,
  215. faux_error_t *error) {
  216. faux_list_node_t *iter = NULL;
  217. kaction_t *action = NULL;
  218. bool_t retcode = BOOL_TRUE;
  219. assert(scheme);
  220. if (!scheme)
  221. return BOOL_FALSE;
  222. assert(action_list);
  223. if (!action_list)
  224. return BOOL_FALSE;
  225. if (faux_list_is_empty(action_list))
  226. return BOOL_TRUE;
  227. iter = faux_list_head(action_list);
  228. while ((action = (kaction_t *)faux_list_each(&iter))) {
  229. ksym_t *sym = NULL;
  230. const char *sym_ref = kaction_sym_ref(action);
  231. sym = kscheme_find_sym(scheme, sym_ref);
  232. if (!sym) {
  233. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  234. sym_ref);
  235. retcode = BOOL_FALSE;
  236. continue;
  237. }
  238. kaction_set_sym(action, sym);
  239. }
  240. return retcode;
  241. }
  242. bool_t kscheme_prepare_param_list(kscheme_t *scheme, faux_list_t *param_list,
  243. faux_error_t *error) {
  244. faux_list_node_t *iter = NULL;
  245. kparam_t *param = NULL;
  246. bool_t retcode = BOOL_TRUE;
  247. assert(scheme);
  248. if (!scheme)
  249. return BOOL_FALSE;
  250. assert(param_list);
  251. if (!param_list)
  252. return BOOL_FALSE;
  253. if (faux_list_is_empty(param_list))
  254. return BOOL_TRUE;
  255. iter = faux_list_head(param_list);
  256. while ((param = (kparam_t *)faux_list_each(&iter))) {
  257. kptype_t *ptype = NULL;
  258. const char *ptype_ref = kparam_ptype_ref(param);
  259. ptype = kscheme_find_ptype(scheme, ptype_ref);
  260. if (!ptype) {
  261. faux_error_sprintf(error, "Can't find ptype \"%s\"",
  262. ptype_ref);
  263. retcode = BOOL_FALSE;
  264. continue;
  265. }
  266. kparam_set_ptype(param, ptype);
  267. // Nested PARAMs
  268. if (!kscheme_prepare_param_list(scheme, kparam_params(param),
  269. error))
  270. retcode = BOOL_FALSE;
  271. }
  272. return retcode;
  273. }
  274. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  275. {
  276. char *saveptr = NULL;
  277. const char *delim = "/";
  278. char *entry_name = NULL;
  279. char *full_name = NULL;
  280. kentry_t *entry = NULL;
  281. assert(scheme);
  282. if (!scheme)
  283. return NULL;
  284. assert(name);
  285. if (!name)
  286. return NULL;
  287. // Get first component of ENTRY path. It will be searched for
  288. // within scheme.
  289. full_name = faux_str_dup(name);
  290. entry_name = strtok_r(full_name, delim, &saveptr);
  291. if (!entry_name) {
  292. faux_str_free(full_name);
  293. return NULL;
  294. }
  295. entry = kscheme_find_entry(scheme, entry_name);
  296. if (!entry) {
  297. faux_str_free(full_name);
  298. return NULL;
  299. }
  300. // Search nested ENTRYs
  301. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  302. entry = kentry_find_entry(entry, entry_name);
  303. if (!entry)
  304. break;
  305. }
  306. faux_str_free(full_name);
  307. return entry;
  308. }
  309. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  310. faux_error_t *error) {
  311. kentry_entrys_node_t *iter = NULL;
  312. kentry_t *nested_entry = NULL;
  313. bool_t retcode = BOOL_TRUE;
  314. const char *ref = NULL;
  315. kentry_t *ref_entry = NULL;
  316. const char *ptype_str = NULL;
  317. assert(scheme);
  318. if (!scheme)
  319. return BOOL_FALSE;
  320. assert(entry);
  321. if (!entry)
  322. return BOOL_FALSE;
  323. // Firstly if ENTRY is link to another ENTRY then make a copy
  324. if (kentry_ref_str(entry)) {
  325. ref_entry = entry;
  326. while ((ref = kentry_ref_str(ref_entry))) {
  327. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  328. if (!ref_entry) {
  329. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  330. return BOOL_FALSE;
  331. }
  332. }
  333. if (!kentry_link(entry, ref_entry)) {
  334. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  335. return BOOL_FALSE;
  336. }
  337. }
  338. // Resolve ptype's ENTRY
  339. if ((ptype_str = kentry_ptype_str(entry))) {
  340. ref_entry = kscheme_find_entry_by_path(scheme, ptype_str);
  341. if (!ref_entry) {
  342. faux_error_sprintf(error, "Can't find ENTRY \"%s\" for ptype",
  343. ptype_str);
  344. retcode = BOOL_FALSE;
  345. }
  346. kentry_set_ptype(entry, ref_entry);
  347. }
  348. // ACTIONs
  349. if (!kscheme_prepare_action_list(scheme, kentry_actions(entry), error))
  350. retcode = BOOL_FALSE;
  351. // Process nested ENTRYs
  352. iter = kentry_entrys_iter(entry);
  353. while ((nested_entry = kentry_entrys_each(&iter))) {
  354. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  355. retcode = BOOL_FALSE;
  356. }
  357. return retcode;
  358. }
  359. /** @brief Prepares schema for execution.
  360. *
  361. * It loads plugins, link unresolved symbols, then iterates all the
  362. * objects and link them to each other, check access
  363. * permissions. Without this function the schema is not fully functional.
  364. */
  365. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  366. {
  367. kscheme_views_node_t *views_iter = NULL;
  368. kview_t *view = NULL;
  369. kscheme_entrys_node_t *entrys_iter = NULL;
  370. kentry_t *entry = NULL;
  371. assert(scheme);
  372. if (!scheme)
  373. return BOOL_FALSE;
  374. if (!context)
  375. return BOOL_FALSE;
  376. if (!kscheme_load_plugins(scheme, context, error))
  377. return BOOL_FALSE;
  378. // Iterate VIEWs
  379. views_iter = kscheme_views_iter(scheme);
  380. while ((view = kscheme_views_each(&views_iter))) {
  381. kview_commands_node_t *commands_iter = NULL;
  382. kcommand_t *command = NULL;
  383. kview_nspaces_node_t *nspaces_iter = NULL;
  384. knspace_t *nspace = NULL;
  385. printf("VIEW: %s\n", kview_name(view));
  386. // Iterate NSPACEs
  387. nspaces_iter = kview_nspaces_iter(view);
  388. while ((nspace = kview_nspaces_each(&nspaces_iter))) {
  389. const char *view_ref = knspace_view_ref(nspace);
  390. kview_t *rview = NULL;
  391. rview = kscheme_find_view(scheme, view_ref);
  392. if (!view)
  393. return BOOL_FALSE;
  394. knspace_set_view(nspace, rview);
  395. printf("NSPACE: %s\n",
  396. kview_name(knspace_view(nspace)));
  397. }
  398. // Iterate COMMANDs
  399. commands_iter = kview_commands_iter(view);
  400. while ((command = kview_commands_each(&commands_iter))) {
  401. printf("COMMAND: %s\n", kcommand_name(command));
  402. // ACTIONs
  403. if (!kscheme_prepare_action_list(scheme,
  404. kcommand_actions(command), error))
  405. return BOOL_FALSE;
  406. // PARAMs
  407. if (!kscheme_prepare_param_list(scheme,
  408. kcommand_params(command), error))
  409. return BOOL_FALSE;
  410. }
  411. }
  412. // Iterate ENTRYs
  413. entrys_iter = kscheme_entrys_iter(scheme);
  414. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  415. if (!kscheme_prepare_entry(scheme, entry, error))
  416. return BOOL_FALSE;
  417. }
  418. return BOOL_TRUE;
  419. }