more CHAR_T changes
[nvi.git] / ex / ex_print.c
blobcd11fc606c8dd3b8e0abd731a58373a53f9f2378
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_print.c,v 10.22 2001/06/17 14:16:51 skimo Exp $ (Berkeley) $Date: 2001/06/17 14:16:51 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <ctype.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <string.h>
25 #ifdef __STDC__
26 #include <stdarg.h>
27 #else
28 #include <varargs.h>
29 #endif
31 #include "../common/common.h"
33 static int ex_prchars __P((SCR *, const CHAR_T *, size_t *, size_t,
34 u_int, int));
37 * ex_list -- :[line [,line]] l[ist] [count] [flags]
39 * Display the addressed lines such that the output is unambiguous.
41 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
43 int
44 ex_list(sp, cmdp)
45 SCR *sp;
46 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(sp, cmdp)
65 SCR *sp;
66 EXCMD *cmdp;
68 if (ex_print(sp, cmdp,
69 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_HASH))
70 return (1);
71 sp->lno = cmdp->addr2.lno;
72 sp->cno = cmdp->addr2.cno;
73 return (0);
77 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
79 * Display the addressed lines.
81 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
83 int
84 ex_pr(sp, cmdp)
85 SCR *sp;
86 EXCMD *cmdp;
88 if (ex_print(sp, cmdp, &cmdp->addr1, &cmdp->addr2, cmdp->iflags))
89 return (1);
90 sp->lno = cmdp->addr2.lno;
91 sp->cno = cmdp->addr2.cno;
92 return (0);
96 * ex_print --
97 * Print the selected lines.
99 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
102 ex_print(sp, cmdp, fp, tp, flags)
103 SCR *sp;
104 EXCMD *cmdp;
105 MARK *fp, *tp;
106 u_int32_t flags;
108 GS *gp;
109 db_recno_t from, to;
110 size_t col, len;
111 CHAR_T *p;
112 CHAR_T buf[10];
113 CHAR_T *wp;
114 size_t wlen;
116 NEEDFILE(sp, cmdp);
118 gp = sp->gp;
119 for (from = fp->lno, to = tp->lno; from <= to; ++from) {
120 col = 0;
123 * Display the line number. The %6 format is specified
124 * by POSIX 1003.2, and is almost certainly large enough.
125 * Check, though, just in case.
127 if (LF_ISSET(E_C_HASH)) {
128 if (from <= 999999) {
129 SPRINTF(buf, SIZE(buf), L("%6ld "), from);
130 p = buf;
131 } else
132 p = L("TOOBIG ");
133 if (ex_prchars(sp, p, &col, 8, 0, 0))
134 return (1);
138 * Display the line. The format for E_C_PRINT isn't very good,
139 * especially in handling end-of-line tabs, but they're almost
140 * backward compatible.
142 if (db_get(sp, from, DBG_FATAL, &p, &len))
143 return (1);
145 if (len == 0 && !LF_ISSET(E_C_LIST))
146 (void)ex_puts(sp, "\n");
147 else if (ex_ldisplay(sp, p, len, col, flags))
148 return (1);
150 if (INTERRUPTED(sp))
151 break;
153 return (0);
157 * ex_ldisplay --
158 * Display a line without any preceding number.
160 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
163 ex_ldisplay(SCR *sp, const CHAR_T *p, size_t len, size_t col, u_int flags)
165 if (len > 0 && ex_prchars(sp, p, &col, len, LF_ISSET(E_C_LIST), 0))
166 return (1);
167 if (!INTERRUPTED(sp) && LF_ISSET(E_C_LIST)) {
168 p = L("$");
169 if (ex_prchars(sp, p, &col, 1, LF_ISSET(E_C_LIST), 0))
170 return (1);
172 if (!INTERRUPTED(sp))
173 (void)ex_puts(sp, "\n");
174 return (0);
178 * ex_scprint --
179 * Display a line for the substitute with confirmation routine.
181 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
184 ex_scprint(sp, fp, tp)
185 SCR *sp;
186 MARK *fp, *tp;
188 CHAR_T *p;
189 size_t col, len;
191 col = 0;
192 if (O_ISSET(sp, O_NUMBER)) {
193 p = L(" ");
194 if (ex_prchars(sp, p, &col, 8, 0, 0))
195 return (1);
198 if (db_get(sp, fp->lno, DBG_FATAL, &p, &len))
199 return (1);
201 if (ex_prchars(sp, p, &col, fp->cno, 0, ' '))
202 return (1);
203 p += fp->cno;
204 if (ex_prchars(sp,
205 p, &col, tp->cno == fp->cno ? 1 : tp->cno - fp->cno, 0, '^'))
206 return (1);
207 if (INTERRUPTED(sp))
208 return (1);
209 p = L("[ynq]"); /* XXX: should be msg_cat. */
210 if (ex_prchars(sp, p, &col, 5, 0, 0))
211 return (1);
212 (void)ex_fflush(sp);
213 return (0);
217 * ex_prchars --
218 * Local routine to dump characters to the screen.
220 static int
221 ex_prchars(SCR *sp, const CHAR_T *p, size_t *colp, size_t len,
222 u_int flags, int repeatc)
224 CHAR_T ch;
225 char *kp;
226 GS *gp;
227 size_t col, tlen, ts;
229 if (O_ISSET(sp, O_LIST))
230 LF_SET(E_C_LIST);
231 gp = sp->gp;
232 ts = O_VAL(sp, O_TABSTOP);
233 for (col = *colp; len--;)
234 if ((ch = *p++) == L('\t') && !LF_ISSET(E_C_LIST))
235 for (tlen = ts - col % ts;
236 col < sp->cols && tlen--; ++col) {
237 (void)ex_printf(sp,
238 "%c", repeatc ? repeatc : ' ');
239 if (INTERRUPTED(sp))
240 goto intr;
242 else {
243 /* XXXX */
244 if (INTISWIDE(ch)) {
245 CHAR_T str[2] = {0, 0};
246 str[0] = ch;
247 INT2CHAR(sp, str, 2, kp, tlen);
248 } else {
249 kp = KEY_NAME(sp, ch);
250 tlen = KEY_LEN(sp, ch);
252 if (!repeatc && col + tlen < sp->cols) {
253 (void)ex_puts(sp, kp);
254 col += tlen;
255 } else
256 for (; tlen--; ++kp, ++col) {
257 if (col == sp->cols) {
258 col = 0;
259 (void)ex_puts(sp, "\n");
261 (void)ex_printf(sp,
262 "%c", repeatc ? repeatc : *kp);
263 if (INTERRUPTED(sp))
264 goto intr;
267 intr: *colp = col;
268 return (0);
272 * ex_printf --
273 * Ex's version of printf.
275 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
278 #ifdef __STDC__
279 ex_printf(SCR *sp, const char *fmt, ...)
280 #else
281 ex_printf(sp, fmt, va_alist)
282 SCR *sp;
283 const char *fmt;
284 va_dcl
285 #endif
287 EX_PRIVATE *exp;
288 va_list ap;
289 size_t n;
290 CHAR_T *kp;
292 exp = EXP(sp);
294 #ifdef __STDC__
295 va_start(ap, fmt);
296 #else
297 va_start(ap);
298 #endif
299 exp->obp_len += n = vsnprintf(exp->obp + exp->obp_len,
300 sizeof(exp->obp) - exp->obp_len, fmt, ap);
301 va_end(ap);
303 /* Flush when reach a <newline> or half the buffer. */
304 if (exp->obp[exp->obp_len - 1] == '\n' ||
305 exp->obp_len > sizeof(exp->obp) / 2)
306 (void)ex_fflush(sp);
307 return (n);
311 * ex_puts --
312 * Ex's version of puts.
314 * PUBLIC: int ex_puts __P((SCR *, const char *));
317 ex_puts(sp, str)
318 SCR *sp;
319 const char *str;
321 EX_PRIVATE *exp;
322 int doflush, n;
324 exp = EXP(sp);
326 /* Flush when reach a <newline> or the end of the buffer. */
327 for (doflush = n = 0; *str != '\0'; ++n) {
328 if (exp->obp_len > sizeof(exp->obp))
329 (void)ex_fflush(sp);
330 if ((exp->obp[exp->obp_len++] = *str++) == '\n')
331 doflush = 1;
333 if (doflush)
334 (void)ex_fflush(sp);
335 return (n);
339 * ex_fflush --
340 * Ex's version of fflush.
342 * PUBLIC: int ex_fflush __P((SCR *sp));
345 ex_fflush(sp)
346 SCR *sp;
348 EX_PRIVATE *exp;
350 exp = EXP(sp);
352 if (exp->obp_len != 0) {
353 sp->gp->scr_msg(sp, M_NONE, exp->obp, exp->obp_len);
354 exp->obp_len = 0;
356 return (0);