ischeme.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/ischeme.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kptype.h>
  11. #include <klish/kview.h>
  12. #include <klish/kscheme.h>
  13. #define TAG "SCHEME"
  14. bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
  15. faux_error_t *error)
  16. {
  17. bool_t retval = BOOL_TRUE;
  18. if (!kscheme || !ischeme) {
  19. faux_error_add(error, TAG": Internal error");
  20. return BOOL_FALSE;
  21. }
  22. // PLUGIN list
  23. if (ischeme->plugins) {
  24. iplugin_t **p_iplugin = NULL;
  25. for (p_iplugin = *ischeme->plugins; *p_iplugin; p_iplugin++) {
  26. kplugin_t *kplugin = NULL;
  27. iplugin_t *iplugin = *p_iplugin;
  28. kplugin = kplugin_from_iplugin(iplugin, error);
  29. if (!kplugin) {
  30. retval = BOOL_FALSE; // Don't stop
  31. continue;
  32. }
  33. if (!kscheme_add_plugin(kscheme, kplugin)) {
  34. // Search for PLUGIN duplicates
  35. if (kscheme_find_plugin(kscheme,
  36. kplugin_name(kplugin))) {
  37. faux_error_sprintf(error,
  38. TAG": Can't add duplicate PLUGIN "
  39. "\"%s\"", kplugin_name(kplugin));
  40. } else {
  41. faux_error_sprintf(error,
  42. TAG": Can't add PLUGIN \"%s\"",
  43. kplugin_name(kplugin));
  44. }
  45. kplugin_free(kplugin);
  46. retval = BOOL_FALSE;
  47. }
  48. }
  49. }
  50. // PTYPE list
  51. if (ischeme->ptypes) {
  52. iptype_t **p_iptype = NULL;
  53. for (p_iptype = *ischeme->ptypes; *p_iptype; p_iptype++) {
  54. kptype_t *kptype = NULL;
  55. iptype_t *iptype = *p_iptype;
  56. kptype = kptype_from_iptype(iptype, error);
  57. if (!kptype) {
  58. retval = BOOL_FALSE; // Don't stop
  59. continue;
  60. }
  61. if (!kscheme_add_ptype(kscheme, kptype)) {
  62. // Search for PTYPE duplicates
  63. if (kscheme_find_ptype(kscheme,
  64. kptype_name(kptype))) {
  65. faux_error_sprintf(error,
  66. TAG": Can't add duplicate PTYPE "
  67. "\"%s\"", kptype_name(kptype));
  68. } else {
  69. faux_error_sprintf(error,
  70. TAG": Can't add PTYPE \"%s\"",
  71. kptype_name(kptype));
  72. }
  73. kptype_free(kptype);
  74. retval = BOOL_FALSE;
  75. }
  76. }
  77. }
  78. // VIEW list
  79. // VIEW entries can be duplicate. Duplicated entries will add nested
  80. // elements to existent VIEW. Also it can overwrite VIEW attributes.
  81. // So there is no special rule which attribute value will be "on top".
  82. // It's a random. Technically later VIEW entries will rewrite previous
  83. // values.
  84. if (ischeme->views) {
  85. iview_t **p_iview = NULL;
  86. for (p_iview = *ischeme->views; *p_iview; p_iview++) {
  87. kview_t *kview = NULL;
  88. iview_t *iview = *p_iview;
  89. const char *view_name = iview->name;
  90. if (view_name)
  91. kview = kscheme_find_view(kscheme, view_name);
  92. // VIEW already exists
  93. if (kview) {
  94. if (!kview_parse(kview, iview, error)) {
  95. retval = BOOL_FALSE;
  96. continue;
  97. }
  98. if (!kview_nested_from_iview(kview, iview,
  99. error)) {
  100. retval = BOOL_FALSE;
  101. continue;
  102. }
  103. continue;
  104. }
  105. // New VIEW
  106. kview = kview_from_iview(iview, error);
  107. if (!kview) {
  108. retval = BOOL_FALSE;
  109. continue;
  110. }
  111. if (!kscheme_add_view(kscheme, kview)) {
  112. faux_error_sprintf(error,
  113. TAG": Can't add VIEW \"%s\"",
  114. kview_name(kview));
  115. kview_free(kview);
  116. retval = BOOL_FALSE;
  117. continue;
  118. }
  119. }
  120. }
  121. if (!retval)
  122. faux_error_sprintf(error, TAG": Illegal nested elements");
  123. return retval;
  124. }
  125. kscheme_t *kscheme_from_ischeme(ischeme_t *ischeme, faux_error_t *error)
  126. {
  127. kscheme_t *kscheme = NULL;
  128. kscheme = kscheme_new();
  129. if (!kscheme) {
  130. faux_error_sprintf(error, TAG": Can't create object");
  131. return NULL;
  132. }
  133. // Parse nested elements
  134. if (!kscheme_nested_from_ischeme(kscheme, ischeme, error)) {
  135. kscheme_free(kscheme);
  136. return NULL;
  137. }
  138. return kscheme;
  139. }
  140. char *ischeme_to_text(const ischeme_t *ischeme, int level)
  141. {
  142. char *str = NULL;
  143. char *tmp = 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. if (ischeme->plugins) {
  149. iplugin_t **p_iplugin = NULL;
  150. tmp = faux_str_sprintf("\n%*cPLUGIN_LIST\n\n", level, ' ');
  151. faux_str_cat(&str, tmp);
  152. faux_str_free(tmp);
  153. for (p_iplugin = *ischeme->plugins; *p_iplugin; p_iplugin++) {
  154. iplugin_t *iplugin = *p_iplugin;
  155. tmp = iplugin_to_text(iplugin, 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. if (ischeme->ptypes) {
  165. iptype_t **p_iptype = NULL;
  166. tmp = faux_str_sprintf("\n%*cPTYPE_LIST\n\n", level, ' ');
  167. faux_str_cat(&str, tmp);
  168. faux_str_free(tmp);
  169. for (p_iptype = *ischeme->ptypes; *p_iptype; p_iptype++) {
  170. iptype_t *iptype = *p_iptype;
  171. tmp = iptype_to_text(iptype, 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. if (ischeme->views) {
  181. iview_t **p_iview = NULL;
  182. tmp = faux_str_sprintf("\n%*cVIEW_LIST\n\n", level + 1, ' ');
  183. faux_str_cat(&str, tmp);
  184. faux_str_free(tmp);
  185. for (p_iview = *ischeme->views; *p_iview; p_iview++) {
  186. iview_t *iview = *p_iview;
  187. tmp = iview_to_text(iview, 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. }