iptype.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/kptype.h>
  10. #include <klish/kaction.h>
  11. #include <klish/iptype.h>
  12. #define TAG "PTYPE"
  13. bool_t iptype_parse(const iptype_t *info, kptype_t *ptype, faux_error_t *error)
  14. {
  15. bool_t retcode = BOOL_TRUE;
  16. if (!info)
  17. return BOOL_FALSE;
  18. if (!ptype)
  19. return BOOL_FALSE;
  20. // Help
  21. if (!faux_str_is_empty(info->name)) {
  22. if (!kptype_set_help(ptype, info->help)) {
  23. faux_error_add(error, TAG": Illegal 'help' attribute");
  24. retcode = BOOL_FALSE;
  25. }
  26. }
  27. return retcode;
  28. }
  29. bool_t iptype_parse_nested(const iptype_t *iptype, kptype_t *kptype,
  30. faux_error_t *error)
  31. {
  32. bool_t retval = BOOL_TRUE;
  33. if (!kptype || !iptype) {
  34. faux_error_add(error, TAG": Internal error");
  35. return BOOL_FALSE;
  36. }
  37. // ACTION list
  38. if (iptype->actions) {
  39. iaction_t **p_iaction = NULL;
  40. for (p_iaction = *iptype->actions; *p_iaction; p_iaction++) {
  41. kaction_t *kaction = NULL;
  42. iaction_t *iaction = *p_iaction;
  43. kaction = iaction_load(iaction, error);
  44. if (!kaction) {
  45. retval = BOOL_FALSE;
  46. continue;
  47. }
  48. if (!kptype_add_actions(kptype, kaction)) {
  49. faux_error_sprintf(error,
  50. TAG": Can't add ACTION #%d",
  51. kptype_actions_len(kptype) + 1);
  52. kaction_free(kaction);
  53. retval = BOOL_FALSE;
  54. continue;
  55. }
  56. }
  57. }
  58. if (!retval)
  59. faux_error_sprintf(error,
  60. "PTYPE \"%s\": Illegal nested elements",
  61. kptype_name(kptype));
  62. return retval;
  63. }
  64. kptype_t *iptype_load(const iptype_t *iptype, faux_error_t *error)
  65. {
  66. kptype_t *kptype = NULL;
  67. if (!iptype)
  68. return NULL;
  69. // Name [mandatory]
  70. if (faux_str_is_empty(iptype->name)) {
  71. faux_error_add(error, TAG": Empty 'name' attribute");
  72. return NULL;
  73. }
  74. kptype = kptype_new(iptype->name);
  75. if (!kptype) {
  76. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  77. iptype->name);
  78. return NULL;
  79. }
  80. if (!iptype_parse(iptype, kptype, error)) {
  81. kptype_free(kptype);
  82. return NULL;
  83. }
  84. // Parse nested elements
  85. if (!iptype_parse_nested(iptype, kptype, error)) {
  86. kptype_free(kptype);
  87. return NULL;
  88. }
  89. return kptype;
  90. }
  91. char *iptype_deploy(const kptype_t *kptype, int level)
  92. {
  93. char *str = NULL;
  94. char *tmp = NULL;
  95. kptype_actions_node_t *actions_iter = NULL;
  96. tmp = faux_str_sprintf("%*cPTYPE {\n", level, ' ');
  97. faux_str_cat(&str, tmp);
  98. faux_str_free(tmp);
  99. attr2ctext(&str, "name", kptype_name(kptype), level + 1);
  100. attr2ctext(&str, "help", kptype_help(kptype), level + 1);
  101. // ACTION list
  102. actions_iter = kptype_actions_iter(kptype);
  103. if (actions_iter) {
  104. kaction_t *action = NULL;
  105. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  106. faux_str_cat(&str, tmp);
  107. faux_str_free(tmp);
  108. while ((action = kptype_actions_each(&actions_iter))) {
  109. tmp = iaction_deploy(action, level + 2);
  110. faux_str_cat(&str, tmp);
  111. faux_str_free(tmp);
  112. }
  113. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  114. faux_str_cat(&str, tmp);
  115. faux_str_free(tmp);
  116. }
  117. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  118. faux_str_cat(&str, tmp);
  119. faux_str_free(tmp);
  120. return str;
  121. }