action.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. static void clish_action_init(clish_action_t *this)
  15. {
  16. this->script = NULL;
  17. this->builtin = NULL;
  18. this->shebang = NULL;
  19. }
  20. /*--------------------------------------------------------- */
  21. static void clish_action_fini(clish_action_t *this)
  22. {
  23. lub_string_free(this->script);
  24. lub_string_free(this->shebang);
  25. }
  26. /*--------------------------------------------------------- */
  27. clish_action_t *clish_action_new(void)
  28. {
  29. clish_action_t *this = malloc(sizeof(clish_action_t));
  30. if (this)
  31. clish_action_init(this);
  32. return this;
  33. }
  34. /*--------------------------------------------------------- */
  35. void clish_action_delete(clish_action_t *this)
  36. {
  37. clish_action_fini(this);
  38. free(this);
  39. }
  40. /*--------------------------------------------------------- */
  41. void clish_action__set_script(clish_action_t *this, const char *script)
  42. {
  43. if (this->script)
  44. lub_string_free(this->script);
  45. this->script = lub_string_dup(script);
  46. }
  47. CLISH_GET(action, const char *, script);
  48. /*--------------------------------------------------------- */
  49. void clish_action__set_builtin(clish_action_t *this, clish_sym_t *builtin)
  50. {
  51. this->builtin = builtin;
  52. }
  53. CLISH_GET(action, clish_sym_t *, builtin);
  54. /*--------------------------------------------------------- */
  55. void clish_action__set_shebang(clish_action_t *this, const char *shebang)
  56. {
  57. const char *prog = shebang;
  58. const char *prefix = "#!";
  59. if (this->shebang)
  60. lub_string_free(this->shebang);
  61. if (lub_string_nocasestr(shebang, prefix) == shebang)
  62. prog += strlen(prefix);
  63. this->shebang = lub_string_dup(prog);
  64. }
  65. //CLISH_GET(clish_action__get_script, clish_action_t, const char *, script);
  66. /*--------------------------------------------------------- */
  67. const char *clish_action__get_shebang(const clish_action_t *this)
  68. {
  69. return this->shebang;
  70. }