iview.c 3.0 KB

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