kcommand_parse.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/icommand.h>
  10. #include <klish/kparam.h>
  11. #include <klish/kaction.h>
  12. #include <klish/kcommand.h>
  13. #define TAG "COMMAND"
  14. bool_t kcommand_parse(kcommand_t *command, const icommand_t *info,
  15. faux_error_t *error)
  16. {
  17. bool_t retcode = BOOL_TRUE;
  18. // Help
  19. if (!faux_str_is_empty(info->help)) {
  20. if (!kcommand_set_help(command, info->help)) {
  21. faux_error_add(error, TAG": Illegal 'help' attribute");
  22. retcode = BOOL_FALSE;
  23. }
  24. }
  25. return retcode;
  26. }
  27. bool_t kcommand_nested_from_icommand(kcommand_t *kcommand, icommand_t *icommand,
  28. faux_error_t *error)
  29. {
  30. bool_t retval = BOOL_TRUE;
  31. if (!kcommand || !icommand) {
  32. faux_error_add(error, TAG": Internal error");
  33. return BOOL_FALSE;
  34. }
  35. // PARAM list
  36. if (icommand->params) {
  37. iparam_t **p_iparam = NULL;
  38. for (p_iparam = *icommand->params; *p_iparam; p_iparam++) {
  39. kparam_t *kparam = NULL;
  40. iparam_t *iparam = *p_iparam;
  41. kparam = kparam_from_iparam(iparam, error);
  42. if (!kparam) {
  43. retval = BOOL_FALSE;
  44. continue;
  45. }
  46. if (!kcommand_add_param(kcommand, kparam)) {
  47. // Search for PARAM duplicates
  48. if (kcommand_find_param(kcommand,
  49. kparam_name(kparam))) {
  50. faux_error_sprintf(error,
  51. TAG": Can't add duplicate PARAM "
  52. "\"%s\"", kparam_name(kparam));
  53. } else {
  54. faux_error_sprintf(error,
  55. TAG": Can't add PARAM \"%s\"",
  56. kparam_name(kparam));
  57. }
  58. kparam_free(kparam);
  59. retval = BOOL_FALSE;
  60. continue;
  61. }
  62. }
  63. }
  64. // ACTION list
  65. if (icommand->actions) {
  66. iaction_t **p_iaction = NULL;
  67. for (p_iaction = *icommand->actions; *p_iaction; p_iaction++) {
  68. kaction_t *kaction = NULL;
  69. iaction_t *iaction = *p_iaction;
  70. kaction = kaction_from_iaction(iaction, error);
  71. if (!kaction) {
  72. retval = BOOL_FALSE;
  73. continue;
  74. }
  75. if (!kcommand_add_action(kcommand, kaction)) {
  76. faux_error_sprintf(error,
  77. TAG": Can't add ACTION #%d",
  78. kcommand_actions_len(kcommand) + 1);
  79. kaction_free(kaction);
  80. retval = BOOL_FALSE;
  81. continue;
  82. }
  83. }
  84. }
  85. if (!retval)
  86. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  87. kcommand_name(kcommand));
  88. return retval;
  89. }
  90. kcommand_t *kcommand_from_icommand(icommand_t *icommand, faux_error_t *error)
  91. {
  92. kcommand_t *kcommand = NULL;
  93. // Name [mandatory]
  94. if (faux_str_is_empty(icommand->name)) {
  95. faux_error_add(error, TAG": Empty 'name' attribute");
  96. return NULL;
  97. }
  98. kcommand = kcommand_new(icommand->name);
  99. if (!kcommand) {
  100. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  101. icommand->name);
  102. return NULL;
  103. }
  104. if (!kcommand_parse(kcommand, icommand, error)) {
  105. kcommand_free(kcommand);
  106. return NULL;
  107. }
  108. // Parse nested elements
  109. if (!kcommand_nested_from_icommand(kcommand, icommand, error)) {
  110. kcommand_free(kcommand);
  111. return NULL;
  112. }
  113. return kcommand;
  114. }