summaryrefslogtreecommitdiff
path: root/ptyim.c
diff options
context:
space:
mode:
authorHaoran S. Diao (刁浩然) <0@hairydiode.xyz>2025-09-12 22:10:16 -0700
committerHaoran S. Diao (刁浩然) <0@hairydiode.xyz>2025-09-12 22:10:16 -0700
commit9f80defcd51d2c429de55e1209dfa8f6fd22bc69 (patch)
treeae85eb7f102e52c4bed629bb94650039a4383497 /ptyim.c
parent9e388285b965509fea6aa9cdb52b4aacf3a8728c (diff)
added escape sequence detector state machine
fixed glitch where exiting only happens after 1 final keypress input
Diffstat (limited to 'ptyim.c')
-rw-r--r--ptyim.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/ptyim.c b/ptyim.c
index ab4ebe6..34fdbec 100644
--- a/ptyim.c
+++ b/ptyim.c
@@ -12,12 +12,15 @@
#include <signal.h>
#include <sys/prctl.h>
#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);