ex: convert line input buffer from file encoding to internal encoding
[nvi.git] / ex / ex_print.c
blob6cfdb3ca7364f676d1fe29ca55a347ddf12af39c
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.24 2001/07/29 19:07:29 skimo Exp $ (Berkeley) $Date: 2001/07/29 19:07:29 $";
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(SCR *sp, EXCMD *cmdp)
46 if (ex_print(sp, cmdp,
47 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_LIST))
48 return (1);
49 sp->lno = cmdp->addr2.lno;
50 sp->cno = cmdp->addr2.cno;
51 return (0);
55 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
57 * Display the addressed lines with a leading line number.
59 * PUBLIC: int ex_number __P((SCR *, EXCMD *));
61 int
62 ex_number(SCR *sp, EXCMD *cmdp)
64 if (ex_print(sp, cmdp,
65 &cmdp->addr1, &cmdp->addr2, cmdp->iflags | E_C_HASH))
66 return (1);
67 sp->lno = cmdp->addr2.lno;
68 sp->cno = cmdp->addr2.cno;
69 return (0);
73 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
75 * Display the addressed lines.
77 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
79 int
80 ex_pr(SCR *sp, EXCMD *cmdp)
82 if (ex_print(sp, cmdp, &cmdp->addr1, &cmdp->addr2, cmdp->iflags))
83 return (1);
84 sp->lno = cmdp->addr2.lno;
85 sp->cno = cmdp->addr2.cno;
86 return (0);
90 * ex_print --
91 * Print the selected lines.
93 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
95 int
96 ex_print(SCR *sp, EXCMD *cmdp, MARK *fp, MARK *tp, u_int32_t flags)
98 GS *gp;
99 db_recno_t from, to;
100 size_t col, len;
101 CHAR_T *p;
102 CHAR_T buf[10];
103 CHAR_T *wp;
104 size_t wlen;
106 NEEDFILE(sp, cmdp);
108 gp = sp->gp;
109 for (from = fp->lno, to = tp->lno; from <= to; ++from) {
110 col = 0;
113 * Display the line number. The %6 format is specified
114 * by POSIX 1003.2, and is almost certainly large enough.
115 * Check, though, just in case.
117 if (LF_ISSET(E_C_HASH)) {
118 if (from <= 999999) {
119 SPRINTF(buf, SIZE(buf), L("%6ld "), from);
120 p = buf;
121 } else
122 p = L("TOOBIG ");
123 if (ex_prchars(sp, p, &col, 8, 0, 0))
124 return (1);
128 * Display the line. The format for E_C_PRINT isn't very good,
129 * especially in handling end-of-line tabs, but they're almost
130 * backward compatible.
132 if (db_get(sp, from, DBG_FATAL, &p, &len))
133 return (1);
135 if (len == 0 && !LF_ISSET(E_C_LIST))
136 (void)ex_puts(sp, "\n");
137 else if (ex_ldisplay(sp, p, len, col, flags))
138 return (1);
140 if (INTERRUPTED(sp))
141 break;
143 return (0);
147 * ex_ldisplay --
148 * Display a line without any preceding number.
150 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
153 ex_ldisplay(SCR *sp, const CHAR_T *p, size_t len, size_t col, u_int flags)
155 if (len > 0 && ex_prchars(sp, p, &col, len, LF_ISSET(E_C_LIST), 0))
156 return (1);
157 if (!INTERRUPTED(sp) && LF_ISSET(E_C_LIST)) {
158 p = L("$");
159 if (ex_prchars(sp, p, &col, 1, LF_ISSET(E_C_LIST), 0))
160 return (1);
162 if (!INTERRUPTED(sp))
163 (void)ex_puts(sp, "\n");
164 return (0);
168 * ex_scprint --
169 * Display a line for the substitute with confirmation routine.
171 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
174 ex_scprint(SCR *sp, MARK *fp, MARK *tp)
176 CHAR_T *p;
177 size_t col, len;
179 col = 0;
180 if (O_ISSET(sp, O_NUMBER)) {
181 p = L(" ");
182 if (ex_prchars(sp, p, &col, 8, 0, 0))
183 return (1);
186 if (db_get(sp, fp->lno, DBG_FATAL, &p, &len))
187 return (1);
189 if (ex_prchars(sp, p, &col, fp->cno, 0, ' '))
190 return (1);
191 p += fp->cno;
192 if (ex_prchars(sp,
193 p, &col, tp->cno == fp->cno ? 1 : tp->cno - fp->cno, 0, '^'))
194 return (1);
195 if (INTERRUPTED(sp))
196 return (1);
197 p = L("[ynq]"); /* XXX: should be msg_cat. */
198 if (ex_prchars(sp, p, &col, 5, 0, 0))
199 return (1);
200 (void)ex_fflush(sp);
201 return (0);
205 * ex_prchars --
206 * Local routine to dump characters to the screen.
208 static int
209 ex_prchars(SCR *sp, const CHAR_T *p, size_t *colp, size_t len,
210 u_int flags, int repeatc)
212 CHAR_T ch;
213 char *kp;
214 GS *gp;
215 size_t col, tlen, ts;
217 if (O_ISSET(sp, O_LIST))
218 LF_SET(E_C_LIST);
219 gp = sp->gp;
220 ts = O_VAL(sp, O_TABSTOP);
221 for (col = *colp; len--;)
222 if ((ch = *p++) == L('\t') && !LF_ISSET(E_C_LIST))
223 for (tlen = ts - col % ts;
224 col < sp->cols && tlen--; ++col) {
225 (void)ex_printf(sp,
226 "%c", repeatc ? repeatc : ' ');
227 if (INTERRUPTED(sp))
228 goto intr;
230 else {
231 /* XXXX */
232 if (INTISWIDE(ch)) {
233 CHAR_T str[2] = {0, 0};
234 str[0] = ch;
235 INT2CHAR(sp, str, 2, kp, tlen);
236 } else {
237 kp = KEY_NAME(sp, ch);
238 tlen = KEY_LEN(sp, ch);
240 if (!repeatc && col + tlen < sp->cols) {
241 (void)ex_puts(sp, kp);
242 col += tlen;
243 } else
244 for (; tlen--; ++kp, ++col) {
245 if (col == sp->cols) {
246 col = 0;
247 (void)ex_puts(sp, "\n");
249 (void)ex_printf(sp,
250 "%c", repeatc ? repeatc : *kp);
251 if (INTERRUPTED(sp))
252 goto intr;
255 intr: *colp = col;
256 return (0);
260 * ex_printf --
261 * Ex's version of printf.
263 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
266 #ifdef __STDC__
267 ex_printf(SCR *sp, const char *fmt, ...)
268 #else
269 ex_printf(sp, fmt, va_alist)
270 SCR *sp;
271 const char *fmt;
272 va_dcl
273 #endif
275 EX_PRIVATE *exp;
276 va_list ap;
277 size_t n;
278 CHAR_T *kp;
280 exp = EXP(sp);
282 #ifdef __STDC__
283 va_start(ap, fmt);
284 #else
285 va_start(ap);
286 #endif
287 exp->obp_len += n = vsnprintf(exp->obp + exp->obp_len,
288 sizeof(exp->obp) - exp->obp_len, fmt, ap);
289 va_end(ap);
291 /* Flush when reach a <newline> or half the buffer. */
292 if (exp->obp[exp->obp_len - 1] == '\n' ||
293 exp->obp_len > sizeof(exp->obp) / 2)
294 (void)ex_fflush(sp);
295 return (n);
299 * ex_puts --
300 * Ex's version of puts.
302 * PUBLIC: int ex_puts __P((SCR *, const char *));
305 ex_puts(SCR *sp, const char *str)
307 EX_PRIVATE *exp;
308 int doflush, n;
310 exp = EXP(sp);
312 /* Flush when reach a <newline> or the end of the buffer. */
313 for (doflush = n = 0; *str != '\0'; ++n) {
314 if (exp->obp_len > sizeof(exp->obp))
315 (void)ex_fflush(sp);
316 if ((exp->obp[exp->obp_len++] = *str++) == '\n')
317 doflush = 1;
319 if (doflush)
320 (void)ex_fflush(sp);
321 return (n);
325 * ex_fflush --
326 * Ex's version of fflush.
328 * PUBLIC: int ex_fflush __P((SCR *sp));
331 ex_fflush(SCR *sp)
333 EX_PRIVATE *exp;
335 exp = EXP(sp);
337 if (exp->obp_len != 0) {
338 sp->wp->scr_msg(sp, M_NONE, exp->obp, exp->obp_len);
339 exp->obp_len = 0;
341 return (0);