action.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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->builtin);
  27. lub_string_free(this->shebang);
  28. }
  29. /*---------------------------------------------------------
  30. * PUBLIC META FUNCTIONS
  31. *--------------------------------------------------------- */
  32. clish_action_t *clish_action_new(void)
  33. {
  34. clish_action_t *this = malloc(sizeof(clish_action_t));
  35. if (this)
  36. clish_action_init(this);
  37. return this;
  38. }
  39. /*---------------------------------------------------------
  40. * PUBLIC METHODS
  41. *--------------------------------------------------------- */
  42. void clish_action_delete(clish_action_t *this)
  43. {
  44. clish_action_fini(this);
  45. free(this);
  46. }
  47. /*---------------------------------------------------------
  48. * PUBLIC ATTRIBUTES
  49. *--------------------------------------------------------- */
  50. void clish_action__set_script(clish_action_t *this, const char *script)
  51. {
  52. if (this->script)
  53. lub_string_free(this->script);
  54. this->script = lub_string_dup(script);
  55. }
  56. /*--------------------------------------------------------- */
  57. char *clish_action__get_script(const clish_action_t *this)
  58. {
  59. return this->script;
  60. }
  61. /*--------------------------------------------------------- */
  62. void clish_action__set_builtin(clish_action_t *this, const char *builtin)
  63. {
  64. if (this->builtin)
  65. lub_string_free(this->builtin);
  66. this->builtin = lub_string_dup(builtin);
  67. }
  68. /*--------------------------------------------------------- */
  69. const char *clish_action__get_builtin(const clish_action_t *this)
  70. {
  71. return this->builtin;
  72. }
  73. /*--------------------------------------------------------- */
  74. void clish_action__set_shebang(clish_action_t *this, const char *shebang)
  75. {
  76. const char *prog = shebang;
  77. const char *prefix = "#!";
  78. if (this->shebang)
  79. lub_string_free(this->shebang);
  80. if (lub_string_nocasestr(shebang, prefix) == shebang)
  81. prog += strlen(prefix);
  82. this->shebang = lub_string_dup(prog);
  83. }
  84. /*--------------------------------------------------------- */
  85. const char *clish_action__get_shebang(const clish_action_t *this)
  86. {
  87. return this->shebang;
  88. }