kcontext.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/conv.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kcontext.h>
  10. #include <klish/kscheme.h>
  11. struct kcontext_s {
  12. kcontext_type_e type;
  13. int retcode;
  14. kplugin_t *plugin;
  15. kcommand_t *command;
  16. kaction_t *action;
  17. ksym_t *sym;
  18. int stdin;
  19. int stdout;
  20. int stderr;
  21. };
  22. // Simple methods
  23. // Type
  24. KGET(context, kcontext_type_e, type);
  25. FAUX_HIDDEN KSET(context, kcontext_type_e, type);
  26. // RetCode
  27. KGET(context, int, retcode);
  28. FAUX_HIDDEN KSET(context, int, retcode);
  29. // Plugin
  30. KGET(context, kplugin_t *, plugin);
  31. FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
  32. // Sym
  33. KGET(context, ksym_t *, sym);
  34. FAUX_HIDDEN KSET(context, ksym_t *, sym);
  35. // Action
  36. KGET(context, kaction_t *, action);
  37. FAUX_HIDDEN KSET(context, kaction_t *, action);
  38. // Command
  39. KGET(context, kcommand_t *, command);
  40. FAUX_HIDDEN KSET(context, kcommand_t *, command);
  41. // STDIN
  42. KGET(context, int, stdin);
  43. FAUX_HIDDEN KSET(context, int, stdin);
  44. // STDOUT
  45. KGET(context, int, stdout);
  46. FAUX_HIDDEN KSET(context, int, stdout);
  47. // STDERR
  48. KGET(context, int, stderr);
  49. FAUX_HIDDEN KSET(context, int, stderr);
  50. kcontext_t *kcontext_new(kcontext_type_e type)
  51. {
  52. kcontext_t *context = NULL;
  53. context = faux_zmalloc(sizeof(*context));
  54. assert(context);
  55. if (!context)
  56. return NULL;
  57. // Initialize
  58. context->type = type;
  59. context->plugin = NULL;
  60. context->command = NULL;
  61. context->action = NULL;
  62. context->sym = NULL;
  63. // I/O
  64. context->stdin = -1;
  65. context->stdout = -1;
  66. context->stderr = -1;
  67. return context;
  68. }
  69. void kcontext_free(kcontext_t *context)
  70. {
  71. if (!context)
  72. return;
  73. faux_free(context);
  74. }