kview.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. printf("command %s\n", icommand->name);
  119. // kcommand = kcommand_from_icommand(icommand, error_stack);
  120. // if (!kcommand) {
  121. // retval = BOOL_FALSE;
  122. // continue;
  123. // }
  124. kcommand = kcommand;
  125. }
  126. }
  127. return retval;
  128. }
  129. kview_t *kview_from_iview(iview_t *iview, faux_error_t *error_stack)
  130. {
  131. kview_t *kview = NULL;
  132. kview_error_e kview_error = KVIEW_ERROR_OK;
  133. kview = kview_new(iview, &kview_error);
  134. if (!kview) {
  135. char *msg = NULL;
  136. msg = faux_str_sprintf("VIEW \"%s\": %s",
  137. iview->name ? iview->name : "(null)",
  138. kview_strerror(kview_error));
  139. faux_error_add(error_stack, msg);
  140. faux_str_free(msg);
  141. return NULL;
  142. }
  143. printf("view %s\n", kview_name(kview));
  144. // Parse nested elements
  145. if (!kview_nested_from_iview(kview, iview, error_stack)) {
  146. char *msg = NULL;
  147. msg = faux_str_sprintf("VIEW \"%s\": Illegal nested elements",
  148. kview_name(kview));
  149. faux_error_add(error_stack, msg);
  150. faux_str_free(msg);
  151. kview_free(kview);
  152. return NULL;
  153. }
  154. return kview;
  155. }