summaryrefslogtreecommitdiff
path: root/ptyim.c
diff options
context:
space:
mode:
Diffstat (limited to 'ptyim.c')
-rw-r--r--ptyim.c40
1 files changed, 25 insertions, 15 deletions
diff --git a/ptyim.c b/ptyim.c
index 34fdbec..eedffd3 100644
--- a/ptyim.c
+++ b/ptyim.c
@@ -16,12 +16,6 @@
#define WRITE_BUFFER_LEN 128
//determines if the master process is running
int running = 1;
-//hook for when the pty child process dies
-void childhook(int a) {
- running = 0;
- //this way it exists even if the loop is waiting for 1 last read char
- _exit(0);
-}
//pty master and slave fd
int pty_slave = -1;
int pty_master = -1;
@@ -45,6 +39,9 @@ void winchhook(int a) {
ioctl(pty_slave, TIOCSWINSZ, &sz);
}
+//child process pids
+pid_t shell_pid;
+pid_t om_pid;
//return 0 on success, -1 on error
//sets the global vars pty_master and pty_slave
int make_pty() {
@@ -88,6 +85,24 @@ void condition_stdin() {
void restore_stdin() {
tcsetattr(STDIN_FILENO,TCSANOW,&stdin_termio_bk);
}
+//main thread exit
+void exit_main() {
+ //kill shell , can not be blocked
+ kill(shell_pid, SIGKILL);
+ //exit, only the master process should reach this point
+ close(pty_master);
+ close(pty_slave);
+ restore_stdin();
+ //clears the screen so the im statusline goes away
+ write(STDIN_FILENO,"\033[2J", 4);
+ _exit(0);
+}
+//hook for when the pty child process dies
+void childhook(int a) {
+ running = 0;
+ //this way it exists even if the loop is waiting for 1 last read char
+ exit_main();
+}
int main (int argc, char * argv[], char * envp[]) {
condition_stdin();
if (make_pty() != 0) {
@@ -98,7 +113,7 @@ int main (int argc, char * argv[], char * envp[]) {
//resize interrupt handler
winchhook(0);
//forks for running bash
- pid_t shell_pid = fork();
+ shell_pid = fork();
if (shell_pid == 0) {
//shell child process
//set self to be session leader
@@ -137,7 +152,7 @@ int main (int argc, char * argv[], char * envp[]) {
} else {
//forks for read and write, master process handles im, child
//process just handles transfering pty output to stdout
- pid_t om_pid = fork();
+ om_pid = fork();
if (om_pid == 0) {
//sets the name for cleaner ps output
prctl(PR_SET_NAME, "ptyim om thread");
@@ -179,7 +194,7 @@ int main (int argc, char * argv[], char * envp[]) {
fprintf(stderr, "%c: %d\n", read_buffer[0],in_state);
//skips if the char was anything but
//normal
- if (in_state == NORMAL) {
+ if (in_state == NORMAL || in_state ==DOUBLE_ESC) {
//feeds the input char into the im state machine
char input_buffer[IM_TABLE_ENTRY_LEN+1] = {0};
int im_chars = update_im_state(read_buffer[0],
@@ -203,11 +218,6 @@ int main (int argc, char * argv[], char * envp[]) {
kill(om_pid, SIGKILL);
}
}
- //kill shell , can not be blocked
- kill(shell_pid, SIGKILL);
- //exit, only the master process should reach this point
- close(pty_master);
- close(pty_slave);
- restore_stdin();
+ exit_main();
return 0;
}