Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_print.c
bloba30e9e2d5e5845521ffe3f87d0b2bc7b9f90d96b
1 /* $NetBSD: ex_print.c,v 1.1.1.2 2008/05/18 14:31:17 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: ex_print.c,v 10.24 2001/07/29 19:07:29 skimo Exp (Berkeley) Date: 2001/07/29 19:07:29";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <string.h>
27 #ifdef __STDC__
28 #include <stdarg.h>
29 #else
30 #include <varargs.h>
31 #endif
33 #include "../common/common.h"
35 static int ex_prchars __P((SCR *, const CHAR_T *, size_t *, size_t,
36 u_int, int));
39 * ex_list -- :[line [,line]] l[ist] [count] [flags]
41 * Display the addressed lines such that the output is unambiguous.
43 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
45 int
46 ex_list(SCR *sp, EXCMD *cmdp)
48 if (ex_print(sp, cmdp,
49 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_LIST))
50 return (1);
51 sp->lno = cmdp->addr2.lno;
52 sp->cno = cmdp->addr2.cno;
53 return (0);
57 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
59 * Display the addressed lines with a leading line number.
61 * PUBLIC: int ex_number __P((SCR *, EXCMD *));
63 int
64 ex_number(SCR *sp, EXCMD *cmdp)
66 if (ex_print(sp, cmdp,
67 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_HASH))
68 return (1);
69 sp->lno = cmdp->addr2.lno;
70 sp->cno = cmdp->addr2.cno;
71 return (0);
75 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
77 * Display the addressed lines.
79 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
81 int
82 ex_pr(SCR *sp, EXCMD *cmdp)
84 if (ex_print(sp, cmdp, &cmdp->addr1, &cmdp->addr2, cmdp->iflags))
85 return (1);
86 sp->lno = cmdp->addr2.lno;
87 sp->cno = cmdp->addr2.cno;
88 return (0);
92 * ex_print --
93 * Print the selected lines.
95 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
97 int
98 ex_print(SCR *sp, EXCMD *cmdp, MARK *fp, MARK *tp, u_int32_t flags)
100 GS *gp;
101 db_recno_t from, to;
102 size_t col, len;
103 const CHAR_T *p;
104 CHAR_T buf[10];
105 CHAR_T *q;
107 NEEDFILE(sp, cmdp);
109 gp = sp->gp;
110 for (from = fp->lno, to = tp->lno; from <= to; ++from) {
111 col = 0;
114 * Display the line number. The %6 format is specified
115 * by POSIX 1003.2, and is almost certainly large enough.
116 * Check, though, just in case.
118 if (LF_ISSET(E_C_HASH)) {
119 if (from <= 999999) {
120 SPRINTF(buf, SIZE(buf), L("%6ld "), from);
121 p = buf;
122 } else
123 p = L("TOOBIG ");
124 if (ex_prchars(sp, p, &col, 8, 0, 0))
125 return (1);
129 * Display the line. The format for E_C_PRINT isn't very good,
130 * especially in handling end-of-line tabs, but they're almost
131 * backward compatible.
133 if (db_get(sp, from, DBG_FATAL, &q, &len))
134 return (1);
135 p = q;
137 if (len == 0 && !LF_ISSET(E_C_LIST))
138 (void)ex_puts(sp, "\n");
139 else if (ex_ldisplay(sp, p, len, col, flags))
140 return (1);
142 if (INTERRUPTED(sp))
143 break;
145 return (0);
149 * ex_ldisplay --
150 * Display a line without any preceding number.
152 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
155 ex_ldisplay(SCR *sp, const CHAR_T *p, size_t len, size_t col, u_int flags)
157 if (len > 0 && ex_prchars(sp, p, &col, len, LF_ISSET(E_C_LIST), 0))
158 return (1);
159 if (!INTERRUPTED(sp) && LF_ISSET(E_C_LIST)) {
160 p = L("$");
161 if (ex_prchars(sp, p, &col, 1, LF_ISSET(E_C_LIST), 0))
162 return (1);
164 if (!INTERRUPTED(sp))
165 (void)ex_puts(sp, "\n");
166 return (0);
170 * ex_scprint --
171 * Display a line for the substitute with confirmation routine.
173 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
176 ex_scprint(SCR *sp, MARK *fp, MARK *tp)
178 const CHAR_T *p;
179 CHAR_T *q;
180 size_t col, len;
182 col = 0;
183 if (O_ISSET(sp, O_NUMBER)) {
184 p = L(" ");
185 if (ex_prchars(sp, p, &col, 8, 0, 0))
186 return (1);
189 if (db_get(sp, fp->lno, DBG_FATAL, &q, &len))
190 return (1);
191 p = q;
193 if (ex_prchars(sp, p, &col, fp->cno, 0, ' '))
194 return (1);
195 p += fp->cno;
196 if (ex_prchars(sp,
197 p, &col, tp->cno == fp->cno ? 1 : tp->cno - fp->cno, 0, '^'))
198 return (1);
199 if (INTERRUPTED(sp))
200 return (1);
201 p = L("[ynq]"); /* XXX: should be msg_cat. */
202 if (ex_prchars(sp, p, &col, 5, 0, 0))
203 return (1);
204 (void)ex_fflush(sp);
205 return (0);
209 * ex_prchars --
210 * Local routine to dump characters to the screen.
212 static int
213 ex_prchars(SCR *sp, const CHAR_T *p, size_t *colp, size_t len,
214 u_int flags, int repeatc)
216 CHAR_T ch;
217 const char *kp;
218 GS *gp;
219 size_t col, tlen, ts;
221 if (O_ISSET(sp, O_LIST))
222 LF_SET(E_C_LIST);
223 gp = sp->gp;
224 ts = O_VAL(sp, O_TABSTOP);
225 for (col = *colp; len--;)
226 if ((ch = *p++) == L('\t') && !LF_ISSET(E_C_LIST))
227 for (tlen = ts - col % ts;
228 col < sp->cols && tlen--; ++col) {
229 (void)ex_printf(sp,
230 "%c", repeatc ? repeatc : ' ');
231 if (INTERRUPTED(sp))
232 goto intr;
234 else {
235 /* XXXX */
236 if (INTISWIDE(ch)) {
237 CHAR_T str[2] = {0, 0};
238 str[0] = ch;
239 INT2CHAR(sp, str, 2, kp, tlen);
240 } else {
241 kp = (char *)KEY_NAME(sp, ch);
242 tlen = KEY_LEN(sp, ch);
244 if (!repeatc && col + tlen < sp->cols) {
245 (void)ex_puts(sp, kp);
246 col += tlen;
247 } else
248 for (; tlen--; ++kp, ++col) {
249 if (col == sp->cols) {
250 col = 0;
251 (void)ex_puts(sp, "\n");
253 (void)ex_printf(sp,
254 "%c", repeatc ? repeatc : *kp);
255 if (INTERRUPTED(sp))
256 goto intr;
259 intr: *colp = col;
260 return (0);
264 * ex_printf --
265 * Ex's version of printf.
267 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
270 #ifdef __STDC__
271 ex_printf(SCR *sp, const char *fmt, ...)
272 #else
273 ex_printf(sp, fmt, va_alist)
274 SCR *sp;
275 const char *fmt;
276 va_dcl
277 #endif
279 EX_PRIVATE *exp;
280 va_list ap;
281 size_t n;
283 exp = EXP(sp);
285 #ifdef __STDC__
286 va_start(ap, fmt);
287 #else
288 va_start(ap);
289 #endif
290 exp->obp_len += n = vsnprintf(exp->obp + exp->obp_len,
291 sizeof(exp->obp) - exp->obp_len, fmt, ap);
292 va_end(ap);
294 /* Flush when reach a <newline> or half the buffer. */
295 if (exp->obp[exp->obp_len - 1] == '\n' ||
296 exp->obp_len > sizeof(exp->obp) / 2)
297 (void)ex_fflush(sp);
298 return (n);
302 * ex_puts --
303 * Ex's version of puts.
305 * PUBLIC: int ex_puts __P((SCR *, const char *));
308 ex_puts(SCR *sp, const char *str)
310 EX_PRIVATE *exp;
311 int doflush, n;
313 exp = EXP(sp);
315 /* Flush when reach a <newline> or the end of the buffer. */
316 for (doflush = n = 0; *str != '\0'; ++n) {
317 if (exp->obp_len > sizeof(exp->obp))
318 (void)ex_fflush(sp);
319 if ((exp->obp[exp->obp_len++] = *str++) == '\n')
320 doflush = 1;
322 if (doflush)
323 (void)ex_fflush(sp);
324 return (n);
328 * ex_fflush --
329 * Ex's version of fflush.
331 * PUBLIC: int ex_fflush __P((SCR *sp));
334 ex_fflush(SCR *sp)
336 EX_PRIVATE *exp;
338 exp = EXP(sp);
340 if (exp->obp_len != 0) {
341 sp->wp->scr_msg(sp, M_NONE, exp->obp, exp->obp_len);
342 exp->obp_len = 0;
344 return (0);