ischeme.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <klish/khelper.h>
  8. #include <klish/kplugin.h>
  9. #include <klish/kptype.h>
  10. #include <klish/kview.h>
  11. #include <klish/kscheme.h>
  12. #include <klish/iview.h>
  13. #include <klish/ischeme.h>
  14. #define TAG "SCHEME"
  15. bool_t ischeme_parse_nested(const ischeme_t *ischeme, kscheme_t *kscheme,
  16. faux_error_t *error)
  17. {
  18. bool_t retval = BOOL_TRUE;
  19. if (!kscheme || !ischeme) {
  20. faux_error_add(error, TAG": Internal error");
  21. return BOOL_FALSE;
  22. }
  23. // PLUGIN list
  24. if (ischeme->plugins) {
  25. iplugin_t **p_iplugin = NULL;
  26. for (p_iplugin = *ischeme->plugins; *p_iplugin; p_iplugin++) {
  27. kplugin_t *kplugin = NULL;
  28. iplugin_t *iplugin = *p_iplugin;
  29. kplugin = iplugin_load(iplugin, error);
  30. if (!kplugin) {
  31. retval = BOOL_FALSE; // Don't stop
  32. continue;
  33. }
  34. if (!kscheme_add_plugins(kscheme, kplugin)) {
  35. // Search for PLUGIN duplicates
  36. if (kscheme_find_plugin(kscheme,
  37. kplugin_name(kplugin))) {
  38. faux_error_sprintf(error,
  39. TAG": Can't add duplicate PLUGIN "
  40. "\"%s\"", kplugin_name(kplugin));
  41. } else {
  42. faux_error_sprintf(error,
  43. TAG": Can't add PLUGIN \"%s\"",
  44. kplugin_name(kplugin));
  45. }
  46. kplugin_free(kplugin);
  47. retval = BOOL_FALSE;
  48. }
  49. }
  50. }
  51. // PTYPE list
  52. if (ischeme->ptypes) {
  53. iptype_t **p_iptype = NULL;
  54. for (p_iptype = *ischeme->ptypes; *p_iptype; p_iptype++) {
  55. kptype_t *kptype = NULL;
  56. iptype_t *iptype = *p_iptype;
  57. kptype = iptype_load(iptype, error);
  58. if (!kptype) {
  59. retval = BOOL_FALSE; // Don't stop
  60. continue;
  61. }
  62. if (!kscheme_add_ptypes(kscheme, kptype)) {
  63. // Search for PTYPE duplicates
  64. if (kscheme_find_ptype(kscheme,
  65. kptype_name(kptype))) {
  66. faux_error_sprintf(error,
  67. TAG": Can't add duplicate PTYPE "
  68. "\"%s\"", kptype_name(kptype));
  69. } else {
  70. faux_error_sprintf(error,
  71. TAG": Can't add PTYPE \"%s\"",
  72. kptype_name(kptype));
  73. }
  74. kptype_free(kptype);
  75. retval = BOOL_FALSE;
  76. }
  77. }
  78. }
  79. // VIEW list
  80. // VIEW entries can be duplicate. Duplicated entries will add nested
  81. // elements to existent VIEW. Also it can overwrite VIEW attributes.
  82. // So there is no special rule which attribute value will be "on top".
  83. // It's a random. Technically later VIEW entries will rewrite previous
  84. // values.
  85. if (ischeme->views) {
  86. iview_t **p_iview = NULL;
  87. for (p_iview = *ischeme->views; *p_iview; p_iview++) {
  88. kview_t *kview = NULL;
  89. iview_t *iview = *p_iview;
  90. const char *view_name = iview->name;
  91. if (view_name)
  92. kview = kscheme_find_view(kscheme, view_name);
  93. // VIEW already exists
  94. if (kview) {
  95. if (!iview_parse(iview, kview, error)) {
  96. retval = BOOL_FALSE;
  97. continue;
  98. }
  99. if (!iview_parse_nested(iview, kview,
  100. error)) {
  101. retval = BOOL_FALSE;
  102. continue;
  103. }
  104. continue;
  105. }
  106. // New VIEW
  107. kview = iview_load(iview, error);
  108. if (!kview) {
  109. retval = BOOL_FALSE;
  110. continue;
  111. }
  112. if (!kscheme_add_views(kscheme, kview)) {
  113. faux_error_sprintf(error,
  114. TAG": Can't add VIEW \"%s\"",
  115. kview_name(kview));
  116. kview_free(kview);
  117. retval = BOOL_FALSE;
  118. continue;
  119. }
  120. }
  121. }
  122. // ENTRY list
  123. // ENTRYs can be duplicate. Duplicated ENTRY will add nested
  124. // elements to existent ENTRY. Also it can overwrite ENTRY attributes.
  125. // So there is no special rule which attribute value will be "on top".
  126. // It's a random. Technically later ENTRYs will rewrite previous
  127. // values.
  128. if (ischeme->entrys) {
  129. ientry_t **p_ientry = NULL;
  130. for (p_ientry = *ischeme->entrys; *p_ientry; p_ientry++) {
  131. kentry_t *nkentry = NULL;
  132. ientry_t *nientry = *p_ientry;
  133. const char *entry_name = nientry->name;
  134. if (entry_name)
  135. nkentry = kscheme_find_entry(kscheme, entry_name);
  136. // ENTRY already exists
  137. if (nkentry) {
  138. if (!ientry_parse(nientry, nkentry, error)) {
  139. retval = BOOL_FALSE;
  140. continue;
  141. }
  142. if (!ientry_parse_nested(nientry, nkentry,
  143. error)) {
  144. retval = BOOL_FALSE;
  145. continue;
  146. }
  147. continue;
  148. }
  149. // New ENTRY
  150. nkentry = ientry_load(nientry, error);
  151. if (!nkentry) {
  152. retval = BOOL_FALSE;
  153. continue;
  154. }
  155. kentry_set_parent(nkentry, NULL); // Set empty parent entry
  156. if (!kscheme_add_entrys(kscheme, nkentry)) {
  157. faux_error_sprintf(error,
  158. TAG": Can't add ENTRY \"%s\"",
  159. kentry_name(nkentry));
  160. kentry_free(nkentry);
  161. retval = BOOL_FALSE;
  162. continue;
  163. }
  164. }
  165. }
  166. if (!retval)
  167. faux_error_sprintf(error, TAG": Illegal nested elements");
  168. return retval;
  169. }
  170. bool_t ischeme_load(const ischeme_t *ischeme, kscheme_t *kscheme,
  171. faux_error_t *error)
  172. {
  173. assert(kscheme);
  174. if (!kscheme) {
  175. faux_error_sprintf(error, TAG": Internal error");
  176. return BOOL_FALSE;
  177. }
  178. // Parse nested elements
  179. return ischeme_parse_nested(ischeme, kscheme, error);
  180. }
  181. char *ischeme_deploy(const kscheme_t *scheme, int level)
  182. {
  183. char *str = NULL;
  184. char *tmp = NULL;
  185. kscheme_plugins_node_t *plugins_iter = NULL;
  186. kscheme_ptypes_node_t *ptypes_iter = NULL;
  187. kscheme_views_node_t *views_iter = NULL;
  188. kscheme_entrys_node_t *entrys_iter = NULL;
  189. tmp = faux_str_sprintf("ischeme_t sch = {\n");
  190. faux_str_cat(&str, tmp);
  191. faux_str_free(tmp);
  192. // PLUGIN list
  193. plugins_iter = kscheme_plugins_iter(scheme);
  194. if (plugins_iter) {
  195. kplugin_t *plugin = NULL;
  196. tmp = faux_str_sprintf("\n%*cPLUGIN_LIST\n\n", level, ' ');
  197. faux_str_cat(&str, tmp);
  198. faux_str_free(tmp);
  199. while ((plugin = kscheme_plugins_each(&plugins_iter))) {
  200. tmp = iplugin_deploy(plugin, level + 2);
  201. faux_str_cat(&str, tmp);
  202. faux_str_free(tmp);
  203. }
  204. tmp = faux_str_sprintf("%*cEND_PLUGIN_LIST,\n", level + 1, ' ');
  205. faux_str_cat(&str, tmp);
  206. faux_str_free(tmp);
  207. }
  208. // PTYPE list
  209. ptypes_iter = kscheme_ptypes_iter(scheme);
  210. if (ptypes_iter) {
  211. kptype_t *ptype = NULL;
  212. tmp = faux_str_sprintf("\n%*cPTYPE_LIST\n\n", level, ' ');
  213. faux_str_cat(&str, tmp);
  214. faux_str_free(tmp);
  215. while ((ptype = kscheme_ptypes_each(&ptypes_iter))) {
  216. tmp = iptype_deploy(ptype, level + 2);
  217. faux_str_cat(&str, tmp);
  218. faux_str_free(tmp);
  219. }
  220. tmp = faux_str_sprintf("%*cEND_PTYPE_LIST,\n", level + 1, ' ');
  221. faux_str_cat(&str, tmp);
  222. faux_str_free(tmp);
  223. }
  224. // VIEW list
  225. views_iter = kscheme_views_iter(scheme);
  226. if (views_iter) {
  227. kview_t *view = NULL;
  228. tmp = faux_str_sprintf("\n%*cVIEW_LIST\n\n", level + 1, ' ');
  229. faux_str_cat(&str, tmp);
  230. faux_str_free(tmp);
  231. while ((view = kscheme_views_each(&views_iter))) {
  232. tmp = iview_deploy(view, level + 2);
  233. faux_str_cat(&str, tmp);
  234. faux_str_free(tmp);
  235. }
  236. tmp = faux_str_sprintf("%*cEND_VIEW_LIST,\n", level + 1, ' ');
  237. faux_str_cat(&str, tmp);
  238. faux_str_free(tmp);
  239. }
  240. // ENTRY list
  241. entrys_iter = kscheme_entrys_iter(scheme);
  242. if (entrys_iter) {
  243. kentry_t *entry = NULL;
  244. tmp = faux_str_sprintf("\n%*cENTRY_LIST\n\n", level, ' ');
  245. faux_str_cat(&str, tmp);
  246. faux_str_free(tmp);
  247. while ((entry = kscheme_entrys_each(&entrys_iter))) {
  248. tmp = ientry_deploy(entry, level + 2);
  249. faux_str_cat(&str, tmp);
  250. faux_str_free(tmp);
  251. }
  252. tmp = faux_str_sprintf("%*cEND_ENTRY_LIST,\n", level + 1, ' ');
  253. faux_str_cat(&str, tmp);
  254. faux_str_free(tmp);
  255. }
  256. tmp = faux_str_sprintf("};\n");
  257. faux_str_cat(&str, tmp);
  258. faux_str_free(tmp);
  259. return str;
  260. }