kexec.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** @file kexec.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kcontext.h>
  10. #include <klish/kexec.h>
  11. struct kexec_s {
  12. faux_list_t *contexts;
  13. int stdin;
  14. int stdout;
  15. int stderr;
  16. };
  17. // STDIN
  18. KGET(exec, int, stdin);
  19. KSET(exec, int, stdin);
  20. // STDOUT
  21. KGET(exec, int, stdout);
  22. KSET(exec, int, stdout);
  23. // STDERR
  24. KGET(exec, int, stderr);
  25. KSET(exec, int, stderr);
  26. kexec_t *kexec_new()
  27. {
  28. kexec_t *exec = NULL;
  29. exec = faux_zmalloc(sizeof(*exec));
  30. assert(exec);
  31. if (!exec)
  32. return NULL;
  33. // List of execute contexts
  34. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  35. NULL, NULL, (void (*)(void *))kcontext_free);
  36. assert(exec->contexts);
  37. // I/O
  38. exec->stdin = -1;
  39. exec->stdout = -1;
  40. exec->stderr = -1;
  41. return exec;
  42. }
  43. void kexec_free(kexec_t *exec)
  44. {
  45. if (!exec)
  46. return;
  47. faux_list_free(exec->contexts);
  48. free(exec);
  49. }
  50. size_t kexec_len(const kexec_t *exec)
  51. {
  52. assert(exec);
  53. if (!exec)
  54. return 0;
  55. return faux_list_len(exec->contexts);
  56. }
  57. size_t kexec_is_empty(const kexec_t *exec)
  58. {
  59. assert(exec);
  60. if (!exec)
  61. return 0;
  62. return faux_list_is_empty(exec->contexts);
  63. }
  64. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  65. {
  66. assert(exec);
  67. assert(context);
  68. if (!exec)
  69. return BOOL_FALSE;
  70. if (!context)
  71. return BOOL_FALSE;
  72. if (!faux_list_add(exec->contexts, context))
  73. return BOOL_FALSE;
  74. return BOOL_TRUE;
  75. }