kaction.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/kcommand.h>
  8. #include <klish/kaction.h>
  9. struct kaction_s {
  10. bool_t is_static;
  11. kaction_info_t info;
  12. faux_list_t *commands;
  13. };
  14. static kaction_t *kaction_new_internal(kaction_info_t info, bool_t is_static)
  15. {
  16. kaction_t *action = NULL;
  17. action = faux_zmalloc(sizeof(*action));
  18. assert(action);
  19. if (!action)
  20. return NULL;
  21. // Initialize
  22. action->is_static = is_static;
  23. action->info = info;
  24. return action;
  25. }
  26. kaction_t *kaction_new(kaction_info_t info)
  27. {
  28. return kaction_new_internal(info, BOOL_FALSE);
  29. }
  30. kaction_t *kaction_new_static(kaction_info_t info)
  31. {
  32. return kaction_new_internal(info, BOOL_TRUE);
  33. }
  34. void kaction_free(kaction_t *action)
  35. {
  36. if (!action)
  37. return;
  38. if (!action->is_static) {
  39. faux_str_free(action->info.sym);
  40. }
  41. faux_free(action);
  42. }
  43. const char *kaction_sym_str(const kaction_t *action)
  44. {
  45. assert(action);
  46. if (!action)
  47. return NULL;
  48. return action->info.sym;
  49. }