kptype.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. struct kptype_s {
  12. char *name;
  13. char *help;
  14. faux_list_t *actions;
  15. };
  16. // Simple methods
  17. // Name
  18. KGET_STR(ptype, name);
  19. // Help
  20. KGET_STR(ptype, help);
  21. KSET_STR(ptype, help);
  22. // ACTION list
  23. KGET(ptype, faux_list_t *, actions);
  24. KADD_NESTED(ptype, kaction_t *, actions);
  25. KNESTED_LEN(ptype, actions);
  26. KNESTED_ITER(ptype, actions);
  27. KNESTED_EACH(ptype, kaction_t *, actions);
  28. kptype_t *kptype_new(const char *name)
  29. {
  30. kptype_t *ptype = NULL;
  31. if (faux_str_is_empty(name))
  32. return NULL;
  33. ptype = faux_zmalloc(sizeof(*ptype));
  34. assert(ptype);
  35. if (!ptype)
  36. return NULL;
  37. // Initialize
  38. ptype->name = faux_str_dup(name);
  39. ptype->help = NULL;
  40. // ACTION list
  41. ptype->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  42. NULL, NULL, (void (*)(void *))kaction_free);
  43. assert(ptype->actions);
  44. return ptype;
  45. }
  46. void kptype_free(kptype_t *ptype)
  47. {
  48. if (!ptype)
  49. return;
  50. faux_str_free(ptype->name);
  51. faux_str_free(ptype->help);
  52. faux_list_free(ptype->actions);
  53. faux_free(ptype);
  54. }