kaction.c 1.1 KB

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