iview.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/kview.h>
  11. #include <klish/iview.h>
  12. #define TAG "VIEW"
  13. bool_t iview_parse(const iview_t *info, kview_t *view, faux_error_t *error)
  14. {
  15. bool_t retcode = BOOL_TRUE;
  16. if (!info)
  17. return BOOL_FALSE;
  18. if (!view)
  19. return BOOL_FALSE;
  20. view = view;
  21. info = info;
  22. error = error;
  23. return retcode;
  24. }
  25. bool_t iview_parse_nested(const iview_t *iview, kview_t *kview,
  26. faux_error_t *error)
  27. {
  28. bool_t retval = BOOL_TRUE;
  29. if (!kview || !iview) {
  30. faux_error_add(error, TAG": Internal error");
  31. return BOOL_FALSE;
  32. }
  33. // COMMAND list
  34. if (iview->commands) {
  35. icommand_t **p_icommand = NULL;
  36. for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
  37. kcommand_t *kcommand = NULL;
  38. icommand_t *icommand = *p_icommand;
  39. kcommand = icommand_load(icommand, error);
  40. if (!kcommand) {
  41. retval = BOOL_FALSE;
  42. continue;
  43. }
  44. if (!kview_add_command(kview, kcommand)) {
  45. // Search for COMMAND duplicates
  46. if (kview_find_command(kview,
  47. kcommand_name(kcommand))) {
  48. faux_error_sprintf(error,
  49. TAG": Can't add duplicate COMMAND "
  50. "\"%s\"", kcommand_name(kcommand));
  51. } else {
  52. faux_error_sprintf(error,
  53. TAG": Can't add COMMAND \"%s\"",
  54. kcommand_name(kcommand));
  55. }
  56. kcommand_free(kcommand);
  57. retval = BOOL_FALSE;
  58. continue;
  59. }
  60. }
  61. }
  62. if (!retval)
  63. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  64. kview_name(kview));
  65. return retval;
  66. }
  67. kview_t *iview_load(const iview_t *iview, faux_error_t *error)
  68. {
  69. kview_t *kview = NULL;
  70. if (!iview)
  71. return NULL;
  72. // Name [mandatory]
  73. if (faux_str_is_empty(iview->name)) {
  74. faux_error_add(error, TAG": Empty 'name' attribute");
  75. return NULL;
  76. }
  77. kview = kview_new(iview->name);
  78. if (!kview) {
  79. faux_error_sprintf(error, TAG": \"%s\": Can't create object",
  80. iview->name);
  81. return NULL;
  82. }
  83. if (!iview_parse(iview, kview, error)) {
  84. kview_free(kview);
  85. return NULL;
  86. }
  87. // Parse nested elements
  88. if (!iview_parse_nested(iview, kview, error)) {
  89. kview_free(kview);
  90. return NULL;
  91. }
  92. return kview;
  93. }
  94. char *iview_deploy(const kview_t *kview, int level)
  95. {
  96. char *str = NULL;
  97. char *tmp = NULL;
  98. kview_commands_node_t *commands_iter = NULL;
  99. if (!kview)
  100. return NULL;
  101. tmp = faux_str_sprintf("%*cVIEW {\n", level, ' ');
  102. faux_str_cat(&str, tmp);
  103. faux_str_free(tmp);
  104. attr2ctext(&str, "name", kview_name(kview), level + 1);
  105. // COMMAND list
  106. commands_iter = kview_commands_iter(kview);
  107. if (commands_iter) {
  108. kcommand_t *command = NULL;
  109. tmp = faux_str_sprintf("\n%*cCOMMAND_LIST\n\n", level + 1, ' ');
  110. faux_str_cat(&str, tmp);
  111. faux_str_free(tmp);
  112. while ((command = kview_commands_each(&commands_iter))) {
  113. tmp = icommand_deploy(command, level + 2);
  114. faux_str_cat(&str, tmp);
  115. faux_str_free(tmp);
  116. }
  117. tmp = faux_str_sprintf("%*cEND_COMMAND_LIST,\n", level + 1, ' ');
  118. faux_str_cat(&str, tmp);
  119. faux_str_free(tmp);
  120. }
  121. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  122. faux_str_cat(&str, tmp);
  123. faux_str_free(tmp);
  124. return str;
  125. }