kscheme.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. 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. 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) && kaction_is_sync(action)) {
  222. faux_error_sprintf(error, "Filter \"%s\" can't contain "
  223. "sync symbol \"%s\"",
  224. kentry_name(entry), sym_ref);
  225. retcode = BOOL_FALSE;
  226. continue;
  227. }
  228. }
  229. return retcode;
  230. }
  231. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  232. {
  233. char *saveptr = NULL;
  234. const char *delim = "/";
  235. char *entry_name = NULL;
  236. char *full_name = NULL;
  237. kentry_t *entry = NULL;
  238. assert(scheme);
  239. if (!scheme)
  240. return NULL;
  241. assert(name);
  242. if (!name)
  243. return NULL;
  244. // Get first component of ENTRY path. It will be searched for
  245. // within scheme.
  246. full_name = faux_str_dup(name);
  247. entry_name = strtok_r(full_name, delim, &saveptr);
  248. if (!entry_name) {
  249. faux_str_free(full_name);
  250. return NULL;
  251. }
  252. entry = kscheme_find_entry(scheme, entry_name);
  253. if (!entry) {
  254. faux_str_free(full_name);
  255. return NULL;
  256. }
  257. // Search nested ENTRYs
  258. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  259. entry = kentry_find_entry(entry, entry_name);
  260. if (!entry)
  261. break;
  262. }
  263. faux_str_free(full_name);
  264. return entry;
  265. }
  266. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  267. faux_error_t *error) {
  268. kentry_entrys_node_t *iter = NULL;
  269. kentry_t *nested_entry = NULL;
  270. bool_t retcode = BOOL_TRUE;
  271. const char *ref = NULL;
  272. kentry_t *ref_entry = NULL;
  273. assert(scheme);
  274. if (!scheme)
  275. return BOOL_FALSE;
  276. assert(entry);
  277. if (!entry)
  278. return BOOL_FALSE;
  279. // Firstly if ENTRY is link to another ENTRY then make a copy
  280. if (kentry_ref_str(entry)) {
  281. ref_entry = entry;
  282. // Find the most deep real (non-link) object to reference to it
  283. // i.e. the link can't reference a link.
  284. while ((ref = kentry_ref_str(ref_entry))) {
  285. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  286. if (!ref_entry) {
  287. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  288. return BOOL_FALSE;
  289. }
  290. }
  291. if (!kentry_link(entry, ref_entry)) {
  292. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  293. return BOOL_FALSE;
  294. }
  295. return BOOL_TRUE;
  296. }
  297. // ACTIONs
  298. if (!kscheme_prepare_action_list(scheme, entry, error))
  299. retcode = BOOL_FALSE;
  300. // Process nested ENTRYs
  301. iter = kentry_entrys_iter(entry);
  302. while ((nested_entry = kentry_entrys_each(&iter))) {
  303. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  304. retcode = BOOL_FALSE;
  305. // Create fast links to nested entries with special purposes
  306. kentry_set_nested_by_purpose(entry,
  307. kentry_purpose(nested_entry), nested_entry);
  308. }
  309. return retcode;
  310. }
  311. /** @brief Prepares schema for execution.
  312. *
  313. * It loads plugins, link unresolved symbols, then iterates all the
  314. * objects and link them to each other, check access
  315. * permissions. Without this function the schema is not fully functional.
  316. */
  317. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  318. {
  319. kscheme_entrys_node_t *entrys_iter = NULL;
  320. kentry_t *entry = NULL;
  321. assert(scheme);
  322. if (!scheme)
  323. return BOOL_FALSE;
  324. if (!context)
  325. return BOOL_FALSE;
  326. if (!kscheme_load_plugins(scheme, context, error))
  327. return BOOL_FALSE;
  328. // Iterate ENTRYs
  329. entrys_iter = kscheme_entrys_iter(scheme);
  330. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  331. if (!kscheme_prepare_entry(scheme, entry, error))
  332. return BOOL_FALSE;
  333. }
  334. return BOOL_TRUE;
  335. }
  336. bool_t kscheme_named_udata_new(kscheme_t *scheme,
  337. const char *name, void *data, kudata_data_free_fn free_fn)
  338. {
  339. kudata_t *udata = NULL;
  340. assert(scheme);
  341. if (!scheme)
  342. return BOOL_FALSE;
  343. assert(scheme->ustore);
  344. udata = kustore_slot_new(scheme->ustore, name, data, free_fn);
  345. if (!udata)
  346. return BOOL_FALSE;
  347. return BOOL_TRUE;
  348. }
  349. void *kscheme_named_udata(kscheme_t *scheme, const char *name)
  350. {
  351. assert(scheme);
  352. if (!scheme)
  353. return BOOL_FALSE;
  354. assert(scheme->ustore);
  355. return kustore_slot_data(scheme->ustore, name);
  356. }