iview.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/khelper.h>
  9. #include <klish/kcommand.h>
  10. #include <klish/knspace.h>
  11. #include <klish/kview.h>
  12. #include <klish/iview.h>
  13. #define TAG "VIEW"
  14. bool_t iview_parse(const iview_t *info, kview_t *view, faux_error_t *error)
  15. {
  16. bool_t retcode = BOOL_TRUE;
  17. if (!info)
  18. return BOOL_FALSE;
  19. if (!view)
  20. return BOOL_FALSE;
  21. view = view;
  22. info = info;
  23. error = error;
  24. return retcode;
  25. }
  26. bool_t iview_parse_nested(const iview_t *iview, kview_t *kview,
  27. faux_error_t *error)
  28. {
  29. bool_t retval = BOOL_TRUE;
  30. if (!kview || !iview) {
  31. faux_error_add(error, TAG": Internal error");
  32. return BOOL_FALSE;
  33. }
  34. // NSPACE list
  35. if (iview->nspaces) {
  36. inspace_t **p_inspace = NULL;
  37. for (p_inspace = *iview->nspaces; *p_inspace; p_inspace++) {
  38. knspace_t *knspace = NULL;
  39. inspace_t *inspace = *p_inspace;
  40. knspace = inspace_load(inspace, error);
  41. if (!knspace) {
  42. retval = BOOL_FALSE;
  43. continue;
  44. }
  45. if (!kview_add_nspaces(kview, knspace)) {
  46. faux_error_sprintf(error,
  47. TAG": Can't add NSPACE \"%s\"",
  48. knspace_view_ref(knspace));
  49. knspace_free(knspace);
  50. retval = BOOL_FALSE;
  51. continue;
  52. }
  53. }
  54. }
  55. // COMMAND list
  56. if (iview->commands) {
  57. icommand_t **p_icommand = NULL;
  58. for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
  59. kcommand_t *kcommand = NULL;
  60. icommand_t *icommand = *p_icommand;
  61. kcommand = icommand_load(icommand, error);
  62. if (!kcommand) {
  63. retval = BOOL_FALSE;
  64. continue;
  65. }
  66. if (!kview_add_commands(kview, kcommand)) {
  67. // Search for COMMAND duplicates
  68. if (kview_find_command(kview,
  69. kcommand_name(kcommand))) {
  70. faux_error_sprintf(error,
  71. TAG": Can't add duplicate COMMAND "
  72. "\"%s\"", kcommand_name(kcommand));
  73. } else {
  74. faux_error_sprintf(error,
  75. TAG": Can't add COMMAND \"%s\"",
  76. kcommand_name(kcommand));
  77. }
  78. kcommand_free(kcommand);
  79. retval = BOOL_FALSE;
  80. continue;
  81. }
  82. }
  83. }
  84. if (!retval)
  85. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  86. kview_name(kview));
  87. return retval;
  88. }
  89. kview_t *iview_load(const iview_t *iview, faux_error_t *error)
  90. {
  91. kview_t *kview = NULL;
  92. if (!iview)
  93. return NULL;
  94. // Name [mandatory]
  95. if (faux_str_is_empty(iview->name)) {
  96. faux_error_add(error, TAG": Empty 'name' attribute");
  97. return NULL;
  98. }
  99. kview = kview_new(iview->name);
  100. if (!kview) {
  101. faux_error_sprintf(error, TAG": \"%s\": Can't create object",
  102. iview->name);
  103. return NULL;
  104. }
  105. if (!iview_parse(iview, kview, error)) {
  106. kview_free(kview);
  107. return NULL;
  108. }
  109. // Parse nested elements
  110. if (!iview_parse_nested(iview, kview, error)) {
  111. kview_free(kview);
  112. return NULL;
  113. }
  114. return kview;
  115. }
  116. char *iview_deploy(const kview_t *kview, int level)
  117. {
  118. char *str = NULL;
  119. char *tmp = NULL;
  120. kview_commands_node_t *commands_iter = NULL;
  121. kview_nspaces_node_t *nspaces_iter = NULL;
  122. if (!kview)
  123. return NULL;
  124. tmp = faux_str_sprintf("%*cVIEW {\n", level, ' ');
  125. faux_str_cat(&str, tmp);
  126. faux_str_free(tmp);
  127. attr2ctext(&str, "name", kview_name(kview), level + 1);
  128. // NSPACE list
  129. nspaces_iter = kview_nspaces_iter(kview);
  130. if (nspaces_iter) {
  131. knspace_t *nspace = NULL;
  132. tmp = faux_str_sprintf("\n%*cNSPACE_LIST\n\n", level + 1, ' ');
  133. faux_str_cat(&str, tmp);
  134. faux_str_free(tmp);
  135. while ((nspace = kview_nspaces_each(&nspaces_iter))) {
  136. tmp = inspace_deploy(nspace, level + 2);
  137. faux_str_cat(&str, tmp);
  138. faux_str_free(tmp);
  139. }
  140. tmp = faux_str_sprintf("%*cEND_NSPACE_LIST,\n", level + 1, ' ');
  141. faux_str_cat(&str, tmp);
  142. faux_str_free(tmp);
  143. }
  144. // COMMAND list
  145. commands_iter = kview_commands_iter(kview);
  146. if (commands_iter) {
  147. kcommand_t *command = NULL;
  148. tmp = faux_str_sprintf("\n%*cCOMMAND_LIST\n\n", level + 1, ' ');
  149. faux_str_cat(&str, tmp);
  150. faux_str_free(tmp);
  151. while ((command = kview_commands_each(&commands_iter))) {
  152. tmp = icommand_deploy(command, level + 2);
  153. faux_str_cat(&str, tmp);
  154. faux_str_free(tmp);
  155. }
  156. tmp = faux_str_sprintf("%*cEND_COMMAND_LIST,\n", level + 1, ' ');
  157. faux_str_cat(&str, tmp);
  158. faux_str_free(tmp);
  159. }
  160. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  161. faux_str_cat(&str, tmp);
  162. faux_str_free(tmp);
  163. return str;
  164. }