kparam_parse.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 kparam_parse(kparam_t *param, const iparam_t *info, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. // Help
  16. if (!faux_str_is_empty(info->help)) {
  17. if (!kparam_set_help(param, info->help)) {
  18. faux_error_add(error, TAG": Illegal 'help' attribute");
  19. retcode = BOOL_FALSE;
  20. }
  21. }
  22. // PTYPE reference
  23. if (!faux_str_is_empty(info->ptype)) {
  24. if (!kparam_set_ptype_ref(param, info->ptype)) {
  25. faux_error_add(error, TAG": Illegal 'ptype' attribute");
  26. retcode = BOOL_FALSE;
  27. }
  28. }
  29. return retcode;
  30. }
  31. bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
  32. faux_error_t *error)
  33. {
  34. bool_t retval = BOOL_TRUE;
  35. if (!kparam || !iparam) {
  36. faux_error_add(error, TAG": Internal error");
  37. return BOOL_FALSE;
  38. }
  39. // Nested PARAM list
  40. if (iparam->params) {
  41. iparam_t **p_iparam = NULL;
  42. for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
  43. kparam_t *nkparam = NULL;
  44. iparam_t *niparam = *p_iparam;
  45. nkparam = kparam_from_iparam(niparam, error);
  46. if (!nkparam) {
  47. retval = BOOL_FALSE;
  48. continue;
  49. }
  50. if (!kparam_add_param(kparam, nkparam)) {
  51. // Search for PARAM duplicates
  52. if (kparam_find_param(kparam,
  53. kparam_name(nkparam))) {
  54. faux_error_sprintf(error,
  55. TAG": Can't add duplicate PARAM "
  56. "\"%s\"", kparam_name(nkparam));
  57. } else {
  58. faux_error_sprintf(error,
  59. TAG": Can't add PARAM \"%s\"",
  60. kparam_name(nkparam));
  61. }
  62. kparam_free(nkparam);
  63. retval = BOOL_FALSE;
  64. continue;
  65. }
  66. }
  67. }
  68. if (!retval)
  69. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  70. kparam_name(kparam));
  71. return retval;
  72. }
  73. kparam_t *kparam_from_iparam(iparam_t *iparam, faux_error_t *error)
  74. {
  75. kparam_t *kparam = NULL;
  76. // Name [mandatory]
  77. if (faux_str_is_empty(iparam->name)) {
  78. faux_error_add(error, TAG": Empty 'name' attribute");
  79. return NULL;
  80. }
  81. kparam = kparam_new(iparam->name);
  82. if (!kparam) {
  83. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  84. iparam->name);
  85. return NULL;
  86. }
  87. if (!kparam_parse(kparam, iparam, error)) {
  88. kparam_free(kparam);
  89. return NULL;
  90. }
  91. // Parse nested elements
  92. if (!kparam_nested_from_iparam(kparam, iparam, error)) {
  93. kparam_free(kparam);
  94. return NULL;
  95. }
  96. return kparam;
  97. }