ischeme.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/kview.h>
  9. #include <klish/kscheme.h>
  10. bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
  11. faux_error_t *error_stack)
  12. {
  13. if (!kview || !iview) {
  14. if (error_stack)
  15. faux_error_add(error_stack,
  16. kview_strerror(KVIEW_ERROR_INTERNAL));
  17. return BOOL_FALSE;
  18. }
  19. // COMMAND list
  20. if (iview->commands) {
  21. icommand_t **p_icommand = NULL;
  22. for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
  23. kcommand_t *kcommand = NULL;
  24. icommand_t *icommand = *p_icommand;
  25. printf("command %s\n", icommand->name);
  26. // kcommand = kcommand_from_icommand(icommand, error_stack);
  27. // if (!kcommand)
  28. // continue;
  29. kcommand = kcommand;
  30. }
  31. }
  32. return BOOL_TRUE;
  33. }
  34. kview_t *kview_from_iview(iview_t *iview, faux_error_t *error_stack)
  35. {
  36. kview_t *kview = NULL;
  37. kview_error_e kview_error = KVIEW_ERROR_OK;
  38. ssize_t error_stack_len = 0;
  39. kview = kview_new(iview, &kview_error);
  40. if (!kview) {
  41. if (error_stack) {
  42. char *msg = NULL;
  43. msg = faux_str_sprintf("VIEW \"%s\": %s",
  44. iview->name ? iview->name : "(null)",
  45. kview_strerror(kview_error));
  46. faux_error_add(error_stack, msg);
  47. faux_str_free(msg);
  48. }
  49. return NULL;
  50. }
  51. printf("view %s\n", kview_name(kview));
  52. // Parse nested elements
  53. if (error_stack)
  54. error_stack_len = faux_error_len(error_stack);
  55. kview_nested_from_iview(kview, iview, error_stack);
  56. if (error_stack && (faux_error_len(error_stack) > error_stack_len)) {
  57. char *msg = NULL;
  58. msg = faux_str_sprintf("VIEW \"%s\": Illegal nested elements",
  59. kview_name(kview));
  60. faux_error_add(error_stack, msg);
  61. faux_str_free(msg);
  62. }
  63. return kview;
  64. }
  65. bool_t kptype_nested_from_iptype(kptype_t *kptype, iptype_t *iptype,
  66. faux_error_t *error_stack)
  67. {
  68. if (!kptype || !iptype) {
  69. if (error_stack)
  70. faux_error_add(error_stack,
  71. kptype_strerror(KPTYPE_ERROR_INTERNAL));
  72. return BOOL_FALSE;
  73. }
  74. // ACTION list
  75. if (iptype->actions) {
  76. iaction_t **p_iaction = NULL;
  77. for (p_iaction = *iptype->actions; *p_iaction; p_iaction++) {
  78. kaction_t *kaction = NULL;
  79. iaction_t *iaction = *p_iaction;
  80. iaction = iaction;
  81. printf("action\n");
  82. // kaction = kaction_from_iaction(iaction, error_stack);
  83. // if (!kaction)
  84. // continue;
  85. kaction = kaction;
  86. }
  87. }
  88. return BOOL_TRUE;
  89. }
  90. kptype_t *kptype_from_iptype(iptype_t *iptype, faux_error_t *error_stack)
  91. {
  92. kptype_t *kptype = NULL;
  93. kptype_error_e kptype_error = KPTYPE_ERROR_OK;
  94. ssize_t error_stack_len = 0;
  95. kptype = kptype_new(iptype, &kptype_error);
  96. if (!kptype) {
  97. if (error_stack) {
  98. char *msg = NULL;
  99. msg = faux_str_sprintf("PTYPE \"%s\": %s",
  100. iptype->name ? iptype->name : "(null)",
  101. kptype_strerror(kptype_error));
  102. faux_error_add(error_stack, msg);
  103. faux_str_free(msg);
  104. }
  105. return NULL;
  106. }
  107. printf("ptype %s\n", kptype_name(kptype));
  108. // Parse nested elements
  109. if (error_stack)
  110. error_stack_len = faux_error_len(error_stack);
  111. kptype_nested_from_iptype(kptype, iptype, error_stack);
  112. if (error_stack && (faux_error_len(error_stack) > error_stack_len)) {
  113. char *msg = NULL;
  114. msg = faux_str_sprintf("PTYPE \"%s\": Illegal nested elements",
  115. kptype_name(kptype));
  116. faux_error_add(error_stack, msg);
  117. faux_str_free(msg);
  118. }
  119. return kptype;
  120. }
  121. bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
  122. faux_error_t *error_stack)
  123. {
  124. bool_t retval = BOOL_TRUE;
  125. if (!kscheme || !ischeme) {
  126. if (error_stack)
  127. faux_error_add(error_stack,
  128. kscheme_strerror(KSCHEME_ERROR_INTERNAL));
  129. return BOOL_FALSE;
  130. }
  131. // PTYPE list
  132. if (ischeme->ptypes) {
  133. iptype_t **p_iptype = NULL;
  134. for (p_iptype = *ischeme->ptypes; *p_iptype; p_iptype++) {
  135. kptype_t *kptype = NULL;
  136. iptype_t *iptype = *p_iptype;
  137. kptype = kptype_from_iptype(iptype, error_stack);
  138. if (!kptype) {
  139. retval = BOOL_FALSE; // Don't stop
  140. continue;
  141. }
  142. if (!kscheme_add_ptype(kscheme, kptype)) {
  143. if (error_stack) {
  144. char *msg = NULL;
  145. // Search for PTYPE duplicates
  146. if (kscheme_find_ptype(kscheme,
  147. kptype_name(kptype))) {
  148. msg = faux_str_sprintf("SCHEME: "
  149. "Can't add duplicate PTYPE "
  150. "\"%s\"",
  151. kptype_name(kptype));
  152. } else {
  153. msg = faux_str_sprintf("SCHEME: "
  154. "Can't add PTYPE \"%s\"",
  155. kptype_name(kptype));
  156. }
  157. faux_error_add(error_stack, msg);
  158. faux_str_free(msg);
  159. }
  160. retval = BOOL_FALSE;
  161. }
  162. }
  163. }
  164. // VIEW list
  165. // VIEW entries can be duplicate. Duplicated entries will add nested
  166. // elements to existent VIEW. Also it can overwrite VIEW attributes.
  167. // So there is no special rule which attribute value will be "on top".
  168. // It's a random. Technically later VIEW entries will rewrite previous
  169. // values.
  170. if (ischeme->views) {
  171. iview_t **p_iview = NULL;
  172. for (p_iview = *ischeme->views; *p_iview; p_iview++) {
  173. kview_t *kview = NULL;
  174. iview_t *iview = *p_iview;
  175. const char *view_name = iview->name;
  176. if (view_name)
  177. kview = kscheme_find_view(kscheme, view_name);
  178. if (kview) {
  179. kview_error_e kview_error = KVIEW_ERROR_OK;
  180. if (!kview_parse(kview, iview, &kview_error)) {
  181. char *msg = faux_str_sprintf("VIEW \"%s\": %s",
  182. iview->name ? iview->name : "(null)",
  183. kview_strerror(kview_error));
  184. faux_error_add(error_stack, msg);
  185. faux_str_free(msg);
  186. retval = BOOL_FALSE;
  187. continue;
  188. }
  189. if (!kview_nested_from_iview(kview, iview,
  190. error_stack)) {
  191. retval = BOOL_FALSE;
  192. continue;
  193. }
  194. } else {
  195. kview = kview_from_iview(iview, error_stack);
  196. }
  197. if (!kview) {
  198. retval = BOOL_FALSE;
  199. continue;
  200. }
  201. if (!kscheme_add_view(kscheme, kview) && error_stack) {
  202. char *msg = faux_str_sprintf("SCHEME: "
  203. "Can't add PTYPE \"%s\"",
  204. kview_name(kview));
  205. faux_error_add(error_stack, msg);
  206. faux_str_free(msg);
  207. retval = BOOL_FALSE;
  208. continue;
  209. }
  210. }
  211. }
  212. return retval;
  213. }
  214. kscheme_t *kscheme_from_ischeme(ischeme_t *ischeme, faux_error_t *error_stack)
  215. {
  216. kscheme_t *kscheme = NULL;
  217. kscheme_error_e kscheme_error = KSCHEME_ERROR_OK;
  218. kscheme = kscheme_new(&kscheme_error);
  219. if (!kscheme) {
  220. if (error_stack)
  221. faux_error_add(error_stack,
  222. kscheme_strerror(kscheme_error));
  223. return NULL;
  224. }
  225. if (!kscheme_nested_from_ischeme(kscheme, ischeme, error_stack)) {
  226. kscheme_free(kscheme);
  227. return NULL;
  228. }
  229. return kscheme;
  230. }