From 9f80defcd51d2c429de55e1209dfa8f6fd22bc69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haoran=20S=2E=20Diao=20=28=E5=88=81=E6=B5=A9=E7=84=B6=29?= <0@hairydiode.xyz> Date: Fri, 12 Sep 2025 22:10:16 -0700 Subject: added escape sequence detector state machine fixed glitch where exiting only happens after 1 final keypress input --- ptyim.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'ptyim.c') diff --git a/ptyim.c b/ptyim.c index ab4ebe6..34fdbec 100644 --- a/ptyim.c +++ b/ptyim.c @@ -12,12 +12,15 @@ #include #include #include "im.h" +#include "escape.h" #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; @@ -170,17 +173,27 @@ int main (int argc, char * argv[], char * envp[]) { while (running) { int rchars_read = read(STDIN_FILENO, read_buffer, 1); if (rchars_read > 0) { - //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], - input_buffer); - //just in case it wasn't null terminated right - input_buffer[im_chars] = 0; - if (im_chars >0) { - //writes the input into pty_master if - //there was an output from the state - //machine - write(pty_master,input_buffer,im_chars); + //feeds input char into the escape state + //machine + in_state = update_esc_state(read_buffer, 1, in_state); + fprintf(stderr, "%c: %d\n", read_buffer[0],in_state); + //skips if the char was anything but + //normal + if (in_state == NORMAL) { + //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], + input_buffer); + //just in case it wasn't null terminated right + input_buffer[im_chars] = 0; + if (im_chars >0) { + //writes the input into pty_master if + //there was an output from the state + //machine + write(pty_master,input_buffer,im_chars); + } + } else { + write(pty_master,read_buffer,1); } //any input update will lead to a disp_im update disp_im(STDIN_FILENO, trows, tcols); -- cgit v1.1