kptype.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. KADD_NESTED(ptype, action);
  24. kptype_t *kptype_new(const char *name)
  25. {
  26. kptype_t *ptype = NULL;
  27. if (faux_str_is_empty(name))
  28. return NULL;
  29. ptype = faux_zmalloc(sizeof(*ptype));
  30. assert(ptype);
  31. if (!ptype)
  32. return NULL;
  33. // Initialize
  34. ptype->name = faux_str_dup(name);
  35. ptype->help = NULL;
  36. // ACTION list
  37. ptype->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  38. NULL, NULL, (void (*)(void *))kaction_free);
  39. assert(ptype->actions);
  40. return ptype;
  41. }
  42. void kptype_free(kptype_t *ptype)
  43. {
  44. if (!ptype)
  45. return;
  46. faux_str_free(ptype->name);
  47. faux_str_free(ptype->help);
  48. faux_list_free(ptype->actions);
  49. faux_free(ptype);
  50. }