icommand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/iparam.h>
  10. #include <klish/iaction.h>
  11. #include <klish/icommand.h>
  12. #include <klish/kparam.h>
  13. #include <klish/kaction.h>
  14. #include <klish/kcommand.h>
  15. #define TAG "COMMAND"
  16. bool_t icommand_parse(const icommand_t *info, kcommand_t *command,
  17. faux_error_t *error)
  18. {
  19. bool_t retcode = BOOL_TRUE;
  20. if (!info)
  21. return BOOL_FALSE;
  22. if (!command)
  23. return BOOL_FALSE;
  24. // Help
  25. if (!faux_str_is_empty(info->help)) {
  26. if (!kcommand_set_help(command, info->help)) {
  27. faux_error_add(error, TAG": Illegal 'help' attribute");
  28. retcode = BOOL_FALSE;
  29. }
  30. }
  31. return retcode;
  32. }
  33. bool_t icommand_parse_nested(const icommand_t *icommand, kcommand_t *kcommand,
  34. faux_error_t *error)
  35. {
  36. bool_t retval = BOOL_TRUE;
  37. if (!kcommand || !icommand) {
  38. faux_error_add(error, TAG": Internal error");
  39. return BOOL_FALSE;
  40. }
  41. // PARAM list
  42. if (icommand->params) {
  43. iparam_t **p_iparam = NULL;
  44. for (p_iparam = *icommand->params; *p_iparam; p_iparam++) {
  45. kparam_t *kparam = NULL;
  46. iparam_t *iparam = *p_iparam;
  47. kparam = iparam_load(iparam, error);
  48. if (!kparam) {
  49. retval = BOOL_FALSE;
  50. continue;
  51. }
  52. if (!kcommand_add_params(kcommand, kparam)) {
  53. // Search for PARAM duplicates
  54. if (kcommand_find_param(kcommand,
  55. kparam_name(kparam))) {
  56. faux_error_sprintf(error,
  57. TAG": Can't add duplicate PARAM "
  58. "\"%s\"", kparam_name(kparam));
  59. } else {
  60. faux_error_sprintf(error,
  61. TAG": Can't add PARAM \"%s\"",
  62. kparam_name(kparam));
  63. }
  64. kparam_free(kparam);
  65. retval = BOOL_FALSE;
  66. continue;
  67. }
  68. }
  69. }
  70. // ACTION list
  71. if (icommand->actions) {
  72. iaction_t **p_iaction = NULL;
  73. for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
  74. kaction_t *kaction = NULL;
  75. iaction_t *iaction = *p_iaction;
  76. kaction = iaction_load(iaction, error);
  77. if (!kaction) {
  78. retval = BOOL_FALSE;
  79. continue;
  80. }
  81. if (!kcommand_add_actions(kcommand, kaction)) {
  82. faux_error_sprintf(error,
  83. TAG": Can't add ACTION #%d",
  84. kcommand_actions_len(kcommand) + 1);
  85. kaction_free(kaction);
  86. retval = BOOL_FALSE;
  87. continue;
  88. }
  89. }
  90. }
  91. if (!retval)
  92. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  93. kcommand_name(kcommand));
  94. return retval;
  95. }
  96. kcommand_t *icommand_load(icommand_t *icommand, faux_error_t *error)
  97. {
  98. kcommand_t *kcommand = NULL;
  99. // Name [mandatory]
  100. if (faux_str_is_empty(icommand->name)) {
  101. faux_error_add(error, TAG": Empty 'name' attribute");
  102. return NULL;
  103. }
  104. kcommand = kcommand_new(icommand->name);
  105. if (!kcommand) {
  106. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  107. icommand->name);
  108. return NULL;
  109. }
  110. if (!icommand_parse(icommand, kcommand, error)) {
  111. kcommand_free(kcommand);
  112. return NULL;
  113. }
  114. // Parse nested elements
  115. if (!icommand_parse_nested(icommand, kcommand, error)) {
  116. kcommand_free(kcommand);
  117. return NULL;
  118. }
  119. return kcommand;
  120. }
  121. char *icommand_deploy(const kcommand_t *kcommand, int level)
  122. {
  123. char *str = NULL;
  124. char *tmp = NULL;
  125. kcommand_params_node_t *params_iter = NULL;
  126. kcommand_actions_node_t *actions_iter = NULL;
  127. if (!kcommand)
  128. return NULL;
  129. tmp = faux_str_sprintf("%*cCOMMAND {\n", level, ' ');
  130. faux_str_cat(&str, tmp);
  131. faux_str_free(tmp);
  132. attr2ctext(&str, "name", kcommand_name(kcommand), level + 1);
  133. attr2ctext(&str, "help", kcommand_help(kcommand), level + 1);
  134. // PARAM list
  135. params_iter = kcommand_params_iter(kcommand);
  136. if (params_iter) {
  137. kparam_t *param = NULL;
  138. tmp = faux_str_sprintf("\n%*cPARAM_LIST\n\n", level + 1, ' ');
  139. faux_str_cat(&str, tmp);
  140. faux_str_free(tmp);
  141. while ((param = kcommand_params_each(&params_iter))) {
  142. tmp = iparam_deploy(param, level + 2);
  143. faux_str_cat(&str, tmp);
  144. faux_str_free(tmp);
  145. }
  146. tmp = faux_str_sprintf("%*cEND_PARAM_LIST,\n", level + 1, ' ');
  147. faux_str_cat(&str, tmp);
  148. faux_str_free(tmp);
  149. }
  150. // ACTION list
  151. actions_iter = kcommand_actions_iter(kcommand);
  152. if (actions_iter) {
  153. kaction_t *action = NULL;
  154. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  155. faux_str_cat(&str, tmp);
  156. faux_str_free(tmp);
  157. while ((action = kcommand_actions_each(&actions_iter))) {
  158. tmp = iaction_deploy(action, level + 2);
  159. faux_str_cat(&str, tmp);
  160. faux_str_free(tmp);
  161. }
  162. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  163. faux_str_cat(&str, tmp);
  164. faux_str_free(tmp);
  165. }
  166. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  167. faux_str_cat(&str, tmp);
  168. faux_str_free(tmp);
  169. return str;
  170. }