kexec.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // CONTEXT list
  27. KADD_NESTED(exec, kcontext_t *, contexts);
  28. KNESTED_LEN(exec, contexts);
  29. KNESTED_IS_EMPTY(exec, contexts);
  30. KNESTED_ITER(exec, contexts);
  31. KNESTED_EACH(exec, kcontext_t *, contexts);
  32. kexec_t *kexec_new()
  33. {
  34. kexec_t *exec = NULL;
  35. exec = faux_zmalloc(sizeof(*exec));
  36. assert(exec);
  37. if (!exec)
  38. return NULL;
  39. // List of execute contexts
  40. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  41. NULL, NULL, (void (*)(void *))kcontext_free);
  42. assert(exec->contexts);
  43. // I/O
  44. exec->stdin = -1;
  45. exec->stdout = -1;
  46. exec->stderr = -1;
  47. return exec;
  48. }
  49. void kexec_free(kexec_t *exec)
  50. {
  51. if (!exec)
  52. return;
  53. faux_list_free(exec->contexts);
  54. free(exec);
  55. }
  56. size_t kexec_len(const kexec_t *exec)
  57. {
  58. assert(exec);
  59. if (!exec)
  60. return 0;
  61. return faux_list_len(exec->contexts);
  62. }
  63. size_t kexec_is_empty(const kexec_t *exec)
  64. {
  65. assert(exec);
  66. if (!exec)
  67. return 0;
  68. return faux_list_is_empty(exec->contexts);
  69. }
  70. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  71. {
  72. assert(exec);
  73. assert(context);
  74. if (!exec)
  75. return BOOL_FALSE;
  76. if (!context)
  77. return BOOL_FALSE;
  78. if (!faux_list_add(exec->contexts, context))
  79. return BOOL_FALSE;
  80. return BOOL_TRUE;
  81. }