mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Fix execute_shell_command to use tty_write
This commit is contained in:
parent
eab0f6245b
commit
2b0e674ee4
2 changed files with 29 additions and 3 deletions
31
src/misc.c
31
src/misc.c
|
|
@ -230,12 +230,22 @@ bool match_patterns(const char *string, const char *patterns)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function that forks subprocess, redirects its stdin and stdout to the
|
// Function that forks subprocess, redirects its stdout and stderr to the
|
||||||
// specified filedescriptor, and runs command.
|
// specified filedescriptor, and runs command.
|
||||||
int execute_shell_command(int fd, const char *command)
|
int execute_shell_command(int fd, const char *command)
|
||||||
{
|
{
|
||||||
|
#define READ_END 0
|
||||||
|
#define WRITE_END 1
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int status;
|
int status;
|
||||||
|
int pipe_fd[2];
|
||||||
|
|
||||||
|
// Create Pipes
|
||||||
|
if (pipe(pipe_fd) == -1)
|
||||||
|
{
|
||||||
|
tio_error_print("pipe() failed (%s)", strerror(errno));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// Fork a child process
|
// Fork a child process
|
||||||
pid = fork();
|
pid = fork();
|
||||||
|
|
@ -252,7 +262,8 @@ int execute_shell_command(int fd, const char *command)
|
||||||
tio_printf("Executing shell command '%s'", command);
|
tio_printf("Executing shell command '%s'", command);
|
||||||
|
|
||||||
// Redirect stdout and stderr to the file descriptor
|
// Redirect stdout and stderr to the file descriptor
|
||||||
if (dup2(fd, STDOUT_FILENO) == -1 || dup2(fd, STDERR_FILENO) == -1)
|
close(pipe_fd[READ_END]);
|
||||||
|
if (dup2(pipe_fd[WRITE_END], STDOUT_FILENO) == -1 || dup2(pipe_fd[WRITE_END], STDERR_FILENO) == -1)
|
||||||
{
|
{
|
||||||
tio_error_print("dup2() failed (%s)", strerror(errno));
|
tio_error_print("dup2() failed (%s)", strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
@ -262,6 +273,7 @@ int execute_shell_command(int fd, const char *command)
|
||||||
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
|
execl("/bin/sh", "sh", "-c", command, (char *)NULL);
|
||||||
|
|
||||||
// If execlp() returns, it means an error occurred
|
// If execlp() returns, it means an error occurred
|
||||||
|
close(pipe_fd[WRITE_END]);
|
||||||
perror("execlp");
|
perror("execlp");
|
||||||
tio_error_print("execlp() failed (%s)", strerror(errno));
|
tio_error_print("execlp() failed (%s)", strerror(errno));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|
@ -269,6 +281,20 @@ int execute_shell_command(int fd, const char *command)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Parent process
|
// Parent process
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
int bytes;
|
||||||
|
|
||||||
|
// Read pipe and transfer to tty device.
|
||||||
|
close(pipe_fd[WRITE_END]);
|
||||||
|
while ( (bytes = read(pipe_fd[READ_END], buf, sizeof(buf))) > 0)
|
||||||
|
{
|
||||||
|
if (tty_write(fd, buf, bytes) <= 0)
|
||||||
|
{
|
||||||
|
tio_warning_printf("Could not write to tty device");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tty_sync(fd);
|
||||||
|
close(pipe_fd[READ_END]);
|
||||||
|
|
||||||
// Wait for the child process to finish
|
// Wait for the child process to finish
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
|
|
@ -284,7 +310,6 @@ int execute_shell_command(int fd, const char *command)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,4 +86,5 @@ void tty_line_set(int fd, tty_line_config_t line_config[]);
|
||||||
void tty_search(void);
|
void tty_search(void);
|
||||||
GList *tty_search_for_serial_devices(void);
|
GList *tty_search_for_serial_devices(void);
|
||||||
void forward_to_tty(int fd, char output_char);
|
void forward_to_tty(int fd, char output_char);
|
||||||
|
ssize_t tty_write(int fd, void *buffer, size_t count);
|
||||||
void tty_sync(int fd);
|
void tty_sync(int fd);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue