Module Name:    src
Committed By:   christos
Date:           Tue Jan 22 20:35:29 UTC 2013

Modified Files:
        src/bin/csh: Makefile const.c csh.c csh.h extern.h glob.c lex.c set.c

Log Message:
Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.


To generate a diff of this commit:
cvs rdiff -u -r1.33 -r1.34 src/bin/csh/Makefile
cvs rdiff -u -r1.9 -r1.10 src/bin/csh/const.c
cvs rdiff -u -r1.43 -r1.44 src/bin/csh/csh.c
cvs rdiff -u -r1.22 -r1.23 src/bin/csh/csh.h
cvs rdiff -u -r1.26 -r1.27 src/bin/csh/extern.h
cvs rdiff -u -r1.25 -r1.26 src/bin/csh/glob.c
cvs rdiff -u -r1.27 -r1.28 src/bin/csh/lex.c
cvs rdiff -u -r1.29 -r1.30 src/bin/csh/set.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/csh/Makefile
diff -u src/bin/csh/Makefile:1.33 src/bin/csh/Makefile:1.34
--- src/bin/csh/Makefile:1.33	Sun Aug 28 03:49:16 2011
+++ src/bin/csh/Makefile	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.33 2011/08/28 07:49:16 christos Exp $
+#	$NetBSD: Makefile,v 1.34 2013/01/22 20:35:29 christos Exp $
 #	@(#)Makefile	8.1 (Berkeley) 5/31/93
 #
 # C Shell with process control; VM/UNIX VAX Makefile
@@ -10,6 +10,10 @@
 
 PROG=	csh
 DFLAGS=-DBUILTIN -DFILEC -DNLS -DSHORT_STRINGS
+# - Not integrated with history
+# - Does not handle escaped prompts.
+# - Does not do completion
+# DFLAGS+=-DEDIT
 CPPFLAGS+=-I${.CURDIR} -I. ${DFLAGS}
 SRCS=	alloc.c char.c const.c csh.c dir.c dol.c err.c exec.c exp.c file.c \
 	func.c glob.c hist.c init.c lex.c misc.c parse.c printf.c proc.c \
@@ -56,8 +60,13 @@ COPTS.err.c = -Wno-format-nonliteral
 COPTS.printf.c = -Wno-format-nonliteral
 COPTS.proc.c = -Wno-format-nonliteral
 
+.if 0
 LDADD+=-lutil
 DPADD+=${LIBUTIL}
+.elese
+LDADD+=-ledit -lutil
+DPADD+=${LIBEDIT} ${LIBUTIL}
+.endif
 
 .include <bsd.prog.mk>
 .include <bsd.subdir.mk>

Index: src/bin/csh/const.c
diff -u src/bin/csh/const.c:1.9 src/bin/csh/const.c:1.10
--- src/bin/csh/const.c:1.9	Thu Aug  7 05:05:03 2003
+++ src/bin/csh/const.c	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: const.c,v 1.9 2003/08/07 09:05:03 agc Exp $ */
+/* $NetBSD: const.c,v 1.10 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)const.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: const.c,v 1.9 2003/08/07 09:05:03 agc Exp $");
+__RCSID("$NetBSD: const.c,v 1.10 2013/01/22 20:35:29 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -83,6 +83,7 @@ Char STRdot[]           = { '.', '\0' };
 Char STRdotdotsl[]	= { '.', '.', '/', '\0' };
 Char STRdotsl[]		= { '.', '/', '\0' };
 Char STRecho[]          = { 'e', 'c', 'h', 'o', '\0' };
+Char STRedit[]          = { 'e', 'd', 'i', 't', '\0' };
 Char STRequal[]		= { '=', '\0' };
 Char STRfakecom[]	= { '{', ' ', '.', '.', '.', ' ', '}', '\0' };
 Char STRfakecom1[]	= { '`', ' ', '.', '.', '.', ' ', '`', '\0' };

Index: src/bin/csh/csh.c
diff -u src/bin/csh/csh.c:1.43 src/bin/csh/csh.c:1.44
--- src/bin/csh/csh.c:1.43	Sun Jan 22 13:36:14 2012
+++ src/bin/csh/csh.c	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: csh.c,v 1.43 2012/01/22 18:36:14 christos Exp $ */
+/* $NetBSD: csh.c,v 1.44 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 19
 #if 0
 static char sccsid[] = "@(#)csh.c	8.2 (Berkeley) 10/12/93";
 #else
-__RCSID("$NetBSD: csh.c,v 1.43 2012/01/22 18:36:14 christos Exp $");
+__RCSID("$NetBSD: csh.c,v 1.44 2013/01/22 20:35:29 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -105,8 +105,9 @@ static void mailchk(void);
 #ifndef _PATH_DEFPATH
 static Char **defaultpath(void);
 #endif
-
-int main(int, char *[]);
+#ifdef EDITING
+int	editing = 0;
+#endif
 
 int
 main(int argc, char *argv[])
@@ -1341,6 +1342,9 @@ printprompt(void)
 {
     Char *cp;
 
+    if (editing)
+	return;
+
     if (!whyles) {
 	for (cp = value(STRprompt); *cp; cp++)
 	    if (*cp == HIST)
@@ -1358,3 +1362,30 @@ printprompt(void)
 	(void)fprintf(cshout, "? ");
     (void)fflush(cshout);
 }
+
+#ifdef EDIT
+char *
+printpromptstr(EditLine *elx) {
+    static char pbuf[1024];
+    static char qspace[] = "? ";
+    Char *cp;
+    size_t i;
+
+    if (whyles)
+	return qspace;
+
+    i = 0;
+    for (cp = value(STRprompt); *cp; cp++) {
+	if (i >= sizeof(pbuf))
+	    break;
+	if (*cp == HIST)
+	    i += snprintf(pbuf + i, sizeof(pbuf) - i, "%d", eventno + 1);
+	else
+	    pbuf[i++] = *cp;
+    }
+    if (i >= sizeof(pbuf))
+	i = sizeof(pbuf) - 1;
+    pbuf[i] = '\0';
+    return pbuf;
+}
+#endif

Index: src/bin/csh/csh.h
diff -u src/bin/csh/csh.h:1.22 src/bin/csh/csh.h:1.23
--- src/bin/csh/csh.h:1.22	Wed Nov  9 14:16:00 2011
+++ src/bin/csh/csh.h	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: csh.h,v 1.22 2011/11/09 19:16:00 christos Exp $ */
+/* $NetBSD: csh.h,v 1.23 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993
@@ -549,4 +549,10 @@ Char *STR_BSHELL;
 Char *STR_WORD_CHARS;
 Char **STR_environ;
 
+#ifdef EDIT
+#include <histedit.h>
+EditLine *el;
+int editing;
+#endif
+
 #endif /* !_CSH_H_ */

Index: src/bin/csh/extern.h
diff -u src/bin/csh/extern.h:1.26 src/bin/csh/extern.h:1.27
--- src/bin/csh/extern.h:1.26	Tue Jan 22 14:28:00 2013
+++ src/bin/csh/extern.h	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: extern.h,v 1.26 2013/01/22 19:28:00 christos Exp $ */
+/* $NetBSD: extern.h,v 1.27 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -48,6 +48,9 @@ void initdesc(void);
 __dead void pintr(int);
 __dead void pintr1(int);
 void printprompt(void);
+#ifdef EDIT
+char *printpromptstr(EditLine *);
+#endif
 void process(int);
 void rechist(void);
 void untty(void);

Index: src/bin/csh/glob.c
diff -u src/bin/csh/glob.c:1.25 src/bin/csh/glob.c:1.26
--- src/bin/csh/glob.c:1.25	Mon Jul 16 14:26:10 2007
+++ src/bin/csh/glob.c	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: glob.c,v 1.25 2007/07/16 18:26:10 christos Exp $ */
+/* $NetBSD: glob.c,v 1.26 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)glob.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: glob.c,v 1.25 2007/07/16 18:26:10 christos Exp $");
+__RCSID("$NetBSD: glob.c,v 1.26 2013/01/22 20:35:29 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -213,13 +213,13 @@ globbrace(Char *s, Char *p, Char ***bl)
 static void
 expbrace(Char ***nvp, Char ***elp, int size)
 {
-    Char **el, **nv, *s, **vl;
+    Char **ex, **nv, *s, **vl;
 
     vl = nv = *nvp;
     if (elp != NULL)
-	el = *elp;
+	ex = *elp;
     else
-	for (el = vl; *el; el++)
+	for (ex = vl; *ex; ex++)
 	    continue;
 
     for (s = *vl; s; s = *++vl) {
@@ -243,24 +243,24 @@ expbrace(Char ***nvp, Char ***elp, int s
 		continue;
 	    }
 	    len = blklen(bl);
-	    if (&el[len] >= &nv[size]) {
+	    if (&ex[len] >= &nv[size]) {
 		int e, l;
 
-		l = &el[len] - &nv[size];
+		l = &ex[len] - &nv[size];
 		size += GLOBSPACE > l ? GLOBSPACE : l;
 		l = vl - nv;
-		e = el - nv;
+		e = ex - nv;
 		nv = (Char **)xrealloc((ptr_t)nv,
 		    (size_t)size * sizeof(Char *));
 		vl = nv + l;
-		el = nv + e;
+		ex = nv + e;
 	    }
 	    vp = vl--;
 	    *vp = *bl;
 	    len--;
-	    for (bp = el; bp != vp; bp--)
+	    for (bp = ex; bp != vp; bp--)
 		bp[len] = *bp;
-	    el += len;
+	    ex += len;
 	    vp++;
 	    for (bp = bl + 1; *bp; *vp++ = *bp++)
 		continue;
@@ -269,14 +269,14 @@ expbrace(Char ***nvp, Char ***elp, int s
 
     }
     if (elp != NULL)
-	*elp = el;
+	*elp = ex;
     *nvp = nv;
 }
 
 static Char **
 globexpand(Char **v)
 {
-    Char **el, **nv, *s, **vl;
+    Char **ex, **nv, *s, **vl;
     int size;
 
     size = GLOBSPACE;
@@ -321,8 +321,8 @@ globexpand(Char **v)
     /*
      * Step 2: expand braces
      */
-    el = vl;
-    expbrace(&nv, &el, size);
+    ex = vl;
+    expbrace(&nv, &ex, size);
 
     /*
      * Step 3: expand ~

Index: src/bin/csh/lex.c
diff -u src/bin/csh/lex.c:1.27 src/bin/csh/lex.c:1.28
--- src/bin/csh/lex.c:1.27	Sun Jan 17 07:15:36 2010
+++ src/bin/csh/lex.c	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.27 2010/01/17 12:15:36 wiz Exp $ */
+/* $NetBSD: lex.c,v 1.28 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)lex.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: lex.c,v 1.27 2010/01/17 12:15:36 wiz Exp $");
+__RCSID("$NetBSD: lex.c,v 1.28 2013/01/22 20:35:29 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -1440,9 +1440,21 @@ again:
 	roomleft = BUFSIZE - off;
 
 #ifdef FILEC
-	roomleft = BUFSIZE - off;
 	for (;;) {
-	    if (filec && intty) {
+	    if ((editing || filec) && intty) {
+#ifdef EDIT
+		if (editing) {
+			const char *p;
+			if ((p = el_gets(el, &c)) != NULL) {
+				size_t i;
+				/* XXX: Truncation */
+				numleft = c > BUFSIZE ? BUFSIZE : c;
+				for (i = 0; *p && i < BUFSIZE; i++, p++)
+					ttyline[i] = *p;
+				ttyline[i - (i == BUFSIZE)] = '\0';
+			}
+		}
+#endif
 		c = numleft ? numleft : tenex(ttyline, BUFSIZE);
 		if (c > roomleft) {
 		    /* start with fresh buffer */

Index: src/bin/csh/set.c
diff -u src/bin/csh/set.c:1.29 src/bin/csh/set.c:1.30
--- src/bin/csh/set.c:1.29	Mon Jul 16 14:26:11 2007
+++ src/bin/csh/set.c	Tue Jan 22 15:35:29 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: set.c,v 1.29 2007/07/16 18:26:11 christos Exp $ */
+/* $NetBSD: set.c,v 1.30 2013/01/22 20:35:29 christos Exp $ */
 
 /*-
  * Copyright (c) 1980, 1991, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)set.c	8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: set.c,v 1.29 2007/07/16 18:26:11 christos Exp $");
+__RCSID("$NetBSD: set.c,v 1.30 2013/01/22 20:35:29 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -174,6 +174,15 @@ doset(Char **v, struct command *t)
 	else if (eq(vp, STRfilec))
 	    filec = 1;
 #endif
+#ifdef EDIT
+	else if (eq(vp, STRedit)) {
+		editing = 1;
+		el = el_init_fd(getprogname(), cshin, cshout, csherr,
+		    SHIN, SHOUT, SHERR);
+		el_set(el, EL_EDITOR, "emacs");
+		el_set(el, EL_PROMPT, printpromptstr);
+	}
+#endif
     } while ((p = *v++) != NULL);
 }
 
@@ -494,16 +503,23 @@ void
 unset(Char **v, struct command *t)
 {
     unset1(v, &shvhed);
-#ifdef FILEC
-    if (adrof(STRfilec) == 0)
-	filec = 0;
-#endif
     if (adrof(STRhistchars) == 0) {
 	HIST = '!';
 	HISTSUB = '^';
     }
-    if (adrof(STRwordchars) == 0)
+    else if (adrof(STRwordchars) == 0)
 	word_chars = STR_WORD_CHARS;
+#ifdef FILEC
+    else if (adrof(STRfilec) == 0)
+	filec = 0;
+#endif
+#ifdef EDIT
+    else if (adrof(STRedit) == 0) {
+	el_end(el);
+	el = NULL;
+	editing = 0;
+    }
+#endif
 }
 
 void

Reply via email to