|
@@ -31,6 +31,8 @@ bool_t clish_script_callback(clish_shell_t * this,
|
|
|
const char *fifo_name;
|
|
|
FILE *rpipe, *wpipe;
|
|
|
char *command = NULL;
|
|
|
+ bool_t is_sh = BOOL_FALSE;
|
|
|
+ char **out = NULL;
|
|
|
|
|
|
|
|
|
struct sigaction sig_old_int;
|
|
@@ -48,79 +50,99 @@ bool_t clish_script_callback(clish_shell_t * this,
|
|
|
if (!shebang)
|
|
|
shebang = clish_shell__get_default_shebang(this);
|
|
|
assert(shebang);
|
|
|
+ if (0 == lub_string_nocasecmp(shebang, "/bin/sh"))
|
|
|
+ is_sh = BOOL_TRUE;
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
fprintf(stderr, "SHEBANG: #!%s\n", shebang);
|
|
|
fprintf(stderr, "SCRIPT: %s\n", script);
|
|
|
#endif
|
|
|
|
|
|
-
|
|
|
- fifo_name = clish_shell__get_fifo(this);
|
|
|
- if (!fifo_name) {
|
|
|
- fprintf(stderr, "System error. Can't create temporary FIFO.\n"
|
|
|
- "The ACTION will be not executed.\n");
|
|
|
- return BOOL_FALSE;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- cpid = fork();
|
|
|
- if (cpid == -1) {
|
|
|
- fprintf(stderr, "System error. Can't fork the write process.\n"
|
|
|
- "The ACTION will be not executed.\n");
|
|
|
- return BOOL_FALSE;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (cpid == 0) {
|
|
|
- int retval;
|
|
|
- wpipe = fopen(fifo_name, "w");
|
|
|
- if (!wpipe)
|
|
|
- _exit(-1);
|
|
|
- fwrite(script, strlen(script) + 1, 1, wpipe);
|
|
|
- fclose(wpipe);
|
|
|
- _exit(0);
|
|
|
+
|
|
|
+ if (!is_sh) {
|
|
|
+
|
|
|
+ fifo_name = clish_shell__get_fifo(this);
|
|
|
+ if (!fifo_name) {
|
|
|
+ fprintf(stderr, "System error. Can't create temporary FIFO.\n"
|
|
|
+ "The ACTION will be not executed.\n");
|
|
|
+ return BOOL_FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ cpid = fork();
|
|
|
+ if (cpid == -1) {
|
|
|
+ fprintf(stderr, "System error. Can't fork the write process.\n"
|
|
|
+ "The ACTION will be not executed.\n");
|
|
|
+ return BOOL_FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (cpid == 0) {
|
|
|
+ int retval;
|
|
|
+ wpipe = fopen(fifo_name, "w");
|
|
|
+ if (!wpipe)
|
|
|
+ _exit(-1);
|
|
|
+ fwrite(script, strlen(script) + 1, 1, wpipe);
|
|
|
+ fclose(wpipe);
|
|
|
+ _exit(0);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- sigemptyset(&sig_set);
|
|
|
- sig_new.sa_flags = 0;
|
|
|
- sig_new.sa_mask = sig_set;
|
|
|
- sig_new.sa_handler = SIG_IGN;
|
|
|
- sigaction(SIGINT, &sig_new, &sig_old_int);
|
|
|
- sigaction(SIGQUIT, &sig_new, &sig_old_quit);
|
|
|
-
|
|
|
|
|
|
- lub_string_cat(&command, shebang);
|
|
|
- lub_string_cat(&command, " ");
|
|
|
- lub_string_cat(&command, fifo_name);
|
|
|
+ if (!is_sh) {
|
|
|
+ lub_string_cat(&command, shebang);
|
|
|
+ lub_string_cat(&command, " ");
|
|
|
+ lub_string_cat(&command, fifo_name);
|
|
|
+ } else {
|
|
|
+ lub_string_cat(&command, script);
|
|
|
+ }
|
|
|
+#ifdef DEBUG
|
|
|
+ fprintf(stderr, "COMMAND: %s\n", command);
|
|
|
+#endif
|
|
|
|
|
|
-
|
|
|
- rpipe = popen(command, "r");
|
|
|
- lub_string_free(command);
|
|
|
- if (!rpipe) {
|
|
|
- fprintf(stderr, "System error. Can't fork the script.\n"
|
|
|
- "The ACTION will be not executed.\n");
|
|
|
- kill(cpid, SIGTERM);
|
|
|
- waitpid(cpid, NULL, 0);
|
|
|
+
|
|
|
+ if (out) {
|
|
|
+
|
|
|
+ sigemptyset(&sig_set);
|
|
|
+ sig_new.sa_flags = 0;
|
|
|
+ sig_new.sa_mask = sig_set;
|
|
|
+ sig_new.sa_handler = SIG_IGN;
|
|
|
+ sigaction(SIGINT, &sig_new, &sig_old_int);
|
|
|
+ sigaction(SIGQUIT, &sig_new, &sig_old_quit);
|
|
|
+
|
|
|
+
|
|
|
+ rpipe = popen(command, "r");
|
|
|
+ if (!rpipe) {
|
|
|
+ fprintf(stderr, "System error. Can't fork the script.\n"
|
|
|
+ "The ACTION will be not executed.\n");
|
|
|
+ lub_string_free(command);
|
|
|
+ kill(cpid, SIGTERM);
|
|
|
+ if (!is_sh)
|
|
|
+ waitpid(cpid, NULL, 0);
|
|
|
+
|
|
|
+
|
|
|
+ sigaction(SIGINT, &sig_old_int, NULL);
|
|
|
+ sigaction(SIGQUIT, &sig_old_quit, NULL);
|
|
|
+
|
|
|
+ return BOOL_FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (read(fileno(rpipe), &buf, 1) > 0)
|
|
|
+ write(fileno(clish_shell__get_ostream(this)), &buf, 1);
|
|
|
+
|
|
|
+ if (!is_sh)
|
|
|
+ waitpid(cpid, NULL, 0);
|
|
|
+
|
|
|
+ res = pclose(rpipe);
|
|
|
|
|
|
|
|
|
sigaction(SIGINT, &sig_old_int, NULL);
|
|
|
sigaction(SIGQUIT, &sig_old_quit, NULL);
|
|
|
-
|
|
|
- return BOOL_FALSE;
|
|
|
+ } else {
|
|
|
+ res = system(command);
|
|
|
}
|
|
|
-
|
|
|
- while (read(fileno(rpipe), &buf, 1) > 0)
|
|
|
- write(fileno(clish_shell__get_ostream(this)), &buf, 1);
|
|
|
-
|
|
|
- waitpid(cpid, NULL, 0);
|
|
|
-
|
|
|
- res = pclose(rpipe);
|
|
|
-
|
|
|
-
|
|
|
- sigaction(SIGINT, &sig_old_int, NULL);
|
|
|
- sigaction(SIGQUIT, &sig_old_quit, NULL);
|
|
|
+ lub_string_free(command);
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
fprintf(stderr, "RETCODE: %d\n", WEXITSTATUS(res));
|