kscheme.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. kustore_free(scheme->ustore);
  66. faux_list_free(scheme->entrys);
  67. // The plugin_free() must be after all other free functions because
  68. // plugins contain free callback function for the other components.
  69. faux_list_free(scheme->plugins);
  70. faux_free(scheme);
  71. }
  72. kentry_t *kscheme_starting_entry(const kscheme_t *scheme,
  73. const char *starting_entry)
  74. {
  75. const char *entry_to_search = NULL;
  76. assert(scheme);
  77. if (!scheme)
  78. return NULL;
  79. // Starting entry can be get from the function argument, from the STARTUP
  80. // tag or default name "main" can be used. Priorities are (from higher):
  81. // argument, STARTUP, default name.
  82. if (starting_entry)
  83. entry_to_search = starting_entry;
  84. // STARTUP is not implemented yet
  85. else
  86. entry_to_search = KSCHEME_DEFAULT_STARTING_ENTRY;
  87. return kscheme_find_entry_by_path(scheme, entry_to_search);
  88. }
  89. #define TAG "PLUGIN"
  90. static bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
  91. faux_error_t *error)
  92. {
  93. bool_t retcode = BOOL_TRUE;
  94. kscheme_plugins_node_t *iter = NULL;
  95. kplugin_t *plugin = NULL;
  96. assert(scheme);
  97. if (!scheme)
  98. return BOOL_FALSE;
  99. assert(scheme->plugins);
  100. if (!context)
  101. return BOOL_FALSE;
  102. iter = kscheme_plugins_iter(scheme);
  103. while ((plugin = kscheme_plugins_each(&iter))) {
  104. int init_retcode = 0;
  105. if (!kplugin_load(plugin)) {
  106. faux_error_sprintf(error,
  107. TAG ": Can't load plugin \"%s\"",
  108. kplugin_name(plugin));
  109. retcode = BOOL_FALSE;
  110. continue; // Try to load all plugins
  111. }
  112. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_INIT);
  113. kcontext_set_plugin(context, plugin);
  114. if ((init_retcode = kplugin_init(plugin, context)) < 0) {
  115. faux_error_sprintf(error,
  116. TAG ": Can't init plugin \"%s\" (%d)",
  117. kplugin_name(plugin), init_retcode);
  118. retcode = BOOL_FALSE;
  119. continue;
  120. }
  121. }
  122. return retcode;
  123. }
  124. static bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
  125. faux_error_t *error)
  126. {
  127. kscheme_plugins_node_t *iter = NULL;
  128. kplugin_t *plugin = NULL;
  129. assert(scheme);
  130. if (!scheme)
  131. return BOOL_FALSE;
  132. assert(scheme->plugins);
  133. if (!context)
  134. return BOOL_FALSE;
  135. iter = kscheme_plugins_iter(scheme);
  136. while ((plugin = kscheme_plugins_each(&iter))) {
  137. int fini_retcode = -1;
  138. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_FINI);
  139. kcontext_set_plugin(context, plugin);
  140. if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
  141. faux_error_sprintf(error,
  142. TAG ": Can't fini plugin \"%s\" (%d)",
  143. kplugin_name(plugin), fini_retcode);
  144. }
  145. }
  146. return BOOL_TRUE;
  147. }
  148. bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  149. {
  150. assert(scheme);
  151. if (!scheme)
  152. return BOOL_FALSE;
  153. if (!context)
  154. return BOOL_FALSE;
  155. if (!kscheme_fini_plugins(scheme, context, error))
  156. return BOOL_FALSE;
  157. return BOOL_TRUE;
  158. }
  159. static ksym_t *kscheme_find_sym(const kscheme_t *scheme, const char *name,
  160. kplugin_t **src_plugin)
  161. {
  162. char *saveptr = NULL;
  163. const char *delim = "@";
  164. char *plugin_name = NULL;
  165. char *cmd_name = NULL;
  166. char *full_name = NULL;
  167. ksym_t *sym = NULL;
  168. kplugin_t *plugin = NULL; // Source of sym
  169. assert(scheme);
  170. if (!scheme)
  171. return NULL;
  172. assert(name);
  173. if (!name)
  174. return NULL;
  175. // Parse full name to get sym name and optional plugin name
  176. full_name = faux_str_dup(name);
  177. cmd_name = strtok_r(full_name, delim, &saveptr);
  178. if (!cmd_name) {
  179. faux_str_free(full_name);
  180. return NULL;
  181. }
  182. plugin_name = strtok_r(NULL, delim, &saveptr);
  183. // Search for symbol within specified PLUGIN only
  184. if (plugin_name) {
  185. plugin = kscheme_find_plugin(scheme, plugin_name);
  186. if (!plugin) {
  187. faux_str_free(full_name);
  188. return NULL;
  189. }
  190. sym = kplugin_find_sym(plugin, cmd_name);
  191. // Search for symbol within all PLUGINs
  192. } else {
  193. kscheme_plugins_node_t *iter = NULL;
  194. iter = kscheme_plugins_iter(scheme);
  195. while ((plugin = kscheme_plugins_each(&iter))) {
  196. sym = kplugin_find_sym(plugin, cmd_name);
  197. if (sym)
  198. break;
  199. }
  200. }
  201. if (sym && src_plugin)
  202. *src_plugin = plugin;
  203. faux_str_free(full_name);
  204. return sym;
  205. }
  206. bool_t kscheme_prepare_action_list(kscheme_t *scheme, kentry_t *entry,
  207. faux_error_t *error)
  208. {
  209. faux_list_node_t *iter = NULL;
  210. kaction_t *action = NULL;
  211. bool_t retcode = BOOL_TRUE;
  212. faux_list_t *action_list = NULL;
  213. assert(scheme);
  214. if (!scheme)
  215. return BOOL_FALSE;
  216. assert(entry);
  217. if (!entry)
  218. return BOOL_FALSE;
  219. action_list = kentry_actions(entry);
  220. assert(action_list);
  221. if (!action_list)
  222. return BOOL_FALSE;
  223. if (faux_list_is_empty(action_list))
  224. return BOOL_TRUE;
  225. iter = faux_list_head(action_list);
  226. while ((action = (kaction_t *)faux_list_each(&iter))) {
  227. ksym_t *sym = NULL;
  228. kplugin_t *plugin = NULL;
  229. const char *sym_ref = kaction_sym_ref(action);
  230. sym = kscheme_find_sym(scheme, sym_ref, &plugin);
  231. if (!sym) {
  232. faux_error_sprintf(error, "Can't find symbol \"%s\"",
  233. sym_ref);
  234. retcode = BOOL_FALSE;
  235. continue;
  236. }
  237. kaction_set_sym(action, sym);
  238. kaction_set_plugin(action, plugin);
  239. // Filter can't contain sync symbols
  240. if ((kentry_filter(entry) != KENTRY_FILTER_FALSE) &&
  241. kaction_is_sync(action)) {
  242. faux_error_sprintf(error, "Filter \"%s\" can't contain "
  243. "sync symbol \"%s\"",
  244. kentry_name(entry), sym_ref);
  245. retcode = BOOL_FALSE;
  246. continue;
  247. }
  248. }
  249. return retcode;
  250. }
  251. kentry_t *kscheme_find_entry_by_path(const kscheme_t *scheme, const char *name)
  252. {
  253. char *saveptr = NULL;
  254. const char *delim = "/";
  255. char *entry_name = NULL;
  256. char *full_name = NULL;
  257. kentry_t *entry = NULL;
  258. assert(scheme);
  259. if (!scheme)
  260. return NULL;
  261. assert(name);
  262. if (!name)
  263. return NULL;
  264. // Get first component of ENTRY path. It will be searched for
  265. // within scheme.
  266. full_name = faux_str_dup(name);
  267. entry_name = strtok_r(full_name, delim, &saveptr);
  268. if (!entry_name) {
  269. faux_str_free(full_name);
  270. return NULL;
  271. }
  272. entry = kscheme_find_entry(scheme, entry_name);
  273. if (!entry) {
  274. faux_str_free(full_name);
  275. return NULL;
  276. }
  277. // Search nested ENTRYs
  278. while ((entry_name = strtok_r(NULL, delim, &saveptr))) {
  279. entry = kentry_find_entry(entry, entry_name);
  280. if (!entry)
  281. break;
  282. }
  283. faux_str_free(full_name);
  284. return entry;
  285. }
  286. bool_t kscheme_prepare_entry(kscheme_t *scheme, kentry_t *entry,
  287. faux_error_t *error) {
  288. kentry_entrys_node_t *iter = NULL;
  289. kentry_t *nested_entry = NULL;
  290. bool_t retcode = BOOL_TRUE;
  291. const char *ref = NULL;
  292. kentry_t *ref_entry = NULL;
  293. assert(scheme);
  294. if (!scheme)
  295. return BOOL_FALSE;
  296. assert(entry);
  297. if (!entry)
  298. return BOOL_FALSE;
  299. // Firstly if ENTRY is link to another ENTRY then make a copy
  300. if (kentry_ref_str(entry)) {
  301. ref_entry = entry;
  302. // Find the most deep real (non-link) object to reference to it
  303. // i.e. the link can't reference a link.
  304. while ((ref = kentry_ref_str(ref_entry))) {
  305. ref_entry = kscheme_find_entry_by_path(scheme, ref);
  306. if (!ref_entry) {
  307. faux_error_sprintf(error, "Can't find ENTRY \"%s\"", ref);
  308. return BOOL_FALSE;
  309. }
  310. }
  311. if (!kentry_link(entry, ref_entry)) {
  312. faux_error_sprintf(error, "Can't create link to ENTRY \"%s\"", ref);
  313. return BOOL_FALSE;
  314. }
  315. return BOOL_TRUE;
  316. }
  317. // ACTIONs
  318. if (!kscheme_prepare_action_list(scheme, entry, error))
  319. retcode = BOOL_FALSE;
  320. // Create fast links to nested entries with special purposes. Do it
  321. // before preparing child elements because some fields can be inhereted
  322. // by child from its parent
  323. iter = kentry_entrys_iter(entry);
  324. while ((nested_entry = kentry_entrys_each(&iter)))
  325. kentry_set_nested_by_purpose(entry,
  326. kentry_purpose(nested_entry), nested_entry);
  327. // Inherit LOG from parent if it's not specified explicitly
  328. if (!kentry_nested_by_purpose(entry, KENTRY_PURPOSE_LOG)) {
  329. kentry_t *parent = kentry_parent(entry);
  330. if (parent) { // Get LOG from parent entry
  331. kentry_set_nested_by_purpose(entry, KENTRY_PURPOSE_LOG,
  332. kentry_nested_by_purpose(parent,
  333. KENTRY_PURPOSE_LOG));
  334. } else { // High level entries has no parents
  335. iter = kscheme_entrys_iter(scheme);
  336. while ((nested_entry = kscheme_entrys_each(&iter))) {
  337. if (kentry_purpose(nested_entry) !=
  338. KENTRY_PURPOSE_LOG)
  339. continue;
  340. kentry_set_nested_by_purpose(entry,
  341. KENTRY_PURPOSE_LOG, nested_entry);
  342. }
  343. }
  344. }
  345. // Process nested ENTRYs
  346. iter = kentry_entrys_iter(entry);
  347. while ((nested_entry = kentry_entrys_each(&iter)))
  348. if (!kscheme_prepare_entry(scheme, nested_entry, error))
  349. retcode = BOOL_FALSE;
  350. return retcode;
  351. }
  352. /** @brief Prepares schema for execution.
  353. *
  354. * It loads plugins, link unresolved symbols, then iterates all the
  355. * objects and link them to each other, check access
  356. * permissions. Without this function the schema is not fully functional.
  357. */
  358. bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
  359. {
  360. kscheme_entrys_node_t *entrys_iter = NULL;
  361. kentry_t *entry = NULL;
  362. assert(scheme);
  363. if (!scheme)
  364. return BOOL_FALSE;
  365. if (!context)
  366. return BOOL_FALSE;
  367. if (!kscheme_load_plugins(scheme, context, error))
  368. return BOOL_FALSE;
  369. // Iterate ENTRYs
  370. entrys_iter = kscheme_entrys_iter(scheme);
  371. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  372. if (!kscheme_prepare_entry(scheme, entry, error))
  373. return BOOL_FALSE;
  374. }
  375. return BOOL_TRUE;
  376. }
  377. bool_t kscheme_named_udata_new(kscheme_t *scheme,
  378. const char *name, void *data, kudata_data_free_fn free_fn)
  379. {
  380. kudata_t *udata = NULL;
  381. assert(scheme);
  382. if (!scheme)
  383. return BOOL_FALSE;
  384. assert(scheme->ustore);
  385. udata = kustore_slot_new(scheme->ustore, name, data, free_fn);
  386. if (!udata)
  387. return BOOL_FALSE;
  388. return BOOL_TRUE;
  389. }
  390. void *kscheme_named_udata(kscheme_t *scheme, const char *name)
  391. {
  392. assert(scheme);
  393. if (!scheme)
  394. return BOOL_FALSE;
  395. assert(scheme->ustore);
  396. return kustore_slot_data(scheme->ustore, name);
  397. }
  398. bool_t kscheme_init_session_plugins(kscheme_t *scheme, kcontext_t *context,
  399. faux_error_t *error)
  400. {
  401. bool_t retcode = BOOL_TRUE;
  402. kscheme_plugins_node_t *iter = NULL;
  403. kplugin_t *plugin = NULL;
  404. assert(scheme);
  405. if (!scheme)
  406. return BOOL_FALSE;
  407. assert(scheme->plugins);
  408. if (!context)
  409. return BOOL_FALSE;
  410. iter = kscheme_plugins_iter(scheme);
  411. while ((plugin = kscheme_plugins_each(&iter))) {
  412. int init_retcode = 0;
  413. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_INIT);
  414. kcontext_set_plugin(context, plugin);
  415. if ((init_retcode = kplugin_init_session(plugin, context)) < 0) {
  416. faux_error_sprintf(error,
  417. TAG ": Can't init session for plugin \"%s\" (%d)",
  418. kplugin_name(plugin), init_retcode);
  419. retcode = BOOL_FALSE;
  420. continue;
  421. }
  422. }
  423. return retcode;
  424. }
  425. bool_t kscheme_fini_session_plugins(kscheme_t *scheme, kcontext_t *context,
  426. faux_error_t *error)
  427. {
  428. kscheme_plugins_node_t *iter = NULL;
  429. kplugin_t *plugin = NULL;
  430. assert(scheme);
  431. if (!scheme)
  432. return BOOL_FALSE;
  433. assert(scheme->plugins);
  434. if (!context)
  435. return BOOL_FALSE;
  436. iter = kscheme_plugins_iter(scheme);
  437. while ((plugin = kscheme_plugins_each(&iter))) {
  438. int fini_retcode = -1;
  439. kcontext_set_type(context, KCONTEXT_TYPE_PLUGIN_FINI);
  440. kcontext_set_plugin(context, plugin);
  441. if ((fini_retcode = kplugin_fini_session(plugin, context)) < 0) {
  442. faux_error_sprintf(error,
  443. TAG ": Can't fini session for plugin \"%s\" (%d)",
  444. kplugin_name(plugin), fini_retcode);
  445. }
  446. }
  447. return BOOL_TRUE;
  448. }