context.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. int clish_context_dup(clish_context_t *dst, const clish_context_t *src)
  40. {
  41. *dst = *src;
  42. return 0;
  43. }
  44. /*--------------------------------------------------------- */
  45. clish_shell_t *clish_context__get_shell(const void *this)
  46. {
  47. const clish_context_t *context = (const clish_context_t *)this;
  48. return context->shell;
  49. }
  50. /*--------------------------------------------------------- */
  51. void clish_context__set_cmd(void *this, const clish_command_t *cmd)
  52. {
  53. clish_context_t *context = (clish_context_t *)this;
  54. assert(context);
  55. context->cmd = cmd;
  56. }
  57. /*--------------------------------------------------------- */
  58. const clish_command_t *clish_context__get_cmd(const void *this)
  59. {
  60. const clish_context_t *context = (const clish_context_t *)this;
  61. return context->cmd;
  62. }
  63. /*--------------------------------------------------------- */
  64. void clish_context__set_pargv(void *this, clish_pargv_t *pargv)
  65. {
  66. clish_context_t *context = (clish_context_t *)this;
  67. assert(context);
  68. context->pargv = pargv;
  69. }
  70. /*--------------------------------------------------------- */
  71. clish_pargv_t *clish_context__get_pargv(const void *this)
  72. {
  73. const clish_context_t *context = (const clish_context_t *)this;
  74. return context->pargv;
  75. }
  76. /*--------------------------------------------------------- */
  77. void clish_context__set_action(void *this, const clish_action_t *action)
  78. {
  79. clish_context_t *context = (clish_context_t *)this;
  80. assert(context);
  81. context->action = action;
  82. }
  83. /*--------------------------------------------------------- */
  84. const clish_action_t *clish_context__get_action(const void *this)
  85. {
  86. const clish_context_t *context = (const clish_context_t *)this;
  87. return context->action;
  88. }
  89. /*----------------------------------------------------------- */
  90. bool_t clish_context__get_expand(const clish_context_t *context)
  91. {
  92. bool_t res;
  93. const clish_shell_t *shell = clish_context__get_shell(context);
  94. const clish_action_t *action = clish_context__get_action(context);
  95. const clish_sym_t *sym = clish_action__get_builtin(action);
  96. /* get global default */
  97. res = clish_shell__get_default_expand(shell);
  98. /* allow builtin to override */
  99. res = lub_tri_default(clish_sym__get_expand(sym), res);
  100. /* allow action to override */
  101. res = lub_tri_default(clish_action__get_expand(action), res);
  102. /* return result */
  103. return res;
  104. }
  105. /*--------------------------------------------------------- */