ischeme.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_plugin(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_ptype(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_view(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. if (!retval)
  123. faux_error_sprintf(error, TAG": Illegal nested elements");
  124. return retval;
  125. }
  126. bool_t ischeme_load(const ischeme_t *ischeme, kscheme_t *kscheme,
  127. faux_error_t *error)
  128. {
  129. assert(kscheme);
  130. if (!kscheme) {
  131. faux_error_sprintf(error, TAG": Internal error");
  132. return BOOL_FALSE;
  133. }
  134. // Parse nested elements
  135. return ischeme_parse_nested(ischeme, kscheme, error);
  136. }
  137. char *ischeme_deploy(const kscheme_t *scheme, int level)
  138. {
  139. char *str = NULL;
  140. char *tmp = NULL;
  141. kscheme_plugins_node_t *plugins_iter = NULL;
  142. kscheme_ptypes_node_t *ptypes_iter = NULL;
  143. kscheme_views_node_t *views_iter = NULL;
  144. tmp = faux_str_sprintf("ischeme_t sch = {\n");
  145. faux_str_cat(&str, tmp);
  146. faux_str_free(tmp);
  147. // PLUGIN list
  148. plugins_iter = kscheme_plugins_iter(scheme);
  149. if (plugins_iter) {
  150. kplugin_t *plugin = NULL;
  151. tmp = faux_str_sprintf("\n%*cPLUGIN_LIST\n\n", level, ' ');
  152. faux_str_cat(&str, tmp);
  153. faux_str_free(tmp);
  154. while ((plugin = kscheme_plugins_each(&plugins_iter))) {
  155. tmp = iplugin_deploy(plugin, level + 2);
  156. faux_str_cat(&str, tmp);
  157. faux_str_free(tmp);
  158. }
  159. tmp = faux_str_sprintf("%*cEND_PLUGIN_LIST,\n", level + 1, ' ');
  160. faux_str_cat(&str, tmp);
  161. faux_str_free(tmp);
  162. }
  163. // PTYPE list
  164. ptypes_iter = kscheme_ptypes_iter(scheme);
  165. if (ptypes_iter) {
  166. kptype_t *ptype = NULL;
  167. tmp = faux_str_sprintf("\n%*cPTYPE_LIST\n\n", level, ' ');
  168. faux_str_cat(&str, tmp);
  169. faux_str_free(tmp);
  170. while ((ptype = kscheme_ptypes_each(&ptypes_iter))) {
  171. tmp = iptype_deploy(ptype, level + 2);
  172. faux_str_cat(&str, tmp);
  173. faux_str_free(tmp);
  174. }
  175. tmp = faux_str_sprintf("%*cEND_PTYPE_LIST,\n", level + 1, ' ');
  176. faux_str_cat(&str, tmp);
  177. faux_str_free(tmp);
  178. }
  179. // VIEW list
  180. views_iter = kscheme_views_iter(scheme);
  181. if (views_iter) {
  182. kview_t *view = NULL;
  183. tmp = faux_str_sprintf("\n%*cVIEW_LIST\n\n", level + 1, ' ');
  184. faux_str_cat(&str, tmp);
  185. faux_str_free(tmp);
  186. while ((view = kscheme_views_each(&views_iter))) {
  187. tmp = iview_deploy(view, level + 2);
  188. faux_str_cat(&str, tmp);
  189. faux_str_free(tmp);
  190. }
  191. tmp = faux_str_sprintf("%*cEND_VIEW_LIST,\n", level + 1, ' ');
  192. faux_str_cat(&str, tmp);
  193. faux_str_free(tmp);
  194. }
  195. tmp = faux_str_sprintf("};\n");
  196. faux_str_cat(&str, tmp);
  197. faux_str_free(tmp);
  198. return str;
  199. }