iparam.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <klish/khelper.h>
  8. #include <klish/iparam.h>
  9. #include <klish/kptype.h>
  10. #include <klish/kparam.h>
  11. #define TAG "PARAM"
  12. bool_t iparam_parse(const iparam_t *info, kparam_t *param, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. if (!info)
  16. return BOOL_FALSE;
  17. if (!param)
  18. return BOOL_FALSE;
  19. // Help
  20. if (!faux_str_is_empty(info->help)) {
  21. if (!kparam_set_help(param, info->help)) {
  22. faux_error_add(error, TAG": Illegal 'help' attribute");
  23. retcode = BOOL_FALSE;
  24. }
  25. }
  26. // PTYPE reference
  27. if (!faux_str_is_empty(info->ptype)) {
  28. if (!kparam_set_ptype_ref(param, info->ptype)) {
  29. faux_error_add(error, TAG": Illegal 'ptype' attribute");
  30. retcode = BOOL_FALSE;
  31. }
  32. }
  33. // Mode
  34. if (!faux_str_is_empty(info->mode)) {
  35. kparam_mode_e mode = KPARAM_NONE;
  36. if (!faux_str_casecmp(info->mode, "common"))
  37. mode = KPARAM_COMMON;
  38. else if (!faux_str_casecmp(info->mode, "switch"))
  39. mode = KPARAM_SWITCH;
  40. else if (!faux_str_casecmp(info->mode, "subcommand"))
  41. mode = KPARAM_SUBCOMMAND;
  42. else if (!faux_str_casecmp(info->mode, "multi"))
  43. mode = KPARAM_MULTI;
  44. if ((KPARAM_NONE == mode) || !kparam_set_mode(param, mode)) {
  45. faux_error_add(error, TAG": Illegal 'mode' attribute");
  46. retcode = BOOL_FALSE;
  47. }
  48. }
  49. return retcode;
  50. }
  51. bool_t iparam_parse_nested(const iparam_t *iparam, kparam_t *kparam,
  52. faux_error_t *error)
  53. {
  54. bool_t retval = BOOL_TRUE;
  55. if (!kparam || !iparam) {
  56. faux_error_add(error, TAG": Internal error");
  57. return BOOL_FALSE;
  58. }
  59. // Nested PARAM list
  60. if (iparam->params) {
  61. iparam_t **p_iparam = NULL;
  62. for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
  63. kparam_t *nkparam = NULL;
  64. iparam_t *niparam = *p_iparam;
  65. nkparam = iparam_load(niparam, error);
  66. if (!nkparam) {
  67. retval = BOOL_FALSE;
  68. continue;
  69. }
  70. if (!kparam_add_params(kparam, nkparam)) {
  71. // Search for PARAM duplicates
  72. if (kparam_find_param(kparam,
  73. kparam_name(nkparam))) {
  74. faux_error_sprintf(error,
  75. TAG": Can't add duplicate PARAM "
  76. "\"%s\"", kparam_name(nkparam));
  77. } else {
  78. faux_error_sprintf(error,
  79. TAG": Can't add PARAM \"%s\"",
  80. kparam_name(nkparam));
  81. }
  82. kparam_free(nkparam);
  83. retval = BOOL_FALSE;
  84. continue;
  85. }
  86. }
  87. }
  88. if (!retval)
  89. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  90. kparam_name(kparam));
  91. return retval;
  92. }
  93. kparam_t *iparam_load(const iparam_t *iparam, faux_error_t *error)
  94. {
  95. kparam_t *kparam = NULL;
  96. if (!iparam)
  97. return NULL;
  98. // Name [mandatory]
  99. if (faux_str_is_empty(iparam->name)) {
  100. faux_error_add(error, TAG": Empty 'name' attribute");
  101. return NULL;
  102. }
  103. kparam = kparam_new(iparam->name);
  104. if (!kparam) {
  105. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  106. iparam->name);
  107. return NULL;
  108. }
  109. if (!iparam_parse(iparam, kparam, error)) {
  110. kparam_free(kparam);
  111. return NULL;
  112. }
  113. // Parse nested elements
  114. if (!iparam_parse_nested(iparam, kparam, error)) {
  115. kparam_free(kparam);
  116. return NULL;
  117. }
  118. return kparam;
  119. }
  120. char *iparam_deploy(const kparam_t *kparam, int level)
  121. {
  122. char *str = NULL;
  123. char *tmp = NULL;
  124. char *mode = NULL;
  125. kparam_params_node_t *params_iter = NULL;
  126. tmp = faux_str_sprintf("%*cPARAM {\n", level, ' ');
  127. faux_str_cat(&str, tmp);
  128. faux_str_free(tmp);
  129. attr2ctext(&str, "name", kparam_name(kparam), level + 1);
  130. attr2ctext(&str, "help", kparam_help(kparam), level + 1);
  131. attr2ctext(&str, "ptype", kparam_ptype_ref(kparam), level + 1);
  132. // Mode
  133. switch (kparam_mode(kparam)) {
  134. case KPARAM_COMMON:
  135. mode = "common";
  136. break;
  137. case KPARAM_SWITCH:
  138. mode = "switch";
  139. break;
  140. case KPARAM_SUBCOMMAND:
  141. mode = "subcommand";
  142. break;
  143. case KPARAM_MULTI:
  144. mode = "multi";
  145. break;
  146. default:
  147. mode = NULL;
  148. }
  149. attr2ctext(&str, "mode", mode, level + 1);
  150. // PARAM list
  151. params_iter = kparam_params_iter(kparam);
  152. if (params_iter) {
  153. kparam_t *nparam = NULL;
  154. tmp = faux_str_sprintf("\n%*cPARAM_LIST\n\n", level + 1, ' ');
  155. faux_str_cat(&str, tmp);
  156. faux_str_free(tmp);
  157. while ((nparam = kparam_params_each(&params_iter))) {
  158. tmp = iparam_deploy(nparam, level + 2);
  159. faux_str_cat(&str, tmp);
  160. faux_str_free(tmp);
  161. }
  162. tmp = faux_str_sprintf("%*cEND_PARAM_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. }