The more I dig into vi, the more portability goo I find: The TRUE and
FALSE constants are defined by <curses.h>, thus no need to check for
them. The mvchgat macro, VWERASE and tons of other macros are present
in OpenBSDs curses implementation, this means a lot of the #ifdef maze
can be removed. SIGWINCH is present in libc, so is MAP_FILE and
NL_ARGMAX and d_namlen. The port.h header file does nothing and can be
removed.
See diff below; binary changes due to d_namlen changes and __LINE__
usage in other areas, which are not touched by this diff.
cheers,
natano
Index: cl/cl.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl.h,v
retrieving revision 1.7
diff -u -r1.7 cl.h
--- cl/cl.h 8 Jan 2006 21:05:39 -0000 1.7
+++ cl/cl.h 11 Nov 2014 20:08:38 -0000
@@ -65,16 +65,4 @@
/* X11 xterm escape sequence to rename the icon/window. */
#define XTERM_RENAME "\033]0;%s\007"
-/*
- * XXX
- * Some implementations of curses.h don't define these for us. Used for
- * compatibility only.
- */
-#ifndef TRUE
-#define TRUE 1
-#endif
-#ifndef FALSE
-#define FALSE 0
-#endif
-
#include "cl_extern.h"
Index: cl/cl_funcs.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_funcs.c,v
retrieving revision 1.15
diff -u -r1.15 cl_funcs.c
--- cl/cl_funcs.c 6 Nov 2014 10:48:52 -0000 1.15
+++ cl/cl_funcs.c 11 Nov 2014 20:08:38 -0000
@@ -300,24 +300,7 @@
*/
if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) {
getyx(stdscr, oldy, oldx);
-#ifdef mvchgat
mvchgat(RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL);
-#else
- for (lno = RLNO(sp, LASTLINE(sp)), col = spcnt = 0;;) {
- (void)move(lno, col);
- ch = winch(stdscr);
- if (isblank(ch))
- ++spcnt;
- else {
- (void)move(lno, col - spcnt);
- for (; spcnt > 0; --spcnt)
- (void)addch(' ');
- (void)addch(ch);
- }
- if (++col >= sp->cols)
- break;
- }
-#endif
(void)move(oldy, oldx);
}
@@ -433,11 +416,9 @@
case KEY_VKILL:
*dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
break;
-#ifdef VWERASE
case KEY_VWERASE:
*dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
break;
-#endif
default:
*dnep = 1;
break;
Index: cl/cl_main.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_main.c,v
retrieving revision 1.21
diff -u -r1.21 cl_main.c
--- cl/cl_main.c 10 Nov 2014 21:34:13 -0000 1.21
+++ cl/cl_main.c 11 Nov 2014 20:08:38 -0000
@@ -297,12 +297,9 @@
sigaddset(&__sigblockset, SIGINT) ||
setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
sigaddset(&__sigblockset, SIGTERM) ||
- setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
-#ifdef SIGWINCH
- ||
+ setsig(SIGTERM, &clp->oact[INDX_TERM], h_term) ||
sigaddset(&__sigblockset, SIGWINCH) ||
setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
-#endif
) {
perr(gp->progname, NULL);
return (1);
@@ -310,11 +307,8 @@
} else
if (setsig(SIGHUP, NULL, h_hup) ||
setsig(SIGINT, NULL, h_int) ||
- setsig(SIGTERM, NULL, h_term)
-#ifdef SIGWINCH
- ||
+ setsig(SIGTERM, NULL, h_term) ||
setsig(SIGWINCH, NULL, h_winch)
-#endif
) {
msgq(sp, M_SYSERR, "signal-reset");
}
@@ -369,9 +363,7 @@
(void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
(void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
(void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
-#ifdef SIGWINCH
(void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
-#endif
}
/*
Index: cl/cl_screen.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_screen.c,v
retrieving revision 1.20
diff -u -r1.20 cl_screen.c
--- cl/cl_screen.c 28 Nov 2013 22:12:40 -0000 1.20
+++ cl/cl_screen.c 11 Nov 2014 20:08:38 -0000
@@ -337,9 +337,7 @@
clp->vi_enter.c_iflag |= IXOFF;
clp->vi_enter.c_lflag |= ISIG;
-#ifdef VDSUSP
clp->vi_enter.c_cc[VDSUSP] = _POSIX_VDISABLE;
-#endif
clp->vi_enter.c_cc[VQUIT] = _POSIX_VDISABLE;
clp->vi_enter.c_cc[VSUSP] = _POSIX_VDISABLE;
@@ -349,15 +347,9 @@
* characters when curses switches into raw mode. It should be OK
* to do it explicitly for everyone.
*/
-#ifdef VDISCARD
clp->vi_enter.c_cc[VDISCARD] = _POSIX_VDISABLE;
-#endif
-#ifdef VLNEXT
clp->vi_enter.c_cc[VLNEXT] = _POSIX_VDISABLE;
-#endif
-#ifdef VSTATUS
clp->vi_enter.c_cc[VSTATUS] = _POSIX_VDISABLE;
-#endif
/* Initialize terminal based information. */
if (cl_term_init(sp))
@@ -471,18 +463,10 @@
* to make all ex printf's output \r\n instead of \n.
*/
clp->ex_enter = clp->orig;
- clp->ex_enter.c_lflag |= ECHO | ECHOE | ECHOK | ICANON | IEXTEN | ISIG;
-#ifdef ECHOCTL
- clp->ex_enter.c_lflag |= ECHOCTL;
-#endif
-#ifdef ECHOKE
- clp->ex_enter.c_lflag |= ECHOKE;
-#endif
+ clp->ex_enter.c_lflag |=
+ ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ICANON | IEXTEN | ISIG;
clp->ex_enter.c_iflag |= ICRNL;
- clp->ex_enter.c_oflag |= OPOST;
-#ifdef ONLCR
- clp->ex_enter.c_oflag |= ONLCR;
-#endif
+ clp->ex_enter.c_oflag |= ONLCR | OPOST;
fast: if (tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->ex_enter)) {
if (errno == EINTR)
Index: cl/cl_term.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_term.c,v
retrieving revision 1.17
diff -u -r1.17 cl_term.c
--- cl/cl_term.c 6 Nov 2014 10:48:52 -0000 1.17
+++ cl/cl_term.c 11 Nov 2014 20:08:38 -0000
@@ -334,9 +334,7 @@
size_t *rowp, *colp;
int *changedp;
{
-#ifdef TIOCGWINSZ
struct winsize win;
-#endif
size_t col, row;
int rval;
char *p;
@@ -357,12 +355,10 @@
* Try TIOCGWINSZ.
*/
row = col = 0;
-#ifdef TIOCGWINSZ
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1) {
row = win.ws_row;
col = win.ws_col;
}
-#endif
/* If here because of suspend or a signal, only trust TIOCGWINSZ. */
if (sigwinch) {
/*
Index: common/exf.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/exf.c,v
retrieving revision 1.29
diff -u -r1.29 exf.c
--- common/exf.c 10 Nov 2014 21:31:42 -0000 1.29
+++ common/exf.c 11 Nov 2014 20:08:38 -0000
@@ -12,7 +12,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/queue.h>
#include <sys/stat.h>
@@ -1452,11 +1451,9 @@
* they are the former. There's no portable way to do this.
*/
errno = 0;
- return (flock(fd, LOCK_EX | LOCK_NB) ? errno == EAGAIN
-#ifdef EWOULDBLOCK
- || errno == EWOULDBLOCK
-#endif
- ? LOCK_UNAVAIL : LOCK_FAILED : LOCK_SUCCESS);
+ return (flock(fd, LOCK_EX | LOCK_NB) ?
+ errno == EAGAIN || errno == EWOULDBLOCK ? LOCK_UNAVAIL :
LOCK_FAILED :
+ LOCK_SUCCESS);
#endif
#ifdef HAVE_LOCK_FCNTL /* Gag me. We've got fcntl(2). */
{
@@ -1499,11 +1496,8 @@
* as returning EACCESS and EAGAIN; add EWOULDBLOCK for good measure,
* and assume they are the former. There's no portable way to do this.
*/
- return (errno == EACCES || errno == EAGAIN
-#ifdef EWOULDBLOCK
- || errno == EWOULDBLOCK
-#endif
- ? LOCK_UNAVAIL : LOCK_FAILED);
+ return (errno == EACCES || errno == EAGAIN || errno == EWOULDBLOCK ?
+ LOCK_UNAVAIL : LOCK_FAILED);
}
#endif
#if !defined(HAVE_LOCK_FLOCK) && !defined(HAVE_LOCK_FCNTL)
Index: common/msg.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/msg.c,v
retrieving revision 1.19
diff -u -r1.19 msg.c
--- common/msg.c 26 Nov 2013 17:48:01 -0000 1.19
+++ common/msg.c 11 Nov 2014 20:08:38 -0000
@@ -12,7 +12,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
@@ -40,27 +39,11 @@
void
msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
{
-#ifndef NL_ARGMAX
-#define __NL_ARGMAX 20 /* Set to 9 by System V. */
- struct {
- const char *str; /* String pointer. */
- size_t arg; /* Argument number. */
- size_t prefix; /* Prefix string length. */
- size_t skip; /* Skipped string length. */
- size_t suffix; /* Suffix string length. */
- } str[__NL_ARGMAX];
-#endif
static int reenter; /* STATIC: Re-entrancy check. */
GS *gp;
size_t blen, len, mlen, nlen;
const char *p;
char *bp, *mp;
-#ifndef NL_ARGMAX
- size_t cnt1, cnt2, soff;
- CHAR_T ch;
- const char *t, *u;
- char *rbp, *s_rbp;
-#endif
va_list ap;
/*
@@ -166,151 +149,12 @@
}
fmt = msg_cat(sp, fmt, NULL);
-#ifndef NL_ARGMAX
- /*
- * Nvi should run on machines that don't support the numbered argument
- * specifications (%[digit]*$). We do this by reformatting the string
- * so that we can hand it to vsnprintf(3) and it will use the arguments
- * in the right order. When vsnprintf returns, we put the string back
- * into the right order. It's undefined, according to SVID III, to mix
- * numbered argument specifications with the standard style arguments,
- * so this should be safe.
- *
- * In addition, we also need a character that is known to not occur in
- * any vi message, for separating the parts of the string. As callers
- * of msgq are responsible for making sure that all the non-printable
- * characters are formatted for printing before calling msgq, we use a
- * random non-printable character selected at terminal initialization
- * time. This code isn't fast by any means, but as messages should be
- * relatively short and normally have only a few arguments, it won't be
- * too bad. Regardless, nobody has come up with any other solution.
- *
- * The result of this loop is an array of pointers into the message
- * string, with associated lengths and argument numbers. The array
- * is in the "correct" order, and the arg field contains the argument
- * order.
- */
- for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
- for (t = p; *p != '\0' && *p != '%'; ++p);
- if (*p == '\0')
- break;
- ++p;
- if (!isdigit(*p)) {
- if (*p == '%')
- ++p;
- continue;
- }
- for (u = p; isdigit(*++p););
- if (*p != '$')
- continue;
-
- /* Up to, and including the % character. */
- str[soff].str = t;
- str[soff].prefix = u - t;
-
- /* Up to, and including the $ character. */
- str[soff].arg = atoi(u);
- str[soff].skip = (p - u) + 1;
- if (str[soff].arg >= __NL_ARGMAX)
- goto ret;
-
- /* Up to, and including the conversion character. */
- for (u = p; (ch = *++p) != '\0';)
- if (isalpha(ch) &&
- strchr("diouxXfeEgGcspn", ch) != NULL)
- break;
- str[soff].suffix = p - u;
- if (ch != '\0')
- ++p;
- ++soff;
- }
-
- /* If no magic strings, we're done. */
- if (soff == 0)
- goto format;
-
- /* Get space for the reordered strings. */
- if ((rbp = malloc(nlen)) == NULL)
- goto ret;
- s_rbp = rbp;
-
- /*
- * Reorder the strings into the message string based on argument
- * order.
- *
- * !!!
- * We ignore arguments that are out of order, i.e. if we don't find
- * an argument, we continue. Assume (almost certainly incorrectly)
- * that whoever created the string knew what they were doing.
- *
- * !!!
- * Brute force "sort", but since we don't expect more than one or two
- * arguments in a string, the setup cost of a fast sort will be more
- * expensive than the loop.
- */
- for (cnt1 = 1; cnt1 <= soff; ++cnt1)
- for (cnt2 = 0; cnt2 < soff; ++cnt2)
- if (cnt1 == str[cnt2].arg) {
- memmove(s_rbp, str[cnt2].str, str[cnt2].prefix);
- memmove(s_rbp + str[cnt2].prefix,
- str[cnt2].str + str[cnt2].prefix +
- str[cnt2].skip, str[cnt2].suffix);
- s_rbp += str[cnt2].prefix + str[cnt2].suffix;
- *s_rbp++ =
- gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
- break;
- }
- *s_rbp = '\0';
- fmt = rbp;
-
-format:
-#endif
/* Format the arguments into the string. */
va_start(ap, fmt);
len = vsnprintf(mp, REM, fmt, ap);
va_end(ap);
if (len >= nlen)
goto retry;
-
-#ifndef NL_ARGMAX
- if (soff == 0)
- goto nofmt;
-
- /*
- * Go through the resulting string, and, for each separator character
- * separated string, enter its new starting position and length in the
- * array.
- */
- for (p = t = mp, cnt1 = 1,
- ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
- if (*p == ch) {
- for (cnt2 = 0; cnt2 < soff; ++cnt2)
- if (str[cnt2].arg == cnt1)
- break;
- str[cnt2].str = t;
- str[cnt2].prefix = p - t;
- t = p + 1;
- ++cnt1;
- }
-
- /*
- * Reorder the strings once again, putting them back into the
- * message buffer.
- *
- * !!!
- * Note, the length of the message gets decremented once for
- * each substring, when we discard the separator character.
- */
- for (s_rbp = rbp, cnt1 = 0; cnt1 < soff; ++cnt1) {
- memmove(rbp, str[cnt1].str, str[cnt1].prefix);
- rbp += str[cnt1].prefix;
- --len;
- }
- memmove(mp, s_rbp, rbp - s_rbp);
-
- /* Free the reordered string memory. */
- free(s_rbp);
-#endif
nofmt: mp += len;
if ((mlen += len) > blen)
Index: common/recover.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/recover.c,v
retrieving revision 1.16
diff -u -r1.16 recover.c
--- common/recover.c 10 Nov 2014 21:31:42 -0000 1.16
+++ common/recover.c 11 Nov 2014 20:08:38 -0000
@@ -12,7 +12,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/queue.h>
#include <sys/stat.h>
Index: ex/ex_argv.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_argv.c,v
retrieving revision 1.14
diff -u -r1.14 ex_argv.c
--- ex/ex_argv.c 14 Oct 2014 22:23:12 -0000 1.14
+++ ex/ex_argv.c 11 Nov 2014 20:08:38 -0000
@@ -517,7 +517,7 @@
DIR *dirp;
EX_PRIVATE *exp;
int off;
- size_t dlen, len, nlen;
+ size_t dlen, nlen;
char *dname, *name, *p;
exp = EXP(sp);
@@ -540,11 +540,6 @@
}
nlen = strlen(name);
- /*
- * XXX
- * We don't use the d_namlen field, it's not portable enough; we
- * assume that d_name is nul terminated, instead.
- */
if ((dirp = opendir(dname)) == NULL) {
msgq_str(sp, M_SYSERR, dname, "%s");
return (1);
@@ -553,15 +548,14 @@
if (nlen == 0) {
if (dp->d_name[0] == '.')
continue;
- len = strlen(dp->d_name);
} else {
- len = strlen(dp->d_name);
- if (len < nlen || memcmp(dp->d_name, name, nlen))
+ if (dp->d_namlen < nlen ||
+ memcmp(dp->d_name, name, nlen))
continue;
}
/* Directory + name + slash + null. */
- argv_alloc(sp, dlen + len + 2);
+ argv_alloc(sp, dlen + dp->d_namlen + 2);
p = exp->args[exp->argsoff]->bp;
if (dlen != 0) {
memcpy(p, dname, dlen);
@@ -569,8 +563,8 @@
if (dlen > 1 || dname[0] != '/')
*p++ = '/';
}
- memcpy(p, dp->d_name, len + 1);
- exp->args[exp->argsoff]->len = dlen + len + 1;
+ memcpy(p, dp->d_name, dp->d_namlen + 1);
+ exp->args[exp->argsoff]->len = dlen + dp->d_namlen + 1;
++exp->argsoff;
excp->argv = exp->args;
excp->argc = exp->argsoff;
Index: ex/ex_cscope.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_cscope.c,v
retrieving revision 1.20
diff -u -r1.20 ex_cscope.c
--- ex/ex_cscope.c 1 Dec 2013 19:26:37 -0000 1.20
+++ ex/ex_cscope.c 11 Nov 2014 20:08:38 -0000
@@ -12,7 +12,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
Index: ex/ex_init.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_init.c,v
retrieving revision 1.11
diff -u -r1.11 ex_init.c
--- ex/ex_init.c 1 Dec 2013 16:47:59 -0000 1.11
+++ ex/ex_init.c 11 Nov 2014 20:08:38 -0000
@@ -12,7 +12,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/queue.h>
#include <sys/stat.h>
Index: ex/ex_script.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_script.c,v
retrieving revision 1.19
diff -u -r1.19 ex_script.c
--- ex/ex_script.c 28 Nov 2013 22:12:40 -0000 1.19
+++ ex/ex_script.c 11 Nov 2014 20:08:38 -0000
@@ -148,15 +148,12 @@
_exit(126);
(void)setsid();
-#ifdef TIOCSCTTY
/*
* 4.4BSD allocates a controlling terminal using the TIOCSCTTY
* ioctl, not by opening a terminal device file. POSIX 1003.1
- * doesn't define a portable way to do this. If TIOCSCTTY is
- * not available, hope that the open does it.
+ * doesn't define a portable way to do this.
*/
(void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
-#endif
(void)close(sc->sh_master);
(void)dup2(sc->sh_slave, STDIN_FILENO);
(void)dup2(sc->sh_slave, STDOUT_FILENO);
Index: ex/ex_tag.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_tag.c,v
retrieving revision 1.18
diff -u -r1.18 ex_tag.c
--- ex/ex_tag.c 10 Nov 2014 21:40:11 -0000 1.18
+++ ex/ex_tag.c 11 Nov 2014 20:08:38 -0000
@@ -15,7 +15,6 @@
#include "config.h"
#include <sys/param.h>
-#include <sys/types.h> /* XXX: param.h may not have included types.h */
#include <sys/mman.h>
#include <sys/queue.h>
#include <sys/stat.h>
@@ -1054,14 +1053,6 @@
return (1);
}
- /*
- * XXX
- * Some old BSD systems require MAP_FILE as an argument when mapping
- * regular files.
- */
-#ifndef MAP_FILE
-#define MAP_FILE 0
-#endif
/*
* XXX
* We'd like to test if the file is too big to mmap. Since we don't
Index: ex/script.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/script.h,v
retrieving revision 1.3
diff -u -r1.3 script.h
--- ex/script.h 29 Jan 2001 01:58:46 -0000 1.3
+++ ex/script.h 11 Nov 2014 20:08:38 -0000
@@ -18,8 +18,6 @@
char *sh_prompt; /* Prompt. */
size_t sh_prompt_len; /* Prompt length. */
char sh_name[64]; /* Pty name */
-#ifdef TIOCGWINSZ
struct winsize sh_win; /* Window size. */
-#endif
struct termios sh_term; /* Terminal information. */
};
Finally, the removal of port.h. Beware; requires a manual cvs rm.
Index: common/common.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/common.h,v
retrieving revision 1.6
diff -u -r1.6 common.h
--- common/common.h 6 Nov 2014 11:35:02 -0000 1.6
+++ common/common.h 11 Nov 2014 20:08:38 -0000
@@ -11,12 +11,6 @@
* @(#)common.h 10.13 (Berkeley) 9/25/96
*/
-/*
- * Porting information built at configuration time. Included before
- * any of nvi's include files.
- */
-#include "port.h"
-
#include <db.h>
#include <regex.h>
Index: build/port.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/build/port.h,v
retrieving revision 1.4
diff -u -r1.4 port.h
--- build/port.h 26 Nov 1999 22:49:08 -0000 1.4
+++ build/port.h 11 Nov 2014 20:08:38 -0000
@@ -1,5 +0,0 @@
-/* $OpenBSD: port.h,v 1.4 1999/11/26 22:49:08 millert Exp $ */
-
-/*
- * Nothing needed here for OpenBSD.
- */