clish_script_callback.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * clish_script_callback.c
  3. *
  4. *
  5. * Callback hook to action a shell script.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include "private.h"
  12. /*--------------------------------------------------------- */
  13. bool_t clish_script_callback(const clish_shell_t * this,
  14. const clish_command_t * cmd, const char *script)
  15. {
  16. FILE *wpipe;
  17. const char * shebang = NULL;
  18. assert(this);
  19. assert(cmd);
  20. if (!script) /* Nothing to do */
  21. return BOOL_TRUE;
  22. shebang = clish_command__get_shebang(cmd);
  23. #ifdef DEBUG
  24. if (shebang)
  25. fprintf(stderr, "SHEBANG: #!%s\n", shebang);
  26. fprintf(stderr, "SYSTEM: %s\n", script);
  27. #endif /* DEBUG */
  28. if (!shebang)
  29. return (0 == system(script)) ? BOOL_TRUE : BOOL_FALSE;
  30. /* The shebang is specified */
  31. wpipe = popen(shebang, "w");
  32. if (!wpipe)
  33. return BOOL_FALSE;
  34. fwrite(script, strlen(script) + 1, 1, wpipe);
  35. return (0 == pclose(wpipe)) ? BOOL_TRUE : BOOL_FALSE;
  36. }
  37. /*--------------------------------------------------------- */
  38. bool_t clish_dryrun_callback(const clish_shell_t * this,
  39. const clish_command_t * cmd, const char *script)
  40. {
  41. #ifdef DEBUG
  42. fprintf(stderr, "DRY-RUN: %s\n", script);
  43. #endif /* DEBUG */
  44. return BOOL_TRUE;
  45. }
  46. /*--------------------------------------------------------- */