kcontext.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. KGET(context, kplugin_t *, 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. // Action iterator
  56. KGET(context, faux_list_node_t *, action_iter);
  57. FAUX_HIDDEN KSET(context, faux_list_node_t *, action_iter);
  58. // STDIN
  59. KGET(context, int, stdin);
  60. FAUX_HIDDEN KSET(context, int, stdin);
  61. // STDOUT
  62. KGET(context, int, stdout);
  63. FAUX_HIDDEN KSET(context, int, stdout);
  64. // STDERR
  65. KGET(context, int, stderr);
  66. FAUX_HIDDEN KSET(context, int, stderr);
  67. // PID
  68. KGET(context, pid_t, pid);
  69. FAUX_HIDDEN KSET(context, pid_t, pid);
  70. // Session
  71. KGET(context, ksession_t *, session);
  72. FAUX_HIDDEN KSET(context, ksession_t *, session);
  73. // Done
  74. KGET_BOOL(context, done);
  75. FAUX_HIDDEN KSET_BOOL(context, done);
  76. kcontext_t *kcontext_new(kcontext_type_e type)
  77. {
  78. kcontext_t *context = NULL;
  79. context = faux_zmalloc(sizeof(*context));
  80. assert(context);
  81. if (!context)
  82. return NULL;
  83. // Initialize
  84. context->type = type;
  85. context->scheme = NULL;
  86. context->retcode = 0;
  87. context->plugin = NULL;
  88. context->pargv = NULL;
  89. context->parent_pargv = NULL; // Don't free
  90. context->action_iter = NULL;
  91. context->sym = NULL;
  92. context->stdin = -1;
  93. context->stdout = -1;
  94. context->stderr = -1;
  95. context->pid = -1; // PID of currently executed ACTION
  96. context->session = NULL; // Don't free
  97. context->done = BOOL_FALSE;
  98. return context;
  99. }
  100. void kcontext_free(kcontext_t *context)
  101. {
  102. if (!context)
  103. return;
  104. kpargv_free(context->pargv);
  105. if (context->stdin != -1)
  106. close(context->stdin);
  107. if (context->stdout != -1)
  108. close(context->stdout);
  109. if (context->stderr != -1)
  110. close(context->stderr);
  111. faux_free(context);
  112. }
  113. kparg_t *kcontext_candidate_parg(const kcontext_t *context)
  114. {
  115. assert(context);
  116. if (!context)
  117. return NULL;
  118. return kpargv_candidate_parg(kcontext_parent_pargv(context));
  119. }
  120. const kentry_t *kcontext_candidate_entry(const kcontext_t *context)
  121. {
  122. assert(context);
  123. if (!context)
  124. return NULL;
  125. return kparg_entry(kcontext_candidate_parg(context));
  126. }
  127. const char *kcontext_candidate_value(const kcontext_t *context)
  128. {
  129. assert(context);
  130. if (!context)
  131. return NULL;
  132. return kparg_value(kcontext_candidate_parg(context));
  133. }
  134. const kaction_t *kcontext_action(const kcontext_t *context)
  135. {
  136. faux_list_node_t *node = NULL;
  137. assert(context);
  138. if (!context)
  139. return NULL;
  140. node = kcontext_action_iter(context);
  141. if (!node)
  142. return NULL;
  143. return (const kaction_t *)faux_list_data(node);
  144. }
  145. const char *kcontext_script(const kcontext_t *context)
  146. {
  147. const kaction_t *action = NULL;
  148. assert(context);
  149. if (!context)
  150. return NULL;
  151. action = kcontext_action(context);
  152. if (!action)
  153. return NULL;
  154. return kaction_script(action);
  155. }
  156. bool_t kcontext_named_udata_new(kcontext_t *context,
  157. const char *name, void *data, kudata_data_free_fn free_fn)
  158. {
  159. assert(context);
  160. if (!context)
  161. return BOOL_FALSE;
  162. return kscheme_named_udata_new(context->scheme, name, data, free_fn);
  163. }
  164. void *kcontext_named_udata(kcontext_t *context, const char *name)
  165. {
  166. assert(context);
  167. if (!context)
  168. return NULL;
  169. return kscheme_named_udata(context->scheme, name);
  170. }