kscheme.c 11 KB

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