sh: initial readline tiny changes
authorAlan Cox <alan@linux.intel.com>
Sat, 6 Oct 2018 00:01:37 +0000 (01:01 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 6 Oct 2018 00:01:37 +0000 (01:01 +0100)
This is still a work in progress to get us a /bin/sh with nice modern editing
behaviour

Applications/V7/cmd/sh/Makefile.z80
Applications/V7/cmd/sh/main.c
Applications/V7/cmd/sh/mode.h

index 8825fda..c661a79 100644 (file)
@@ -9,19 +9,25 @@ SRCS  = args.c blok.c builtin.c cmd.c ctype.c error.c expand.c fault.c io.c \
 INCS  = brkincr.h ctype.h defs.h mac.h mode.h name.h stak.h sym.h timeout.h
 
 OBJS = $(SRCS:.c=.rel)
+FOBJS = $(patsubst %.c,fshbuild/%.rel, $(SRCS))
 
-all: sh
+all: fsh sh
 
 sh: $(OBJS)
        $(FCC) $(Z80_PLATFORM) $(OBJS) -o $@
 
+fsh: $(FOBJS)
+       $(FCC) $(Z80_PLATFORM) $(FOBJS) -o $@ -lreadline
+
 $(OBJS): $(INCS)
 
-.c.rel:
+$(FOBJS): $(INCS)
+
+$(OBJS): %.rel: %.c
        $(FCC) $(Z80_PLATFORM) -c $<
 
-%: %.rel
-       $(FCC) $(Z80_PLATFORM) $< -o $@
+$(FOBJS): fshbuild/%.rel: %.c
+       $(FCC) $(Z80_PLATFORM) -c -DBUILD_FSH $< -o $@
 
 clean:
        rm -f $(OBJS) sh $(SRCS:.c=) core *~ *.asm *.lst *.sym *.map *.noi *.lk *.ihx *.tmp *.bin
index 2c0b136..d69de09 100644 (file)
@@ -18,7 +18,8 @@
 #include       "timeout.h"
 #include       <sys/types.h>
 #include       <sys/stat.h>
-#include    <setjmp.h>
+#include       <setjmp.h>
+#include       <readline/readline.h>
 
 UFD output = 2;
 static BOOL beenhere = FALSE;
@@ -29,6 +30,33 @@ char* tempfile;
 
 static void exfile(BOOL);
 
+#ifdef BUILD_FSH
+static char history[1024];
+
+static int line_input(char *prmpt)
+{
+       int l;
+       if (!isatty(standin->fdes))
+               return -1;      /* Not a tty */
+       do {
+               l = rl_edit(standin->fdes, output,
+                       prmpt,
+                       standin->fbuf, standin->fsiz);
+               if (l >= 0) {
+                       standin->fbuf[l] = '\n';
+                       standin->fnxt = standin->fbuf;
+                       standin->fend = standin->fbuf + l;
+               }
+       } while(l == -2);
+       /* 0 - EOF, 1+ buffer including \n */
+       return l + 1;
+}
+
+#else
+#define rl_hinit(x,y)
+#define line_input(x)  (-1)
+#endif
+
 int main(int c, const char *v[])
 {
        register int rflag = ttyflg;
@@ -41,6 +69,8 @@ int main(int c, const char *v[])
        setbrk(BRKINCR);
        addblok((POS) 0);
 
+       rl_hinit(history, sizeof(history));
+
        /* set names from userenv */
        sh_getenv();
 
@@ -158,8 +188,11 @@ static void exfile(BOOL prof)
                                prs(mailmsg);
                        }
                        mailtime = statb.st_mtime;
-                       prs(ps1nod.namval);
-                       alarm(TIMEOUT);
+                       if (line_input(ps1nod.namval) < 0)
+                       {
+                               prs(ps1nod.namval);
+                               alarm(TIMEOUT);
+                       }
                        flags |= waiting;
                }
 
@@ -176,8 +209,10 @@ static void exfile(BOOL prof)
 
 void chkpr(char eor)
 {
-       if ((flags & prompt) && standin->fstak == 0 && eor == NL)
-               prs(ps2nod.namval);
+       if ((flags & prompt) && standin->fstak == 0 && eor == NL) {
+               if (line_input(ps2nod.namval) < 0)
+                       prs(ps2nod.namval);
+       }
 }
 
 void settmp(void)
index 9a975cf..efe1ad0 100644 (file)
@@ -71,7 +71,14 @@ struct blk {
        BLKPTR word;
 };
 
+/* We need a real line worth of buffering for fsh to work nicely and as it's
+   a bigger target we can afford it */
+#ifdef BUILD_FSH
+#define BUFSIZ 256
+#else
 #define        BUFSIZ  64
+#endif
+
 struct fileblk {
        UFD fdes;
        POS flin;