From 3d9175241b8994bc707d6edbf18d23ed453be2e4 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sun, 7 Oct 2018 00:45:08 +0100 Subject: [PATCH] sh: more work on fsh (fuzix sh - with editing) 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 | 41 +++++++++++++++++++++++++++++++---- Applications/V7/cmd/sh/mode.h | 6 +++-- Applications/V7/cmd/sh/word.c | 10 ++++++++- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/Applications/V7/cmd/sh/main.c b/Applications/V7/cmd/sh/main.c index d69de093..94a13e0b 100644 --- a/Applications/V7/cmd/sh/main.c +++ b/Applications/V7/cmd/sh/main.c @@ -12,6 +12,7 @@ #include "defs.h" #include +#include #include #include #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) diff --git a/Applications/V7/cmd/sh/mode.h b/Applications/V7/cmd/sh/mode.h index efe1ad0c..5f551446 100644 --- a/Applications/V7/cmd/sh/mode.h +++ b/Applications/V7/cmd/sh/mode.h @@ -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; diff --git a/Applications/V7/cmd/sh/word.c b/Applications/V7/cmd/sh/word.c index 5139ca08..ed330dce 100644 --- a/Applications/V7/cmd/sh/word.c +++ b/Applications/V7/cmd/sh/word.c @@ -13,6 +13,10 @@ #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); } -- 2.34.1