sh: more work on fsh (fuzix sh - with editing)
authorAlan Cox <alan@linux.intel.com>
Sat, 6 Oct 2018 23:45:08 +0000 (00:45 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 6 Oct 2018 23:45:08 +0000 (00:45 +0100)
We can't just dump /bin/sh because fsh won't fit on 32/32K systems. It sort
of works at this point but there are bugs somewhere in the readline history
logic that mess stuff up

Applications/V7/cmd/sh/main.c
Applications/V7/cmd/sh/mode.h
Applications/V7/cmd/sh/word.c

index d69de09..94a13e0 100644 (file)
@@ -12,6 +12,7 @@
 
 #include       "defs.h"
 #include       <stdlib.h>
+#include       <string.h>
 #include       <unistd.h>
 #include       <fcntl.h>
 #include       "sym.h"
@@ -32,6 +33,10 @@ static void exfile(BOOL);
 
 #ifdef BUILD_FSH
 static char history[1024];
+static char inbuf[256];
+static unsigned int inleft;
+static char *inptr;
+static char ineof;
 
 static int line_input(char *prmpt)
 {
@@ -41,17 +46,45 @@ static int line_input(char *prmpt)
        do {
                l = rl_edit(standin->fdes, output,
                        prmpt,
-                       standin->fbuf, standin->fsiz);
+                       inbuf, 256);
                if (l >= 0) {
-                       standin->fbuf[l] = '\n';
-                       standin->fnxt = standin->fbuf;
-                       standin->fend = standin->fbuf + l;
+                       inbuf[l] = '\n';
+                       inptr = inbuf;
+                       inleft = l + 1;
+                       ineof = 0;
                }
+               else
+                       ineof = 1;
        } while(l == -2);
        /* 0 - EOF, 1+ buffer including \n */
        return l + 1;
 }
 
+int lineread(int fd, char *buf, int len)
+{
+       int bias = 0;
+       int r;
+       if (fd == standin->fdes && inleft) {
+               if (len <= inleft) {
+                       memcpy(buf, inptr, len);
+                       inleft -= len;
+                       inptr += len;
+                       return len;
+               }
+               memcpy(buf, inptr, inleft);
+               len -= inleft;
+               buf += inleft;
+               bias = inleft;
+               inleft = 0;
+       }
+       r = 0;
+       if (!ineof)
+               r = read(fd, buf, len);
+       if (r >= 0)
+               r += bias;
+       return r;
+}
+
 #else
 #define rl_hinit(x,y)
 #define line_input(x)  (-1)
index efe1ad0..5f55144 100644 (file)
@@ -75,15 +75,17 @@ struct blk {
    a bigger target we can afford it */
 #ifdef BUILD_FSH
 #define BUFSIZ 256
+#define BUFN POS
 #else
 #define        BUFSIZ  64
+#define BUFN CHAR
 #endif
 
 struct fileblk {
        UFD fdes;
        POS flin;
        BOOL feof;
-       CHAR fsiz;
+       BUFN fsiz;
        char * fnxt;
        char * fend;
        char * *feval;
@@ -96,7 +98,7 @@ struct filehdr {
        UFD fdes;
        POS flin;
        BOOL feof;
-       CHAR fsiz;
+       BUFN fsiz;
        char * fnxt;
        char * fend;
        char * *feval;
index 5139ca0..ed330dc 100644 (file)
 #include       "defs.h"
 #include       "sym.h"
 
+#ifdef BUILD_FSH
+extern int lineread(int fd, char *buf, int len);
+#endif
+
 static int readb(void);
 
 
@@ -173,6 +177,10 @@ static int readb(void)
                        newline();
                        sigchk();
                }
-       } while ((len = read(f->fdes, f->fbuf, f->fsiz)) < 0 && trapnote);
+#ifdef BUILD_FSH
+        } while ((len = lineread(f->fdes, f->fbuf, f->fsiz)) < 0 && trapnote);
+#else
+        } while ((len = read(f->fdes, f->fbuf, f->fsiz)) < 0 && trapnote);
+#endif
        return (len);
 }