kcontext.c 5.6 KB

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