diff options
author | Haoran S. Diao (刁浩然) <0@hairydiode.xyz> | 2025-09-12 22:33:39 -0700 |
---|---|---|
committer | Haoran S. Diao (刁浩然) <0@hairydiode.xyz> | 2025-09-12 22:33:39 -0700 |
commit | 9a6954f9720784bc79668bcc6456f6a3f6a0c5a1 (patch) | |
tree | c605b3dab85848841a779bc8b0f864d67464ab6f /ptyim.c | |
parent | 9f80defcd51d2c429de55e1209dfa8f6fd22bc69 (diff) |
Made it so pressing escape twice sends an escape to the IM, fixed the status
line persisting even on exit. Refactored main process exit
Diffstat (limited to 'ptyim.c')
-rw-r--r-- | ptyim.c | 40 |
1 files changed, 25 insertions, 15 deletions
@@ -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; } |