kview.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. struct kview_s {
  12. char *name;
  13. faux_list_t *commands;
  14. };
  15. // Simple attributes
  16. // Name
  17. KGET_STR(view, name);
  18. KSET_STR_ONCE(view, name);
  19. // COMMAND list
  20. KCMP_NESTED(view, command, name);
  21. KCMP_NESTED_BY_KEY(view, command, name);
  22. KADD_NESTED(view, command);
  23. KFIND_NESTED(view, command);
  24. static kview_t *kview_new_empty(void)
  25. {
  26. kview_t *view = NULL;
  27. view = faux_zmalloc(sizeof(*view));
  28. assert(view);
  29. if (!view)
  30. return NULL;
  31. // Initialize
  32. view->name = NULL;
  33. view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  34. kview_command_compare, kview_command_kcompare,
  35. (void (*)(void *))kcommand_free);
  36. assert(view->commands);
  37. return view;
  38. }
  39. kview_t *kview_new(const iview_t *info, kview_error_e *error)
  40. {
  41. kview_t *view = NULL;
  42. view = kview_new_empty();
  43. assert(view);
  44. if (!view) {
  45. if (error)
  46. *error = KVIEW_ERROR_ALLOC;
  47. return NULL;
  48. }
  49. if (!info)
  50. return view;
  51. if (!kview_parse(view, info, error)) {
  52. kview_free(view);
  53. return NULL;
  54. }
  55. return view;
  56. }
  57. void kview_free(kview_t *view)
  58. {
  59. if (!view)
  60. return;
  61. faux_str_free(view->name);
  62. faux_list_free(view->commands);
  63. faux_free(view);
  64. }
  65. const char *kview_strerror(kview_error_e error)
  66. {
  67. const char *str = NULL;
  68. switch (error) {
  69. case KVIEW_ERROR_OK:
  70. str = "Ok";
  71. break;
  72. case KVIEW_ERROR_INTERNAL:
  73. str = "Internal error";
  74. break;
  75. case KVIEW_ERROR_ALLOC:
  76. str = "Memory allocation error";
  77. break;
  78. case KVIEW_ERROR_ATTR_NAME:
  79. str = "Illegal 'name' attribute";
  80. break;
  81. default:
  82. str = "Unknown error";
  83. break;
  84. }
  85. return str;
  86. }
  87. bool_t kview_parse(kview_t *view, const iview_t *info, kview_error_e *error)
  88. {
  89. // Name [mandatory]
  90. if (faux_str_is_empty(info->name)) {
  91. if (error)
  92. *error = KVIEW_ERROR_ATTR_NAME;
  93. return BOOL_FALSE;
  94. } else {
  95. if (!kview_set_name(view, info->name)) {
  96. if (error)
  97. *error = KVIEW_ERROR_ATTR_NAME;
  98. return BOOL_FALSE;
  99. }
  100. }
  101. return BOOL_TRUE;
  102. }
  103. bool_t kview_nested_from_iview(kview_t *kview, iview_t *iview,
  104. faux_error_t *error_stack)
  105. {
  106. bool_t retval = BOOL_TRUE;
  107. if (!kview || !iview) {
  108. faux_error_add(error_stack,
  109. kview_strerror(KVIEW_ERROR_INTERNAL));
  110. return BOOL_FALSE;
  111. }
  112. // COMMAND list
  113. if (iview->commands) {
  114. icommand_t **p_icommand = NULL;
  115. for (p_icommand = *iview->commands; *p_icommand; p_icommand++) {
  116. kcommand_t *kcommand = NULL;
  117. icommand_t *icommand = *p_icommand;
  118. kcommand = kcommand_from_icommand(icommand, error_stack);
  119. if (!kcommand) {
  120. retval = BOOL_FALSE;
  121. continue;
  122. }
  123. if (!kview_add_command(kview, kcommand)) {
  124. // Search for COMMAND duplicates
  125. if (kview_find_command(kview,
  126. kcommand_name(kcommand))) {
  127. faux_error_sprintf(error_stack,
  128. "VIEW: "
  129. "Can't add duplicate COMMAND "
  130. "\"%s\"",
  131. kcommand_name(kcommand));
  132. } else {
  133. faux_error_sprintf(error_stack,
  134. "VIEW: "
  135. "Can't add COMMAND \"%s\"",
  136. kcommand_name(kcommand));
  137. }
  138. kcommand_free(kcommand);
  139. retval = BOOL_FALSE;
  140. continue;
  141. }
  142. }
  143. }
  144. if (!retval)
  145. faux_error_sprintf(error_stack,
  146. "VIEW \"%s\": Illegal nested elements",
  147. kview_name(kview));
  148. return retval;
  149. }
  150. kview_t *kview_from_iview(iview_t *iview, faux_error_t *error_stack)
  151. {
  152. kview_t *kview = NULL;
  153. kview_error_e kview_error = KVIEW_ERROR_OK;
  154. kview = kview_new(iview, &kview_error);
  155. if (!kview) {
  156. faux_error_sprintf(error_stack, "VIEW \"%s\": %s",
  157. iview->name ? iview->name : "(null)",
  158. kview_strerror(kview_error));
  159. return NULL;
  160. }
  161. // Parse nested elements
  162. if (!kview_nested_from_iview(kview, iview, error_stack)) {
  163. kview_free(kview);
  164. return NULL;
  165. }
  166. return kview;
  167. }