xref: /minix/external/bsd/nvi/dist/common/msg.c (revision 0a6a1f1d)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: msg.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1991, 1993, 1994
484d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
584d9c625SLionel Sambuc  * Copyright (c) 1991, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
784d9c625SLionel Sambuc  *
884d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc  */
1084d9c625SLionel Sambuc 
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc 
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc static const char sccsid[] = "Id: msg.c,v 10.61 2003/07/18 23:17:30 skimo Exp  (Berkeley) Date: 2003/07/18 23:17:30 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: msg.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc 
2284d9c625SLionel Sambuc #include <sys/param.h>
2384d9c625SLionel Sambuc #include <sys/types.h>		/* XXX: param.h may not have included types.h */
2484d9c625SLionel Sambuc #include <sys/queue.h>
2584d9c625SLionel Sambuc #include <sys/stat.h>
2684d9c625SLionel Sambuc #include <sys/time.h>
2784d9c625SLionel Sambuc 
2884d9c625SLionel Sambuc #include <bitstring.h>
2984d9c625SLionel Sambuc #include <ctype.h>
3084d9c625SLionel Sambuc #include <errno.h>
3184d9c625SLionel Sambuc #include <fcntl.h>
3284d9c625SLionel Sambuc #include <limits.h>
3384d9c625SLionel Sambuc #include <stdio.h>
3484d9c625SLionel Sambuc #include <stdlib.h>
3584d9c625SLionel Sambuc #include <string.h>
3684d9c625SLionel Sambuc #include <unistd.h>
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc #ifdef __STDC__
3984d9c625SLionel Sambuc #include <stdarg.h>
4084d9c625SLionel Sambuc #else
4184d9c625SLionel Sambuc #include <varargs.h>
4284d9c625SLionel Sambuc #endif
4384d9c625SLionel Sambuc 
4484d9c625SLionel Sambuc #include "common.h"
4584d9c625SLionel Sambuc #include "dbinternal.h"
4684d9c625SLionel Sambuc #include "../vi/vi.h"
4784d9c625SLionel Sambuc 
4884d9c625SLionel Sambuc /*
4984d9c625SLionel Sambuc  * msgq --
5084d9c625SLionel Sambuc  *	Display a message.
5184d9c625SLionel Sambuc  *
5284d9c625SLionel Sambuc  * PUBLIC: void msgq __P((SCR *, mtype_t, const char *, ...));
5384d9c625SLionel Sambuc  */
5484d9c625SLionel Sambuc void
5584d9c625SLionel Sambuc #ifdef __STDC__
msgq(SCR * sp,mtype_t mt,const char * fmt,...)5684d9c625SLionel Sambuc msgq(SCR *sp, mtype_t mt, const char *fmt, ...)
5784d9c625SLionel Sambuc #else
5884d9c625SLionel Sambuc msgq(sp, mt, fmt, va_alist)
5984d9c625SLionel Sambuc 	SCR *sp;
6084d9c625SLionel Sambuc 	mtype_t mt;
6184d9c625SLionel Sambuc         const char *fmt;
6284d9c625SLionel Sambuc         va_dcl
6384d9c625SLionel Sambuc #endif
6484d9c625SLionel Sambuc {
6584d9c625SLionel Sambuc #ifndef NL_ARGMAX
6684d9c625SLionel Sambuc #define	__NL_ARGMAX	20		/* Set to 9 by System V. */
6784d9c625SLionel Sambuc 	struct {
6884d9c625SLionel Sambuc 		const char *str;	/* String pointer. */
6984d9c625SLionel Sambuc 		size_t	 arg;		/* Argument number. */
7084d9c625SLionel Sambuc 		size_t	 prefix;	/* Prefix string length. */
7184d9c625SLionel Sambuc 		size_t	 skip;		/* Skipped string length. */
7284d9c625SLionel Sambuc 		size_t	 suffix;	/* Suffix string length. */
7384d9c625SLionel Sambuc 	} str[__NL_ARGMAX];
7484d9c625SLionel Sambuc #endif
7584d9c625SLionel Sambuc 	static int reenter;		/* STATIC: Re-entrancy check. */
7684d9c625SLionel Sambuc 	GS *gp;
7784d9c625SLionel Sambuc 	WIN *wp = NULL;
7884d9c625SLionel Sambuc 	size_t blen, len, mlen, nlen;
7984d9c625SLionel Sambuc 	const char *p;
8084d9c625SLionel Sambuc 	char *bp, *mp;
8184d9c625SLionel Sambuc         va_list ap;
8284d9c625SLionel Sambuc #ifndef NL_ARGMAX
8384d9c625SLionel Sambuc 	int ch;
8484d9c625SLionel Sambuc 	char *rbp, *s_rbp;
8584d9c625SLionel Sambuc 	const char *t, *u;
8684d9c625SLionel Sambuc 	size_t cnt1, cnt2, soff;
8784d9c625SLionel Sambuc #endif
8884d9c625SLionel Sambuc 
8984d9c625SLionel Sambuc 	/*
9084d9c625SLionel Sambuc 	 * !!!
9184d9c625SLionel Sambuc 	 * It's possible to enter msg when there's no screen to hold the
9284d9c625SLionel Sambuc 	 * message.  If sp is NULL, ignore the special cases and put the
9384d9c625SLionel Sambuc 	 * message out to stderr.
9484d9c625SLionel Sambuc 	 */
9584d9c625SLionel Sambuc 	if (sp == NULL) {
9684d9c625SLionel Sambuc 		gp = NULL;
9784d9c625SLionel Sambuc 		if (mt == M_BERR)
9884d9c625SLionel Sambuc 			mt = M_ERR;
9984d9c625SLionel Sambuc 		else if (mt == M_VINFO)
10084d9c625SLionel Sambuc 			mt = M_INFO;
10184d9c625SLionel Sambuc 	} else {
10284d9c625SLionel Sambuc 		gp = sp->gp;
10384d9c625SLionel Sambuc 		wp = sp->wp;
10484d9c625SLionel Sambuc 		switch (mt) {
10584d9c625SLionel Sambuc 		case M_BERR:
10684d9c625SLionel Sambuc 			if (F_ISSET(sp, SC_VI) && !O_ISSET(sp, O_VERBOSE)) {
10784d9c625SLionel Sambuc 				F_SET(gp, G_BELLSCHED);
10884d9c625SLionel Sambuc 				return;
10984d9c625SLionel Sambuc 			}
11084d9c625SLionel Sambuc 			mt = M_ERR;
11184d9c625SLionel Sambuc 			break;
11284d9c625SLionel Sambuc 		case M_VINFO:
11384d9c625SLionel Sambuc 			if (!O_ISSET(sp, O_VERBOSE))
11484d9c625SLionel Sambuc 				return;
11584d9c625SLionel Sambuc 			mt = M_INFO;
11684d9c625SLionel Sambuc 			/* FALLTHROUGH */
11784d9c625SLionel Sambuc 		case M_INFO:
11884d9c625SLionel Sambuc 			if (F_ISSET(sp, SC_EX_SILENT))
11984d9c625SLionel Sambuc 				return;
12084d9c625SLionel Sambuc 			break;
12184d9c625SLionel Sambuc 		case M_ERR:
12284d9c625SLionel Sambuc 		case M_SYSERR:
12384d9c625SLionel Sambuc 		case M_DBERR:
12484d9c625SLionel Sambuc 			break;
12584d9c625SLionel Sambuc 		default:
12684d9c625SLionel Sambuc 			abort();
12784d9c625SLionel Sambuc 		}
12884d9c625SLionel Sambuc 	}
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc 	/*
13184d9c625SLionel Sambuc 	 * It's possible to reenter msg when it allocates space.  We're
13284d9c625SLionel Sambuc 	 * probably dead anyway, but there's no reason to drop core.
13384d9c625SLionel Sambuc 	 *
13484d9c625SLionel Sambuc 	 * XXX
13584d9c625SLionel Sambuc 	 * Yes, there's a race, but it should only be two instructions.
13684d9c625SLionel Sambuc 	 */
13784d9c625SLionel Sambuc 	if (reenter++)
13884d9c625SLionel Sambuc 		return;
13984d9c625SLionel Sambuc 
14084d9c625SLionel Sambuc 	/* Get space for the message. */
14184d9c625SLionel Sambuc 	nlen = 1024;
14284d9c625SLionel Sambuc 	if (0) {
14384d9c625SLionel Sambuc retry:		FREE_SPACE(sp, bp, blen);
14484d9c625SLionel Sambuc 		nlen *= 2;
14584d9c625SLionel Sambuc 	}
14684d9c625SLionel Sambuc 	bp = NULL;
14784d9c625SLionel Sambuc 	blen = 0;
14884d9c625SLionel Sambuc 	GET_SPACE_GOTOC(sp, bp, blen, nlen);
14984d9c625SLionel Sambuc 
15084d9c625SLionel Sambuc 	/*
15184d9c625SLionel Sambuc 	 * Error prefix.
15284d9c625SLionel Sambuc 	 *
15384d9c625SLionel Sambuc 	 * mp:	 pointer to the current next character to be written
15484d9c625SLionel Sambuc 	 * mlen: length of the already written characters
15584d9c625SLionel Sambuc 	 * blen: total length of the buffer
15684d9c625SLionel Sambuc 	 */
15784d9c625SLionel Sambuc #define	REM	(blen - mlen)
15884d9c625SLionel Sambuc 	mp = bp;
15984d9c625SLionel Sambuc 	mlen = 0;
16084d9c625SLionel Sambuc 	if (mt == M_SYSERR || mt == M_DBERR) {
16184d9c625SLionel Sambuc 		p = msg_cat(sp, "020|Error: ", &len);
16284d9c625SLionel Sambuc 		if (REM < len)
16384d9c625SLionel Sambuc 			goto retry;
16484d9c625SLionel Sambuc 		memcpy(mp, p, len);
16584d9c625SLionel Sambuc 		mp += len;
16684d9c625SLionel Sambuc 		mlen += len;
16784d9c625SLionel Sambuc 	}
16884d9c625SLionel Sambuc 
16984d9c625SLionel Sambuc 	/*
17084d9c625SLionel Sambuc 	 * If we're running an ex command that the user didn't enter, display
17184d9c625SLionel Sambuc 	 * the file name and line number prefix.
17284d9c625SLionel Sambuc 	 */
17384d9c625SLionel Sambuc 	if ((mt == M_ERR || mt == M_SYSERR) &&
17484d9c625SLionel Sambuc 	    sp != NULL && wp != NULL && wp->if_name != NULL) {
17584d9c625SLionel Sambuc 		for (p = wp->if_name; *p != '\0'; ++p) {
17684d9c625SLionel Sambuc 			len = snprintf(mp, REM, "%s", KEY_NAME(sp, *p));
17784d9c625SLionel Sambuc 			mp += len;
17884d9c625SLionel Sambuc 			if ((mlen += len) > blen)
17984d9c625SLionel Sambuc 				goto retry;
18084d9c625SLionel Sambuc 		}
18184d9c625SLionel Sambuc 		len = snprintf(mp, REM, ", %d: ", wp->if_lno);
18284d9c625SLionel Sambuc 		mp += len;
18384d9c625SLionel Sambuc 		if ((mlen += len) > blen)
18484d9c625SLionel Sambuc 			goto retry;
18584d9c625SLionel Sambuc 	}
18684d9c625SLionel Sambuc 
18784d9c625SLionel Sambuc 	/* If nothing to format, we're done. */
18884d9c625SLionel Sambuc 	if (fmt == NULL)
18984d9c625SLionel Sambuc 		goto nofmt;
19084d9c625SLionel Sambuc 	fmt = msg_cat(sp, fmt, NULL);
19184d9c625SLionel Sambuc 
19284d9c625SLionel Sambuc #ifndef NL_ARGMAX
19384d9c625SLionel Sambuc 	/*
19484d9c625SLionel Sambuc 	 * Nvi should run on machines that don't support the numbered argument
19584d9c625SLionel Sambuc 	 * specifications (%[digit]*$).  We do this by reformatting the string
19684d9c625SLionel Sambuc 	 * so that we can hand it to vsprintf(3) and it will use the arguments
19784d9c625SLionel Sambuc 	 * in the right order.  When vsprintf returns, we put the string back
19884d9c625SLionel Sambuc 	 * into the right order.  It's undefined, according to SVID III, to mix
19984d9c625SLionel Sambuc 	 * numbered argument specifications with the standard style arguments,
20084d9c625SLionel Sambuc 	 * so this should be safe.
20184d9c625SLionel Sambuc 	 *
20284d9c625SLionel Sambuc 	 * In addition, we also need a character that is known to not occur in
20384d9c625SLionel Sambuc 	 * any vi message, for separating the parts of the string.  As callers
20484d9c625SLionel Sambuc 	 * of msgq are responsible for making sure that all the non-printable
20584d9c625SLionel Sambuc 	 * characters are formatted for printing before calling msgq, we use a
20684d9c625SLionel Sambuc 	 * random non-printable character selected at terminal initialization
20784d9c625SLionel Sambuc 	 * time.  This code isn't fast by any means, but as messages should be
20884d9c625SLionel Sambuc 	 * relatively short and normally have only a few arguments, it won't be
20984d9c625SLionel Sambuc 	 * too bad.  Regardless, nobody has come up with any other solution.
21084d9c625SLionel Sambuc 	 *
21184d9c625SLionel Sambuc 	 * The result of this loop is an array of pointers into the message
21284d9c625SLionel Sambuc 	 * string, with associated lengths and argument numbers.  The array
21384d9c625SLionel Sambuc 	 * is in the "correct" order, and the arg field contains the argument
21484d9c625SLionel Sambuc 	 * order.
21584d9c625SLionel Sambuc 	 */
21684d9c625SLionel Sambuc 	for (p = fmt, soff = 0; soff < __NL_ARGMAX;) {
21784d9c625SLionel Sambuc 		for (t = p; *p != '\0' && *p != '%'; ++p);
21884d9c625SLionel Sambuc 		if (*p == '\0')
21984d9c625SLionel Sambuc 			break;
22084d9c625SLionel Sambuc 		++p;
22184d9c625SLionel Sambuc 		if (!isdigit((unsigned char)*p)) {
22284d9c625SLionel Sambuc 			if (*p == '%')
22384d9c625SLionel Sambuc 				++p;
22484d9c625SLionel Sambuc 			continue;
22584d9c625SLionel Sambuc 		}
22684d9c625SLionel Sambuc 		for (u = p; *++p != '\0' && isdigit((unsigned char)*p););
22784d9c625SLionel Sambuc 		if (*p != '$')
22884d9c625SLionel Sambuc 			continue;
22984d9c625SLionel Sambuc 
23084d9c625SLionel Sambuc 		/* Up to, and including the % character. */
23184d9c625SLionel Sambuc 		str[soff].str = t;
23284d9c625SLionel Sambuc 		str[soff].prefix = u - t;
23384d9c625SLionel Sambuc 
23484d9c625SLionel Sambuc 		/* Up to, and including the $ character. */
23584d9c625SLionel Sambuc 		str[soff].arg = atoi(u);
23684d9c625SLionel Sambuc 		str[soff].skip = (p - u) + 1;
23784d9c625SLionel Sambuc 		if (str[soff].arg >= __NL_ARGMAX)
23884d9c625SLionel Sambuc 			goto ret;
23984d9c625SLionel Sambuc 
24084d9c625SLionel Sambuc 		/* Up to, and including the conversion character. */
24184d9c625SLionel Sambuc 		for (u = p; (ch = (unsigned char)*++p) != '\0';)
24284d9c625SLionel Sambuc 			if (isalpha(ch) &&
24384d9c625SLionel Sambuc 			    strchr("diouxXfeEgGcspn", ch) != NULL)
24484d9c625SLionel Sambuc 				break;
24584d9c625SLionel Sambuc 		str[soff].suffix = p - u;
24684d9c625SLionel Sambuc 		if (ch != '\0')
24784d9c625SLionel Sambuc 			++p;
24884d9c625SLionel Sambuc 		++soff;
24984d9c625SLionel Sambuc 	}
25084d9c625SLionel Sambuc 
25184d9c625SLionel Sambuc 	/* If no magic strings, we're done. */
25284d9c625SLionel Sambuc 	if (soff == 0)
25384d9c625SLionel Sambuc 		goto format;
25484d9c625SLionel Sambuc 
25584d9c625SLionel Sambuc 	 /* Get space for the reordered strings. */
25684d9c625SLionel Sambuc 	if ((rbp = malloc(nlen)) == NULL)
25784d9c625SLionel Sambuc 		goto ret;
25884d9c625SLionel Sambuc 	s_rbp = rbp;
25984d9c625SLionel Sambuc 
26084d9c625SLionel Sambuc 	/*
26184d9c625SLionel Sambuc 	 * Reorder the strings into the message string based on argument
26284d9c625SLionel Sambuc 	 * order.
26384d9c625SLionel Sambuc 	 *
26484d9c625SLionel Sambuc 	 * !!!
26584d9c625SLionel Sambuc 	 * We ignore arguments that are out of order, i.e. if we don't find
26684d9c625SLionel Sambuc 	 * an argument, we continue.  Assume (almost certainly incorrectly)
26784d9c625SLionel Sambuc 	 * that whoever created the string knew what they were doing.
26884d9c625SLionel Sambuc 	 *
26984d9c625SLionel Sambuc 	 * !!!
27084d9c625SLionel Sambuc 	 * Brute force "sort", but since we don't expect more than one or two
27184d9c625SLionel Sambuc 	 * arguments in a string, the setup cost of a fast sort will be more
27284d9c625SLionel Sambuc 	 * expensive than the loop.
27384d9c625SLionel Sambuc 	 */
27484d9c625SLionel Sambuc 	for (cnt1 = 1; cnt1 <= soff; ++cnt1)
27584d9c625SLionel Sambuc 		for (cnt2 = 0; cnt2 < soff; ++cnt2)
27684d9c625SLionel Sambuc 			if (cnt1 == str[cnt2].arg) {
27784d9c625SLionel Sambuc 				memmove(s_rbp, str[cnt2].str, str[cnt2].prefix);
27884d9c625SLionel Sambuc 				memmove(s_rbp + str[cnt2].prefix,
27984d9c625SLionel Sambuc 				    str[cnt2].str + str[cnt2].prefix +
28084d9c625SLionel Sambuc 				    str[cnt2].skip, str[cnt2].suffix);
28184d9c625SLionel Sambuc 				s_rbp += str[cnt2].prefix + str[cnt2].suffix;
28284d9c625SLionel Sambuc 				*s_rbp++ =
28384d9c625SLionel Sambuc 				    gp == NULL ? DEFAULT_NOPRINT : gp->noprint;
28484d9c625SLionel Sambuc 				break;
28584d9c625SLionel Sambuc 			}
28684d9c625SLionel Sambuc 	*s_rbp = '\0';
28784d9c625SLionel Sambuc 	fmt = rbp;
28884d9c625SLionel Sambuc #endif
28984d9c625SLionel Sambuc 
29084d9c625SLionel Sambuc #ifndef NL_ARGMAX
29184d9c625SLionel Sambuc format:	/* Format the arguments into the string. */
29284d9c625SLionel Sambuc #endif
29384d9c625SLionel Sambuc #ifdef __STDC__
29484d9c625SLionel Sambuc         va_start(ap, fmt);
29584d9c625SLionel Sambuc #else
29684d9c625SLionel Sambuc         va_start(ap);
29784d9c625SLionel Sambuc #endif
29884d9c625SLionel Sambuc 	len = vsnprintf(mp, REM, fmt, ap);
29984d9c625SLionel Sambuc 	va_end(ap);
30084d9c625SLionel Sambuc 	if (len >= nlen)
30184d9c625SLionel Sambuc 		goto retry;
30284d9c625SLionel Sambuc 
30384d9c625SLionel Sambuc #ifndef NL_ARGMAX
30484d9c625SLionel Sambuc 	if (soff == 0)
30584d9c625SLionel Sambuc 		goto nofmt;
30684d9c625SLionel Sambuc 
30784d9c625SLionel Sambuc 	/*
30884d9c625SLionel Sambuc 	 * Go through the resulting string, and, for each separator character
30984d9c625SLionel Sambuc 	 * separated string, enter its new starting position and length in the
31084d9c625SLionel Sambuc 	 * array.
31184d9c625SLionel Sambuc 	 */
31284d9c625SLionel Sambuc 	for (p = t = mp, cnt1 = 1,
31384d9c625SLionel Sambuc 	    ch = gp == NULL ? DEFAULT_NOPRINT : gp->noprint; *p != '\0'; ++p)
31484d9c625SLionel Sambuc 		if (*p == ch) {
31584d9c625SLionel Sambuc 			for (cnt2 = 0; cnt2 < soff; ++cnt2)
31684d9c625SLionel Sambuc 				if (str[cnt2].arg == cnt1)
31784d9c625SLionel Sambuc 					break;
31884d9c625SLionel Sambuc 			str[cnt2].str = t;
31984d9c625SLionel Sambuc 			str[cnt2].prefix = p - t;
32084d9c625SLionel Sambuc 			t = p + 1;
32184d9c625SLionel Sambuc 			++cnt1;
32284d9c625SLionel Sambuc 		}
32384d9c625SLionel Sambuc 
32484d9c625SLionel Sambuc 	/*
32584d9c625SLionel Sambuc 	 * Reorder the strings once again, putting them back into the
32684d9c625SLionel Sambuc 	 * message buffer.
32784d9c625SLionel Sambuc 	 *
32884d9c625SLionel Sambuc 	 * !!!
32984d9c625SLionel Sambuc 	 * Note, the length of the message gets decremented once for
33084d9c625SLionel Sambuc 	 * each substring, when we discard the separator character.
33184d9c625SLionel Sambuc 	 */
33284d9c625SLionel Sambuc 	for (s_rbp = rbp, cnt1 = 0; cnt1 < soff; ++cnt1) {
33384d9c625SLionel Sambuc 		memmove(rbp, str[cnt1].str, str[cnt1].prefix);
33484d9c625SLionel Sambuc 		rbp += str[cnt1].prefix;
33584d9c625SLionel Sambuc 		--len;
33684d9c625SLionel Sambuc 	}
33784d9c625SLionel Sambuc 	memmove(mp, s_rbp, rbp - s_rbp);
33884d9c625SLionel Sambuc 
33984d9c625SLionel Sambuc 	/* Free the reordered string memory. */
34084d9c625SLionel Sambuc 	free(s_rbp);
34184d9c625SLionel Sambuc #endif
34284d9c625SLionel Sambuc 
34384d9c625SLionel Sambuc nofmt:	mp += len;
34484d9c625SLionel Sambuc 	if ((mlen += len) > blen)
34584d9c625SLionel Sambuc 		goto retry;
34684d9c625SLionel Sambuc 	if (mt == M_SYSERR) {
34784d9c625SLionel Sambuc 		len = snprintf(mp, REM, ": %s", strerror(errno));
34884d9c625SLionel Sambuc 		mp += len;
34984d9c625SLionel Sambuc 		if ((mlen += len) > blen)
35084d9c625SLionel Sambuc 			goto retry;
35184d9c625SLionel Sambuc 		mt = M_ERR;
35284d9c625SLionel Sambuc 	}
35384d9c625SLionel Sambuc 	if (mt == M_DBERR) {
35484d9c625SLionel Sambuc 		len = snprintf(mp, REM, ": %s", db_strerror(sp->db_error));
35584d9c625SLionel Sambuc 		mp += len;
35684d9c625SLionel Sambuc 		if ((mlen += len) > blen)
35784d9c625SLionel Sambuc 			goto retry;
35884d9c625SLionel Sambuc 		mt = M_ERR;
35984d9c625SLionel Sambuc 	}
36084d9c625SLionel Sambuc 
36184d9c625SLionel Sambuc 	/* Add trailing newline. */
36284d9c625SLionel Sambuc 	if ((mlen += 1) > blen)
36384d9c625SLionel Sambuc 		goto retry;
36484d9c625SLionel Sambuc 	*mp = '\n';
36584d9c625SLionel Sambuc 
36684d9c625SLionel Sambuc 	if (sp != NULL && sp->ep != NULL)
36784d9c625SLionel Sambuc 		(void)ex_fflush(sp);
36884d9c625SLionel Sambuc 	if (wp != NULL)
36984d9c625SLionel Sambuc 		wp->scr_msg(sp, mt, bp, mlen);
37084d9c625SLionel Sambuc 	else
37184d9c625SLionel Sambuc 		(void)fprintf(stderr, "%.*s", (int)mlen, bp);
37284d9c625SLionel Sambuc 
37384d9c625SLionel Sambuc 	/* Cleanup. */
37484d9c625SLionel Sambuc #ifndef NL_ARGMAX
37584d9c625SLionel Sambuc ret:
37684d9c625SLionel Sambuc #endif
37784d9c625SLionel Sambuc 	FREE_SPACE(sp, bp, blen);
37884d9c625SLionel Sambuc alloc_err:
37984d9c625SLionel Sambuc 	reenter = 0;
38084d9c625SLionel Sambuc }
38184d9c625SLionel Sambuc 
38284d9c625SLionel Sambuc /*
38384d9c625SLionel Sambuc  * msgq_str --
38484d9c625SLionel Sambuc  *	Display a message with an embedded string.
38584d9c625SLionel Sambuc  *
38684d9c625SLionel Sambuc  * PUBLIC: void msgq_wstr __P((SCR *, mtype_t, const CHAR_T *, const char *));
38784d9c625SLionel Sambuc  */
38884d9c625SLionel Sambuc void
msgq_wstr(SCR * sp,mtype_t mtype,const CHAR_T * str,const char * fmt)38984d9c625SLionel Sambuc msgq_wstr(SCR *sp, mtype_t mtype, const CHAR_T *str, const char *fmt)
39084d9c625SLionel Sambuc {
39184d9c625SLionel Sambuc 	size_t nlen;
39284d9c625SLionel Sambuc 	const char *nstr;
39384d9c625SLionel Sambuc 
39484d9c625SLionel Sambuc 	if (str == NULL) {
39584d9c625SLionel Sambuc 		msgq(sp, mtype, "%s", fmt);
39684d9c625SLionel Sambuc 		return;
39784d9c625SLionel Sambuc 	}
39884d9c625SLionel Sambuc 	INT2CHAR(sp, str, STRLEN(str) + 1, nstr, nlen);
39984d9c625SLionel Sambuc 	msgq_str(sp, mtype, nstr, fmt);
40084d9c625SLionel Sambuc }
40184d9c625SLionel Sambuc 
40284d9c625SLionel Sambuc /*
40384d9c625SLionel Sambuc  * msgq_str --
40484d9c625SLionel Sambuc  *	Display a message with an embedded string.
40584d9c625SLionel Sambuc  *
40684d9c625SLionel Sambuc  * PUBLIC: void msgq_str __P((SCR *, mtype_t, const char *, const char *));
40784d9c625SLionel Sambuc  */
40884d9c625SLionel Sambuc void
msgq_str(SCR * sp,mtype_t mtype,const char * str,const char * fmt)40984d9c625SLionel Sambuc msgq_str(SCR *sp, mtype_t mtype, const char *str, const char *fmt)
41084d9c625SLionel Sambuc {
41184d9c625SLionel Sambuc 	int nf, sv_errno;
41284d9c625SLionel Sambuc 	char *p;
41384d9c625SLionel Sambuc 
41484d9c625SLionel Sambuc 	if (str == NULL) {
41584d9c625SLionel Sambuc 		msgq(sp, mtype, "%s", fmt);
41684d9c625SLionel Sambuc 		return;
41784d9c625SLionel Sambuc 	}
41884d9c625SLionel Sambuc 
41984d9c625SLionel Sambuc 	sv_errno = errno;
42084d9c625SLionel Sambuc 	p = msg_print(sp, str, &nf);
42184d9c625SLionel Sambuc 	errno = sv_errno;
42284d9c625SLionel Sambuc 	msgq(sp, mtype, fmt, p);
42384d9c625SLionel Sambuc 	if (nf)
42484d9c625SLionel Sambuc 		FREE_SPACE(sp, p, 0);
42584d9c625SLionel Sambuc }
42684d9c625SLionel Sambuc 
42784d9c625SLionel Sambuc /*
42884d9c625SLionel Sambuc  * mod_rpt --
42984d9c625SLionel Sambuc  *	Report on the lines that changed.
43084d9c625SLionel Sambuc  *
43184d9c625SLionel Sambuc  * !!!
43284d9c625SLionel Sambuc  * Historic vi documentation (USD:15-8) claimed that "The editor will also
43384d9c625SLionel Sambuc  * always tell you when a change you make affects text which you cannot see."
43484d9c625SLionel Sambuc  * This wasn't true -- edit a large file and do "100d|1".  We don't implement
43584d9c625SLionel Sambuc  * this semantic since it requires tracking each line that changes during a
43684d9c625SLionel Sambuc  * command instead of just keeping count.
43784d9c625SLionel Sambuc  *
43884d9c625SLionel Sambuc  * Line counts weren't right in historic vi, either.  For example, given the
43984d9c625SLionel Sambuc  * file:
44084d9c625SLionel Sambuc  *	abc
44184d9c625SLionel Sambuc  *	def
44284d9c625SLionel Sambuc  * the command 2d}, from the 'b' would report that two lines were deleted,
44384d9c625SLionel Sambuc  * not one.
44484d9c625SLionel Sambuc  *
44584d9c625SLionel Sambuc  * PUBLIC: void mod_rpt __P((SCR *));
44684d9c625SLionel Sambuc  */
44784d9c625SLionel Sambuc void
mod_rpt(SCR * sp)44884d9c625SLionel Sambuc mod_rpt(SCR *sp)
44984d9c625SLionel Sambuc {
45084d9c625SLionel Sambuc 	static const char * const action[] = {
45184d9c625SLionel Sambuc 		"293|added",
45284d9c625SLionel Sambuc 		"294|changed",
45384d9c625SLionel Sambuc 		"295|deleted",
45484d9c625SLionel Sambuc 		"296|joined",
45584d9c625SLionel Sambuc 		"297|moved",
45684d9c625SLionel Sambuc 		"298|shifted",
45784d9c625SLionel Sambuc 		"299|yanked",
45884d9c625SLionel Sambuc 	};
45984d9c625SLionel Sambuc 	static const char * const lines[] = {
46084d9c625SLionel Sambuc 		"300|line",
46184d9c625SLionel Sambuc 		"301|lines",
46284d9c625SLionel Sambuc 	};
46384d9c625SLionel Sambuc 	db_recno_t total;
46484d9c625SLionel Sambuc 	u_long rptval;
46584d9c625SLionel Sambuc 	int first;
46684d9c625SLionel Sambuc 	size_t cnt, blen, len, tlen;
46784d9c625SLionel Sambuc 	const char *t;
46884d9c625SLionel Sambuc 	const char * const *ap;
46984d9c625SLionel Sambuc 	char *bp, *p;
47084d9c625SLionel Sambuc 
47184d9c625SLionel Sambuc 	/* Change reports are turned off in batch mode. */
47284d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_EX_SILENT))
47384d9c625SLionel Sambuc 		return;
47484d9c625SLionel Sambuc 
47584d9c625SLionel Sambuc 	/* Reset changing line number. */
47684d9c625SLionel Sambuc 	sp->rptlchange = OOBLNO;
47784d9c625SLionel Sambuc 
47884d9c625SLionel Sambuc 	/*
47984d9c625SLionel Sambuc 	 * Don't build a message if not enough changed.
48084d9c625SLionel Sambuc 	 *
48184d9c625SLionel Sambuc 	 * !!!
48284d9c625SLionel Sambuc 	 * And now, a vi clone test.  Historically, vi reported if the number
48384d9c625SLionel Sambuc 	 * of changed lines was > than the value, not >=, unless it was a yank
48484d9c625SLionel Sambuc 	 * command, which used >=.  No lie.  Furthermore, an action was never
48584d9c625SLionel Sambuc 	 * reported for a single line action.  This is consistent for actions
48684d9c625SLionel Sambuc 	 * other than yank, but yank didn't report single line actions even if
48784d9c625SLionel Sambuc 	 * the report edit option was set to 1.  In addition, setting report to
48884d9c625SLionel Sambuc 	 * 0 in the 4BSD historic vi was equivalent to setting it to 1, for an
48984d9c625SLionel Sambuc 	 * unknown reason (this bug was fixed in System III/V at some point).
49084d9c625SLionel Sambuc 	 * I got complaints, so nvi conforms to System III/V historic practice
49184d9c625SLionel Sambuc 	 * except that we report a yank of 1 line if report is set to 1.
49284d9c625SLionel Sambuc 	 */
49384d9c625SLionel Sambuc #define	ARSIZE(a)	sizeof(a) / sizeof (*a)
49484d9c625SLionel Sambuc #define	MAXNUM		25
49584d9c625SLionel Sambuc 	rptval = O_VAL(sp, O_REPORT);
49684d9c625SLionel Sambuc 	for (cnt = 0, total = 0; cnt < ARSIZE(action); ++cnt)
49784d9c625SLionel Sambuc 		total += sp->rptlines[cnt];
49884d9c625SLionel Sambuc 	if (total == 0)
49984d9c625SLionel Sambuc 		return;
50084d9c625SLionel Sambuc 	if (total <= rptval && sp->rptlines[L_YANKED] < rptval) {
50184d9c625SLionel Sambuc 		for (cnt = 0; cnt < ARSIZE(action); ++cnt)
50284d9c625SLionel Sambuc 			sp->rptlines[cnt] = 0;
50384d9c625SLionel Sambuc 		return;
50484d9c625SLionel Sambuc 	}
50584d9c625SLionel Sambuc 
50684d9c625SLionel Sambuc 	/* Build and display the message. */
50784d9c625SLionel Sambuc 	GET_SPACE_GOTOC(sp, bp, blen, sizeof(action) * MAXNUM + 1);
50884d9c625SLionel Sambuc 	for (p = bp, first = 1, tlen = 0,
50984d9c625SLionel Sambuc 	    ap = action, cnt = 0; cnt < ARSIZE(action); ++ap, ++cnt)
51084d9c625SLionel Sambuc 		if (sp->rptlines[cnt] != 0) {
51184d9c625SLionel Sambuc 			if (first)
51284d9c625SLionel Sambuc 				first = 0;
51384d9c625SLionel Sambuc 			else {
51484d9c625SLionel Sambuc 				*p++ = ';';
51584d9c625SLionel Sambuc 				*p++ = ' ';
51684d9c625SLionel Sambuc 				tlen += 2;
51784d9c625SLionel Sambuc 			}
51884d9c625SLionel Sambuc 			len = snprintf(p, MAXNUM, "%lu ",
51984d9c625SLionel Sambuc 				(unsigned long) sp->rptlines[cnt]);
52084d9c625SLionel Sambuc 			p += len;
52184d9c625SLionel Sambuc 			tlen += len;
52284d9c625SLionel Sambuc 			t = msg_cat(sp,
52384d9c625SLionel Sambuc 			    lines[sp->rptlines[cnt] == 1 ? 0 : 1], &len);
52484d9c625SLionel Sambuc 			memcpy(p, t, len);
52584d9c625SLionel Sambuc 			p += len;
52684d9c625SLionel Sambuc 			tlen += len;
52784d9c625SLionel Sambuc 			*p++ = ' ';
52884d9c625SLionel Sambuc 			++tlen;
52984d9c625SLionel Sambuc 			t = msg_cat(sp, *ap, &len);
53084d9c625SLionel Sambuc 			memcpy(p, t, len);
53184d9c625SLionel Sambuc 			p += len;
53284d9c625SLionel Sambuc 			tlen += len;
53384d9c625SLionel Sambuc 			sp->rptlines[cnt] = 0;
53484d9c625SLionel Sambuc 		}
53584d9c625SLionel Sambuc 
53684d9c625SLionel Sambuc 	/* Add trailing newline. */
53784d9c625SLionel Sambuc 	*p = '\n';
53884d9c625SLionel Sambuc 	++tlen;
53984d9c625SLionel Sambuc 
54084d9c625SLionel Sambuc 	(void)ex_fflush(sp);
54184d9c625SLionel Sambuc 	sp->wp->scr_msg(sp, M_INFO, bp, tlen);
54284d9c625SLionel Sambuc 
54384d9c625SLionel Sambuc 	FREE_SPACE(sp, bp, blen);
54484d9c625SLionel Sambuc alloc_err:
54584d9c625SLionel Sambuc 	return;
54684d9c625SLionel Sambuc 
54784d9c625SLionel Sambuc #undef ARSIZE
54884d9c625SLionel Sambuc #undef MAXNUM
54984d9c625SLionel Sambuc }
55084d9c625SLionel Sambuc 
55184d9c625SLionel Sambuc /*
55284d9c625SLionel Sambuc  * msgq_status --
55384d9c625SLionel Sambuc  *	Report on the file's status.
55484d9c625SLionel Sambuc  *
55584d9c625SLionel Sambuc  * PUBLIC: void msgq_status __P((SCR *, db_recno_t, u_int));
55684d9c625SLionel Sambuc  */
55784d9c625SLionel Sambuc void
msgq_status(SCR * sp,db_recno_t lno,u_int flags)55884d9c625SLionel Sambuc msgq_status(SCR *sp, db_recno_t lno, u_int flags)
55984d9c625SLionel Sambuc {
56084d9c625SLionel Sambuc 	db_recno_t last;
56184d9c625SLionel Sambuc 	size_t blen, len;
56284d9c625SLionel Sambuc 	int cnt, needsep;
56384d9c625SLionel Sambuc 	const char *t;
56484d9c625SLionel Sambuc 	char **ap, *bp, *np, *p, *s;
56584d9c625SLionel Sambuc 
56684d9c625SLionel Sambuc 	/* Get sufficient memory. */
56784d9c625SLionel Sambuc 	len = strlen(sp->frp->name);
56884d9c625SLionel Sambuc 	GET_SPACE_GOTOC(sp, bp, blen, len * MAX_CHARACTER_COLUMNS + 128);
56984d9c625SLionel Sambuc 	p = bp;
57084d9c625SLionel Sambuc 
57184d9c625SLionel Sambuc 	/* Copy in the filename. */
57284d9c625SLionel Sambuc 	for (p = bp, t = sp->frp->name; *t != '\0'; ++t) {
57384d9c625SLionel Sambuc 		len = KEY_LEN(sp, *t);
57484d9c625SLionel Sambuc 		memcpy(p, KEY_NAME(sp, *t), len);
57584d9c625SLionel Sambuc 		p += len;
57684d9c625SLionel Sambuc 	}
57784d9c625SLionel Sambuc 	np = p;
57884d9c625SLionel Sambuc 	*p++ = ':';
57984d9c625SLionel Sambuc 	*p++ = ' ';
58084d9c625SLionel Sambuc 
58184d9c625SLionel Sambuc 	/* Copy in the argument count. */
58284d9c625SLionel Sambuc 	if (F_ISSET(sp, SC_STATUS_CNT) && sp->argv != NULL) {
58384d9c625SLionel Sambuc 		for (cnt = 0, ap = sp->argv; *ap != NULL; ++ap, ++cnt);
58484d9c625SLionel Sambuc 		if (cnt > 1) {
58584d9c625SLionel Sambuc 			(void)sprintf(p,
58684d9c625SLionel Sambuc 			    msg_cat(sp, "317|%d files to edit", NULL), cnt);
58784d9c625SLionel Sambuc 			p += strlen(p);
58884d9c625SLionel Sambuc 			*p++ = ':';
58984d9c625SLionel Sambuc 			*p++ = ' ';
59084d9c625SLionel Sambuc 		}
59184d9c625SLionel Sambuc 		F_CLR(sp, SC_STATUS_CNT);
59284d9c625SLionel Sambuc 	}
59384d9c625SLionel Sambuc 
59484d9c625SLionel Sambuc 	/*
59584d9c625SLionel Sambuc 	 * See nvi/exf.c:file_init() for a description of how and when the
59684d9c625SLionel Sambuc 	 * read-only bit is set.
59784d9c625SLionel Sambuc 	 *
59884d9c625SLionel Sambuc 	 * !!!
59984d9c625SLionel Sambuc 	 * The historic display for "name changed" was "[Not edited]".
60084d9c625SLionel Sambuc 	 */
60184d9c625SLionel Sambuc 	needsep = 0;
60284d9c625SLionel Sambuc 	if (F_ISSET(sp->frp, FR_NEWFILE)) {
60384d9c625SLionel Sambuc 		F_CLR(sp->frp, FR_NEWFILE);
60484d9c625SLionel Sambuc 		t = msg_cat(sp, "021|new file", &len);
60584d9c625SLionel Sambuc 		memcpy(p, t, len);
60684d9c625SLionel Sambuc 		p += len;
60784d9c625SLionel Sambuc 		needsep = 1;
60884d9c625SLionel Sambuc 	} else {
60984d9c625SLionel Sambuc 		if (F_ISSET(sp->frp, FR_NAMECHANGE)) {
61084d9c625SLionel Sambuc 			t = msg_cat(sp, "022|name changed", &len);
61184d9c625SLionel Sambuc 			memcpy(p, t, len);
61284d9c625SLionel Sambuc 			p += len;
61384d9c625SLionel Sambuc 			needsep = 1;
61484d9c625SLionel Sambuc 		}
61584d9c625SLionel Sambuc 		if (needsep) {
61684d9c625SLionel Sambuc 			*p++ = ',';
61784d9c625SLionel Sambuc 			*p++ = ' ';
61884d9c625SLionel Sambuc 		}
61984d9c625SLionel Sambuc 		if (F_ISSET(sp->ep, F_MODIFIED))
62084d9c625SLionel Sambuc 			t = msg_cat(sp, "023|modified", &len);
62184d9c625SLionel Sambuc 		else
62284d9c625SLionel Sambuc 			t = msg_cat(sp, "024|unmodified", &len);
62384d9c625SLionel Sambuc 		memcpy(p, t, len);
62484d9c625SLionel Sambuc 		p += len;
62584d9c625SLionel Sambuc 		needsep = 1;
62684d9c625SLionel Sambuc 	}
62784d9c625SLionel Sambuc 	if (F_ISSET(sp->frp, FR_UNLOCKED)) {
62884d9c625SLionel Sambuc 		if (needsep) {
62984d9c625SLionel Sambuc 			*p++ = ',';
63084d9c625SLionel Sambuc 			*p++ = ' ';
63184d9c625SLionel Sambuc 		}
63284d9c625SLionel Sambuc 		t = msg_cat(sp, "025|UNLOCKED", &len);
63384d9c625SLionel Sambuc 		memcpy(p, t, len);
63484d9c625SLionel Sambuc 		p += len;
63584d9c625SLionel Sambuc 		needsep = 1;
63684d9c625SLionel Sambuc 	}
63784d9c625SLionel Sambuc 	if (O_ISSET(sp, O_READONLY)) {
63884d9c625SLionel Sambuc 		if (needsep) {
63984d9c625SLionel Sambuc 			*p++ = ',';
64084d9c625SLionel Sambuc 			*p++ = ' ';
64184d9c625SLionel Sambuc 		}
64284d9c625SLionel Sambuc 		t = msg_cat(sp, "026|readonly", &len);
64384d9c625SLionel Sambuc 		memcpy(p, t, len);
64484d9c625SLionel Sambuc 		p += len;
64584d9c625SLionel Sambuc 		needsep = 1;
64684d9c625SLionel Sambuc 	}
64784d9c625SLionel Sambuc 	if (needsep) {
64884d9c625SLionel Sambuc 		*p++ = ':';
64984d9c625SLionel Sambuc 		*p++ = ' ';
65084d9c625SLionel Sambuc 	}
65184d9c625SLionel Sambuc 	if (LF_ISSET(MSTAT_SHOWLAST)) {
65284d9c625SLionel Sambuc 		if (db_last(sp, &last))
65384d9c625SLionel Sambuc 			return;
65484d9c625SLionel Sambuc 		if (last == 0) {
65584d9c625SLionel Sambuc 			t = msg_cat(sp, "028|empty file", &len);
65684d9c625SLionel Sambuc 			memcpy(p, t, len);
65784d9c625SLionel Sambuc 			p += len;
65884d9c625SLionel Sambuc 		} else {
65984d9c625SLionel Sambuc 			t = msg_cat(sp, "027|line %lu of %lu [%ld%%]", &len);
66084d9c625SLionel Sambuc 			(void)sprintf(p, t, lno, last, (lno * 100) / last);
66184d9c625SLionel Sambuc 			p += strlen(p);
66284d9c625SLionel Sambuc 		}
66384d9c625SLionel Sambuc 	} else {
66484d9c625SLionel Sambuc 		t = msg_cat(sp, "029|line %lu", &len);
66584d9c625SLionel Sambuc 		(void)sprintf(p, t, lno);
66684d9c625SLionel Sambuc 		p += strlen(p);
66784d9c625SLionel Sambuc 	}
66884d9c625SLionel Sambuc #ifdef DEBUG
66984d9c625SLionel Sambuc 	(void)sprintf(p, " (pid %lu)", (u_long)getpid());
67084d9c625SLionel Sambuc 	p += strlen(p);
67184d9c625SLionel Sambuc #endif
67284d9c625SLionel Sambuc 	*p++ = '\n';
67384d9c625SLionel Sambuc 	len = p - bp;
67484d9c625SLionel Sambuc 
67584d9c625SLionel Sambuc 	/*
67684d9c625SLionel Sambuc 	 * There's a nasty problem with long path names.  Cscope and tags files
67784d9c625SLionel Sambuc 	 * can result in long paths and vi will request a continuation key from
67884d9c625SLionel Sambuc 	 * the user as soon as it starts the screen.  Unfortunately, the user
67984d9c625SLionel Sambuc 	 * has already typed ahead, and chaos results.  If we assume that the
68084d9c625SLionel Sambuc 	 * characters in the filenames and informational messages only take a
68184d9c625SLionel Sambuc 	 * single screen column each, we can trim the filename.
68284d9c625SLionel Sambuc 	 *
68384d9c625SLionel Sambuc 	 * XXX
68484d9c625SLionel Sambuc 	 * Status lines get put up at fairly awkward times.  For example, when
68584d9c625SLionel Sambuc 	 * you do a filter read (e.g., :read ! echo foo) in the top screen of a
68684d9c625SLionel Sambuc 	 * split screen, we have to repaint the status lines for all the screens
68784d9c625SLionel Sambuc 	 * below the top screen.  We don't want users having to enter continue
68884d9c625SLionel Sambuc 	 * characters for those screens.  Make it really hard to screw this up.
68984d9c625SLionel Sambuc 	 */
69084d9c625SLionel Sambuc 	s = bp;
69184d9c625SLionel Sambuc 	if (LF_ISSET(MSTAT_TRUNCATE) && len > sp->cols) {
69284d9c625SLionel Sambuc 		for (; s < np && (*s != '/' || (size_t)(p - s) > sp->cols - 3); ++s);
69384d9c625SLionel Sambuc 		if (s == np) {
69484d9c625SLionel Sambuc 			s = p - (sp->cols - 5);
69584d9c625SLionel Sambuc 			*--s = ' ';
69684d9c625SLionel Sambuc 		}
69784d9c625SLionel Sambuc 		*--s = '.';
69884d9c625SLionel Sambuc 		*--s = '.';
69984d9c625SLionel Sambuc 		*--s = '.';
70084d9c625SLionel Sambuc 		len = p - s;
70184d9c625SLionel Sambuc 	}
70284d9c625SLionel Sambuc 
70384d9c625SLionel Sambuc 	/* Flush any waiting ex messages. */
70484d9c625SLionel Sambuc 	(void)ex_fflush(sp);
70584d9c625SLionel Sambuc 
70684d9c625SLionel Sambuc 	sp->wp->scr_msg(sp, M_INFO, s, len);
70784d9c625SLionel Sambuc 
70884d9c625SLionel Sambuc 	FREE_SPACE(sp, bp, blen);
70984d9c625SLionel Sambuc alloc_err:
71084d9c625SLionel Sambuc 	return;
71184d9c625SLionel Sambuc }
71284d9c625SLionel Sambuc 
71384d9c625SLionel Sambuc /*
71484d9c625SLionel Sambuc  * msg_open --
71584d9c625SLionel Sambuc  *	Open the message catalogs.
71684d9c625SLionel Sambuc  *
71784d9c625SLionel Sambuc  * PUBLIC: int msg_open __P((SCR *, const char *));
71884d9c625SLionel Sambuc  */
71984d9c625SLionel Sambuc int
msg_open(SCR * sp,const char * file)72084d9c625SLionel Sambuc msg_open(SCR *sp, const char *file)
72184d9c625SLionel Sambuc {
72284d9c625SLionel Sambuc 	/*
72384d9c625SLionel Sambuc 	 * !!!
72484d9c625SLionel Sambuc 	 * Assume that the first file opened is the system default, and that
72584d9c625SLionel Sambuc 	 * all subsequent ones user defined.  Only display error messages
72684d9c625SLionel Sambuc 	 * if we can't open the user defined ones -- it's useful to know if
72784d9c625SLionel Sambuc 	 * the system one wasn't there, but if nvi is being shipped with an
72884d9c625SLionel Sambuc 	 * installed system, the file will be there, if it's not, then the
72984d9c625SLionel Sambuc 	 * message will be repeated every time nvi is started up.
73084d9c625SLionel Sambuc 	 */
73184d9c625SLionel Sambuc 	static int first = 1;
73284d9c625SLionel Sambuc 	DB *db;
73384d9c625SLionel Sambuc 	DBT data, key;
73484d9c625SLionel Sambuc 	db_recno_t msgno;
73584d9c625SLionel Sambuc 	char buf[MAXPATHLEN];
73684d9c625SLionel Sambuc 	const char *p, *t;
73784d9c625SLionel Sambuc 
73884d9c625SLionel Sambuc 	if ((p = strrchr(file, '/')) != NULL && p[1] == '\0' &&
73984d9c625SLionel Sambuc 	    (((t = getenv("LC_MESSAGES")) != NULL && t[0] != '\0') ||
74084d9c625SLionel Sambuc 	    ((t = getenv("LANG")) != NULL && t[0] != '\0'))) {
74184d9c625SLionel Sambuc 		(void)snprintf(buf, sizeof(buf), "%s%s", file, t);
74284d9c625SLionel Sambuc 		p = buf;
74384d9c625SLionel Sambuc 	} else
74484d9c625SLionel Sambuc 		p = file;
74584d9c625SLionel Sambuc 	if (db_msg_open(sp, p, &db)) {
74684d9c625SLionel Sambuc 		if (first) {
74784d9c625SLionel Sambuc 			first = 0;
74884d9c625SLionel Sambuc 			return (1);
74984d9c625SLionel Sambuc 		}
75084d9c625SLionel Sambuc 		msgq_str(sp, M_DBERR, p, "%s");
75184d9c625SLionel Sambuc 		return (1);
75284d9c625SLionel Sambuc 	}
75384d9c625SLionel Sambuc 
75484d9c625SLionel Sambuc 	/*
75584d9c625SLionel Sambuc 	 * Test record 1 for the magic string.  The msgq call is here so
75684d9c625SLionel Sambuc 	 * the message catalog build finds it.
75784d9c625SLionel Sambuc 	 */
75884d9c625SLionel Sambuc #define	VMC	"VI_MESSAGE_CATALOG"
75984d9c625SLionel Sambuc 	memset(&key, 0, sizeof(key));
76084d9c625SLionel Sambuc 	key.data = &msgno;
76184d9c625SLionel Sambuc 	key.size = sizeof(db_recno_t);
76284d9c625SLionel Sambuc 	memset(&data, 0, sizeof(data));
76384d9c625SLionel Sambuc 	msgno = 1;
76484d9c625SLionel Sambuc 	if ((sp->db_error = db_get_low(db, &key, &data, 0)) != 0 ||
76584d9c625SLionel Sambuc 	    data.size != sizeof(VMC) - 1 ||
76684d9c625SLionel Sambuc 	    memcmp(data.data, VMC, sizeof(VMC) - 1)) {
76784d9c625SLionel Sambuc 		(void)db_close(db);
76884d9c625SLionel Sambuc 		if (first) {
76984d9c625SLionel Sambuc 			first = 0;
77084d9c625SLionel Sambuc 			return (1);
77184d9c625SLionel Sambuc 		}
77284d9c625SLionel Sambuc 		msgq_str(sp, M_DBERR, p,
77384d9c625SLionel Sambuc 		    "030|The file %s is not a message catalog");
77484d9c625SLionel Sambuc 		return (1);
77584d9c625SLionel Sambuc 	}
77684d9c625SLionel Sambuc 	first = 0;
77784d9c625SLionel Sambuc 
77884d9c625SLionel Sambuc 	if (sp->gp->msg != NULL)
77984d9c625SLionel Sambuc 		(void)db_close(sp->gp->msg);
78084d9c625SLionel Sambuc 	sp->gp->msg = db;
78184d9c625SLionel Sambuc 	return (0);
78284d9c625SLionel Sambuc }
78384d9c625SLionel Sambuc 
78484d9c625SLionel Sambuc /*
78584d9c625SLionel Sambuc  * msg_close --
78684d9c625SLionel Sambuc  *	Close the message catalogs.
78784d9c625SLionel Sambuc  *
78884d9c625SLionel Sambuc  * PUBLIC: void msg_close __P((GS *));
78984d9c625SLionel Sambuc  */
79084d9c625SLionel Sambuc void
msg_close(GS * gp)79184d9c625SLionel Sambuc msg_close(GS *gp)
79284d9c625SLionel Sambuc {
79384d9c625SLionel Sambuc 	if (gp->msg != NULL) {
79484d9c625SLionel Sambuc 		(void)db_close(gp->msg);
79584d9c625SLionel Sambuc 		gp->msg = NULL;
79684d9c625SLionel Sambuc 	}
79784d9c625SLionel Sambuc }
79884d9c625SLionel Sambuc 
79984d9c625SLionel Sambuc /*
80084d9c625SLionel Sambuc  * msg_cont --
80184d9c625SLionel Sambuc  *	Return common continuation messages.
80284d9c625SLionel Sambuc  *
80384d9c625SLionel Sambuc  * PUBLIC: const char *msg_cmsg __P((SCR *, cmsg_t, size_t *));
80484d9c625SLionel Sambuc  */
80584d9c625SLionel Sambuc const char *
msg_cmsg(SCR * sp,cmsg_t which,size_t * lenp)80684d9c625SLionel Sambuc msg_cmsg(SCR *sp, cmsg_t which, size_t *lenp)
80784d9c625SLionel Sambuc {
80884d9c625SLionel Sambuc 	switch (which) {
80984d9c625SLionel Sambuc 	case CMSG_CONF:
81084d9c625SLionel Sambuc 		return (msg_cat(sp, "268|confirm? [ynq]", lenp));
81184d9c625SLionel Sambuc 	case CMSG_CONT:
81284d9c625SLionel Sambuc 		return (msg_cat(sp, "269|Press any key to continue: ", lenp));
81384d9c625SLionel Sambuc 	case CMSG_CONT_EX:
81484d9c625SLionel Sambuc 		return (msg_cat(sp,
81584d9c625SLionel Sambuc 	    "270|Press any key to continue [: to enter more ex commands]: ",
81684d9c625SLionel Sambuc 		    lenp));
81784d9c625SLionel Sambuc 	case CMSG_CONT_R:
81884d9c625SLionel Sambuc 		return (msg_cat(sp, "161|Press Enter to continue: ", lenp));
81984d9c625SLionel Sambuc 	case CMSG_CONT_S:
82084d9c625SLionel Sambuc 		return (msg_cat(sp, "275| cont?", lenp));
82184d9c625SLionel Sambuc 	case CMSG_CONT_Q:
82284d9c625SLionel Sambuc 		return (msg_cat(sp,
82384d9c625SLionel Sambuc 		    "271|Press any key to continue [q to quit]: ", lenp));
82484d9c625SLionel Sambuc 	default:
82584d9c625SLionel Sambuc 		abort();
82684d9c625SLionel Sambuc 	}
82784d9c625SLionel Sambuc 	/* NOTREACHED */
82884d9c625SLionel Sambuc }
82984d9c625SLionel Sambuc 
83084d9c625SLionel Sambuc /*
83184d9c625SLionel Sambuc  * msg_cat --
83284d9c625SLionel Sambuc  *	Return a single message from the catalog, plus its length.
83384d9c625SLionel Sambuc  *
83484d9c625SLionel Sambuc  * !!!
83584d9c625SLionel Sambuc  * Only a single catalog message can be accessed at a time, if multiple
83684d9c625SLionel Sambuc  * ones are needed, they must be copied into local memory.
83784d9c625SLionel Sambuc  *
83884d9c625SLionel Sambuc  * PUBLIC: const char *msg_cat __P((SCR *, const char *, size_t *));
83984d9c625SLionel Sambuc  */
84084d9c625SLionel Sambuc const char *
msg_cat(SCR * sp,const char * str,size_t * lenp)84184d9c625SLionel Sambuc msg_cat(SCR *sp, const char *str, size_t *lenp)
84284d9c625SLionel Sambuc {
84384d9c625SLionel Sambuc 	GS *gp;
84484d9c625SLionel Sambuc 	DBT data, key;
84584d9c625SLionel Sambuc 	db_recno_t msgno;
84684d9c625SLionel Sambuc 
84784d9c625SLionel Sambuc 	/*
84884d9c625SLionel Sambuc 	 * If it's not a catalog message, i.e. has doesn't have a leading
84984d9c625SLionel Sambuc 	 * number and '|' symbol, we're done.
85084d9c625SLionel Sambuc 	 */
85184d9c625SLionel Sambuc 	if (isdigit((unsigned char)str[0]) &&
85284d9c625SLionel Sambuc 	    isdigit((unsigned char)str[1]) && isdigit((unsigned char)str[2]) &&
85384d9c625SLionel Sambuc 	    str[3] == '|') {
85484d9c625SLionel Sambuc 		memset(&key, 0, sizeof(key));
85584d9c625SLionel Sambuc 		key.data = &msgno;
85684d9c625SLionel Sambuc 		key.size = sizeof(db_recno_t);
85784d9c625SLionel Sambuc 		memset(&data, 0, sizeof(data));
85884d9c625SLionel Sambuc 		msgno = atoi(str);
85984d9c625SLionel Sambuc 
86084d9c625SLionel Sambuc 		/*
86184d9c625SLionel Sambuc 		 * XXX
86284d9c625SLionel Sambuc 		 * Really sleazy hack -- we put an extra character on the
86384d9c625SLionel Sambuc 		 * end of the format string, and then we change it to be
86484d9c625SLionel Sambuc 		 * the nul termination of the string.  There ought to be
86584d9c625SLionel Sambuc 		 * a better way.  Once we can allocate multiple temporary
86684d9c625SLionel Sambuc 		 * memory buffers, maybe we can use one of them instead.
86784d9c625SLionel Sambuc 		 */
86884d9c625SLionel Sambuc 		gp = sp == NULL ? NULL : sp->gp;
86984d9c625SLionel Sambuc 		if (gp != NULL && gp->msg != NULL &&
87084d9c625SLionel Sambuc 		    db_get_low(gp->msg, &key, &data, 0) == 0 &&
87184d9c625SLionel Sambuc 		    data.size != 0) {
87284d9c625SLionel Sambuc 			if (lenp != NULL)
87384d9c625SLionel Sambuc 				*lenp = data.size - 1;
87484d9c625SLionel Sambuc 			((char *)data.data)[data.size - 1] = '\0';
87584d9c625SLionel Sambuc 			return (data.data);
87684d9c625SLionel Sambuc 		}
87784d9c625SLionel Sambuc 		str = &str[4];
87884d9c625SLionel Sambuc 	}
87984d9c625SLionel Sambuc 	if (lenp != NULL)
88084d9c625SLionel Sambuc 		*lenp = strlen(str);
88184d9c625SLionel Sambuc 	return (str);
88284d9c625SLionel Sambuc }
88384d9c625SLionel Sambuc 
88484d9c625SLionel Sambuc /*
88584d9c625SLionel Sambuc  * msg_print --
88684d9c625SLionel Sambuc  *	Return a printable version of a string, in allocated memory.
88784d9c625SLionel Sambuc  *
88884d9c625SLionel Sambuc  * PUBLIC: char *msg_print __P((SCR *, const char *, int *));
88984d9c625SLionel Sambuc  */
89084d9c625SLionel Sambuc char *
msg_print(SCR * sp,const char * s,int * needfree)89184d9c625SLionel Sambuc msg_print(SCR *sp, const char *s, int *needfree)
89284d9c625SLionel Sambuc {
89384d9c625SLionel Sambuc 	size_t blen, nlen;
89484d9c625SLionel Sambuc 	const char *cp;
89584d9c625SLionel Sambuc 	char *bp, *ep, *p;
89684d9c625SLionel Sambuc 	unsigned char *t;
89784d9c625SLionel Sambuc 
89884d9c625SLionel Sambuc 	*needfree = 0;
89984d9c625SLionel Sambuc 
90084d9c625SLionel Sambuc 	for (cp = s; *cp != '\0'; ++cp)
90184d9c625SLionel Sambuc 		if (!isprint((unsigned char)*cp))
90284d9c625SLionel Sambuc 			break;
90384d9c625SLionel Sambuc 	if (*cp == '\0')
90484d9c625SLionel Sambuc 		return ((char *)__UNCONST(s));	/* SAFE: needfree set to 0. */
90584d9c625SLionel Sambuc 
90684d9c625SLionel Sambuc 	nlen = 0;
90784d9c625SLionel Sambuc 	if (0) {
90884d9c625SLionel Sambuc retry:		if (sp == NULL)
90984d9c625SLionel Sambuc 			free(bp);
91084d9c625SLionel Sambuc 		else
91184d9c625SLionel Sambuc 			FREE_SPACE(sp, bp, blen);
91284d9c625SLionel Sambuc 		*needfree = 0;
91384d9c625SLionel Sambuc 	}
91484d9c625SLionel Sambuc 	nlen += 256;
91584d9c625SLionel Sambuc 	if (sp == NULL) {
91684d9c625SLionel Sambuc 		if ((bp = malloc(nlen)) == NULL)
91784d9c625SLionel Sambuc 			goto alloc_err;
91884d9c625SLionel Sambuc 	} else
91984d9c625SLionel Sambuc 		GET_SPACE_GOTOC(sp, bp, blen, nlen);
92084d9c625SLionel Sambuc 	if (0) {
92184d9c625SLionel Sambuc alloc_err:	return __UNCONST("");
92284d9c625SLionel Sambuc 	}
92384d9c625SLionel Sambuc 	*needfree = 1;
92484d9c625SLionel Sambuc 
92584d9c625SLionel Sambuc 	for (p = bp, ep = (bp + blen) - 1, cp = s; *cp != '\0' && p < ep; ++cp)
92684d9c625SLionel Sambuc 		for (t = KEY_NAME(sp, *cp); *t != '\0' && p < ep; *p++ = *t++);
92784d9c625SLionel Sambuc 	if (p == ep)
92884d9c625SLionel Sambuc 		goto retry;
92984d9c625SLionel Sambuc 	*p = '\0';
93084d9c625SLionel Sambuc 	return (bp);
93184d9c625SLionel Sambuc }
932