kcontext.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. struct kcontext_s {
  15. kcontext_type_e type;
  16. int retcode;
  17. kplugin_t *plugin;
  18. kpargv_t *pargv;
  19. const kpargv_t *parent_pargv; // Parent
  20. faux_list_node_t *action_iter; // Current action
  21. ksym_t *sym;
  22. int stdin;
  23. int stdout;
  24. int stderr;
  25. pid_t pid;
  26. bool_t done; // If all actions are done
  27. };
  28. // Simple methods
  29. // Type
  30. KGET(context, kcontext_type_e, type);
  31. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  32. // RetCode
  33. KGET(context, int, retcode);
  34. FAUX_HIDDEN KSET(context, int, retcode);
  35. // Plugin
  36. KGET(context, kplugin_t *, plugin);
  37. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  38. // Sym
  39. KGET(context, ksym_t *, sym);
  40. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  41. // Pargv
  42. KGET(context, kpargv_t *, pargv);
  43. FAUX_HIDDEN KSET(context, kpargv_t *, pargv);
  44. // Parent pargv
  45. KGET(context, const kpargv_t *, parent_pargv);
  46. FAUX_HIDDEN KSET(context, const kpargv_t *, parent_pargv);
  47. // Action iterator
  48. KGET(context, faux_list_node_t *, action_iter);
  49. FAUX_HIDDEN KSET(context, faux_list_node_t *, action_iter);
  50. // STDIN
  51. KGET(context, int, stdin);
  52. FAUX_HIDDEN KSET(context, int, stdin);
  53. // STDOUT
  54. KGET(context, int, stdout);
  55. FAUX_HIDDEN KSET(context, int, stdout);
  56. // STDERR
  57. KGET(context, int, stderr);
  58. FAUX_HIDDEN KSET(context, int, stderr);
  59. // PID
  60. KGET(context, pid_t, pid);
  61. FAUX_HIDDEN KSET(context, pid_t, pid);
  62. // Done
  63. KGET_BOOL(context, done);
  64. FAUX_HIDDEN KSET_BOOL(context, done);
  65. kcontext_t *kcontext_new(kcontext_type_e type)
  66. {
  67. kcontext_t *context = NULL;
  68. context = faux_zmalloc(sizeof(*context));
  69. assert(context);
  70. if (!context)
  71. return NULL;
  72. // Initialize
  73. context->type = type;
  74. context->retcode = 0;
  75. context->plugin = NULL;
  76. context->pargv = NULL;
  77. context->parent_pargv = NULL; // Don't free
  78. context->action_iter = NULL;
  79. context->sym = NULL;
  80. context->stdin = -1;
  81. context->stdout = -1;
  82. context->stderr = -1;
  83. context->pid = -1; // PID of currently executed ACTION
  84. context->done = BOOL_FALSE;
  85. return context;
  86. }
  87. void kcontext_free(kcontext_t *context)
  88. {
  89. if (!context)
  90. return;
  91. kpargv_free(context->pargv);
  92. if (context->stdin != -1)
  93. close(context->stdin);
  94. if (context->stdout != -1)
  95. close(context->stdout);
  96. if (context->stderr != -1)
  97. close(context->stderr);
  98. faux_free(context);
  99. }