kscheme.c 9.5 KB

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