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