xref: /dragonfly/contrib/nvi2/ex/ex_print.c (revision b1ac2ebb)
1e0b8e63eSJohn Marino /*-
2e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994
3e0b8e63eSJohn Marino  *	The Regents of the University of California.  All rights reserved.
4e0b8e63eSJohn Marino  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5e0b8e63eSJohn Marino  *	Keith Bostic.  All rights reserved.
6e0b8e63eSJohn Marino  *
7e0b8e63eSJohn Marino  * See the LICENSE file for redistribution information.
8e0b8e63eSJohn Marino  */
9e0b8e63eSJohn Marino 
10e0b8e63eSJohn Marino #include "config.h"
11e0b8e63eSJohn Marino 
12e0b8e63eSJohn Marino #include <sys/types.h>
13e0b8e63eSJohn Marino #include <sys/queue.h>
14e0b8e63eSJohn Marino #include <sys/time.h>
15e0b8e63eSJohn Marino 
16e0b8e63eSJohn Marino #include <bitstring.h>
17e0b8e63eSJohn Marino #include <ctype.h>
18e0b8e63eSJohn Marino #include <limits.h>
19e0b8e63eSJohn Marino #include <stdarg.h>
20e0b8e63eSJohn Marino #include <stdio.h>
21e0b8e63eSJohn Marino #include <string.h>
22e0b8e63eSJohn Marino 
23e0b8e63eSJohn Marino #include "../common/common.h"
24e0b8e63eSJohn Marino 
25e0b8e63eSJohn Marino static int ex_prchars(SCR *,
26e0b8e63eSJohn Marino     const CHAR_T *, size_t *, size_t, u_int, int);
27e0b8e63eSJohn Marino 
28e0b8e63eSJohn Marino /*
29e0b8e63eSJohn Marino  * ex_list -- :[line [,line]] l[ist] [count] [flags]
30e0b8e63eSJohn Marino  *
31e0b8e63eSJohn Marino  *	Display the addressed lines such that the output is unambiguous.
32e0b8e63eSJohn Marino  *
33e0b8e63eSJohn Marino  * PUBLIC: int ex_list(SCR *, EXCMD *);
34e0b8e63eSJohn Marino  */
35e0b8e63eSJohn Marino int
ex_list(SCR * sp,EXCMD * cmdp)36e0b8e63eSJohn Marino ex_list(SCR *sp, EXCMD *cmdp)
37e0b8e63eSJohn Marino {
38e0b8e63eSJohn Marino 	if (ex_print(sp, cmdp,
39e0b8e63eSJohn Marino 	    &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_LIST))
40e0b8e63eSJohn Marino 		return (1);
41e0b8e63eSJohn Marino 	sp->lno = cmdp->addr2.lno;
42e0b8e63eSJohn Marino 	sp->cno = cmdp->addr2.cno;
43e0b8e63eSJohn Marino 	return (0);
44e0b8e63eSJohn Marino }
45e0b8e63eSJohn Marino 
46e0b8e63eSJohn Marino /*
47e0b8e63eSJohn Marino  * ex_number -- :[line [,line]] nu[mber] [count] [flags]
48e0b8e63eSJohn Marino  *
49e0b8e63eSJohn Marino  *	Display the addressed lines with a leading line number.
50e0b8e63eSJohn Marino  *
51e0b8e63eSJohn Marino  * PUBLIC: int ex_number(SCR *, EXCMD *);
52e0b8e63eSJohn Marino  */
53e0b8e63eSJohn Marino int
ex_number(SCR * sp,EXCMD * cmdp)54e0b8e63eSJohn Marino ex_number(SCR *sp, EXCMD *cmdp)
55e0b8e63eSJohn Marino {
56e0b8e63eSJohn Marino 	if (ex_print(sp, cmdp,
57e0b8e63eSJohn Marino 	    &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_HASH))
58e0b8e63eSJohn Marino 		return (1);
59e0b8e63eSJohn Marino 	sp->lno = cmdp->addr2.lno;
60e0b8e63eSJohn Marino 	sp->cno = cmdp->addr2.cno;
61e0b8e63eSJohn Marino 	return (0);
62e0b8e63eSJohn Marino }
63e0b8e63eSJohn Marino 
64e0b8e63eSJohn Marino /*
65e0b8e63eSJohn Marino  * ex_pr -- :[line [,line]] p[rint] [count] [flags]
66e0b8e63eSJohn Marino  *
67e0b8e63eSJohn Marino  *	Display the addressed lines.
68e0b8e63eSJohn Marino  *
69e0b8e63eSJohn Marino  * PUBLIC: int ex_pr(SCR *, EXCMD *);
70e0b8e63eSJohn Marino  */
71e0b8e63eSJohn Marino int
ex_pr(SCR * sp,EXCMD * cmdp)72e0b8e63eSJohn Marino ex_pr(SCR *sp, EXCMD *cmdp)
73e0b8e63eSJohn Marino {
74e0b8e63eSJohn Marino 	if (ex_print(sp, cmdp, &cmdp->addr1, &cmdp->addr2, cmdp->iflags))
75e0b8e63eSJohn Marino 		return (1);
76e0b8e63eSJohn Marino 	sp->lno = cmdp->addr2.lno;
77e0b8e63eSJohn Marino 	sp->cno = cmdp->addr2.cno;
78e0b8e63eSJohn Marino 	return (0);
79e0b8e63eSJohn Marino }
80e0b8e63eSJohn Marino 
81e0b8e63eSJohn Marino /*
82e0b8e63eSJohn Marino  * ex_print --
83e0b8e63eSJohn Marino  *	Print the selected lines.
84e0b8e63eSJohn Marino  *
85e0b8e63eSJohn Marino  * PUBLIC: int ex_print(SCR *, EXCMD *, MARK *, MARK *, u_int32_t);
86e0b8e63eSJohn Marino  */
87e0b8e63eSJohn Marino int
ex_print(SCR * sp,EXCMD * cmdp,MARK * fp,MARK * tp,u_int32_t flags)88e0b8e63eSJohn Marino ex_print(SCR *sp, EXCMD *cmdp, MARK *fp, MARK *tp, u_int32_t flags)
89e0b8e63eSJohn Marino {
90e0b8e63eSJohn Marino 	GS *gp;
91e0b8e63eSJohn Marino 	recno_t from, to;
92e0b8e63eSJohn Marino 	size_t col, len;
93e0b8e63eSJohn Marino 	CHAR_T *p;
94e0b8e63eSJohn Marino 	CHAR_T buf[10];
95e0b8e63eSJohn Marino 
96e0b8e63eSJohn Marino 	NEEDFILE(sp, cmdp);
97e0b8e63eSJohn Marino 
98e0b8e63eSJohn Marino 	gp = sp->gp;
99e0b8e63eSJohn Marino 	for (from = fp->lno, to = tp->lno; from <= to; ++from) {
100e0b8e63eSJohn Marino 		col = 0;
101e0b8e63eSJohn Marino 
102e0b8e63eSJohn Marino 		/*
103e0b8e63eSJohn Marino 		 * Display the line number.  The %6 format is specified
104e0b8e63eSJohn Marino 		 * by POSIX 1003.2, and is almost certainly large enough.
105e0b8e63eSJohn Marino 		 * Check, though, just in case.
106e0b8e63eSJohn Marino 		 */
107e0b8e63eSJohn Marino 		if (LF_ISSET(E_C_HASH)) {
108e0b8e63eSJohn Marino 			if (from <= 999999) {
109e0b8e63eSJohn Marino 				SPRINTF(buf, SIZE(buf), L("%6u  "), from);
110e0b8e63eSJohn Marino 				p = buf;
111e0b8e63eSJohn Marino 			} else
112e0b8e63eSJohn Marino 				p = L("TOOBIG  ");
113e0b8e63eSJohn Marino 			if (ex_prchars(sp, p, &col, 8, 0, 0))
114e0b8e63eSJohn Marino 				return (1);
115e0b8e63eSJohn Marino 		}
116e0b8e63eSJohn Marino 
117e0b8e63eSJohn Marino 		/*
118e0b8e63eSJohn Marino 		 * Display the line.  The format for E_C_PRINT isn't very good,
119e0b8e63eSJohn Marino 		 * especially in handling end-of-line tabs, but they're almost
120e0b8e63eSJohn Marino 		 * backward compatible.
121e0b8e63eSJohn Marino 		 */
122e0b8e63eSJohn Marino 		if (db_get(sp, from, DBG_FATAL, &p, &len))
123e0b8e63eSJohn Marino 			return (1);
124e0b8e63eSJohn Marino 
125e0b8e63eSJohn Marino 		if (len == 0 && !LF_ISSET(E_C_LIST))
126e0b8e63eSJohn Marino 			(void)ex_puts(sp, "\n");
127e0b8e63eSJohn Marino 		else if (ex_ldisplay(sp, p, len, col, flags))
128e0b8e63eSJohn Marino 			return (1);
129e0b8e63eSJohn Marino 
130e0b8e63eSJohn Marino 		if (INTERRUPTED(sp))
131e0b8e63eSJohn Marino 			break;
132e0b8e63eSJohn Marino 	}
133e0b8e63eSJohn Marino 	return (0);
134e0b8e63eSJohn Marino }
135e0b8e63eSJohn Marino 
136e0b8e63eSJohn Marino /*
137e0b8e63eSJohn Marino  * ex_ldisplay --
138e0b8e63eSJohn Marino  *	Display a line without any preceding number.
139e0b8e63eSJohn Marino  *
140e0b8e63eSJohn Marino  * PUBLIC: int ex_ldisplay(SCR *, const CHAR_T *, size_t, size_t, u_int);
141e0b8e63eSJohn Marino  */
142e0b8e63eSJohn Marino int
ex_ldisplay(SCR * sp,const CHAR_T * p,size_t len,size_t col,u_int flags)143e0b8e63eSJohn Marino ex_ldisplay(SCR *sp, const CHAR_T *p, size_t len, size_t col, u_int flags)
144e0b8e63eSJohn Marino {
145e0b8e63eSJohn Marino 	if (len > 0 && ex_prchars(sp, p, &col, len, LF_ISSET(E_C_LIST), 0))
146e0b8e63eSJohn Marino 		return (1);
147e0b8e63eSJohn Marino 	if (!INTERRUPTED(sp) && LF_ISSET(E_C_LIST)) {
148e0b8e63eSJohn Marino 		p = L("$");
149e0b8e63eSJohn Marino 		if (ex_prchars(sp, p, &col, 1, LF_ISSET(E_C_LIST), 0))
150e0b8e63eSJohn Marino 			return (1);
151e0b8e63eSJohn Marino 	}
152e0b8e63eSJohn Marino 	if (!INTERRUPTED(sp))
153e0b8e63eSJohn Marino 		(void)ex_puts(sp, "\n");
154e0b8e63eSJohn Marino 	return (0);
155e0b8e63eSJohn Marino }
156e0b8e63eSJohn Marino 
157e0b8e63eSJohn Marino /*
158e0b8e63eSJohn Marino  * ex_scprint --
159e0b8e63eSJohn Marino  *	Display a line for the substitute with confirmation routine.
160e0b8e63eSJohn Marino  *
161e0b8e63eSJohn Marino  * PUBLIC: int ex_scprint(SCR *, MARK *, MARK *);
162e0b8e63eSJohn Marino  */
163e0b8e63eSJohn Marino int
ex_scprint(SCR * sp,MARK * fp,MARK * tp)164e0b8e63eSJohn Marino ex_scprint(SCR *sp, MARK *fp, MARK *tp)
165e0b8e63eSJohn Marino {
166e0b8e63eSJohn Marino 	CHAR_T *p;
167e0b8e63eSJohn Marino 	size_t col, len;
168e0b8e63eSJohn Marino 
169e0b8e63eSJohn Marino 	col = 0;
170e0b8e63eSJohn Marino 	if (O_ISSET(sp, O_NUMBER)) {
171e0b8e63eSJohn Marino 		p = L("        ");
172e0b8e63eSJohn Marino 		if (ex_prchars(sp, p, &col, 8, 0, 0))
173e0b8e63eSJohn Marino 			return (1);
174e0b8e63eSJohn Marino 	}
175e0b8e63eSJohn Marino 
176e0b8e63eSJohn Marino 	if (db_get(sp, fp->lno, DBG_FATAL, &p, &len))
177e0b8e63eSJohn Marino 		return (1);
178e0b8e63eSJohn Marino 
179e0b8e63eSJohn Marino 	if (ex_prchars(sp, p, &col, fp->cno, 0, ' '))
180e0b8e63eSJohn Marino 		return (1);
181e0b8e63eSJohn Marino 	p += fp->cno;
182e0b8e63eSJohn Marino 	if (ex_prchars(sp,
183e0b8e63eSJohn Marino 	    p, &col, tp->cno == fp->cno ? 1 : tp->cno - fp->cno, 0, '^'))
184e0b8e63eSJohn Marino 		return (1);
185e0b8e63eSJohn Marino 	if (INTERRUPTED(sp))
186e0b8e63eSJohn Marino 		return (1);
187e0b8e63eSJohn Marino 	p = L("[ynq]");		/* XXX: should be msg_cat. */
188e0b8e63eSJohn Marino 	if (ex_prchars(sp, p, &col, 5, 0, 0))
189e0b8e63eSJohn Marino 		return (1);
190e0b8e63eSJohn Marino 	(void)ex_fflush(sp);
191e0b8e63eSJohn Marino 	return (0);
192e0b8e63eSJohn Marino }
193e0b8e63eSJohn Marino 
194e0b8e63eSJohn Marino /*
195e0b8e63eSJohn Marino  * ex_prchars --
196e0b8e63eSJohn Marino  *	Local routine to dump characters to the screen.
197e0b8e63eSJohn Marino  */
198e0b8e63eSJohn Marino static int
ex_prchars(SCR * sp,const CHAR_T * p,size_t * colp,size_t len,u_int flags,int repeatc)199e0b8e63eSJohn Marino ex_prchars(SCR *sp, const CHAR_T *p, size_t *colp, size_t len,
200e0b8e63eSJohn Marino 	    u_int flags, int repeatc)
201e0b8e63eSJohn Marino {
202e0b8e63eSJohn Marino 	CHAR_T ch;
203e0b8e63eSJohn Marino 	char *kp;
204e0b8e63eSJohn Marino 	GS *gp;
205e0b8e63eSJohn Marino 	size_t col, tlen, ts;
206e0b8e63eSJohn Marino 
207e0b8e63eSJohn Marino 	if (O_ISSET(sp, O_LIST))
208e0b8e63eSJohn Marino 		LF_SET(E_C_LIST);
209e0b8e63eSJohn Marino 	gp = sp->gp;
210e0b8e63eSJohn Marino 	ts = O_VAL(sp, O_TABSTOP);
211e0b8e63eSJohn Marino 	for (col = *colp; len--;)
212*b1ac2ebbSDaniel Fojt 		if ((ch = *p++) == '\t' && !LF_ISSET(E_C_LIST))
213e0b8e63eSJohn Marino 			for (tlen = ts - col % ts;
214e0b8e63eSJohn Marino 			    col < sp->cols && tlen--; ++col) {
215e0b8e63eSJohn Marino 				(void)ex_printf(sp,
216e0b8e63eSJohn Marino 				    "%c", repeatc ? repeatc : ' ');
217e0b8e63eSJohn Marino 				if (INTERRUPTED(sp))
218e0b8e63eSJohn Marino 					goto intr;
219e0b8e63eSJohn Marino 			}
220e0b8e63eSJohn Marino 		else {
221e0b8e63eSJohn Marino 			kp = KEY_NAME(sp, ch);
222e0b8e63eSJohn Marino 			tlen = KEY_COL(sp, ch);
223e0b8e63eSJohn Marino 
224e0b8e63eSJohn Marino 			/*
225e0b8e63eSJohn Marino 			 * Start a new line if the last character does not fit
226e0b8e63eSJohn Marino 			 * into the current line.  The implicit new lines are
227e0b8e63eSJohn Marino 			 * not interruptible.
228e0b8e63eSJohn Marino 			 */
229e0b8e63eSJohn Marino 			if (col + tlen > sp->cols) {
230e0b8e63eSJohn Marino 				col = 0;
231e0b8e63eSJohn Marino 				(void)ex_puts(sp, "\n");
232e0b8e63eSJohn Marino 			}
233e0b8e63eSJohn Marino 
234e0b8e63eSJohn Marino 			col += tlen;
235e0b8e63eSJohn Marino 			if (!repeatc) {
236e0b8e63eSJohn Marino 				(void)ex_puts(sp, kp);
237e0b8e63eSJohn Marino 				if (INTERRUPTED(sp))
238e0b8e63eSJohn Marino 					goto intr;
239e0b8e63eSJohn Marino 			} else while (tlen--) {
240e0b8e63eSJohn Marino 				(void)ex_printf(sp, "%c", repeatc);
241e0b8e63eSJohn Marino 				if (INTERRUPTED(sp))
242e0b8e63eSJohn Marino 					goto intr;
243e0b8e63eSJohn Marino 			}
244e0b8e63eSJohn Marino 			if (col == sp->cols) {
245e0b8e63eSJohn Marino 				col = 0;
246e0b8e63eSJohn Marino 				(void)ex_puts(sp, "\n");
247e0b8e63eSJohn Marino 			}
248e0b8e63eSJohn Marino 		}
249e0b8e63eSJohn Marino intr:	*colp = col;
250e0b8e63eSJohn Marino 	return (0);
251e0b8e63eSJohn Marino }
252e0b8e63eSJohn Marino 
253e0b8e63eSJohn Marino /*
254e0b8e63eSJohn Marino  * ex_printf --
255e0b8e63eSJohn Marino  *	Ex's version of printf.
256e0b8e63eSJohn Marino  *
257e0b8e63eSJohn Marino  * PUBLIC: int ex_printf(SCR *, const char *, ...);
258e0b8e63eSJohn Marino  */
259e0b8e63eSJohn Marino int
ex_printf(SCR * sp,const char * fmt,...)260e0b8e63eSJohn Marino ex_printf(
261e0b8e63eSJohn Marino 	SCR *sp,
262e0b8e63eSJohn Marino 	const char *fmt,
263e0b8e63eSJohn Marino 	...)
264e0b8e63eSJohn Marino {
265e0b8e63eSJohn Marino 	EX_PRIVATE *exp;
266e0b8e63eSJohn Marino 	va_list ap;
267e0b8e63eSJohn Marino 	size_t n;
268e0b8e63eSJohn Marino 
269e0b8e63eSJohn Marino 	exp = EXP(sp);
270e0b8e63eSJohn Marino 
271e0b8e63eSJohn Marino 	va_start(ap, fmt);
272e0b8e63eSJohn Marino 	exp->obp_len += n = vsnprintf(exp->obp + exp->obp_len,
273e0b8e63eSJohn Marino 	    sizeof(exp->obp) - exp->obp_len, fmt, ap);
274e0b8e63eSJohn Marino 	va_end(ap);
275e0b8e63eSJohn Marino 
276e0b8e63eSJohn Marino 	/* Flush when reach a <newline> or half the buffer. */
277e0b8e63eSJohn Marino 	if (exp->obp[exp->obp_len - 1] == '\n' ||
278e0b8e63eSJohn Marino 	    exp->obp_len > sizeof(exp->obp) / 2)
279e0b8e63eSJohn Marino 		(void)ex_fflush(sp);
280e0b8e63eSJohn Marino 	return (n);
281e0b8e63eSJohn Marino }
282e0b8e63eSJohn Marino 
283e0b8e63eSJohn Marino /*
284e0b8e63eSJohn Marino  * ex_puts --
285e0b8e63eSJohn Marino  *	Ex's version of puts.
286e0b8e63eSJohn Marino  *
287e0b8e63eSJohn Marino  * PUBLIC: int ex_puts(SCR *, const char *);
288e0b8e63eSJohn Marino  */
289e0b8e63eSJohn Marino int
ex_puts(SCR * sp,const char * str)290e0b8e63eSJohn Marino ex_puts(SCR *sp, const char *str)
291e0b8e63eSJohn Marino {
292e0b8e63eSJohn Marino 	EX_PRIVATE *exp;
293e0b8e63eSJohn Marino 	int doflush, n;
294e0b8e63eSJohn Marino 
295e0b8e63eSJohn Marino 	exp = EXP(sp);
296e0b8e63eSJohn Marino 
297e0b8e63eSJohn Marino 	/* Flush when reach a <newline> or the end of the buffer. */
298e0b8e63eSJohn Marino 	for (doflush = n = 0; *str != '\0'; ++n) {
299e0b8e63eSJohn Marino 		if (exp->obp_len > sizeof(exp->obp))
300e0b8e63eSJohn Marino 			(void)ex_fflush(sp);
301e0b8e63eSJohn Marino 		if ((exp->obp[exp->obp_len++] = *str++) == '\n')
302e0b8e63eSJohn Marino 			doflush = 1;
303e0b8e63eSJohn Marino 	}
304e0b8e63eSJohn Marino 	if (doflush)
305e0b8e63eSJohn Marino 		(void)ex_fflush(sp);
306e0b8e63eSJohn Marino 	return (n);
307e0b8e63eSJohn Marino }
308e0b8e63eSJohn Marino 
309e0b8e63eSJohn Marino /*
310e0b8e63eSJohn Marino  * ex_fflush --
311e0b8e63eSJohn Marino  *	Ex's version of fflush.
312e0b8e63eSJohn Marino  *
313e0b8e63eSJohn Marino  * PUBLIC: int ex_fflush(SCR *sp);
314e0b8e63eSJohn Marino  */
315e0b8e63eSJohn Marino int
ex_fflush(SCR * sp)316e0b8e63eSJohn Marino ex_fflush(SCR *sp)
317e0b8e63eSJohn Marino {
318e0b8e63eSJohn Marino 	EX_PRIVATE *exp;
319e0b8e63eSJohn Marino 
320e0b8e63eSJohn Marino 	exp = EXP(sp);
321e0b8e63eSJohn Marino 
322e0b8e63eSJohn Marino 	if (exp->obp_len != 0) {
323e0b8e63eSJohn Marino 		sp->gp->scr_msg(sp, M_NONE, exp->obp, exp->obp_len);
324e0b8e63eSJohn Marino 		exp->obp_len = 0;
325e0b8e63eSJohn Marino 	}
326e0b8e63eSJohn Marino 	return (0);
327e0b8e63eSJohn Marino }
328