On Tue, Feb 15, 2011 at 01:01:33PM +0100, Otto Moerbeek wrote:

> On Tue, Feb 15, 2011 at 12:23:41PM +0100, Gabriel Linder wrote:
> 
> > On 02/14/11 17:43, Christiano F. Haesbaert wrote:
> > >Any news on this ?
> > >
> > 
> > The diff still apply on -current. If there are changes needed I can
> > work on it, but I have no news.
> 
> sorry, in have a diff in my tree, but I see myself always ignoring it.
> I just almost never actually use the editing capabilities. It seems my
> 25yr finger memory is too strong.  So I'm not a proper tester for this
> diff, other than it does not seem to cause a regression. 
> 
>       -Otto

Lets try to make some progress here.  This is the diff I have had in
my tree for a while. It is a port of the freebsd code. 

What is lacking is the man page stuff. So if sombody could merge that
from freebsd, I'd be happy.

        -Otto

Index: Makefile
===================================================================
RCS file: /cvs/src/usr.bin/bc/Makefile,v
retrieving revision 1.5
diff -u -p -r1.5 Makefile
--- Makefile    17 Oct 2010 22:54:37 -0000      1.5
+++ Makefile    28 Oct 2010 07:22:05 -0000
@@ -5,6 +5,9 @@ SRCS=           bc.y scan.l
 CPPFLAGS+=     -I. -I${.CURDIR}
 CFLAGS+=       -Wall -Wno-unused
 YFLAGS+=
+LDADD+=                -ledit -lcurses
+DPADD+=                ${LIBEDIT} ${LIBCURSES}
+
 
 beforeinstall:
        install -c -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/bc.library \
Index: bc.y
===================================================================
RCS file: /cvs/src/usr.bin/bc/bc.y,v
retrieving revision 1.33
diff -u -p -r1.33 bc.y
--- bc.y        27 Oct 2009 23:59:36 -0000      1.33
+++ bc.y        5 Feb 2010 19:09:43 -0000
@@ -36,6 +36,7 @@
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <histedit.h>
 #include <limits.h>
 #include <search.h>
 #include <signal.h>
@@ -1073,6 +1074,13 @@ sigchld(int signo)
        }
 }
 
+static const char *
+dummy_prompt(void)
+{
+
+        return ("");
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -1129,6 +1137,16 @@ main(int argc, char *argv[])
                        dup(p[1]);
                        close(p[0]);
                        close(p[1]);
+                       if (interactive) {
+                               el = el_init("bc", stdin, stderr, stderr);
+                               hist = history_init();
+                               history(hist, &he, H_SETSIZE, 100);
+                               el_set(el, EL_HIST, history, hist);
+                               el_set(el, EL_EDITOR, "emacs");
+                               el_set(el, EL_SIGNAL, 1);
+                               el_set(el, EL_PROMPT, dummy_prompt);
+                               el_source(el, NULL);
+                       }
                } else {
                        close(STDIN_FILENO);
                        dup(p[0]);
Index: extern.h
===================================================================
RCS file: /cvs/src/usr.bin/bc/extern.h,v
retrieving revision 1.6
diff -u -p -r1.6 extern.h
--- extern.h    18 Mar 2006 20:44:43 -0000      1.6
+++ extern.h    5 Feb 2010 09:23:08 -0000
@@ -35,5 +35,10 @@ extern int   fileindex;
 extern int     sargc;
 extern char    **sargv;
 extern char    *filename;
+extern bool             interactive;
+extern EditLine                *el;
+extern History         *hist;
+extern HistEvent        he;
 extern char    *cmdexpr;
+
 bool           interactive;
Index: scan.l
===================================================================
RCS file: /cvs/src/usr.bin/bc/scan.l,v
retrieving revision 1.23
diff -u -p -r1.23 scan.l
--- scan.l      27 Oct 2009 23:59:36 -0000      1.23
+++ scan.l      5 Feb 2010 19:10:39 -0000
@@ -18,6 +18,7 @@
  */
 
 #include <err.h>
+#include <histedit.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <string.h>
@@ -30,6 +31,10 @@
 int            lineno;
 bool           interactive;
 
+HistEvent       he;
+EditLine       *el;
+History                *hist;
+
 static char    *strbuf = NULL;
 static size_t  strbuf_sz = 1;
 static bool    dot_seen;
@@ -37,6 +42,12 @@ static bool  dot_seen;
 static void    init_strbuf(void);
 static void    add_str(const char *);
 
+static int      bc_yyinput(char *, int);
+
+#undef YY_INPUT
+#define YY_INPUT(buf,retval,max) \
+       (retval = bc_yyinput(buf, max))
+
 %}
 
 %option always-interactive
@@ -279,4 +290,33 @@ yywrap(void)
        }
        return (1);
 }
+
+static int
+bc_yyinput(char *buf, int maxlen)
+{
+       int num;
+       if (yyin == stdin && interactive) {
+               const char *bp;
+
+               if ((bp = el_gets(el, &num)) == NULL || num == 0)
+                       return (0);
+               if (num > maxlen) {
+                       el_push(el, (char *)(void *)bp + maxlen);
+                       num = maxlen;
+               }
+               memcpy(buf, bp, num);
+               history(hist, &he, H_ENTER, bp);
+       } else {
+               int c = '*';
+               for (num = 0; num < maxlen &&
+                   (c = getc(yyin)) != EOF && c != '\n'; ++num)
+                       buf[num] = (char) c;
+               if (c == '\n')
+                       buf[num++] = (char) c;
+               if (c == EOF && ferror(yyin))
+                       YY_FATAL_ERROR( "input in flex scanner failed" );
+       }
+       return (num);
+}
+

Reply via email to