kscheme.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. struct kscheme_s {
  14. faux_list_t *plugins;
  15. faux_list_t *entrys;
  16. };
  17. // Simple methods
  18. // PLUGIN list
  19. KGET(scheme, faux_list_t *, plugins);
  20. KCMP_NESTED(scheme, plugin, name);
  21. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  22. KADD_NESTED(scheme, kplugin_t *, plugins);
  23. KFIND_NESTED(scheme, plugin);
  24. KNESTED_LEN(scheme, plugins);
  25. KNESTED_ITER(scheme, plugins);
  26. KNESTED_EACH(scheme, kplugin_t *, plugins);
  27. // ENTRY list
  28. KGET(scheme, faux_list_t *, entrys);
  29. KCMP_NESTED(scheme, entry, name);
  30. KCMP_NESTED_BY_KEY(scheme, entry, name);
  31. KADD_NESTED(scheme, kentry_t *, entrys);
  32. KFIND_NESTED(scheme, entry);
  33. KNESTED_LEN(scheme, entrys);
  34. KNESTED_ITER(scheme, entrys);
  35. KNESTED_EACH(scheme, kentry_t *, entrys);
  36. kscheme_t *kscheme_new(void)
  37. {
  38. kscheme_t *scheme = NULL;
  39. scheme = faux_zmalloc(sizeof(*scheme));
  40. assert(scheme);
  41. if (!scheme)
  42. return NULL;
  43. // PLUGIN list
  44. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  45. kscheme_plugin_compare, kscheme_plugin_kcompare,
  46. (void (*)(void *))kplugin_free);
  47. assert(scheme->plugins);
  48. // ENTRY list
  49. // Must be unsorted because order is important
  50. scheme->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  51. kscheme_entry_compare, kscheme_entry_kcompare,
  52. (void (*)(void *))kentry_free);
  53. assert(scheme->entrys);
  54. return scheme;
  55. }
  56. void kscheme_free(kscheme_t *scheme)
  57. {
  58. if (!scheme)
  59. return;
  60. faux_list_free(scheme->plugins);
  61. faux_list_free(scheme->entrys);
  62. faux_free(scheme);
  63. }
  64. #define TAG "PLUGIN"
  65. bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  66. faux_error_t *error)
  67. {
  68. bool_t retcode = BOOL_TRUE;
  69. kscheme_plugins_node_t *iter = NULL;
  70. kplugin_t *plugin = NULL;
  71. assert(scheme);
  72. if (!scheme)
  73. return BOOL_FALSE;
  74. assert(scheme->plugins);
  75. if (!context)
  76. return BOOL_FALSE;
  77. iter = kscheme_plugins_iter(scheme);
  78. while ((plugin = kscheme_plugins_each(&iter))) {
  79. int init_retcode = 0;
  80. if (!kplugin_load(plugin)) {
  81. faux_error_sprintf(error,
  82. TAG ": Can't load plugin \"%s\"",
  83. kplugin_name(plugin));
  84. retcode = BOOL_FALSE;
  85. continue; // Try to load all plugins
  86. }
  87. kcontext_set_type(context, KCONTEXT_PLUGIN_INIT);
  88. kcontext_set_plugin(context, plugin);
  89. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  90. faux_error_sprintf(error,
  91. TAG ": Can't init plugin \"%s\" (%d)",
  92. kplugin_name(plugin), init_retcode);
  93. retcode = BOOL_FALSE;
  94. continue;
  95. }
  96. }
  97. return retcode;
  98. }
  99. bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  100. faux_error_t *error)
  101. {
  102. kscheme_plugins_node_t *iter = NULL;
  103. kplugin_t *plugin = NULL;
  104. assert(scheme);
  105. if (!scheme)
  106. return BOOL_FALSE;
  107. assert(scheme->plugins);
  108. if (!context)
  109. return BOOL_FALSE;
  110. iter = kscheme_plugins_iter(scheme);
  111. while ((plugin = kscheme_plugins_each(&iter))) {
  112. int fini_retcode = -1;
  113. kcontext_set_type(context, KCONTEXT_PLUGIN_FINI);
  114. kcontext_set_plugin(context, plugin);
  115. if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
  116. faux_error_sprintf(error,
  117. TAG ": Can't fini plugin \"%s\" (%d)",
  118. kplugin_name(plugin), fini_retcode);
  119. }
  120. }
  121. return BOOL_TRUE;
  122. }
  123. bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  124. {
  125. assert(scheme);
  126. if (!scheme)
  127. return BOOL_FALSE;
  128. if (!context)
  129. return BOOL_FALSE;
  130. if (!kscheme_fini_plugins(scheme, context, error))
  131. return BOOL_FALSE;
  132. return BOOL_TRUE;
  133. }
  134. ksym_t *kscheme_find_sym(const kscheme_t *scheme, const char *name)
  135. {
  136. char *saveptr = NULL;
  137. const char *delim = "@";
  138. char *plugin_name = NULL;
  139. char *cmd_name = NULL;
  140. char *full_name = NULL;
  141. ksym_t *sym = NULL;
  142. assert(scheme);
  143. if (!scheme)
  144. return NULL;
  145. assert(name);
  146. if (!name)
  147. return NULL;
  148. // Parse full name to get sym name and optional plugin name
  149. full_name = faux_str_dup(name);
  150. cmd_name = strtok_r(full_name, delim, &saveptr);
  151. if (!cmd_name) {
  152. faux_str_free(full_name);
  153. return NULL;
  154. }
  155. plugin_name = strtok_r(NULL, delim, &saveptr);
  156. // Search for symbol within specified PLUGIN only
  157. if (plugin_name) {
  158. kplugin_t *plugin = NULL;
  159. plugin = kscheme_find_plugin(scheme, plugin_name);
  160. if (!plugin) {
  161. faux_str_free(full_name);
  162. return NULL;
  163. }
  164. sym = kplugin_find_sym(plugin, cmd_name);
  165. // Search for symbol within all PLUGINs
  166. } else {
  167. kscheme_plugins_node_t *iter = NULL;
  168. kplugin_t *plugin = NULL;
  169. iter = kscheme_plugins_iter(scheme);
  170. while ((plugin = kscheme_plugins_each(&iter))) {
  171. sym = kplugin_find_sym(plugin, cmd_name);
  172. if (sym)
  173. break;
  174. }
  175. }
  176. faux_str_free(full_name);
  177. return sym;
  178. }
  179. bool_t kscheme_prepare_action_list(kscheme_t *scheme, kentry_t *entry,
  180. faux_error_t *error) {
  181. faux_list_node_t *iter = NULL;
  182. kaction_t *action = NULL;
  183. bool_t retcode = BOOL_TRUE;
  184. faux_list_t *action_list = NULL;
  185. assert(scheme);
  186. if (!scheme)
  187. return BOOL_FALSE;
  188. assert(entry);
  189. if (!entry)
  190. return BOOL_FALSE;
  191. action_list = kentry_actions(entry);
  192. assert(action_list);
  193. if (!action_list)
  194. return BOOL_FALSE;
  195. if (faux_list_is_empty(action_list))
  196. return BOOL_TRUE;
  197. iter = faux_list_head(action_list);
  198. while ((action = (kaction_t *)faux_list_each(&iter))) {
  199. ksym_t *sym = NULL;
  200. const char *sym_ref = kaction_sym_ref(action);
  201. sym = kscheme_find_sym(scheme, sym_ref);
  202. if (!sym) {
  203. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  204. sym_ref);
  205. retcode = BOOL_FALSE;
  206. continue;
  207. }
  208. kaction_set_sym(action, sym);
  209. // Filter can't contain sync symbols.
  210. if (kentry_filter(entry) && kaction_is_sync(action)) {
  211. faux_error_sprintf(error, "Filter \"%s\" can't contain "
  212. "sync symbol \"%s\"",
  213. kentry_name(entry), sym_ref);
  214. retcode = BOOL_FALSE;
  215. continue;
  216. }
  217. }
  218. return retcode;
  219. }
  220. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  221. {
  222. char *saveptr = NULL;
  223. const char *delim = "/";
  224. char *entry_name = NULL;
  225. char *full_name = NULL;
  226. kentry_t *entry = NULL;
  227. assert(scheme);
  228. if (!scheme)
  229. return NULL;
  230. assert(name);
  231. if (!name)
  232. return NULL;
  233. // Get first component of ENTRY path. It will be searched for
  234. // within scheme.
  235. full_name = faux_str_dup(name);
  236. entry_name = strtok_r(full_name, delim, &saveptr);
  237. if (!entry_name) {
  238. faux_str_free(full_name);
  239. return NULL;
  240. }
  241. entry = kscheme_find_entry(scheme, entry_name);
  242. if (!entry) {
  243. faux_str_free(full_name);
  244. return NULL;
  245. }
  246. // Search nested ENTRYs
  247. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  248. entry = kentry_find_entry(entry, entry_name);
  249. if (!entry)
  250. break;
  251. }
  252. faux_str_free(full_name);
  253. return entry;
  254. }
  255. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  256. faux_error_t *error) {
  257. kentry_entrys_node_t *iter = NULL;
  258. kentry_t *nested_entry = NULL;
  259. bool_t retcode = BOOL_TRUE;
  260. const char *ref = NULL;
  261. kentry_t *ref_entry = NULL;
  262. assert(scheme);
  263. if (!scheme)
  264. return BOOL_FALSE;
  265. assert(entry);
  266. if (!entry)
  267. return BOOL_FALSE;
  268. // Firstly if ENTRY is link to another ENTRY then make a copy
  269. if (kentry_ref_str(entry)) {
  270. ref_entry = entry;
  271. while ((ref = kentry_ref_str(ref_entry))) {
  272. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  273. if (!ref_entry) {
  274. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  275. return BOOL_FALSE;
  276. }
  277. }
  278. if (!kentry_link(entry, ref_entry)) {
  279. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  280. return BOOL_FALSE;
  281. }
  282. }
  283. // ACTIONs
  284. if (!kscheme_prepare_action_list(scheme, entry, error))
  285. retcode = BOOL_FALSE;
  286. // Process nested ENTRYs
  287. iter = kentry_entrys_iter(entry);
  288. while ((nested_entry = kentry_entrys_each(&iter))) {
  289. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  290. retcode = BOOL_FALSE;
  291. // Create fast links to nested entries with special purposes
  292. kentry_set_nested_by_purpose(entry,
  293. kentry_purpose(nested_entry), nested_entry);
  294. }
  295. return retcode;
  296. }
  297. /** @brief Prepares schema for execution.
  298. *
  299. * It loads plugins, link unresolved symbols, then iterates all the
  300. * objects and link them to each other, check access
  301. * permissions. Without this function the schema is not fully functional.
  302. */
  303. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  304. {
  305. kscheme_entrys_node_t *entrys_iter = NULL;
  306. kentry_t *entry = NULL;
  307. assert(scheme);
  308. if (!scheme)
  309. return BOOL_FALSE;
  310. if (!context)
  311. return BOOL_FALSE;
  312. if (!kscheme_load_plugins(scheme, context, error))
  313. return BOOL_FALSE;
  314. // Iterate ENTRYs
  315. entrys_iter = kscheme_entrys_iter(scheme);
  316. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  317. if (!kscheme_prepare_entry(scheme, entry, error))
  318. return BOOL_FALSE;
  319. }
  320. return BOOL_TRUE;
  321. }