kscheme.c 9.3 KB

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