kcontext.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. assert(context);
  115. if (!context)
  116. return NULL;
  117. return kpargv_candidate_parg(kcontext_parent_pargv(context));
  118. }
  119. const kentry_t *kcontext_candidate_entry(const kcontext_t *context)
  120. {
  121. assert(context);
  122. if (!context)
  123. return NULL;
  124. return kparg_entry(kcontext_candidate_parg(context));
  125. }
  126. const char *kcontext_candidate_value(const kcontext_t *context)
  127. {
  128. assert(context);
  129. if (!context)
  130. return NULL;
  131. return kparg_value(kcontext_candidate_parg(context));
  132. }
  133. const kaction_t *kcontext_action(const kcontext_t *context)
  134. {
  135. faux_list_node_t *node = NULL;
  136. assert(context);
  137. if (!context)
  138. return NULL;
  139. node = kcontext_action_iter(context);
  140. if (!node)
  141. return NULL;
  142. return (const kaction_t *)faux_list_data(node);
  143. }
  144. const char *kcontext_script(const kcontext_t *context)
  145. {
  146. const kaction_t *action = NULL;
  147. assert(context);
  148. if (!context)
  149. return NULL;
  150. action = kcontext_action(context);
  151. if (!action)
  152. return NULL;
  153. return kaction_script(action);
  154. }
  155. bool_t kcontext_named_udata_new(kcontext_t *context,
  156. const char *name, void *data, kudata_data_free_fn free_fn)
  157. {
  158. assert(context);
  159. if (!context)
  160. return BOOL_FALSE;
  161. return kscheme_named_udata_new(context->scheme, name, data, free_fn);
  162. }
  163. void *kcontext_named_udata(const kcontext_t *context, const char *name)
  164. {
  165. assert(context);
  166. if (!context)
  167. return NULL;
  168. return kscheme_named_udata(context->scheme, name);
  169. }
  170. void *kcontext_udata(const kcontext_t *context)
  171. {
  172. kplugin_t *plugin = NULL;
  173. assert(context);
  174. if (!context)
  175. return NULL;
  176. plugin = kcontext_plugin(context);
  177. if (!plugin)
  178. return NULL;
  179. return kplugin_udata(plugin);
  180. }
  181. const kentry_t *kcontext_command(const kcontext_t *context)
  182. {
  183. assert(context);
  184. if (!context)
  185. return NULL;
  186. return kpargv_command(kcontext_pargv(context));
  187. }
  188. kplugin_t *kcontext_plugin(const kcontext_t *context)
  189. {
  190. const kaction_t *action = NULL;
  191. assert(context);
  192. if (!context)
  193. return NULL;
  194. // If plugin field is specified then return it. It is specified for
  195. // plugin's init() and fini() functions.
  196. if (context->plugin)
  197. return context->plugin;
  198. // If plugin is not explicitly specified then return parent plugin for
  199. // currently executed sym (ACTION structure contains it).
  200. action = kcontext_action(context);
  201. if (!action)
  202. return NULL;
  203. return kaction_plugin(action);
  204. }