git: 4365415046a3 - stable/12 - Update nvi to 2.2.0-05ed8b9

Baptiste Daroussin bapt at FreeBSD.org
Wed Sep 22 10:31:09 UTC 2021


The branch stable/12 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=4365415046a352c2b93553502ed3a3758da93197

commit 4365415046a352c2b93553502ed3a3758da93197
Author:     Baptiste Daroussin <bapt at FreeBSD.org>
AuthorDate: 2020-10-01 04:46:23 +0000
Commit:     Baptiste Daroussin <bapt at FreeBSD.org>
CommitDate: 2021-09-22 10:28:14 +0000

    Update nvi to 2.2.0-05ed8b9
    
    This version incorporates many fixes in particular a fix for vi -w
    Another approach was proposed to merge those fixes (see review), I find
    it easier to track changes if we keep importing snapshot on regular
    basis
    
    PR:             241985
    Reported by:    fernape
    Differential Revision:  https://reviews.freebsd.org/D26158
    
    (cherry picked from commit 755cc40c21ca63388c6a67ba848a908b429e9391)
---
 contrib/nvi/CMakeLists.txt       | 24 ++++++++++++++--
 contrib/nvi/catalog/dump.c       | 12 ++++----
 contrib/nvi/cl/cl.h              |  4 ++-
 contrib/nvi/cl/cl_read.c         |  1 +
 contrib/nvi/cl/cl_term.c         |  6 ++--
 contrib/nvi/common/common.h      | 10 ++++++-
 contrib/nvi/common/cut.h         |  4 +--
 contrib/nvi/common/exf.c         | 17 +++++++-----
 contrib/nvi/common/key.c         |  4 +--
 contrib/nvi/common/log.c         |  4 +--
 contrib/nvi/common/main.c        | 11 ++++----
 contrib/nvi/common/mark.c        |  3 +-
 contrib/nvi/common/mem.h         | 60 ++++++++++++++++++++--------------------
 contrib/nvi/common/msg.c         |  4 +++
 contrib/nvi/common/options.c     | 20 ++++++++++----
 contrib/nvi/common/put.c         |  3 +-
 contrib/nvi/common/recover.c     |  9 +++---
 contrib/nvi/common/util.c        | 10 +++++--
 contrib/nvi/ex/ex.c              |  9 ++++--
 contrib/nvi/ex/ex.h              |  8 +++---
 contrib/nvi/ex/ex_argv.c         |  8 ++++++
 contrib/nvi/ex/ex_bang.c         |  3 +-
 contrib/nvi/ex/ex_cscope.c       | 10 ++++---
 contrib/nvi/ex/ex_filter.c       |  3 +-
 contrib/nvi/ex/ex_global.c       |  3 +-
 contrib/nvi/ex/ex_script.c       |  2 ++
 contrib/nvi/ex/ex_shell.c        |  3 +-
 contrib/nvi/ex/ex_subst.c        | 27 +++++++++---------
 contrib/nvi/files/config.h.in    |  9 ++++++
 contrib/nvi/files/pathnames.h.in |  5 ++--
 contrib/nvi/regex/engine.c       |  2 +-
 contrib/nvi/regex/regexec.c      |  6 ++--
 contrib/nvi/vi/v_itxt.c          |  4 +--
 contrib/nvi/vi/v_paragraph.c     | 10 ++++---
 contrib/nvi/vi/v_section.c       |  3 +-
 contrib/nvi/vi/v_sentence.c      |  3 +-
 contrib/nvi/vi/v_txt.c           | 11 ++++----
 contrib/nvi/vi/vi.c              |  4 +--
 contrib/nvi/vi/vs_line.c         | 10 ++++---
 contrib/nvi/vi/vs_msg.c          | 13 +++++----
 contrib/nvi/vi/vs_refresh.c      |  9 ++++--
 contrib/nvi/vi/vs_relative.c     |  7 +++--
 contrib/nvi/vi/vs_smap.c         |  4 +--
 usr.bin/vi/catalog/Makefile      |  3 +-
 usr.bin/vi/pathnames.h           |  5 ++--
 45 files changed, 243 insertions(+), 147 deletions(-)

diff --git a/contrib/nvi/CMakeLists.txt b/contrib/nvi/CMakeLists.txt
index 00f43283f022..996e0e72de99 100644
--- a/contrib/nvi/CMakeLists.txt
+++ b/contrib/nvi/CMakeLists.txt
@@ -13,6 +13,7 @@ project(nvi2 C)
 
 include(CheckIncludeFiles)
 include(CheckFunctionExists)
+include(CheckStructHasMember)
 include(CheckCSourceCompiles)
 
 mark_as_advanced(CMAKE_INSTALL_PREFIX)
@@ -127,6 +128,7 @@ endif()
 
 if(USE_WIDECHAR)
     find_library(CURSES_LIBRARY NAMES ncursesw cursesw curses HINTS /usr/lib)
+    find_library(TERMINFO_LIBRARY NAMES tinfow terminfo HINTS /usr/lib)
 
     # link to the wchar_t awared BSD libregex.a
     add_library(regex STATIC)
@@ -136,13 +138,14 @@ if(USE_WIDECHAR)
     target_link_libraries(nvi PRIVATE regex)
 else()
     find_library(CURSES_LIBRARY NAMES ncurses curses HINTS /usr/lib)
+    find_library(TERMINFO_LIBRARY NAMES tinfo terminfo HINTS /usr/lib)
     target_compile_options(nvi PRIVATE -Wno-pointer-sign)
 endif()
 
-target_link_libraries(nvi PRIVATE ${CURSES_LIBRARY})
+target_link_libraries(nvi PRIVATE ${CURSES_LIBRARY} ${TERMINFO_LIBRARY})
 
 if(USE_ICONV)
-    check_function_exists(__iconv ICONV_IN_LIBC)
+    check_function_exists(iconv ICONV_IN_LIBC)
     if(NOT ICONV_IN_LIBC)
         find_path(ICONV_INCLUDE_DIR iconv.h)
         find_library(ICONV_LIBRARY iconv)
@@ -173,9 +176,26 @@ if(USE_ICONV)
     target_link_libraries(nvi PRIVATE ${ICONV_LIBRARY})
 endif()
 
+check_function_exists(getprogname GETPROGNAME_IN_LIBC)
+check_function_exists(strlcpy STRLCPY_IN_LIBC)
+if(NOT GETPROGNAME_IN_LIBC OR NOT STRLCPY_IN_LIBC)
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(LIBBSD libbsd-overlay)
+    add_definitions(${LIBBSD_CFLAGS})
+    target_link_libraries(nvi PRIVATE ${LIBBSD_LIBRARIES})
+endif()
+
+check_function_exists(dbopen DBOPEN_IN_LIBC)
+if(NOT DBOPEN_IN_LIBC)
+    target_link_libraries(nvi PRIVATE db1)
+endif()
+
 check_include_files(libutil.h HAVE_LIBUTIL_H)
 check_include_files(ncurses.h HAVE_NCURSES_H)
+check_include_files(ncursesw/ncurses.h HAVE_NCURSESW_NCURSES_H)
+check_include_files(pty.h HAVE_PTY_H)
 check_include_files(term.h HAVE_TERM_H)
+check_struct_has_member("struct dirent" d_namlen dirent.h HAVE_DIRENT_D_NAMLEN LANGUAGE C)
 
 configure_file(files/config.h.in config.h)
 
diff --git a/contrib/nvi/catalog/dump.c b/contrib/nvi/catalog/dump.c
index 74ab53b2f12c..8390e5022841 100644
--- a/contrib/nvi/catalog/dump.c
+++ b/contrib/nvi/catalog/dump.c
@@ -36,24 +36,24 @@ parse(FILE *fp)
 {
 	int ch, s1, s2, s3;
 
-#define	TESTD(s) {							\
+#define	TESTD(s) do {							\
 	if ((s = getc(fp)) == EOF)					\
 		return;							\
 	if (!isdigit(s))						\
 		continue;						\
-}
-#define	TESTP {								\
+} while (0)
+#define	TESTP do {							\
 	if ((ch = getc(fp)) == EOF)					\
 		return;							\
 	if (ch != '|')							\
 		continue;						\
-}
-#define	MOVEC(t) {							\
+} while (0)
+#define	MOVEC(t) do {							\
 	do {								\
 		if ((ch = getc(fp)) == EOF)				\
 			return;						\
 	} while (ch != (t));						\
-}
+} while (0)
 	for (;;) {
 		MOVEC('"');
 		TESTD(s1);
diff --git a/contrib/nvi/cl/cl.h b/contrib/nvi/cl/cl.h
index 894e5ede7fa9..d230a55a56d7 100644
--- a/contrib/nvi/cl/cl.h
+++ b/contrib/nvi/cl/cl.h
@@ -10,7 +10,9 @@
 #ifdef USE_WIDECHAR
 #define _XOPEN_SOURCE_EXTENDED
 #endif
-#ifdef HAVE_NCURSES_H
+#ifdef HAVE_NCURSESW_NCURSES_H
+#include <ncursesw/ncurses.h>
+#elif defined HAVE_NCURSES_H
 #include <ncurses.h>
 #else
 #include <curses.h>
diff --git a/contrib/nvi/cl/cl_read.c b/contrib/nvi/cl/cl_read.c
index f5f18fd616f1..ddf3acc1918c 100644
--- a/contrib/nvi/cl/cl_read.c
+++ b/contrib/nvi/cl/cl_read.c
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <termios.h>
 #include <unistd.h>
 
diff --git a/contrib/nvi/cl/cl_term.c b/contrib/nvi/cl/cl_term.c
index 601460585530..106f37149565 100644
--- a/contrib/nvi/cl/cl_term.c
+++ b/contrib/nvi/cl/cl_term.c
@@ -424,16 +424,18 @@ cl_ssize(SCR *sp, int sigwinch, size_t *rowp, size_t *colp, int *changedp)
 	if (row == 0 || col == 0) {
 		if ((p = getenv("TERM")) == NULL)
 			goto noterm;
-		if (row == 0)
+		if (row == 0) {
 			if ((rval = tigetnum("lines")) < 0)
 				msgq(sp, M_SYSERR, "tigetnum: lines");
 			else
 				row = rval;
-		if (col == 0)
+		}
+		if (col == 0) {
 			if ((rval = tigetnum("cols")) < 0)
 				msgq(sp, M_SYSERR, "tigetnum: cols");
 			else
 				col = rval;
+		}
 	}
 
 	/* If nothing else, well, it's probably a VT100. */
diff --git a/contrib/nvi/common/common.h b/contrib/nvi/common/common.h
index 256fe6a0029a..dc4155610225 100644
--- a/contrib/nvi/common/common.h
+++ b/contrib/nvi/common/common.h
@@ -7,7 +7,15 @@
  * See the LICENSE file for redistribution information.
  */
 
-#include <db.h>			/* Only include db1. */
+#ifndef TCSASOFT
+#define TCSASOFT 0
+#endif
+
+#ifdef __linux__
+#include "/usr/include/db1/db.h"	/* Only include db1. */
+#else
+#include "/usr/include/db.h"	/* Only include db1. */
+#endif
 #include <regex.h>		/* May refer to the bundled regex. */
 
 /*
diff --git a/contrib/nvi/common/cut.h b/contrib/nvi/common/cut.h
index d220cd8cc69c..a94c3f9ce58d 100644
--- a/contrib/nvi/common/cut.h
+++ b/contrib/nvi/common/cut.h
@@ -63,13 +63,13 @@ struct _text {				/* Text: a linked list of lines. */
  * Get named buffer 'name'.
  * Translate upper-case buffer names to lower-case buffer names.
  */
-#define	CBNAME(sp, cbp, nch) {						\
+#define	CBNAME(sp, cbp, nch) do {					\
 	CHAR_T L__name;							\
 	L__name = isupper(nch) ? tolower(nch) : (nch);			\
 	SLIST_FOREACH(cbp, sp->gp->cutq, q)				\
 		if (cbp->name == L__name)				\
 			break;						\
-}
+} while (0)
 
 /* Flags to the cut() routine. */
 #define	CUT_LINEMODE	0x01		/* Cut in line mode. */
diff --git a/contrib/nvi/common/exf.c b/contrib/nvi/common/exf.c
index ccfa66ef4089..f9eb2150276d 100644
--- a/contrib/nvi/common/exf.c
+++ b/contrib/nvi/common/exf.c
@@ -199,7 +199,7 @@ file_init(SCR *sp, FREF *frp, char *rcv_name, int flags)
 		if (!LF_ISSET(FS_OPENERR))
 			F_SET(frp, FR_NEWFILE);
 
-		ep->mtim = sb.st_mtimespec;
+		ep->mtim = sb.st_mtim;
 	} else {
 		/*
 		 * XXX
@@ -218,7 +218,7 @@ file_init(SCR *sp, FREF *frp, char *rcv_name, int flags)
 		ep->mdev = sb.st_dev;
 		ep->minode = sb.st_ino;
 
-		ep->mtim = sb.st_mtimespec;
+		ep->mtim = sb.st_mtim;
 
 		if (!S_ISREG(sb.st_mode))
 			msgq_str(sp, M_ERR, oname,
@@ -796,7 +796,7 @@ file_write(SCR *sp, MARK *fm, MARK *tm, char *name, int flags)
 		if (noname && !LF_ISSET(FS_FORCE | FS_APPEND) &&
 		    ((F_ISSET(ep, F_DEVSET) &&
 		    (sb.st_dev != ep->mdev || sb.st_ino != ep->minode)) ||
-		    timespeccmp(&sb.st_mtimespec, &ep->mtim, !=))) {
+		    timespeccmp(&sb.st_mtim, &ep->mtim, !=))) {
 			msgq_str(sp, M_ERR, name, LF_ISSET(FS_POSSIBLE) ?
 "250|%s: file modified more recently than this copy; use ! to override" :
 "251|%s: file modified more recently than this copy");
@@ -887,7 +887,7 @@ success_open:
 	 * we re-init the time.  That way the user can clean up the disk
 	 * and rewrite without having to force it.
 	 */
-	if (noname)
+	if (noname) {
 		if (stat(name, &sb))
 			timepoint_system(&ep->mtim);
 		else {
@@ -895,8 +895,9 @@ success_open:
 			ep->mdev = sb.st_dev;
 			ep->minode = sb.st_ino;
 
-			ep->mtim = sb.st_mtimespec;
+			ep->mtim = sb.st_mtim;
 		}
+	}
 
 	/*
 	 * If the write failed, complain loudly.  ex_writefp() has already
@@ -925,11 +926,12 @@ success_open:
 	 */
 	if (LF_ISSET(FS_ALL) && !LF_ISSET(FS_APPEND)) {
 		F_CLR(ep, F_MODIFIED);
-		if (F_ISSET(frp, FR_TMPFILE))
+		if (F_ISSET(frp, FR_TMPFILE)) {
 			if (noname)
 				F_SET(frp, FR_TMPEXIT);
 			else
 				F_CLR(frp, FR_TMPEXIT);
+		}
 	}
 
 	p = msg_print(sp, name, &nf);
@@ -1290,7 +1292,7 @@ file_m1(SCR *sp, int force, int flags)
 	 * unless force is also set.  Otherwise, we fail unless forced or
 	 * there's another open screen on this file.
 	 */
-	if (F_ISSET(ep, F_MODIFIED))
+	if (F_ISSET(ep, F_MODIFIED)) {
 		if (O_ISSET(sp, O_AUTOWRITE)) {
 			if (!force && file_aw(sp, flags))
 				return (1);
@@ -1300,6 +1302,7 @@ file_m1(SCR *sp, int force, int flags)
 "263|File modified since last complete write; write or use :edit! to override");
 			return (1);
 		}
+	}
 
 	return (file_m3(sp, force));
 }
diff --git a/contrib/nvi/common/key.c b/contrib/nvi/common/key.c
index 23da0498a757..e71396893fd4 100644
--- a/contrib/nvi/common/key.c
+++ b/contrib/nvi/common/key.c
@@ -424,12 +424,12 @@ v_event_append(SCR *sp, EVENT *argp)
 }
 
 /* Remove events from the queue. */
-#define	QREM(len) {							\
+#define	QREM(len) do {							\
 	if ((gp->i_cnt -= len) == 0)					\
 		gp->i_next = 0;						\
 	else								\
 		gp->i_next += len;					\
-}
+} while (0)
 
 /*
  * v_event_get --
diff --git a/contrib/nvi/common/log.c b/contrib/nvi/common/log.c
index 5dcd9a2945fc..96b246efad02 100644
--- a/contrib/nvi/common/log.c
+++ b/contrib/nvi/common/log.c
@@ -69,10 +69,10 @@ static int	apply_with(int (*)(SCR *, recno_t, CHAR_T *, size_t),
 					SCR *, recno_t, u_char *, size_t);
 
 /* Try and restart the log on failure, i.e. if we run out of memory. */
-#define	LOG_ERR {							\
+#define	LOG_ERR do {							\
 	log_err(sp, __FILE__, __LINE__);				\
 	return (1);							\
-}
+} while (0)
 
 /* offset of CHAR_T string in log needs to be aligned on some systems
  * because it is passed to db_set as a string
diff --git a/contrib/nvi/common/main.c b/contrib/nvi/common/main.c
index c0b81960bd55..a7e60f1af806 100644
--- a/contrib/nvi/common/main.c
+++ b/contrib/nvi/common/main.c
@@ -48,8 +48,8 @@ editor(GS *gp, int argc, char *argv[])
 	size_t len;
 	u_int flags;
 	int ch, flagchk, lflag, secure, startup, readonly, rval, silent;
-	char *tag_f, *wsizearg, path[256];
-	CHAR_T *w;
+	char *tag_f, *wsizearg;
+	CHAR_T *w, path[256];
 	size_t wlen;
 
 	/* Initialize the busy routine, if not defined by the screen. */
@@ -242,9 +242,9 @@ editor(GS *gp, int argc, char *argv[])
 	}
 	if (wsizearg != NULL) {
 		ARGS *av[2], a, b;
-		(void)snprintf(path, sizeof(path), "window=%s", wsizearg);
+		(void)SPRINTF(path, SIZE(path), L("window=%s"), wsizearg);
 		a.bp = (CHAR_T *)path;
-		a.len = strlen(path);
+		a.len = SIZE(path);
 		b.bp = NULL;
 		b.len = 0;
 		av[0] = &a;
@@ -533,7 +533,7 @@ v_obsolete(char *argv[])
 				argv[0][1] = 'c';
 				(void)strlcpy(argv[0] + 2, p + 1, len);
 			}
-		} else if (argv[0][0] == '-')
+		} else if (argv[0][0] == '-') {
 			if (argv[0][1] == '\0') {
 				argv[0] = strdup("-s");
 				if (argv[0] == NULL) {
@@ -545,6 +545,7 @@ nomem:					warn(NULL);
 				    argv[0][1] == 't' || argv[0][1] == 'w') &&
 				    argv[0][2] == '\0')
 					++argv;
+		}
 	return (0);
 }
 
diff --git a/contrib/nvi/common/mark.c b/contrib/nvi/common/mark.c
index 9af4612cb22a..86e3cb8ea6d9 100644
--- a/contrib/nvi/common/mark.c
+++ b/contrib/nvi/common/mark.c
@@ -216,12 +216,13 @@ mark_insdel(SCR *sp, lnop_t op, recno_t lno)
 		abort();
 	case LINE_DELETE:
 		SLIST_FOREACH(lmp, sp->ep->marks, q)
-			if (lmp->lno >= lno)
+			if (lmp->lno >= lno) {
 				if (lmp->lno == lno) {
 					F_SET(lmp, MARK_DELETED);
 					(void)log_mark(sp, lmp);
 				} else
 					--lmp->lno;
+			}
 		break;
 	case LINE_INSERT:
 		/*
diff --git a/contrib/nvi/common/mem.h b/contrib/nvi/common/mem.h
index 1e26a6ea6549..d24ec0b50b09 100644
--- a/contrib/nvi/common/mem.h
+++ b/contrib/nvi/common/mem.h
@@ -17,7 +17,7 @@
 /* Increase the size of a malloc'd buffer.  Two versions, one that
  * returns, one that jumps to an error label.
  */
-#define	BINC_GOTO(sp, type, lp, llen, nlen) {				\
+#define	BINC_GOTO(sp, type, lp, llen, nlen) do {			\
 	CHECK_TYPE(type *, lp)						\
 	void *L__bincp;							\
 	if ((nlen) > llen) {						\
@@ -29,12 +29,12 @@
 		 */							\
 		lp = L__bincp;						\
 	}								\
-}
+} while (0)
 #define	BINC_GOTOC(sp, lp, llen, nlen)					\
 	BINC_GOTO(sp, char, lp, llen, nlen)
 #define	BINC_GOTOW(sp, lp, llen, nlen)					\
 	BINC_GOTO(sp, CHAR_T, lp, llen, (nlen) * sizeof(CHAR_T))
-#define	BINC_RET(sp, type, lp, llen, nlen) {				\
+#define	BINC_RET(sp, type, lp, llen, nlen) do {				\
 	CHECK_TYPE(type *, lp)						\
 	void *L__bincp;							\
 	if ((nlen) > llen) {						\
@@ -46,7 +46,7 @@
 		 */							\
 		lp = L__bincp;						\
 	}								\
-}
+} while (0)
 #define	BINC_RETC(sp, lp, llen, nlen)					\
 	BINC_RET(sp, char, lp, llen, nlen)
 #define	BINC_RETW(sp, lp, llen, nlen)					\
@@ -57,7 +57,7 @@
  * from a malloc'd buffer otherwise.  Two versions, one that returns, one
  * that jumps to an error label.
  */
-#define	GET_SPACE_GOTO(sp, type, bp, blen, nlen) {			\
+#define	GET_SPACE_GOTO(sp, type, bp, blen, nlen) do {			\
 	CHECK_TYPE(type *, bp)						\
 	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
 	if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) {		\
@@ -70,12 +70,12 @@
 		blen = L__gp->tmp_blen;					\
 		F_SET(L__gp, G_TMP_INUSE);				\
 	}								\
-}
+} while (0)
 #define	GET_SPACE_GOTOC(sp, bp, blen, nlen)				\
 	GET_SPACE_GOTO(sp, char, bp, blen, nlen)
 #define	GET_SPACE_GOTOW(sp, bp, blen, nlen)				\
 	GET_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
-#define	GET_SPACE_RET(sp, type, bp, blen, nlen) {			\
+#define	GET_SPACE_RET(sp, type, bp, blen, nlen) do {			\
 	CHECK_TYPE(type *, bp)						\
 	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
 	if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) {		\
@@ -88,7 +88,7 @@
 		blen = L__gp->tmp_blen;					\
 		F_SET(L__gp, G_TMP_INUSE);				\
 	}								\
-}
+} while (0)
 #define	GET_SPACE_RETC(sp, bp, blen, nlen)				\
 	GET_SPACE_RET(sp, char, bp, blen, nlen)
 #define	GET_SPACE_RETW(sp, bp, blen, nlen)				\
@@ -98,7 +98,7 @@
  * Add space to a GET_SPACE returned buffer.  Two versions, one that
  * returns, one that jumps to an error label.
  */
-#define	ADD_SPACE_GOTO(sp, type, bp, blen, nlen) {			\
+#define	ADD_SPACE_GOTO(sp, type, bp, blen, nlen) do {			\
 	CHECK_TYPE(type *, bp)						\
 	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
 	if (L__gp == NULL || bp == (type *)L__gp->tmp_bp) {		\
@@ -109,12 +109,12 @@
 		F_SET(L__gp, G_TMP_INUSE);				\
 	} else								\
 		BINC_GOTO(sp, type, bp, blen, nlen);			\
-}
+} while (0)
 #define	ADD_SPACE_GOTOC(sp, bp, blen, nlen)				\
 	ADD_SPACE_GOTO(sp, char, bp, blen, nlen)
 #define	ADD_SPACE_GOTOW(sp, bp, blen, nlen)				\
 	ADD_SPACE_GOTO(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
-#define	ADD_SPACE_RET(sp, type, bp, blen, nlen) {			\
+#define	ADD_SPACE_RET(sp, type, bp, blen, nlen) do {			\
 	CHECK_TYPE(type *, bp)						\
 	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
 	if (L__gp == NULL || bp == (type *)L__gp->tmp_bp) {		\
@@ -125,70 +125,70 @@
 		F_SET(L__gp, G_TMP_INUSE);				\
 	} else								\
 		BINC_RET(sp, type, bp, blen, nlen);			\
-}
+} while (0)
 #define	ADD_SPACE_RETC(sp, bp, blen, nlen)				\
 	ADD_SPACE_RET(sp, char, bp, blen, nlen)
 #define	ADD_SPACE_RETW(sp, bp, blen, nlen)				\
 	ADD_SPACE_RET(sp, CHAR_T, bp, blen, (nlen) * sizeof(CHAR_T))
 
 /* Free a GET_SPACE returned buffer. */
-#define	FREE_SPACE(sp, bp, blen) {					\
+#define	FREE_SPACE(sp, bp, blen) do {					\
 	GS *L__gp = (sp) == NULL ? NULL : (sp)->gp;			\
 	if (L__gp != NULL && bp == L__gp->tmp_bp)			\
 		F_CLR(L__gp, G_TMP_INUSE);				\
 	else								\
 		free(bp);						\
-}
-#define	FREE_SPACEW(sp, bp, blen) {					\
+} while (0)
+#define	FREE_SPACEW(sp, bp, blen) do {					\
 	CHECK_TYPE(CHAR_T *, bp)					\
 	FREE_SPACE(sp, (char *)bp, blen);				\
-}
+} while (0)
 
 /*
  * Malloc a buffer, casting the return pointer.  Various versions.
  */
-#define	CALLOC(sp, p, nmemb, size) {					\
+#define	CALLOC(sp, p, nmemb, size) do {					\
 	if ((p = calloc(nmemb, size)) == NULL)				\
 		msgq(sp, M_SYSERR, NULL);				\
-}
-#define	CALLOC_GOTO(sp, p, nmemb, size) {				\
+} while (0)
+#define	CALLOC_GOTO(sp, p, nmemb, size) do {				\
 	if ((p = calloc(nmemb, size)) == NULL)				\
 		goto alloc_err;						\
-}
-#define	CALLOC_RET(sp, p, nmemb, size) {				\
+} while (0)
+#define	CALLOC_RET(sp, p, nmemb, size) do {				\
 	if ((p = calloc(nmemb, size)) == NULL) {			\
 		msgq(sp, M_SYSERR, NULL);				\
 		return (1);						\
 	}								\
-}
+} while (0)
 
-#define	MALLOC(sp, p, size) {						\
+#define	MALLOC(sp, p, size) do {					\
 	if ((p = malloc(size)) == NULL)					\
 		msgq(sp, M_SYSERR, NULL);				\
-}
-#define	MALLOC_GOTO(sp, p, size) {					\
+} while (0)
+#define	MALLOC_GOTO(sp, p, size) do {					\
 	if ((p = malloc(size)) == NULL)					\
 		goto alloc_err;						\
-}
-#define	MALLOC_RET(sp, p, size) {					\
+} while (0)
+#define	MALLOC_RET(sp, p, size) do {					\
 	if ((p = malloc(size)) == NULL) {				\
 		msgq(sp, M_SYSERR, NULL);				\
 		return (1);						\
 	}								\
-}
+} while (0)
 
 /*
  * Resize a buffer, free any already held memory if we can't get more.
  * FreeBSD's reallocf(3) does the same thing, but it's not portable yet.
  */
-#define	REALLOC(sp, p, cast, size) {					\
+#define	REALLOC(sp, p, cast, size) do {					\
 	cast newp;							\
 	if ((newp = realloc(p, size)) == NULL) {			\
 		free(p);						\
 		msgq(sp, M_SYSERR, NULL);				\
 	}								\
 	p = newp;							\
-}
+} while (0)
 
 /* 
  * p2roundup --
diff --git a/contrib/nvi/common/msg.c b/contrib/nvi/common/msg.c
index d2463a3ecccd..7499f451be59 100644
--- a/contrib/nvi/common/msg.c
+++ b/contrib/nvi/common/msg.c
@@ -731,7 +731,11 @@ msg_open(SCR *sp, char *file)
 		 * corrupt catalog file.  Errno == 0 is not rare; add
 		 * EFTYPE, which is seen on FreeBSD, for a good measure.
 		 */
+#ifdef EFTYPE
 		if (errno == 0 || errno == EFTYPE)
+#else
+		if (errno == 0)
+#endif
 			msgq_str(sp, M_ERR, p,
 			    "030|The file %s is not a message catalog");
 		else
diff --git a/contrib/nvi/common/options.c b/contrib/nvi/common/options.c
index 25b10fe67012..d5c039f97228 100644
--- a/contrib/nvi/common/options.c
+++ b/contrib/nvi/common/options.c
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unctrl.h>
 #include <unistd.h>
 
 #include "common.h"
@@ -312,7 +313,7 @@ opts_init(SCR *sp, int *oargs)
 	argv[1] = &b;
 
 	/* Set numeric and string default values. */
-#define	OI(indx, str) {							\
+#define	OI(indx, str) do {						\
 	a.len = STRLEN(str);						\
 	if ((CHAR_T*)str != b2)	  /* GCC puts strings in text-space. */	\
 		(void)MEMCPY(b2, str, a.len+1);				\
@@ -320,7 +321,7 @@ opts_init(SCR *sp, int *oargs)
 		 optindx = indx;					\
 		goto err;						\
 	}								\
-}
+} while (0)
 	/*
 	 * Indirect global options to global space.  Specifically, set up
 	 * terminal, lines, columns first, they're used by other options.
@@ -366,7 +367,7 @@ opts_init(SCR *sp, int *oargs)
 	OI(O_PARAGRAPHS, L("paragraphs=IPLPPPQPP LIpplpipbp"));
 	(void)SPRINTF(b2, SIZE(b2), L("path=%s"), "");
 	OI(O_PATH, b2);
-	(void)SPRINTF(b2, SIZE(b2), L("recdir=%s"), _PATH_PRESERVE);
+	(void)SPRINTF(b2, SIZE(b2), L("recdir=%s"), NVI_PATH_PRESERVE);
 	OI(O_RECDIR, b2);
 	OI(O_SECTIONS, L("sections=NHSHH HUnhsh"));
 	(void)SPRINTF(b2, SIZE(b2),
@@ -573,13 +574,14 @@ opts_set(SCR *sp, ARGS *argv[], char *usage)
 			 * functions can be expensive.
 			 */
 			isset = !turnoff;
-			if (!F_ISSET(op, OPT_ALWAYS))
+			if (!F_ISSET(op, OPT_ALWAYS)) {
 				if (isset) {
 					if (O_ISSET(sp, offset))
 						break;
 				} else
 					if (!O_ISSET(sp, offset))
 						break;
+			}
 
 			/* Report to subsystems. */
 			if ((op->func != NULL &&
@@ -945,6 +947,7 @@ static int
 opts_print(SCR *sp, OPTLIST const *op)
 {
 	int curlen, offset;
+	const char *p;
 
 	curlen = 0;
 	offset = op - optlist;
@@ -958,8 +961,13 @@ opts_print(SCR *sp, OPTLIST const *op)
 		curlen += ex_printf(sp, WS"=%ld", op->name, O_VAL(sp, offset));
 		break;
 	case OPT_STR:
-		curlen += ex_printf(sp, WS"=\"%s\"", op->name,
-		    O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
+		curlen += ex_printf(sp, WS"=\"", op->name);
+		p = O_STR(sp, offset);
+		/* Keep correct count for unprintable character sequences */
+		if (p != NULL)
+			for (; *p != '\0'; ++p)
+				curlen += ex_puts(sp, unctrl(*p));
+		curlen += ex_puts(sp, "\"");
 		break;
 	}
 	return (curlen);
diff --git a/contrib/nvi/common/put.c b/contrib/nvi/common/put.c
index 26a67ac8711c..f39948808e7d 100644
--- a/contrib/nvi/common/put.c
+++ b/contrib/nvi/common/put.c
@@ -39,7 +39,7 @@ put(SCR *sp, CB *cbp, CHAR_T *namep, MARK *cp, MARK *rp, int append)
 	CHAR_T *bp, *t;
 	CHAR_T *p;
 
-	if (cbp == NULL)
+	if (cbp == NULL) {
 		if (namep == NULL) {
 			cbp = sp->gp->dcbp;
 			if (cbp == NULL) {
@@ -56,6 +56,7 @@ put(SCR *sp, CB *cbp, CHAR_T *namep, MARK *cp, MARK *rp, int append)
 				return (1);
 			}
 		}
+	}
 	tp = TAILQ_FIRST(cbp->textq);
 
 	/*
diff --git a/contrib/nvi/common/recover.c b/contrib/nvi/common/recover.c
index 89f25a4146e9..120cf4f60b19 100644
--- a/contrib/nvi/common/recover.c
+++ b/contrib/nvi/common/recover.c
@@ -421,7 +421,7 @@ rcv_mailfile(SCR *sp, int issync, char *cp_path)
 	    getprogname(), " -r ", qt);
 	free(qt);
 	free(host);
-	if (buf == NULL) {
+	if (len == -1) {
 		msgq(sp, M_SYSERR, NULL);
 		goto err;
 	}
@@ -701,7 +701,7 @@ rcv_read(SCR *sp, FREF *frp)
 		/* If we've found more than one, take the most recent. */
 		(void)fstat(fileno(fp), &sb);
 		if (recp == NULL ||
-		    timespeccmp(&rec_mtim, &sb.st_mtimespec, <)) {
+		    timespeccmp(&rec_mtim, &sb.st_mtim, <)) {
 			p = recp;
 			t = pathp;
 			recp = recpath;
@@ -710,7 +710,7 @@ rcv_read(SCR *sp, FREF *frp)
 				free(p);
 				free(t);
 			}
-			rec_mtim = sb.st_mtimespec;
+			rec_mtim = sb.st_mtim;
 			if (sv_fd != -1)
 				(void)close(sv_fd);
 			sv_fd = dup(fileno(fp));
@@ -813,8 +813,7 @@ rcv_email(SCR *sp, char *fname)
 {
 	char *buf;
 
-	(void)asprintf(&buf, _PATH_SENDMAIL " -odb -t < %s", fname);
-	if (buf == NULL) {
+	if (asprintf(&buf, _PATH_SENDMAIL " -odb -t < %s", fname) == -1) {
 		msgq_str(sp, M_ERR, strerror(errno),
 		    "071|not sending email: %s");
 		return;
diff --git a/contrib/nvi/common/util.c b/contrib/nvi/common/util.c
index 1e87c6293550..8046ce373016 100644
--- a/contrib/nvi/common/util.c
+++ b/contrib/nvi/common/util.c
@@ -111,8 +111,9 @@ join(char *path1, char *path2)
 
 	if (path1[0] == '\0' || path2[0] == '/')
 		return strdup(path2);
-	(void)asprintf(&p, path1[strlen(path1)-1] == '/' ?
-	    "%s%s" : "%s/%s", path1, path2);
+	if (asprintf(&p, path1[strlen(path1)-1] == '/' ?
+	    "%s%s" : "%s/%s", path1, path2) == -1)
+		return NULL;
 	return p;
 }
 
@@ -140,8 +141,13 @@ expanduser(char *str)
 		continue;
 	if (t == p) {
 		/* ~ */
+#ifdef __GLIBC__
+		extern char *secure_getenv(const char *);
+		if ((h = secure_getenv("HOME")) == NULL) {
+#else
 		if (issetugid() != 0 ||
 		    (h = getenv("HOME")) == NULL) {
+#endif
 			if (((h = getlogin()) != NULL &&
 			     (pwd = getpwnam(h)) != NULL) ||
 			    (pwd = getpwuid(getuid())) != NULL)
diff --git a/contrib/nvi/ex/ex.c b/contrib/nvi/ex/ex.c
index c93d49717b22..343131537afa 100644
--- a/contrib/nvi/ex/ex.c
+++ b/contrib/nvi/ex/ex.c
@@ -811,13 +811,14 @@ skip_srch:	if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
 	 * this isn't a particularly complex trap, and if backslashes were
 	 * legal in set commands, this would have to be much more complicated.
 	 */
-	if (ecp->cmd == &cmds[C_SET])
+	if (ecp->cmd == &cmds[C_SET]) {
 		for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
 			if (IS_ESCAPE(sp, ecp, *p) && len > 1) {
 				--len;
 				++p;
 			} else if (*p == '\\')
 				*p = CH_LITERAL;
+	}
 
 	/*
 	 * Set the default addresses.  It's an error to specify an address for
@@ -1254,7 +1255,7 @@ addr_verify:
 				ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
 				goto err;
 			}
-		} else if (!db_exist(sp, ecp->addr2.lno))
+		} else if (!db_exist(sp, ecp->addr2.lno)) {
 			if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
 				if (db_last(sp, &lno))
 					goto err;
@@ -1263,6 +1264,7 @@ addr_verify:
 				ex_badaddr(sp, NULL, A_EOF, NUM_OK);
 				goto err;
 			}
+		}
 		/* FALLTHROUGH */
 	case 1:
 		if (ecp->addr1.lno == 0) {
@@ -2117,7 +2119,7 @@ ex_load(SCR *sp)
 
 			/* If it's a global/v command, fix up the last line. */
 			if (FL_ISSET(ecp->agv_flags,
-			    AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
+			    AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO) {
 				if (db_exist(sp, ecp->range_lno))
 					sp->lno = ecp->range_lno;
 				else {
@@ -2126,6 +2128,7 @@ ex_load(SCR *sp)
 					if (sp->lno == 0)
 						sp->lno = 1;
 				}
+			}
 			free(ecp->o_cp);
 		}
 
diff --git a/contrib/nvi/ex/ex.h b/contrib/nvi/ex/ex.h
index 5ccf7c6c362b..b2d8ad0f4fef 100644
--- a/contrib/nvi/ex/ex.h
+++ b/contrib/nvi/ex/ex.h
@@ -55,12 +55,12 @@ extern EXCMDLIST const cmds[];		/* Table of ex commands. */
  * at any time, and most of them won't work well if a file hasn't yet been read
  * in.  Historic vi generally took the easy way out and dropped core.
  */
-#define	NEEDFILE(sp, cmdp) {						\
+#define	NEEDFILE(sp, cmdp) do {						\
 	if ((sp)->ep == NULL) {						\
 		ex_wemsg(sp, (cmdp)->cmd->name, EXM_NOFILEYET);		\
 		return (1);						\
 	}								\
-}
+} while (0)
 
 /* Range structures for global and @ commands. */
 typedef struct _range RANGE;
@@ -102,12 +102,12 @@ struct _excmd {
 	u_int8_t  agv_flags;
 
 	/* Clear the structure before each ex command. */
-#define	CLEAR_EX_CMD(cmdp) {						\
+#define	CLEAR_EX_CMD(cmdp) do {						\
 	u_int32_t L__f = F_ISSET(cmdp, E_PRESERVE);			\
 	memset(&((cmdp)->buffer), 0, ((char *)&(cmdp)->flags -		\
 	    (char *)&((cmdp)->buffer)) + sizeof((cmdp)->flags));	\
 	F_SET(cmdp, L__f);						\
-}
+} while (0)
 
 	CHAR_T	  buffer;		/* Command: named buffer. */
 	recno_t	  lineno;		/* Command: line number. */
diff --git a/contrib/nvi/ex/ex_argv.c b/contrib/nvi/ex/ex_argv.c
index d6f9a8bb8aa5..8b1fd7858fb1 100644
--- a/contrib/nvi/ex/ex_argv.c
+++ b/contrib/nvi/ex/ex_argv.c
@@ -611,9 +611,17 @@ argv_flt_path(SCR *sp, EXCMD *excp, CHAR_T *path, size_t plen)
 		if (nlen == 0) {
 			if (dp->d_name[0] == '.')
 				continue;
+#ifdef HAVE_DIRENT_D_NAMLEN
 			len = dp->d_namlen;
+#else
+			len = strlen(dp->d_name);
+#endif
 		} else {
+#ifdef HAVE_DIRENT_D_NAMLEN
 			len = dp->d_namlen;
+#else
+			len = strlen(dp->d_name);
+#endif
 			if (len < nlen || memcmp(dp->d_name, name, nlen))
 				continue;
 		}
diff --git a/contrib/nvi/ex/ex_bang.c b/contrib/nvi/ex/ex_bang.c
index ff2b39bbff9a..c5744708664a 100644
--- a/contrib/nvi/ex/ex_bang.c
+++ b/contrib/nvi/ex/ex_bang.c
@@ -96,7 +96,7 @@ ex_bang(SCR *sp, EXCMD *cmdp)
 	 */
 	if (cmdp->addrcnt == 0) {
 		msg = NULL;
-		if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED))
+		if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED)) {
 			if (O_ISSET(sp, O_AUTOWRITE)) {
 				if (file_aw(sp, FS_ALL))
 					return (0);
@@ -105,6 +105,7 @@ ex_bang(SCR *sp, EXCMD *cmdp)
 				msg = msg_cat(sp,
 				    "303|File modified since last write.",
 				    NULL);
+		}
 
 		/* If we're still in a vi screen, move out explicitly. */
 		INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
diff --git a/contrib/nvi/ex/ex_cscope.c b/contrib/nvi/ex/ex_cscope.c
index 629861d55680..74d7f8af95be 100644
--- a/contrib/nvi/ex/ex_cscope.c
+++ b/contrib/nvi/ex/ex_cscope.c
@@ -261,7 +261,7 @@ cscope_add(SCR *sp, EXCMD *cmdp, CHAR_T *dname)
 	csc->dname = csc->buf;
 	csc->dlen = len;
 	memcpy(csc->dname, np, len);
-	csc->mtim = sb.st_mtimespec;
+	csc->mtim = sb.st_mtim;
 
 	/* Get the search paths for the cscope. */
 	if (get_paths(sp, csc))
@@ -411,7 +411,8 @@ err:		if (to_cs[0] != -1)
 			free(dn);
 			goto nomem;
 		}
-		(void)asprintf(&cmd, CSCOPE_CMD_FMT, dn, dbn);
+		if (asprintf(&cmd, CSCOPE_CMD_FMT, dn, dbn) == -1)
+			cmd = NULL;
 		free(dbn);
 		free(dn);
 		if (cmd == NULL) {
@@ -812,7 +813,7 @@ csc_file(SCR *sp, CSC *csc, char *name, char **dirp, size_t *dlenp, int *isolder
 			*dirp = *pp;
 			*dlenp = strlen(*pp);
 			*isolderp = timespeccmp(
-			    &sb.st_mtimespec, &csc->mtim, <);
+			    &sb.st_mtim, &csc->mtim, <);
 			return;
 		}
 		free(buf);
@@ -843,7 +844,7 @@ csc_help(SCR *sp, char *cmd)
 {
 	CC const *ccp;
 
-	if (cmd != NULL && *cmd != '\0')
+	if (cmd != NULL && *cmd != '\0') {
 		if ((ccp = lookup_ccmd(cmd)) == NULL) {
 			ex_printf(sp,
 			    "%s doesn't match any cscope command\n", cmd);
@@ -854,6 +855,7 @@ csc_help(SCR *sp, char *cmd)
*** 675 LINES SKIPPED ***


More information about the dev-commits-src-branches mailing list