kcontext.c 2.7 KB

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