kcontext.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/kcontext.h>
  12. #include <klish/kscheme.h>
  13. struct kcontext_s {
  14. kcontext_type_e type;
  15. int retcode;
  16. kplugin_t *plugin;
  17. kcommand_t *command;
  18. kaction_t *action;
  19. ksym_t *sym;
  20. int stdin;
  21. int stdout;
  22. int stderr;
  23. pid_t pid;
  24. };
  25. // Simple methods
  26. // Type
  27. KGET(context, kcontext_type_e, type);
  28. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  29. // RetCode
  30. KGET(context, int, retcode);
  31. FAUX_HIDDEN KSET(context, int, retcode);
  32. // Plugin
  33. KGET(context, kplugin_t *, plugin);
  34. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  35. // Sym
  36. KGET(context, ksym_t *, sym);
  37. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  38. // Action
  39. KGET(context, kaction_t *, action);
  40. FAUX_HIDDEN KSET(context, kaction_t *, action);
  41. // Command
  42. KGET(context, kcommand_t *, command);
  43. FAUX_HIDDEN KSET(context, kcommand_t *, command);
  44. // STDIN
  45. KGET(context, int, stdin);
  46. FAUX_HIDDEN KSET(context, int, stdin);
  47. // STDOUT
  48. KGET(context, int, stdout);
  49. FAUX_HIDDEN KSET(context, int, stdout);
  50. // STDERR
  51. KGET(context, int, stderr);
  52. FAUX_HIDDEN KSET(context, int, stderr);
  53. // STDERR
  54. KGET(context, pid_t, pid);
  55. FAUX_HIDDEN KSET(context, pid_t, pid);
  56. kcontext_t *kcontext_new(kcontext_type_e type)
  57. {
  58. kcontext_t *context = NULL;
  59. context = faux_zmalloc(sizeof(*context));
  60. assert(context);
  61. if (!context)
  62. return NULL;
  63. // Initialize
  64. context->type = type;
  65. context->plugin = NULL;
  66. context->command = NULL;
  67. context->action = NULL;
  68. context->sym = NULL;
  69. // I/O
  70. context->stdin = -1;
  71. context->stdout = -1;
  72. context->stderr = -1;
  73. // PID
  74. context->pid = 0; // PID of currently executed ACTION
  75. return context;
  76. }
  77. void kcontext_free(kcontext_t *context)
  78. {
  79. if (!context)
  80. return;
  81. faux_free(context);
  82. }