kcontext.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. kaction_t *action; // Current action
  20. ksym_t *sym;
  21. int stdin;
  22. int stdout;
  23. int stderr;
  24. pid_t pid;
  25. bool_t done; // If all actions are done
  26. };
  27. // Simple methods
  28. // Type
  29. KGET(context, kcontext_type_e, type);
  30. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  31. // RetCode
  32. KGET(context, int, retcode);
  33. FAUX_HIDDEN KSET(context, int, retcode);
  34. // Plugin
  35. KGET(context, kplugin_t *, plugin);
  36. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  37. // Sym
  38. KGET(context, ksym_t *, sym);
  39. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  40. // Action
  41. KGET(context, kaction_t *, action);
  42. FAUX_HIDDEN KSET(context, kaction_t *, action);
  43. // Pargv
  44. KGET(context, kpargv_t *, pargv);
  45. FAUX_HIDDEN KSET(context, kpargv_t *, pargv);
  46. // STDIN
  47. KGET(context, int, stdin);
  48. FAUX_HIDDEN KSET(context, int, stdin);
  49. // STDOUT
  50. KGET(context, int, stdout);
  51. FAUX_HIDDEN KSET(context, int, stdout);
  52. // STDERR
  53. KGET(context, int, stderr);
  54. FAUX_HIDDEN KSET(context, int, stderr);
  55. // STDERR
  56. KGET(context, pid_t, pid);
  57. FAUX_HIDDEN KSET(context, pid_t, pid);
  58. // Done
  59. KGET_BOOL(context, done);
  60. FAUX_HIDDEN KSET_BOOL(context, done);
  61. kcontext_t *kcontext_new(kcontext_type_e type)
  62. {
  63. kcontext_t *context = NULL;
  64. context = faux_zmalloc(sizeof(*context));
  65. assert(context);
  66. if (!context)
  67. return NULL;
  68. // Initialize
  69. context->type = type;
  70. context->plugin = NULL;
  71. context->pargv = NULL;
  72. context->action = NULL;
  73. context->sym = NULL;
  74. // I/O
  75. context->stdin = -1;
  76. context->stdout = -1;
  77. context->stderr = -1;
  78. // PID
  79. context->pid = 0; // PID of currently executed ACTION
  80. return context;
  81. }
  82. void kcontext_free(kcontext_t *context)
  83. {
  84. if (!context)
  85. return;
  86. faux_free(context);
  87. }