kscheme.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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, faux_list_t *action_list,
  180. faux_error_t *error) {
  181. faux_list_node_t *iter = NULL;
  182. kaction_t *action = NULL;
  183. bool_t retcode = BOOL_TRUE;
  184. assert(scheme);
  185. if (!scheme)
  186. return BOOL_FALSE;
  187. assert(action_list);
  188. if (!action_list)
  189. return BOOL_FALSE;
  190. if (faux_list_is_empty(action_list))
  191. return BOOL_TRUE;
  192. iter = faux_list_head(action_list);
  193. while ((action = (kaction_t *)faux_list_each(&iter))) {
  194. ksym_t *sym = NULL;
  195. const char *sym_ref = kaction_sym_ref(action);
  196. sym = kscheme_find_sym(scheme, sym_ref);
  197. if (!sym) {
  198. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  199. sym_ref);
  200. retcode = BOOL_FALSE;
  201. continue;
  202. }
  203. kaction_set_sym(action, sym);
  204. }
  205. return retcode;
  206. }
  207. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  208. {
  209. char *saveptr = NULL;
  210. const char *delim = "/";
  211. char *entry_name = NULL;
  212. char *full_name = NULL;
  213. kentry_t *entry = NULL;
  214. assert(scheme);
  215. if (!scheme)
  216. return NULL;
  217. assert(name);
  218. if (!name)
  219. return NULL;
  220. // Get first component of ENTRY path. It will be searched for
  221. // within scheme.
  222. full_name = faux_str_dup(name);
  223. entry_name = strtok_r(full_name, delim, &saveptr);
  224. if (!entry_name) {
  225. faux_str_free(full_name);
  226. return NULL;
  227. }
  228. entry = kscheme_find_entry(scheme, entry_name);
  229. if (!entry) {
  230. faux_str_free(full_name);
  231. return NULL;
  232. }
  233. // Search nested ENTRYs
  234. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  235. entry = kentry_find_entry(entry, entry_name);
  236. if (!entry)
  237. break;
  238. }
  239. faux_str_free(full_name);
  240. return entry;
  241. }
  242. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  243. faux_error_t *error) {
  244. kentry_entrys_node_t *iter = NULL;
  245. kentry_t *nested_entry = NULL;
  246. bool_t retcode = BOOL_TRUE;
  247. const char *ref = NULL;
  248. kentry_t *ref_entry = NULL;
  249. const char *ptype_str = NULL;
  250. assert(scheme);
  251. if (!scheme)
  252. return BOOL_FALSE;
  253. assert(entry);
  254. if (!entry)
  255. return BOOL_FALSE;
  256. // Firstly if ENTRY is link to another ENTRY then make a copy
  257. if (kentry_ref_str(entry)) {
  258. ref_entry = entry;
  259. while ((ref = kentry_ref_str(ref_entry))) {
  260. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  261. if (!ref_entry) {
  262. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  263. return BOOL_FALSE;
  264. }
  265. }
  266. if (!kentry_link(entry, ref_entry)) {
  267. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  268. return BOOL_FALSE;
  269. }
  270. }
  271. // Resolve ptype's ENTRY
  272. if ((ptype_str = kentry_ptype_str(entry))) {
  273. ref_entry = kscheme_find_entry_by_path(scheme, ptype_str);
  274. if (!ref_entry) {
  275. faux_error_sprintf(error, "Can't find ENTRY \"%s\" for ptype",
  276. ptype_str);
  277. retcode = BOOL_FALSE;
  278. }
  279. kentry_set_ptype(entry, ref_entry);
  280. }
  281. // ACTIONs
  282. if (!kscheme_prepare_action_list(scheme, kentry_actions(entry), error))
  283. retcode = BOOL_FALSE;
  284. // Process nested ENTRYs
  285. iter = kentry_entrys_iter(entry);
  286. while ((nested_entry = kentry_entrys_each(&iter))) {
  287. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  288. retcode = BOOL_FALSE;
  289. }
  290. return retcode;
  291. }
  292. /** @brief Prepares schema for execution.
  293. *
  294. * It loads plugins, link unresolved symbols, then iterates all the
  295. * objects and link them to each other, check access
  296. * permissions. Without this function the schema is not fully functional.
  297. */
  298. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  299. {
  300. kscheme_entrys_node_t *entrys_iter = NULL;
  301. kentry_t *entry = NULL;
  302. assert(scheme);
  303. if (!scheme)
  304. return BOOL_FALSE;
  305. if (!context)
  306. return BOOL_FALSE;
  307. if (!kscheme_load_plugins(scheme, context, error))
  308. return BOOL_FALSE;
  309. // Iterate ENTRYs
  310. entrys_iter = kscheme_entrys_iter(scheme);
  311. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  312. if (!kscheme_prepare_entry(scheme, entry, error))
  313. return BOOL_FALSE;
  314. }
  315. return BOOL_TRUE;
  316. }