Module Name:    src
Committed By:   snj
Date:           Sun Dec 18 06:37:30 UTC 2016

Modified Files:
        src/external/bsd/nvi [netbsd-7]: Makefile.inc
        src/external/bsd/nvi/dist/cl [netbsd-7]: cl.h cl_main.c cl_screen.c
            cl_term.c
        src/external/bsd/nvi/dist/common [netbsd-7]: key.h vi_db1.c
        src/external/bsd/nvi/dist/docs/vi.man [netbsd-7]: vi.1
        src/external/bsd/nvi/dist/ex [netbsd-7]: ex_map.c ex_script.c
        src/external/bsd/nvi/usr.bin/nvi [netbsd-7]: Makefile

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #1312):
        external/bsd/nvi/Makefile.inc: revisions 1.2-1.4
        external/bsd/nvi/dist/cl/cl.h: revision 1.3
        external/bsd/nvi/dist/cl/cl_main.c: revision 1.5
        external/bsd/nvi/dist/cl/cl_screen.c: revision 1.5
        external/bsd/nvi/dist/cl/cl_term.c: revision 1.5
        external/bsd/nvi/dist/common/key.h: revision 1.3
        external/bsd/nvi/dist/common/vi_db1.c: revision 1.8
        external/bsd/nvi/dist/docs/vi.man/vi.1: revisions 1.3, 1.4
        external/bsd/nvi/dist/ex/ex_map.c: revision 1.4
        external/bsd/nvi/dist/ex/ex_script.c: revisions 1.5, 1.6
        external/bsd/nvi/usr.bin/nvi/Makefile: revision 1.7
PR/50092: Rin Okuyama: Fix memory leaks in vi when resizing.
--
PR/50484: Rin Okuyama: fix the script command of vi(1)
--
Fix > 1024 char lines in script. (Rin Okuyama)
--
remove CONST; it is unused from Brad Harder
--
add default: to appease gcc.
--
Restore the first line of the copyright header, which accidentally got
zapped in -r1.2.
--
PR 51446 Brad Harder: fix synopsis for :tagprev


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.1.6.1 src/external/bsd/nvi/Makefile.inc
cvs rdiff -u -r1.2 -r1.2.6.1 src/external/bsd/nvi/dist/cl/cl.h
cvs rdiff -u -r1.4 -r1.4.6.1 src/external/bsd/nvi/dist/cl/cl_main.c \
    src/external/bsd/nvi/dist/cl/cl_screen.c \
    src/external/bsd/nvi/dist/cl/cl_term.c
cvs rdiff -u -r1.2 -r1.2.6.1 src/external/bsd/nvi/dist/common/key.h
cvs rdiff -u -r1.7 -r1.7.6.1 src/external/bsd/nvi/dist/common/vi_db1.c
cvs rdiff -u -r1.2 -r1.2.6.1 src/external/bsd/nvi/dist/docs/vi.man/vi.1
cvs rdiff -u -r1.3 -r1.3.6.1 src/external/bsd/nvi/dist/ex/ex_map.c
cvs rdiff -u -r1.4 -r1.4.6.1 src/external/bsd/nvi/dist/ex/ex_script.c
cvs rdiff -u -r1.5 -r1.5.2.1 src/external/bsd/nvi/usr.bin/nvi/Makefile

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

Modified files:

Index: src/external/bsd/nvi/Makefile.inc
diff -u src/external/bsd/nvi/Makefile.inc:1.1 src/external/bsd/nvi/Makefile.inc:1.1.6.1
--- src/external/bsd/nvi/Makefile.inc:1.1	Fri Nov 22 16:00:45 2013
+++ src/external/bsd/nvi/Makefile.inc	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.inc,v 1.1 2013/11/22 16:00:45 christos Exp $
+#	$NetBSD: Makefile.inc,v 1.1.6.1 2016/12/18 06:37:30 snj Exp $
 
 .include <bsd.own.mk>
 
@@ -7,4 +7,4 @@ DIST= ${NETBSDSRCDIR}/external/bsd/nvi/d
 BINDIR=/usr/bin
 
 CWARNFLAGS.clang+=	-Wno-error=unused-const-variable
-VERSION=1.81.6-2013-11-20
+VERSION=1.81.6-2013-11-20nb3

Index: src/external/bsd/nvi/dist/cl/cl.h
diff -u src/external/bsd/nvi/dist/cl/cl.h:1.2 src/external/bsd/nvi/dist/cl/cl.h:1.2.6.1
--- src/external/bsd/nvi/dist/cl/cl.h:1.2	Fri Nov 22 15:52:05 2013
+++ src/external/bsd/nvi/dist/cl/cl.h	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: cl.h,v 1.2 2013/11/22 15:52:05 christos Exp $ */
+/*	$NetBSD: cl.h,v 1.2.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -42,6 +42,8 @@ typedef struct _cl_private {
 	struct termios ex_enter;/* Terminal values to enter ex. */
 	struct termios vi_enter;/* Terminal values to enter vi. */
 
+	SCREEN	*screen;	/* Curses screen. */
+
 	char	*el;		/* Clear to EOL terminal string. */
 	char	*cup;		/* Cursor movement terminal string. */
 	char	*cuu1;		/* Cursor up terminal string. */
@@ -77,6 +79,8 @@ typedef struct _cl_private {
 #define	CL_SIGTERM	0x0100	/* SIGTERM arrived. */
 #define	CL_SIGWINCH	0x0200	/* SIGWINCH arrived. */
 #define	CL_STDIN_TTY	0x0400	/* Talking to a terminal. */
+#define	CL_SETUPTERM	0x0800	/* Terminal initialized. */
+#define	CL_CHANGE_TERM	0x1000	/* Terminal changed. */
 	u_int32_t flags;
 } CL_PRIVATE;
 

Index: src/external/bsd/nvi/dist/cl/cl_main.c
diff -u src/external/bsd/nvi/dist/cl/cl_main.c:1.4 src/external/bsd/nvi/dist/cl/cl_main.c:1.4.6.1
--- src/external/bsd/nvi/dist/cl/cl_main.c:1.4	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/cl/cl_main.c	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: cl_main.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
+/*	$NetBSD: cl_main.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -16,7 +16,7 @@
 static const char sccsid[] = "Id: cl_main.c,v 10.54 2001/07/29 19:07:27 skimo Exp  (Berkeley) Date: 2001/07/29 19:07:27 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: cl_main.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: cl_main.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -109,6 +109,7 @@ main(int argc, char **argv)
 		ttype = "unknown";
 	}
 	term_init(gp->progname, ttype);
+	F_SET(clp, CL_SETUPTERM);
 
 	/* Add the terminal type to the global structure. */
 	if ((OG_D_STR(gp, GO_TERM) =
@@ -292,6 +293,22 @@ static void
 h_winch(int signo)
 {
 	GLOBAL_CLP;
+	sigset_t sigset;
+	struct timespec timeout;
+
+	/*
+	 * Some window managers continuously change the screen size of terminal
+	 * emulators, by which a lot of SIGWINCH signals are to be received. In
+	 * such a case, we only need to respond the final signal; the remaining
+	 * signals are meaningless.  Thus, we wait here up to 1/10 of a second
+	 * for a succeeding signal received.
+	 */
+	(void)sigemptyset(&sigset);
+	(void)sigaddset(&sigset, SIGWINCH);
+	timeout.tv_sec = 0;
+	timeout.tv_nsec = 100 * 1000 * 1000;
+	while (sigtimedwait(&sigset, NULL, &timeout) != -1)
+		continue;
 
 	F_SET(clp, CL_SIGWINCH);
 }
Index: src/external/bsd/nvi/dist/cl/cl_screen.c
diff -u src/external/bsd/nvi/dist/cl/cl_screen.c:1.4 src/external/bsd/nvi/dist/cl/cl_screen.c:1.4.6.1
--- src/external/bsd/nvi/dist/cl/cl_screen.c:1.4	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/cl/cl_screen.c	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
+/*	$NetBSD: cl_screen.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -16,7 +16,7 @@
 static const char sccsid[] = "Id: cl_screen.c,v 10.56 2002/05/03 19:59:44 skimo Exp  (Berkeley) Date: 2002/05/03 19:59:44 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: cl_screen.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: cl_screen.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -34,6 +34,10 @@ __RCSID("$NetBSD: cl_screen.c,v 1.4 2014
 #include "../common/common.h"
 #include "cl.h"
 
+#ifndef BLOCK_SIGNALS
+extern sigset_t __sigblockset;
+#endif
+
 static int	cl_ex_end __P((GS *));
 static int	cl_ex_init __P((SCR *));
 static void	cl_freecap __P((CL_PRIVATE *));
@@ -53,26 +57,38 @@ cl_screen(SCR *sp, u_int32_t flags)
 	CL_PRIVATE *clp;
 	WINDOW *win;
 	GS *gp;
+	int ret, error;
+	sigset_t oset;
 
 	gp = sp->gp;
 	clp = CLP(sp);
 	win = CLSP(sp) ? CLSP(sp) : stdscr;
 
+	ret = 0;
+
+	/*
+	 * During initialization of the screen, block signals to make sure that
+	 * curses/terminfo routines are not interrupted.
+	 */
+	error = sigprocmask(SIG_BLOCK, &__sigblockset, &oset);
+
 	/* See if the current information is incorrect. */
 	if (F_ISSET(gp, G_SRESTART)) {
 		if (CLSP(sp)) {
 		    delwin(CLSP(sp));
 		    sp->cl_private = NULL;
 		}
-		if (cl_quit(gp))
-			return (1);
+		if (cl_quit(gp)) {
+			ret = 1;
+			goto end;
+		}
 		F_CLR(gp, G_SRESTART);
 	}
 	
 	/* See if we're already in the right mode. */
 	if ((LF_ISSET(SC_EX) && F_ISSET(sp, SC_SCR_EX)) ||
 	    (LF_ISSET(SC_VI) && F_ISSET(sp, SC_SCR_VI)))
-		return (0);
+		goto end;
 
 	/*
 	 * Fake leaving ex mode.
@@ -109,8 +125,10 @@ cl_screen(SCR *sp, u_int32_t flags)
 
 	/* Enter the requested mode. */
 	if (LF_ISSET(SC_EX)) {
-		if (cl_ex_init(sp))
-			return (1);
+		if (cl_ex_init(sp)) {
+			ret = 1;
+			goto end;
+		}
 		F_SET(clp, CL_IN_EX | CL_SCR_EX_INIT);
 
 		/*
@@ -121,12 +139,18 @@ cl_screen(SCR *sp, u_int32_t flags)
 			tputs(tgoto(clp->cup,
 			    0, O_VAL(sp, O_LINES) - 1), 1, cl_putchar);
 	} else {
-		if (cl_vi_init(sp))
-			return (1);
+		if (cl_vi_init(sp)) {
+			ret = 1;
+			goto end;
+		}
 		F_CLR(clp, CL_IN_EX);
 		F_SET(clp, CL_SCR_VI_INIT);
 	}
-	return (0);
+end:
+	/* Unblock signals. */
+	if (error == 0)
+		(void)sigprocmask(SIG_SETMASK, &oset, NULL);
+	return ret;
 }
 
 /*
@@ -234,10 +258,14 @@ cl_vi_init(SCR *sp)
 	o_cols = getenv("COLUMNS");
 	cl_putenv(sp, "COLUMNS", NULL, (u_long)O_VAL(sp, O_COLUMNS));
 
+	/* Delete cur_term if exists. */
+	if (F_ISSET(clp, CL_SETUPTERM)) {
+		if (del_curterm(cur_term))
+			return (1);
+		F_CLR(clp, CL_SETUPTERM);
+	}
+
 	/*
-	 * We don't care about the SCREEN reference returned by newterm, we
-	 * never have more than one SCREEN at a time.
-	 *
 	 * XXX
 	 * The SunOS initscr() can't be called twice.  Don't even think about
 	 * using it.  It fails in subtle ways (e.g. select(2) on fileno(stdin)
@@ -249,7 +277,7 @@ cl_vi_init(SCR *sp)
 	 * have to specify the terminal type.
 	 */
 	errno = 0;
-	if (newterm(__UNCONST(ttype), stdout, stdin) == NULL) {
+	if ((clp->screen = newterm(__UNCONST(ttype), stdout, stdin)) == NULL) {
 		if (errno)
 			msgq(sp, M_SYSERR, "%s", ttype);
 		else
@@ -374,8 +402,6 @@ cl_vi_init(SCR *sp)
 
 fast:	/* Set the terminal modes. */
 	if (tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &clp->vi_enter)) {
-		if (errno == EINTR)
-			goto fast;
 		msgq(sp, M_SYSERR, "tcsetattr");
 err:		(void)cl_vi_end(sp->gp);
 		return (1);
@@ -416,6 +442,9 @@ cl_vi_end(GS *gp)
 	/* End curses window. */
 	(void)endwin();
 
+	/* Delete curses screen. */
+	delscreen(clp->screen);
+
 	/*
 	 * XXX
 	 * The screen TE sequence just got sent.  See the comment in
@@ -434,6 +463,8 @@ static int
 cl_ex_init(SCR *sp)
 {
 	CL_PRIVATE *clp;
+	int error;
+	const char *ttype;
 
 	clp = CLP(sp);
 
@@ -445,6 +476,22 @@ cl_ex_init(SCR *sp)
 	if (!F_ISSET(clp, CL_STDIN_TTY))
 		return (0);
 
+	if (F_ISSET(clp, CL_CHANGE_TERM)) {
+		if (F_ISSET(clp, CL_SETUPTERM) && del_curterm(cur_term))
+			return (1);
+		F_CLR(clp, CL_SETUPTERM | CL_CHANGE_TERM);
+	}
+
+	if (!F_ISSET(clp, CL_SETUPTERM)) {
+		/* We'll need a terminal type. */
+		if (opts_empty(sp, O_TERM, 0))
+			return (1);
+		ttype = O_STR(sp, O_TERM);
+		(void)setupterm(ttype, STDOUT_FILENO, &error);
+		if (error == 0 || error == -1)
+			return (1);
+	}
+
 	/* Get the ex termcap/terminfo strings. */
 	(void)cl_getcap(sp, "cup", &clp->cup);
 	(void)cl_getcap(sp, "smso", &clp->smso);
Index: src/external/bsd/nvi/dist/cl/cl_term.c
diff -u src/external/bsd/nvi/dist/cl/cl_term.c:1.4 src/external/bsd/nvi/dist/cl/cl_term.c:1.4.6.1
--- src/external/bsd/nvi/dist/cl/cl_term.c:1.4	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/cl/cl_term.c	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: cl_term.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
+/*	$NetBSD: cl_term.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -16,7 +16,7 @@
 static const char sccsid[] = "Id: cl_term.c,v 10.31 2001/07/08 13:06:56 skimo Exp  (Berkeley) Date: 2001/07/08 13:06:56 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: cl_term.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: cl_term.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -268,9 +268,12 @@ cl_optchange(SCR *sp, int opt, const cha
 	clp = CLP(sp);
 
 	switch (opt) {
+	case O_TERM:
+		if (F_ISSET(sp, SC_SCR_EX))
+			F_SET(clp, CL_CHANGE_TERM);
+		/* FALLTHROUGH */
 	case O_COLUMNS:
 	case O_LINES:
-	case O_TERM:
 		/*
 		 * Changing the columns, lines or terminal require that
 		 * we restart the screen.
@@ -417,7 +420,6 @@ cl_ssize(SCR *sp, int sigwinch, size_t *
 			*rowp = row;
 		if (colp != NULL)
 			*colp = col;
-		resizeterm(row, col);
 		return (0);
 	}
 

Index: src/external/bsd/nvi/dist/common/key.h
diff -u src/external/bsd/nvi/dist/common/key.h:1.2 src/external/bsd/nvi/dist/common/key.h:1.2.6.1
--- src/external/bsd/nvi/dist/common/key.h:1.2	Fri Nov 22 15:52:05 2013
+++ src/external/bsd/nvi/dist/common/key.h	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: key.h,v 1.2 2013/11/22 15:52:05 christos Exp $ */
+/*	$NetBSD: key.h,v 1.2.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1991, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -25,7 +25,6 @@
     sp->conv.int2sys(sp, w, wlen, &sp->wp->cw, &nlen, &n)
 #define INPUT2INT5(sp,cw,n,nlen,w,wlen)					    \
     sp->conv.input2int(sp, n, nlen, &(cw), &wlen, &w)
-#define CONST
 #define INTISWIDE(c)	(wctob(c) == EOF)	    /* XXX wrong name */
 #define CHAR_WIDTH(sp, ch)  wcwidth(ch)
 #else
@@ -41,7 +40,6 @@
     (n = w, nlen = wlen, 0)
 #define INPUT2INT5(sp,buf,n,nlen,w,wlen) \
     (w = n, wlen = nlen, 0)
-#define CONST const
 #define INTISWIDE(c)	    0
 #define CHAR_WIDTH(sp, ch)  1
 #endif

Index: src/external/bsd/nvi/dist/common/vi_db1.c
diff -u src/external/bsd/nvi/dist/common/vi_db1.c:1.7 src/external/bsd/nvi/dist/common/vi_db1.c:1.7.6.1
--- src/external/bsd/nvi/dist/common/vi_db1.c:1.7	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/common/vi_db1.c	Sun Dec 18 06:37:30 2016
@@ -15,7 +15,7 @@
 static const char sccsid[] = "Id: db1.c,v 10.1 2002/03/09 12:53:57 skimo Exp  (Berkeley) Date: 2002/03/09 12:53:57 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: vi_db1.c,v 1.7 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: vi_db1.c,v 1.7.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -421,10 +421,10 @@ db_insert(SCR *sp, db_recno_t lno, CHAR_
  * db_set --
  *	Store a line in the file.
  *
- * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
+ * PUBLIC: int db_set __P((SCR *, db_recno_t, const CHAR_T *, size_t));
  */
 int
-db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
+db_set(SCR *sp, db_recno_t lno, const CHAR_T *p, size_t len)
 {
 	DBT data, key;
 	EXF *ep;

Index: src/external/bsd/nvi/dist/docs/vi.man/vi.1
diff -u src/external/bsd/nvi/dist/docs/vi.man/vi.1:1.2 src/external/bsd/nvi/dist/docs/vi.man/vi.1:1.2.6.1
--- src/external/bsd/nvi/dist/docs/vi.man/vi.1:1.2	Fri Nov 22 15:52:05 2013
+++ src/external/bsd/nvi/dist/docs/vi.man/vi.1	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,6 @@
-.\"	$NetBSD: vi.1,v 1.2 2013/11/22 15:52:05 christos Exp $
+.\"	$NetBSD: vi.1,v 1.2.6.1 2016/12/18 06:37:30 snj Exp $
+.\"
+.\" Copyright (c) 1994
 .\"     The Regents of the University of California.  All rights reserved.
 .\" Copyright (c) 1994, 1995, 1996
 .\"	Keith Bostic.  All rights reserved.
@@ -987,7 +989,7 @@ Edit the file containing the next contex
 .B "tagp[op][!] [file | number]"
 Pop to the specified tag in the tags stack.
 .TP
-.B "tagp[rev][!]"
+.B "tagpr[ev][!]"
 Edit the file containing the previous context for the current tag.
 .TP
 .B "unm[ap][!] lhs"

Index: src/external/bsd/nvi/dist/ex/ex_map.c
diff -u src/external/bsd/nvi/dist/ex/ex_map.c:1.3 src/external/bsd/nvi/dist/ex/ex_map.c:1.3.6.1
--- src/external/bsd/nvi/dist/ex/ex_map.c:1.3	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/ex/ex_map.c	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ex_map.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
+/*	$NetBSD: ex_map.c,v 1.3.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1992, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -16,7 +16,7 @@
 static const char sccsid[] = "Id: ex_map.c,v 10.11 2001/06/25 15:19:17 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:17 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: ex_map.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: ex_map.c,v 1.3.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -99,6 +99,8 @@ nofunc:	if (stype == SEQ_COMMAND && inpu
 			    "134|The %s character may not be remapped",
 			    KEY_NAME(sp, input[0]));
 			return (1);
+		default:
+			break;
 		}
 	return (seq_set(sp, NULL, 0, input, cmdp->argv[0]->len,
 	    cmdp->argv[1]->bp, cmdp->argv[1]->len, stype, SEQ_USERDEF));

Index: src/external/bsd/nvi/dist/ex/ex_script.c
diff -u src/external/bsd/nvi/dist/ex/ex_script.c:1.4 src/external/bsd/nvi/dist/ex/ex_script.c:1.4.6.1
--- src/external/bsd/nvi/dist/ex/ex_script.c:1.4	Sun Jan 26 21:43:45 2014
+++ src/external/bsd/nvi/dist/ex/ex_script.c	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ex_script.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
+/*	$NetBSD: ex_script.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $ */
 /*-
  * Copyright (c) 1992, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -19,7 +19,7 @@
 static const char sccsid[] = "Id: ex_script.c,v 10.38 2001/06/25 15:19:19 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:19 ";
 #endif /* not lint */
 #else
-__RCSID("$NetBSD: ex_script.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
+__RCSID("$NetBSD: ex_script.c,v 1.4.6.1 2016/12/18 06:37:30 snj Exp $");
 #endif
 
 #include <sys/types.h>
@@ -29,7 +29,7 @@ __RCSID("$NetBSD: ex_script.c,v 1.4 2014
 #include <sys/select.h>
 #endif
 #include <sys/stat.h>
-#if defined(HAVE_SYS5_PTY) && !defined(__NetBSD__)
+#if defined(HAVE_SYS5_PTY)
 #include <sys/stropts.h>
 #endif
 #include <sys/time.h>
@@ -45,6 +45,9 @@ __RCSID("$NetBSD: ex_script.c,v 1.4 2014
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
+#ifdef __NetBSD__
+#include <util.h>
+#endif
 
 #include "../common/common.h"
 #include "../vi/vi.h"
@@ -55,9 +58,12 @@ static void	sscr_check __P((SCR *));
 static int	sscr_getprompt __P((SCR *));
 static int	sscr_init __P((SCR *));
 static int	sscr_insert __P((SCR *));
-static int	sscr_matchprompt __P((SCR *, CHAR_T *, size_t, size_t *));
+#ifdef __NetBSD__
+#define	sscr_pty openpty
+#else
 static int	sscr_pty __P((int *, int *, char *, struct termios *, void *));
-static int	sscr_setprompt __P((SCR *, CHAR_T *, size_t));
+#endif
+static int	sscr_setprompt __P((SCR *, char *, size_t));
 
 /*
  * ex_script -- : sc[ript][!] [file]
@@ -75,6 +81,17 @@ ex_script(SCR *sp, EXCMD *cmdp)
 		return (1);
 	}
 
+	/* Avoid double run. */
+	if (F_ISSET(sp, SC_SCRIPT)) {
+		msgq(sp, M_ERR,
+		    "The script command is already runninng");
+		return (1);
+	}
+
+	/* We're going to need a shell. */
+	if (opts_empty(sp, O_SHELL, 0))
+		return (1);
+
 	/* Switch to the new file. */
 	if (cmdp->argc != 0 && ex_edit(sp, cmdp))
 		return (1);
@@ -96,10 +113,6 @@ sscr_init(SCR *sp)
 	SCRIPT *sc;
 	const char *sh, *sh_path;
 
-	/* We're going to need a shell. */
-	if (opts_empty(sp, O_SHELL, 0))
-		return (1);
-
 	MALLOC_RET(sp, sc, SCRIPT *, sizeof(SCRIPT));
 	sp->script = sc;
 	sc->sh_prompt = NULL;
@@ -209,89 +222,28 @@ static int
 sscr_getprompt(SCR *sp)
 {
 	struct timeval tv;
-	CHAR_T *endp, *p, *t, buf[1024];
-	SCRIPT *sc;
 	fd_set fdset;
-	db_recno_t lline;
-	size_t llen, len;
-	e_key_t value;
-	int nr;
-
-	FD_ZERO(&fdset);
-	endp = buf;
-	len = sizeof(buf);
+	int master;
 
 	/* Wait up to a second for characters to read. */
 	tv.tv_sec = 5;
 	tv.tv_usec = 0;
-	sc = sp->script;
-	FD_SET(sc->sh_master, &fdset);
-	switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
+	master = sp->script->sh_master;
+	FD_ZERO(&fdset);
+	FD_SET(master, &fdset);
+	switch (select(master + 1, &fdset, NULL, NULL, &tv)) {
 	case -1:		/* Error or interrupt. */
 		msgq(sp, M_SYSERR, "select");
-		goto prompterr;
-	case  0:		/* Timeout */
-		msgq(sp, M_ERR, "Error: timed out");
-		goto prompterr;
-	case  1:		/* Characters to read. */
 		break;
-	}
-
-	/* Read the characters. */
-more:	len = sizeof(buf) - (endp - buf);
-	switch (nr = read(sc->sh_master, endp, len)) {
-	case  0:			/* EOF. */
-		msgq(sp, M_ERR, "Error: shell: EOF");
-		goto prompterr;
-	case -1:			/* Error or interrupt. */
-		msgq(sp, M_SYSERR, "shell");
-		goto prompterr;
-	default:
-		endp += nr;
-		break;
-	}
-
-	/* If any complete lines, push them into the file. */
-	for (p = t = buf; p < endp; ++p) {
-		value = KEY_VAL(sp, *p);
-		if (value == K_CR || value == K_NL) {
-			if (db_last(sp, &lline) ||
-			    db_append(sp, 0, lline, t, p - t))
-				goto prompterr;
-			t = p + 1;
-		}
-	}
-	if (p > buf) {
-		MEMMOVE(buf, t, endp - t);
-		endp = buf + (endp - t);
-	}
-	if (endp == buf)
-		goto more;
-
-	/* Wait up 1/10 of a second to make sure that we got it all. */
-	tv.tv_sec = 0;
-	tv.tv_usec = 100000;
-	switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
-	case -1:		/* Error or interrupt. */
-		msgq(sp, M_SYSERR, "select");
-		goto prompterr;
 	case  0:		/* Timeout */
+		msgq(sp, M_ERR, "Error: timed out");
 		break;
 	case  1:		/* Characters to read. */
-		goto more;
-	}
-
-	/* Timed out, so theoretically we have a prompt. */
-	llen = endp - buf;
-	endp = buf;
-
-	/* Append the line into the file. */
-	if (db_last(sp, &lline) || db_append(sp, 0, lline, buf, llen)) {
-prompterr:	sscr_end(sp);
-		return (1);
+		return (sscr_insert(sp) || sp->script == NULL);
 	}
 
-	return (sscr_setprompt(sp, buf, llen));
+	sscr_end(sp);
+	return (1);
 }
 
 /*
@@ -305,47 +257,53 @@ sscr_exec(SCR *sp, db_recno_t lno)
 {
 	SCRIPT *sc;
 	db_recno_t last_lno;
-	size_t blen, len, last_len, tlen;
+	size_t blen, len, last_len;
 	int isempty, matchprompt, rval;
 	ssize_t nw;
-	CHAR_T *bp = NULL;
-	CHAR_T *p;
+	char *bp = NULL;
+	const char *p;
+	const CHAR_T *ip;
+	size_t ilen;
+
+	sc = sp->script;
 
 	/* If there's a prompt on the last line, append the command. */
 	if (db_last(sp, &last_lno))
 		return (1);
-	if (db_get(sp, last_lno, DBG_FATAL, &p, &last_len))
+	if (db_get(sp, last_lno, DBG_FATAL, __UNCONST(&ip), &ilen))
 		return (1);
-	if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
+	INT2CHAR(sp, ip, ilen, p, last_len);
+	if (last_len == sc->sh_prompt_len &&
+	    strnstr(p, sc->sh_prompt, last_len) == p) {
 		matchprompt = 1;
-		GET_SPACE_RETW(sp, bp, blen, last_len + 128);
-		MEMMOVEW(bp, p, last_len);
+		GET_SPACE_RETC(sp, bp, blen, last_len + 128);
+		memmove(bp, p, last_len);
 	} else
 		matchprompt = 0;
 
 	/* Get something to execute. */
-	if (db_eget(sp, lno, &p, &len, &isempty)) {
+	if (db_eget(sp, lno, __UNCONST(&ip), &ilen, &isempty)) {
 		if (isempty)
 			goto empty;
 		goto err1;
 	}
 
 	/* Empty lines aren't interesting. */
-	if (len == 0)
+	if (ilen == 0)
 		goto empty;
+	INT2CHAR(sp, ip, ilen, p, len);
 
 	/* Delete any prompt. */
-	if (sscr_matchprompt(sp, p, len, &tlen)) {
-		if (tlen == len) {
+	if (strnstr(p, sc->sh_prompt, len) == p) {
+		len -= sc->sh_prompt_len;
+		if (len == 0) {
 empty:			msgq(sp, M_BERR, "151|No command to execute");
 			goto err1;
 		}
-		p += (len - tlen);
-		len = tlen;
+		p += sc->sh_prompt_len;
 	}
 
 	/* Push the line to the shell. */
-	sc = sp->script;
 	if ((size_t)(nw = write(sc->sh_master, p, len)) != len)
 		goto err2;
 	rval = 0;
@@ -357,13 +315,14 @@ err2:		if (nw == 0)
 	}
 
 	if (matchprompt) {
-		ADD_SPACE_RETW(sp, bp, blen, last_len + len);
-		MEMMOVEW(bp + last_len, p, len);
-		if (db_set(sp, last_lno, bp, last_len + len))
+		ADD_SPACE_GOTO(sp, char, bp, blen, last_len + len);
+		memmove(bp + last_len, p, len);
+		CHAR2INT(sp, bp, last_len + len, ip, ilen);
+		if (db_set(sp, last_lno, ip, ilen))
 err1:			rval = 1;
 	}
 	if (matchprompt)
-		FREE_SPACEW(sp, bp, blen);
+alloc_err:	FREE_SPACE(sp, bp, blen);
 	return (rval);
 }
 
@@ -465,34 +424,31 @@ static int
 sscr_insert(SCR *sp)
 {
 	struct timeval tv;
-	CHAR_T *endp, *p, *t;
+	char *endp, *p, *t;
 	SCRIPT *sc;
 	fd_set rdfd;
 	db_recno_t lno;
-	size_t blen, len = 0, tlen;
-	e_key_t value;
-	int nr, rval;
-	CHAR_T *bp;
+	size_t len;
+	ssize_t nr;
+	char bp[1024];
+	const CHAR_T *ip;
+	size_t ilen = 0;
 
 	/* Find out where the end of the file is. */
 	if (db_last(sp, &lno))
 		return (1);
 
-#define	MINREAD	1024
-	GET_SPACE_RETW(sp, bp, blen, MINREAD);
 	endp = bp;
 
 	/* Read the characters. */
-	rval = 1;
 	sc = sp->script;
-more:	switch (nr = read(sc->sh_master, endp, MINREAD)) {
+more:	switch (nr = read(sc->sh_master, endp, bp + sizeof(bp) - endp)) {
 	case  0:			/* EOF; shell just exited. */
 		sscr_end(sp);
-		rval = 0;
-		goto ret;
+		return (0);
 	case -1:			/* Error or interrupt. */
 		msgq(sp, M_SYSERR, "shell");
-		goto ret;
+		return (1);
 	default:
 		endp += nr;
 		break;
@@ -500,48 +456,54 @@ more:	switch (nr = read(sc->sh_master, e
 
 	/* Append the lines into the file. */
 	for (p = t = bp; p < endp; ++p) {
-		value = KEY_VAL(sp, *p);
-		if (value == K_CR || value == K_NL) {
+		if (*p == '\r' || *p == '\n') {
 			len = p - t;
-			if (db_append(sp, 1, lno++, t, len))
-				goto ret;
+			if (CHAR2INT(sp, t, len, ip, ilen) ||
+			    db_append(sp, 1, lno++, ip, ilen))
+				return (1);
 			t = p + 1;
 		}
 	}
-	if (p > t) {
-		len = p - t;
-		/*
-		 * If the last thing from the shell isn't another prompt, wait
-		 * up to 1/10 of a second for more stuff to show up, so that
-		 * we don't break the output into two separate lines.  Don't
-		 * want to hang indefinitely because some program is hanging,
-		 * confused the shell, or whatever.
-		 */
-		if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
-			tv.tv_sec = 0;
-			tv.tv_usec = 100000;
-			FD_ZERO(&rdfd);
-			FD_SET(sc->sh_master, &rdfd);
-			if (select(sc->sh_master + 1,
-			    &rdfd, NULL, NULL, &tv) == 1) {
-				MEMMOVE(bp, t, len);
+	/*
+	 * If the last thing from the shell isn't another prompt, wait up to
+	 * 1/10 of a second for more stuff to show up, so that we don't break
+	 * the output into two separate lines.  Don't want to hang indefinitely
+	 * because some program is hanging, confused the shell, or whatever.
+	 * Note that sc->sh_prompt can be NULL here.
+	 */
+	len = p - t;
+	if (sc->sh_prompt == NULL || len != sc->sh_prompt_len ||
+	    strnstr(p, sc->sh_prompt, len) == NULL) {
+		tv.tv_sec = 0;
+		tv.tv_usec = 100000;
+		FD_ZERO(&rdfd);
+		FD_SET(sc->sh_master, &rdfd);
+		if (select(sc->sh_master + 1, &rdfd, NULL, NULL, &tv) == 1) {
+			if (len == sizeof(bp)) {
+				if (CHAR2INT(sp, t, len, ip, ilen) ||
+				    db_append(sp, 1, lno++, ip, ilen))
+					return (1);
+				endp = bp;
+			} else {
+				memmove(bp, t, len);
 				endp = bp + len;
-				goto more;
 			}
+			goto more;
 		}
 		if (sscr_setprompt(sp, t, len))
 			return (1);
-		if (db_append(sp, 1, lno++, t, len))
-			goto ret;
 	}
 
-	/* The cursor moves to EOF. */
+	/* Append the remains into the file, and the cursor moves to EOF. */
+	if (len > 0) {
+		if (CHAR2INT(sp, t, len, ip, ilen) ||
+		    db_append(sp, 1, lno++, ip, ilen))
+			return (1);
+		sp->cno = ilen - 1;
+	} else
+		sp->cno = 0;
 	sp->lno = lno;
-	sp->cno = len ? len - 1 : 0;
-	rval = vs_refresh(sp, 1);
-
-ret:	FREE_SPACEW(sp, bp, blen);
-	return (rval);
+	return (vs_refresh(sp, 1));
 }
 
 /*
@@ -551,11 +513,9 @@ ret:	FREE_SPACEW(sp, bp, blen);
  *
  */
 static int
-sscr_setprompt(SCR *sp, CHAR_T *buf, size_t len)
+sscr_setprompt(SCR *sp, char *buf, size_t len)
 {
 	SCRIPT *sc;
-	const char *np;
-	size_t nlen;
 
 	sc = sp->script;
 	if (sc->sh_prompt)
@@ -565,51 +525,13 @@ sscr_setprompt(SCR *sp, CHAR_T *buf, siz
 		sscr_end(sp);
 		return (1);
 	}
-	INT2CHAR(sp, buf, len, np, nlen);
-	memmove(sc->sh_prompt, np, nlen);
+	memmove(sc->sh_prompt, buf, len);
 	sc->sh_prompt_len = len;
 	sc->sh_prompt[len] = '\0';
 	return (0);
 }
 
 /*
- * sscr_matchprompt --
- *	Check to see if a line matches the prompt.  Nul's indicate
- *	parts that can change, in both content and size.
- */
-static int
-sscr_matchprompt(SCR *sp, CHAR_T *lp, size_t line_len, size_t *lenp)
-{
-	SCRIPT *sc;
-	size_t prompt_len;
-	char *pp;
-
-	sc = sp->script;
-	if (line_len < (prompt_len = sc->sh_prompt_len))
-		return (0);
-
-	for (pp = sc->sh_prompt;
-	    prompt_len && line_len; --prompt_len, --line_len) {
-		if (*pp == '\0') {
-			for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
-			if (!prompt_len)
-				return (0);
-			for (; line_len && *lp != *pp; --line_len, ++lp);
-			if (!line_len)
-				return (0);
-		}
-		if (*pp++ != *lp++)
-			break;
-	}
-
-	if (prompt_len)
-		return (0);
-	if (lenp != NULL)
-		*lenp = line_len;
-	return (1);
-}
-
-/*
  * sscr_end --
  *	End the pipe to a shell.
  *
@@ -664,6 +586,7 @@ sscr_check(SCR *sp)
 	F_CLR(gp, G_SCRWIN);
 }
 
+#ifndef __NetBSD__
 #ifdef HAVE_SYS5_PTY
 static int ptys_open __P((int, char *));
 static int ptym_open __P((char *));
@@ -829,4 +752,6 @@ sscr_pty(amaster, aslave, name, termp, w
 	errno = ENOENT;	/* out of ptys */
 	return (-1);
 }
+
 #endif /* HAVE_SYS5_PTY */
+#endif /* !__NetBSD__ */

Index: src/external/bsd/nvi/usr.bin/nvi/Makefile
diff -u src/external/bsd/nvi/usr.bin/nvi/Makefile:1.5 src/external/bsd/nvi/usr.bin/nvi/Makefile:1.5.2.1
--- src/external/bsd/nvi/usr.bin/nvi/Makefile:1.5	Fri May 23 19:59:17 2014
+++ src/external/bsd/nvi/usr.bin/nvi/Makefile	Sun Dec 18 06:37:30 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.5 2014/05/23 19:59:17 dholland Exp $
+#	$NetBSD: Makefile,v 1.5.2.1 2016/12/18 06:37:30 snj Exp $
 
 .include <bsd.own.mk>
 
@@ -17,8 +17,8 @@ CPPFLAGS+=-I${DIST}/include -I${.CURDIR}
 #COPTS+=-fno-strict-aliasing
 #.endif
 
-LDADD+=	-lcurses -lterminfo
-DPADD+=	${LIBCURSES} ${LIBTERMINFO}
+LDADD+=	-lcurses -lterminfo -lutil
+DPADD+=	${LIBCURSES} ${LIBTERMINFO} ${LIBUTIL}
 PROG=	vi
 SRCS=	api.c cl_bsd.c cl_funcs.c cl_main.c cl_read.c cl_screen.c cl_term.c \
 	conv.c cut.c delete.c ex.c ex_abbrev.c ex_append.c \

Reply via email to