kcontext.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include <klish/kaction.h>
  16. struct kcontext_s {
  17. kcontext_type_e type;
  18. int retcode;
  19. kplugin_t *plugin;
  20. kpargv_t *pargv;
  21. const kpargv_t *parent_pargv; // Parent
  22. faux_list_node_t *action_iter; // Current action
  23. ksym_t *sym;
  24. int stdin;
  25. int stdout;
  26. int stderr;
  27. pid_t pid;
  28. ksession_t *session;
  29. bool_t done; // If all actions are done
  30. };
  31. // Simple methods
  32. // Type
  33. KGET(context, kcontext_type_e, type);
  34. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  35. // RetCode
  36. KGET(context, int, retcode);
  37. FAUX_HIDDEN KSET(context, int, retcode);
  38. // Plugin
  39. KGET(context, kplugin_t *, plugin);
  40. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  41. // Sym
  42. KGET(context, ksym_t *, sym);
  43. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  44. // Pargv
  45. KGET(context, kpargv_t *, pargv);
  46. FAUX_HIDDEN KSET(context, kpargv_t *, pargv);
  47. // Parent pargv
  48. KGET(context, const kpargv_t *, parent_pargv);
  49. FAUX_HIDDEN KSET(context, const kpargv_t *, parent_pargv);
  50. // Action iterator
  51. KGET(context, faux_list_node_t *, action_iter);
  52. FAUX_HIDDEN KSET(context, faux_list_node_t *, action_iter);
  53. // STDIN
  54. KGET(context, int, stdin);
  55. FAUX_HIDDEN KSET(context, int, stdin);
  56. // STDOUT
  57. KGET(context, int, stdout);
  58. FAUX_HIDDEN KSET(context, int, stdout);
  59. // STDERR
  60. KGET(context, int, stderr);
  61. FAUX_HIDDEN KSET(context, int, stderr);
  62. // PID
  63. KGET(context, pid_t, pid);
  64. FAUX_HIDDEN KSET(context, pid_t, pid);
  65. // Session
  66. KGET(context, ksession_t *, session);
  67. FAUX_HIDDEN KSET(context, ksession_t *, session);
  68. // Done
  69. KGET_BOOL(context, done);
  70. FAUX_HIDDEN KSET_BOOL(context, done);
  71. kcontext_t *kcontext_new(kcontext_type_e type)
  72. {
  73. kcontext_t *context = NULL;
  74. context = faux_zmalloc(sizeof(*context));
  75. assert(context);
  76. if (!context)
  77. return NULL;
  78. // Initialize
  79. context->type = type;
  80. context->retcode = 0;
  81. context->plugin = NULL;
  82. context->pargv = NULL;
  83. context->parent_pargv = NULL; // Don't free
  84. context->action_iter = NULL;
  85. context->sym = NULL;
  86. context->stdin = -1;
  87. context->stdout = -1;
  88. context->stderr = -1;
  89. context->pid = -1; // PID of currently executed ACTION
  90. context->session = NULL; // Don't free
  91. context->done = BOOL_FALSE;
  92. return context;
  93. }
  94. void kcontext_free(kcontext_t *context)
  95. {
  96. if (!context)
  97. return;
  98. kpargv_free(context->pargv);
  99. if (context->stdin != -1)
  100. close(context->stdin);
  101. if (context->stdout != -1)
  102. close(context->stdout);
  103. if (context->stderr != -1)
  104. close(context->stderr);
  105. faux_free(context);
  106. }
  107. kparg_t *kcontext_candidate_parg(const kcontext_t *context)
  108. {
  109. assert(context);
  110. if (!context)
  111. return NULL;
  112. return kpargv_candidate_parg(kcontext_parent_pargv(context));
  113. }
  114. const kaction_t *kcontext_action(const kcontext_t *context)
  115. {
  116. faux_list_node_t *node = NULL;
  117. assert(context);
  118. if (!context)
  119. return NULL;
  120. node = kcontext_action_iter(context);
  121. if (!node)
  122. return NULL;
  123. return (const kaction_t *)faux_list_data(node);
  124. }
  125. const char *kcontext_script(const kcontext_t *context)
  126. {
  127. const kaction_t *action = NULL;
  128. assert(context);
  129. if (!context)
  130. return NULL;
  131. action = kcontext_action(context);
  132. if (!action)
  133. return NULL;
  134. return kaction_script(action);
  135. }