action.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * action.c
  3. *
  4. * This file provides the implementation of a action definition
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "private.h"
  11. #include "lub/bintree.h"
  12. #include "lub/string.h"
  13. /*---------------------------------------------------------
  14. * PRIVATE METHODS
  15. *--------------------------------------------------------- */
  16. static void clish_action_init(clish_action_t *this)
  17. {
  18. this->script = NULL;
  19. this->builtin = NULL;
  20. this->shebang = NULL;
  21. }
  22. /*--------------------------------------------------------- */
  23. static void clish_action_fini(clish_action_t *this)
  24. {
  25. lub_string_free(this->script);
  26. lub_string_free(this->shebang);
  27. }
  28. /*---------------------------------------------------------
  29. * PUBLIC META FUNCTIONS
  30. *--------------------------------------------------------- */
  31. clish_action_t *clish_action_new(void)
  32. {
  33. clish_action_t *this = malloc(sizeof(clish_action_t));
  34. if (this)
  35. clish_action_init(this);
  36. return this;
  37. }
  38. /*---------------------------------------------------------
  39. * PUBLIC METHODS
  40. *--------------------------------------------------------- */
  41. void clish_action_delete(clish_action_t *this)
  42. {
  43. clish_action_fini(this);
  44. free(this);
  45. }
  46. /*---------------------------------------------------------
  47. * PUBLIC ATTRIBUTES
  48. *--------------------------------------------------------- */
  49. void clish_action__set_script(clish_action_t *this, const char *script)
  50. {
  51. if (this->script)
  52. lub_string_free(this->script);
  53. this->script = lub_string_dup(script);
  54. }
  55. /*--------------------------------------------------------- */
  56. char *clish_action__get_script(const clish_action_t *this)
  57. {
  58. return this->script;
  59. }
  60. /*--------------------------------------------------------- */
  61. void clish_action__set_builtin(clish_action_t *this, clish_sym_t *builtin)
  62. {
  63. this->builtin = builtin;
  64. }
  65. /*--------------------------------------------------------- */
  66. clish_sym_t *clish_action__get_builtin(const clish_action_t *this)
  67. {
  68. return this->builtin;
  69. }
  70. /*--------------------------------------------------------- */
  71. void clish_action__set_shebang(clish_action_t *this, const char *shebang)
  72. {
  73. const char *prog = shebang;
  74. const char *prefix = "#!";
  75. if (this->shebang)
  76. lub_string_free(this->shebang);
  77. if (lub_string_nocasestr(shebang, prefix) == shebang)
  78. prog += strlen(prefix);
  79. this->shebang = lub_string_dup(prog);
  80. }
  81. /*--------------------------------------------------------- */
  82. const char *clish_action__get_shebang(const clish_action_t *this)
  83. {
  84. return this->shebang;
  85. }