]> git.mdlowis.com Git - projs/tide.git/commitdiff
added read-back of the echo to state machine
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 30 Nov 2019 02:31:41 +0000 (21:31 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 30 Nov 2019 02:31:41 +0000 (21:31 -0500)
src/lib/xpty.c

index 8efac44a3c307089c06fec038526e386308d4220..b7dc217e92de9fba36870902579b4a1bcda0441b 100644 (file)
@@ -18,13 +18,25 @@ static char ArgsBuf[BUFFERSZ+1] = {0};
 static char InputBuf[BUFFERSZ+1] = {0};
 static ssize_t ArgsPos = 0;
 static ssize_t InputPos = 0;
+static ssize_t IgnoreCount = 0;
 static enum {
-    READ_CHAR = 0,
+    READ_ECHO = 0,
+    READ_CHAR,
     READ_ESC,
     READ_OSC,
     READ_OSI,
 } State = READ_CHAR;
 
+static void read_echo(char c)
+{
+    (void)c;
+    IgnoreCount--;
+    if (IgnoreCount <= 0)
+    {
+        State = READ_CHAR;
+    }
+}
+
 static void read_char(char c)
 {
     if (c == '\033')
@@ -40,12 +52,12 @@ static void read_char(char c)
 
 static void read_esc(char c)
 {
+    ArgsBuf[(ArgsPos = 0)] = '\0';
     if (c == '[')
    {
         State = READ_OSI;
    }
-   else
-    if (c == ']')
+   else if (c == ']')
    {
         State = READ_OSC;
    }
@@ -64,8 +76,6 @@ static void read_osc(char c)
         {
             chdir(&ArgsBuf[2]);
         }
-        ArgsPos = 0;
-        ArgsBuf[0] = '\0';
     }
     else
     {
@@ -83,6 +93,7 @@ static void read_osi(char c)
 }
 
 static void (*Handlers[])(char c) = {
+    [READ_ECHO] = read_echo,
     [READ_CHAR] = read_char,
     [READ_ESC] = read_esc,
     [READ_OSC] = read_osc,
@@ -120,8 +131,11 @@ static void putb(int byte)
         EditView->buffer.point.beg = EditView->buffer.point.end;
 
         /* write the data and read back to discard the echoed chars */
+//        printf("write: '%s'\n", str);
         writefd(Pty_Fd, str, slen);
-        (void)readfd(Pty_Fd, str, slen+1);
+        State = READ_ECHO;
+        IgnoreCount = slen+1;
+        InputBuf[(InputPos = 0)] = '\0';
 
         free(str);
     }
@@ -131,6 +145,7 @@ static void writedata(char* buf, long n)
 {
     InputBuf[0] = '\0';
     InputPos = 0;
+//    printf("read: '%s'\n", ReadBuf);
     for (long i = 0; i < n; i++)
     {
         Handlers[State](buf[i]);