1
0

context.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * context.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include "private.h"
  8. /*--------------------------------------------------------- */
  9. int clish_context_init(clish_context_t *this, clish_shell_t *shell)
  10. {
  11. if (!this)
  12. return -1;
  13. memset(this, 0, sizeof(*this));
  14. this->shell = shell;
  15. return 0;
  16. }
  17. /*--------------------------------------------------------- */
  18. clish_context_t *clish_context_new(clish_shell_t *shell)
  19. {
  20. clish_context_t *this;
  21. if (!shell)
  22. return NULL;
  23. if (!(this = malloc(sizeof(*this))))
  24. return NULL;
  25. clish_context_init(this, shell);
  26. return this;
  27. }
  28. /*--------------------------------------------------------- */
  29. /* Note it will not free all content because it's a
  30. * container only.
  31. */
  32. void clish_context_free(clish_context_t *this)
  33. {
  34. if (!this)
  35. return;
  36. free(this);
  37. }
  38. /*--------------------------------------------------------- */
  39. clish_shell_t *clish_context__get_shell(const void *this)
  40. {
  41. const clish_context_t *context = (const clish_context_t *)this;
  42. return context->shell;
  43. }
  44. /*--------------------------------------------------------- */
  45. void clish_context__set_cmd(void *this, const clish_command_t *cmd)
  46. {
  47. clish_context_t *context = (clish_context_t *)this;
  48. assert(context);
  49. context->cmd = cmd;
  50. }
  51. /*--------------------------------------------------------- */
  52. const clish_command_t *clish_context__get_cmd(const void *this)
  53. {
  54. const clish_context_t *context = (const clish_context_t *)this;
  55. return context->cmd;
  56. }
  57. /*--------------------------------------------------------- */
  58. void clish_context__set_pargv(void *this, clish_pargv_t *pargv)
  59. {
  60. clish_context_t *context = (clish_context_t *)this;
  61. assert(context);
  62. context->pargv = pargv;
  63. }
  64. /*--------------------------------------------------------- */
  65. clish_pargv_t *clish_context__get_pargv(const void *this)
  66. {
  67. const clish_context_t *context = (const clish_context_t *)this;
  68. return context->pargv;
  69. }
  70. /*--------------------------------------------------------- */
  71. void clish_context__set_action(void *this, const clish_action_t *action)
  72. {
  73. clish_context_t *context = (clish_context_t *)this;
  74. assert(context);
  75. context->action = action;
  76. }
  77. /*--------------------------------------------------------- */
  78. const clish_action_t *clish_context__get_action(const void *this)
  79. {
  80. const clish_context_t *context = (const clish_context_t *)this;
  81. return context->action;
  82. }
  83. /*--------------------------------------------------------- */