kparam.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/kptype.h>
  9. #include <klish/kparam.h>
  10. struct kparam_s {
  11. char *name;
  12. char *help;
  13. char *ptype_ref; // Text reference to PTYPE
  14. kptype_t *ptype; // Resolved PARAM's PTYPE
  15. faux_list_t *params; // Nested parameters
  16. };
  17. // Simple methods
  18. // Name
  19. KGET_STR(param, name);
  20. KSET_STR_ONCE(param, name);
  21. // Help
  22. KGET_STR(param, help);
  23. KSET_STR(param, help);
  24. // PTYPE reference (must be resolved later)
  25. KGET_STR(param, ptype_ref);
  26. KSET_STR(param, ptype_ref);
  27. // PTYPE (resolved)
  28. KGET(param, kptype_t *, ptype);
  29. KSET(param, kptype_t *, ptype);
  30. // PARAM list
  31. KCMP_NESTED_BY_KEY(param, param, name);
  32. KADD_NESTED(param, param);
  33. KFIND_NESTED(param, param);
  34. static kparam_t *kparam_new_empty(void)
  35. {
  36. kparam_t *param = NULL;
  37. param = faux_zmalloc(sizeof(*param));
  38. assert(param);
  39. if (!param)
  40. return NULL;
  41. // Initialize
  42. param->name = NULL;
  43. param->help = NULL;
  44. param->ptype_ref = NULL;
  45. param->ptype = NULL;
  46. param->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  47. NULL, kparam_param_kcompare,
  48. (void (*)(void *))kparam_free);
  49. assert(param->params);
  50. return param;
  51. }
  52. kparam_t *kparam_new(const iparam_t *info, kparam_error_e *error)
  53. {
  54. kparam_t *param = NULL;
  55. param = kparam_new_empty();
  56. assert(param);
  57. if (!param) {
  58. if (error)
  59. *error = KPARAM_ERROR_ALLOC;
  60. return NULL;
  61. }
  62. if (!info)
  63. return param;
  64. if (!kparam_parse(param, info, error)) {
  65. kparam_free(param);
  66. return NULL;
  67. }
  68. return param;
  69. }
  70. void kparam_free(kparam_t *param)
  71. {
  72. if (!param)
  73. return;
  74. faux_str_free(param->name);
  75. faux_str_free(param->help);
  76. faux_str_free(param->ptype_ref);
  77. faux_list_free(param->params);
  78. faux_free(param);
  79. }
  80. const char *kparam_strerror(kparam_error_e error)
  81. {
  82. const char *str = NULL;
  83. switch (error) {
  84. case KPARAM_ERROR_OK:
  85. str = "Ok";
  86. break;
  87. case KPARAM_ERROR_INTERNAL:
  88. str = "Internal error";
  89. break;
  90. case KPARAM_ERROR_ALLOC:
  91. str = "Memory allocation error";
  92. break;
  93. case KPARAM_ERROR_ATTR_NAME:
  94. str = "Illegal 'name' attribute";
  95. break;
  96. case KPARAM_ERROR_ATTR_HELP:
  97. str = "Illegal 'help' attribute";
  98. break;
  99. case KPARAM_ERROR_ATTR_PTYPE:
  100. str = "Illegal 'ptype' attribute";
  101. break;
  102. default:
  103. str = "Unknown error";
  104. break;
  105. }
  106. return str;
  107. }
  108. bool_t kparam_parse(kparam_t *param, const iparam_t *info, kparam_error_e *error)
  109. {
  110. // Name [mandatory]
  111. if (faux_str_is_empty(info->name)) {
  112. if (error)
  113. *error = KPARAM_ERROR_ATTR_NAME;
  114. return BOOL_FALSE;
  115. } else {
  116. if (!kparam_set_name(param, info->name)) {
  117. if (error)
  118. *error = KPARAM_ERROR_ATTR_NAME;
  119. return BOOL_FALSE;
  120. }
  121. }
  122. // Help
  123. if (!faux_str_is_empty(info->name)) {
  124. if (!kparam_set_help(param, info->help)) {
  125. if (error)
  126. *error = KPARAM_ERROR_ATTR_HELP;
  127. return BOOL_FALSE;
  128. }
  129. }
  130. // PTYPE reference
  131. if (!faux_str_is_empty(info->ptype)) {
  132. if (!kparam_set_ptype_ref(param, info->ptype)) {
  133. if (error)
  134. *error = KPARAM_ERROR_ATTR_PTYPE;
  135. return BOOL_FALSE;
  136. }
  137. }
  138. return BOOL_TRUE;
  139. }
  140. bool_t kparam_nested_from_iparam(kparam_t *kparam, iparam_t *iparam,
  141. faux_error_t *error_stack)
  142. {
  143. bool_t retval = BOOL_TRUE;
  144. if (!kparam || !iparam) {
  145. faux_error_add(error_stack,
  146. kparam_strerror(KPARAM_ERROR_INTERNAL));
  147. return BOOL_FALSE;
  148. }
  149. // Nested PARAM list
  150. if (iparam->params) {
  151. iparam_t **p_iparam = NULL;
  152. for (p_iparam = *iparam->params; *p_iparam; p_iparam++) {
  153. kparam_t *nkparam = NULL;
  154. iparam_t *niparam = *p_iparam;
  155. printf("subparam\n");
  156. nkparam = kparam_from_iparam(niparam, error_stack);
  157. if (!nkparam) {
  158. retval = BOOL_FALSE;
  159. continue;
  160. }
  161. }
  162. }
  163. return retval;
  164. }
  165. kparam_t *kparam_from_iparam(iparam_t *iparam, faux_error_t *error_stack)
  166. {
  167. kparam_t *kparam = NULL;
  168. kparam_error_e kparam_error = KPARAM_ERROR_OK;
  169. kparam = kparam_new(iparam, &kparam_error);
  170. if (!kparam) {
  171. char *msg = NULL;
  172. msg = faux_str_sprintf("PARAM \"%s\": %s",
  173. iparam->name ? iparam->name : "(null)",
  174. kparam_strerror(kparam_error));
  175. faux_error_add(error_stack, msg);
  176. faux_str_free(msg);
  177. return NULL;
  178. }
  179. printf("param %s\n", kparam_name(kparam));
  180. // Parse nested elements
  181. if (!kparam_nested_from_iparam(kparam, iparam, error_stack)) {
  182. char *msg = NULL;
  183. msg = faux_str_sprintf("PARAM \"%s\": Illegal nested elements",
  184. kparam_name(kparam));
  185. faux_error_add(error_stack, msg);
  186. faux_str_free(msg);
  187. kparam_free(kparam);
  188. return NULL;
  189. }
  190. return kparam;
  191. }