kscheme.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. struct kscheme_s {
  13. faux_list_t *plugins;
  14. faux_list_t *ptypes;
  15. faux_list_t *views;
  16. };
  17. // Simple methods
  18. // PLUGIN list
  19. KCMP_NESTED(scheme, plugin, name);
  20. KCMP_NESTED_BY_KEY(scheme, plugin, name);
  21. KADD_NESTED(scheme, plugin);
  22. KFIND_NESTED(scheme, plugin);
  23. // PTYPE list
  24. KCMP_NESTED(scheme, ptype, name);
  25. KCMP_NESTED_BY_KEY(scheme, ptype, name);
  26. KADD_NESTED(scheme, ptype);
  27. KFIND_NESTED(scheme, ptype);
  28. // VIEW list
  29. KCMP_NESTED(scheme, view, name);
  30. KCMP_NESTED_BY_KEY(scheme, view, name);
  31. KADD_NESTED(scheme, view);
  32. KFIND_NESTED(scheme, view);
  33. kscheme_t *kscheme_new(kscheme_error_e *error)
  34. {
  35. kscheme_t *scheme = NULL;
  36. scheme = faux_zmalloc(sizeof(*scheme));
  37. assert(scheme);
  38. if (!scheme) {
  39. if (error)
  40. *error = KSCHEME_ERROR_ALLOC;
  41. return NULL;
  42. }
  43. // PLUGIN list
  44. scheme->plugins = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  45. kscheme_plugin_compare, kscheme_plugin_kcompare,
  46. (void (*)(void *))kplugin_free);
  47. assert(scheme->plugins);
  48. // PTYPE list
  49. scheme->ptypes = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  50. kscheme_ptype_compare, kscheme_ptype_kcompare,
  51. (void (*)(void *))kptype_free);
  52. assert(scheme->ptypes);
  53. // VIEW list
  54. scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  55. kscheme_view_compare, kscheme_view_kcompare,
  56. (void (*)(void *))kview_free);
  57. assert(scheme->views);
  58. return scheme;
  59. }
  60. void kscheme_free(kscheme_t *scheme)
  61. {
  62. if (!scheme)
  63. return;
  64. faux_list_free(scheme->plugins);
  65. faux_list_free(scheme->ptypes);
  66. faux_list_free(scheme->views);
  67. faux_free(scheme);
  68. }
  69. const char *kscheme_strerror(kscheme_error_e error)
  70. {
  71. const char *str = NULL;
  72. switch (error) {
  73. case KSCHEME_ERROR_OK:
  74. str = "Ok";
  75. break;
  76. case KSCHEME_ERROR_INTERNAL:
  77. str = "Internal error";
  78. break;
  79. case KVIEW_ERROR_ALLOC:
  80. str = "Memory allocation error";
  81. break;
  82. default:
  83. str = "Unknown error";
  84. break;
  85. }
  86. return str;
  87. }
  88. bool_t kscheme_nested_from_ischeme(kscheme_t *kscheme, ischeme_t *ischeme,
  89. faux_error_t *error_stack)
  90. {
  91. bool_t retval = BOOL_TRUE;
  92. if (!kscheme || !ischeme) {
  93. faux_error_add(error_stack,
  94. kscheme_strerror(KSCHEME_ERROR_INTERNAL));
  95. return BOOL_FALSE;
  96. }
  97. // PLUGIN list
  98. if (ischeme->plugins) {
  99. iplugin_t **p_iplugin = NULL;
  100. for (p_iplugin = *ischeme->plugins; *p_iplugin; p_iplugin++) {
  101. kplugin_t *kplugin = NULL;
  102. iplugin_t *iplugin = *p_iplugin;
  103. kplugin = kplugin_from_iplugin(iplugin, error_stack);
  104. if (!kplugin) {
  105. retval = BOOL_FALSE; // Don't stop
  106. continue;
  107. }
  108. if (!kscheme_add_plugin(kscheme, kplugin)) {
  109. // Search for PLUGIN duplicates
  110. if (kscheme_find_plugin(kscheme,
  111. kplugin_name(kplugin))) {
  112. faux_error_sprintf(error_stack,
  113. "SCHEME: "
  114. "Can't add duplicate PLUGIN "
  115. "\"%s\"",
  116. kplugin_name(kplugin));
  117. } else {
  118. faux_error_sprintf(error_stack,
  119. "SCHEME: "
  120. "Can't add PLUGIN \"%s\"",
  121. kplugin_name(kplugin));
  122. }
  123. kplugin_free(kplugin);
  124. retval = BOOL_FALSE;
  125. }
  126. }
  127. }
  128. // PTYPE list
  129. if (ischeme->ptypes) {
  130. iptype_t **p_iptype = NULL;
  131. for (p_iptype = *ischeme->ptypes; *p_iptype; p_iptype++) {
  132. kptype_t *kptype = NULL;
  133. iptype_t *iptype = *p_iptype;
  134. kptype = kptype_from_iptype(iptype, error_stack);
  135. if (!kptype) {
  136. retval = BOOL_FALSE; // Don't stop
  137. continue;
  138. }
  139. if (!kscheme_add_ptype(kscheme, kptype)) {
  140. // Search for PTYPE duplicates
  141. if (kscheme_find_ptype(kscheme,
  142. kptype_name(kptype))) {
  143. faux_error_sprintf(error_stack,
  144. "SCHEME: "
  145. "Can't add duplicate PTYPE "
  146. "\"%s\"",
  147. kptype_name(kptype));
  148. } else {
  149. faux_error_sprintf(error_stack,
  150. "SCHEME: "
  151. "Can't add PTYPE \"%s\"",
  152. kptype_name(kptype));
  153. }
  154. kptype_free(kptype);
  155. retval = BOOL_FALSE;
  156. }
  157. }
  158. }
  159. // VIEW list
  160. // VIEW entries can be duplicate. Duplicated entries will add nested
  161. // elements to existent VIEW. Also it can overwrite VIEW attributes.
  162. // So there is no special rule which attribute value will be "on top".
  163. // It's a random. Technically later VIEW entries will rewrite previous
  164. // values.
  165. if (ischeme->views) {
  166. iview_t **p_iview = NULL;
  167. for (p_iview = *ischeme->views; *p_iview; p_iview++) {
  168. kview_t *kview = NULL;
  169. iview_t *iview = *p_iview;
  170. const char *view_name = iview->name;
  171. if (view_name)
  172. kview = kscheme_find_view(kscheme, view_name);
  173. // VIEW already exists
  174. if (kview) {
  175. kview_error_e kview_error = KVIEW_ERROR_OK;
  176. if (!kview_parse(kview, iview, &kview_error)) {
  177. faux_error_sprintf(error_stack,
  178. "VIEW \"%s\": %s",
  179. iview->name ? iview->name : "(null)",
  180. kview_strerror(kview_error));
  181. retval = BOOL_FALSE;
  182. continue;
  183. }
  184. if (!kview_nested_from_iview(kview, iview,
  185. error_stack)) {
  186. retval = BOOL_FALSE;
  187. continue;
  188. }
  189. continue;
  190. }
  191. // New VIEW
  192. kview = kview_from_iview(iview, error_stack);
  193. if (!kview) {
  194. retval = BOOL_FALSE;
  195. continue;
  196. }
  197. if (!kscheme_add_view(kscheme, kview)) {
  198. faux_error_sprintf(error_stack,
  199. "SCHEME: "
  200. "Can't add VIEW \"%s\"",
  201. kview_name(kview));
  202. kview_free(kview);
  203. retval = BOOL_FALSE;
  204. continue;
  205. }
  206. }
  207. }
  208. if (!retval)
  209. faux_error_sprintf(error_stack, "SCHEME: Illegal nested elements");
  210. return retval;
  211. }
  212. kscheme_t *kscheme_from_ischeme(ischeme_t *ischeme, faux_error_t *error_stack)
  213. {
  214. kscheme_t *kscheme = NULL;
  215. kscheme_error_e kscheme_error = KSCHEME_ERROR_OK;
  216. kscheme = kscheme_new(&kscheme_error);
  217. if (!kscheme) {
  218. faux_error_sprintf(error_stack, "SCHEME: %s",
  219. kscheme_strerror(kscheme_error));
  220. return NULL;
  221. }
  222. // Parse nested elements
  223. if (!kscheme_nested_from_ischeme(kscheme, ischeme, error_stack)) {
  224. kscheme_free(kscheme);
  225. return NULL;
  226. }
  227. return kscheme;
  228. }