kparam.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. kparam_mode_e mode;
  16. faux_list_t *params; // Nested parameters
  17. };
  18. // Simple methods
  19. // Name
  20. KGET_STR(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. // Mode
  31. KGET(param, kparam_mode_e, mode);
  32. KSET(param, kparam_mode_e, mode);
  33. // PARAM list
  34. KGET(param, faux_list_t *, params);
  35. static KCMP_NESTED(param, param, name);
  36. static KCMP_NESTED_BY_KEY(param, param, name);
  37. KADD_NESTED(param, kparam_t *, params);
  38. KFIND_NESTED(param, param);
  39. KNESTED_ITER(param, params);
  40. KNESTED_EACH(param, kparam_t *, params);
  41. kparam_t *kparam_new(const char *name)
  42. {
  43. kparam_t *param = NULL;
  44. if (faux_str_is_empty(name))
  45. return NULL;
  46. param = faux_zmalloc(sizeof(*param));
  47. assert(param);
  48. if (!param)
  49. return NULL;
  50. // Initialize
  51. param->name = faux_str_dup(name);
  52. param->help = NULL;
  53. param->ptype_ref = NULL;
  54. param->ptype = NULL;
  55. param->params = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  56. kparam_param_compare, kparam_param_kcompare,
  57. (void (*)(void *))kparam_free);
  58. assert(param->params);
  59. return param;
  60. }
  61. void kparam_free(kparam_t *param)
  62. {
  63. if (!param)
  64. return;
  65. faux_str_free(param->name);
  66. faux_str_free(param->help);
  67. faux_str_free(param->ptype_ref);
  68. faux_list_free(param->params);
  69. faux_free(param);
  70. }