kcontext.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <faux/str.h>
  8. #include <faux/conv.h>
  9. #include <faux/list.h>
  10. #include <klish/khelper.h>
  11. #include <klish/kpargv.h>
  12. #include <klish/kcontext.h>
  13. #include <klish/kscheme.h>
  14. #include <klish/ksession.h>
  15. struct kcontext_s {
  16. kcontext_type_e type;
  17. int retcode;
  18. kplugin_t *plugin;
  19. kpargv_t *pargv;
  20. const kpargv_t *parent_pargv; // Parent
  21. faux_list_node_t *action_iter; // Current action
  22. ksym_t *sym;
  23. int stdin;
  24. int stdout;
  25. int stderr;
  26. pid_t pid;
  27. ksession_t *session;
  28. bool_t done; // If all actions are done
  29. };
  30. // Simple methods
  31. // Type
  32. KGET(context, kcontext_type_e, type);
  33. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  34. // RetCode
  35. KGET(context, int, retcode);
  36. FAUX_HIDDEN KSET(context, int, retcode);
  37. // Plugin
  38. KGET(context, kplugin_t *, plugin);
  39. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  40. // Sym
  41. KGET(context, ksym_t *, sym);
  42. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  43. // Pargv
  44. KGET(context, kpargv_t *, pargv);
  45. FAUX_HIDDEN KSET(context, kpargv_t *, pargv);
  46. // Parent pargv
  47. KGET(context, const kpargv_t *, parent_pargv);
  48. FAUX_HIDDEN KSET(context, const kpargv_t *, parent_pargv);
  49. // Action iterator
  50. KGET(context, faux_list_node_t *, action_iter);
  51. FAUX_HIDDEN KSET(context, faux_list_node_t *, action_iter);
  52. // STDIN
  53. KGET(context, int, stdin);
  54. FAUX_HIDDEN KSET(context, int, stdin);
  55. // STDOUT
  56. KGET(context, int, stdout);
  57. FAUX_HIDDEN KSET(context, int, stdout);
  58. // STDERR
  59. KGET(context, int, stderr);
  60. FAUX_HIDDEN KSET(context, int, stderr);
  61. // PID
  62. KGET(context, pid_t, pid);
  63. FAUX_HIDDEN KSET(context, pid_t, pid);
  64. // Session
  65. KGET(context, ksession_t *, session);
  66. FAUX_HIDDEN KSET(context, ksession_t *, session);
  67. // Done
  68. KGET_BOOL(context, done);
  69. FAUX_HIDDEN KSET_BOOL(context, done);
  70. kcontext_t *kcontext_new(kcontext_type_e type)
  71. {
  72. kcontext_t *context = NULL;
  73. context = faux_zmalloc(sizeof(*context));
  74. assert(context);
  75. if (!context)
  76. return NULL;
  77. // Initialize
  78. context->type = type;
  79. context->retcode = 0;
  80. context->plugin = NULL;
  81. context->pargv = NULL;
  82. context->parent_pargv = NULL; // Don't free
  83. context->action_iter = NULL;
  84. context->sym = NULL;
  85. context->stdin = -1;
  86. context->stdout = -1;
  87. context->stderr = -1;
  88. context->pid = -1; // PID of currently executed ACTION
  89. context->session = NULL; // Don't free
  90. context->done = BOOL_FALSE;
  91. return context;
  92. }
  93. void kcontext_free(kcontext_t *context)
  94. {
  95. if (!context)
  96. return;
  97. kpargv_free(context->pargv);
  98. if (context->stdin != -1)
  99. close(context->stdin);
  100. if (context->stdout != -1)
  101. close(context->stdout);
  102. if (context->stderr != -1)
  103. close(context->stderr);
  104. faux_free(context);
  105. }
  106. kparg_t *kcontext_candidate_parg(const kcontext_t *context)
  107. {
  108. assert(context);
  109. if (!context)
  110. return NULL;
  111. return kpargv_candidate_parg(kcontext_parent_pargv(context));
  112. }