kcontext.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include <klish/kscheme.h>
  17. struct kcontext_s {
  18. kcontext_type_e type;
  19. kscheme_t *scheme;
  20. int retcode;
  21. ksession_t *session;
  22. kplugin_t *plugin;
  23. kpargv_t *pargv;
  24. const kpargv_t *parent_pargv; // Parent
  25. faux_list_node_t *action_iter; // Current action
  26. ksym_t *sym;
  27. int stdin;
  28. int stdout;
  29. int stderr;
  30. pid_t pid;
  31. bool_t done; // If all actions are done
  32. };
  33. // Simple methods
  34. // Type
  35. KGET(context, kcontext_type_e, type);
  36. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  37. // Scheme
  38. KGET(context, kscheme_t *, scheme);
  39. KSET(context, kscheme_t *, scheme);
  40. // RetCode
  41. KGET(context, int, retcode);
  42. FAUX_HIDDEN KSET(context, int, retcode);
  43. // Plugin
  44. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  45. // Sym
  46. KGET(context, ksym_t *, sym);
  47. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  48. // Pargv
  49. KGET(context, kpargv_t *, pargv);
  50. FAUX_HIDDEN KSET(context, kpargv_t *, pargv);
  51. // Parent pargv
  52. KGET(context, const kpargv_t *, parent_pargv);
  53. FAUX_HIDDEN KSET(context, const kpargv_t *, parent_pargv);
  54. // Action iterator
  55. KGET(context, faux_list_node_t *, action_iter);
  56. FAUX_HIDDEN KSET(context, faux_list_node_t *, action_iter);
  57. // STDIN
  58. KGET(context, int, stdin);
  59. FAUX_HIDDEN KSET(context, int, stdin);
  60. // STDOUT
  61. KGET(context, int, stdout);
  62. FAUX_HIDDEN KSET(context, int, stdout);
  63. // STDERR
  64. KGET(context, int, stderr);
  65. FAUX_HIDDEN KSET(context, int, stderr);
  66. // PID
  67. KGET(context, pid_t, pid);
  68. FAUX_HIDDEN KSET(context, pid_t, pid);
  69. // Session
  70. KGET(context, ksession_t *, session);
  71. FAUX_HIDDEN KSET(context, ksession_t *, session);
  72. // Done
  73. KGET_BOOL(context, done);
  74. FAUX_HIDDEN KSET_BOOL(context, done);
  75. kcontext_t *kcontext_new(kcontext_type_e type)
  76. {
  77. kcontext_t *context = NULL;
  78. context = faux_zmalloc(sizeof(*context));
  79. assert(context);
  80. if (!context)
  81. return NULL;
  82. // Initialize
  83. context->type = type;
  84. context->scheme = NULL;
  85. context->retcode = 0;
  86. context->plugin = NULL;
  87. context->pargv = NULL;
  88. context->parent_pargv = NULL; // Don't free
  89. context->action_iter = NULL;
  90. context->sym = NULL;
  91. context->stdin = -1;
  92. context->stdout = -1;
  93. context->stderr = -1;
  94. context->pid = -1; // PID of currently executed ACTION
  95. context->session = NULL; // Don't free
  96. context->done = BOOL_FALSE;
  97. return context;
  98. }
  99. void kcontext_free(kcontext_t *context)
  100. {
  101. if (!context)
  102. return;
  103. kpargv_free(context->pargv);
  104. if (context->stdin != -1)
  105. close(context->stdin);
  106. if (context->stdout != -1)
  107. close(context->stdout);
  108. if (context->stderr != -1)
  109. close(context->stderr);
  110. faux_free(context);
  111. }
  112. kparg_t *kcontext_candidate_parg(const kcontext_t *context)
  113. {
  114. const kpargv_t *pargv = NULL;
  115. assert(context);
  116. if (!context)
  117. return NULL;
  118. pargv = kcontext_parent_pargv(context);
  119. if (!pargv)
  120. return NULL;
  121. return kpargv_candidate_parg(pargv);
  122. }
  123. const kentry_t *kcontext_candidate_entry(const kcontext_t *context)
  124. {
  125. kparg_t *parg = NULL;
  126. assert(context);
  127. if (!context)
  128. return NULL;
  129. parg = kcontext_candidate_parg(context);
  130. if (!parg)
  131. return NULL;
  132. return kparg_entry(parg);
  133. }
  134. const char *kcontext_candidate_value(const kcontext_t *context)
  135. {
  136. kparg_t *parg = NULL;
  137. assert(context);
  138. if (!context)
  139. return NULL;
  140. parg = kcontext_candidate_parg(context);
  141. if (!parg)
  142. return NULL;
  143. return kparg_value(parg);
  144. }
  145. const kaction_t *kcontext_action(const kcontext_t *context)
  146. {
  147. faux_list_node_t *node = NULL;
  148. assert(context);
  149. if (!context)
  150. return NULL;
  151. node = kcontext_action_iter(context);
  152. if (!node)
  153. return NULL;
  154. return (const kaction_t *)faux_list_data(node);
  155. }
  156. const char *kcontext_script(const kcontext_t *context)
  157. {
  158. const kaction_t *action = NULL;
  159. assert(context);
  160. if (!context)
  161. return NULL;
  162. action = kcontext_action(context);
  163. if (!action)
  164. return NULL;
  165. return kaction_script(action);
  166. }
  167. bool_t kcontext_named_udata_new(kcontext_t *context,
  168. const char *name, void *data, kudata_data_free_fn free_fn)
  169. {
  170. assert(context);
  171. if (!context)
  172. return BOOL_FALSE;
  173. return kscheme_named_udata_new(context->scheme, name, data, free_fn);
  174. }
  175. void *kcontext_named_udata(const kcontext_t *context, const char *name)
  176. {
  177. assert(context);
  178. if (!context)
  179. return NULL;
  180. return kscheme_named_udata(context->scheme, name);
  181. }
  182. void *kcontext_udata(const kcontext_t *context)
  183. {
  184. kplugin_t *plugin = NULL;
  185. assert(context);
  186. if (!context)
  187. return NULL;
  188. plugin = kcontext_plugin(context);
  189. if (!plugin)
  190. return NULL;
  191. return kplugin_udata(plugin);
  192. }
  193. const kentry_t *kcontext_command(const kcontext_t *context)
  194. {
  195. kpargv_t *pargv = NULL;
  196. assert(context);
  197. if (!context)
  198. return NULL;
  199. pargv = kcontext_pargv(context);
  200. if (!pargv)
  201. return NULL;
  202. return kpargv_command(pargv);
  203. }
  204. kplugin_t *kcontext_plugin(const kcontext_t *context)
  205. {
  206. const kaction_t *action = NULL;
  207. assert(context);
  208. if (!context)
  209. return NULL;
  210. // If plugin field is specified then return it. It is specified for
  211. // plugin's init() and fini() functions.
  212. if (context->plugin)
  213. return context->plugin;
  214. // If plugin is not explicitly specified then return parent plugin for
  215. // currently executed sym (ACTION structure contains it).
  216. action = kcontext_action(context);
  217. if (!action)
  218. return NULL;
  219. return kaction_plugin(action);
  220. }