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.
13 static const char sccsid
[] = "@(#)ex_print.c 10.18 (Berkeley) 5/12/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
31 #include "../common/common.h"
33 static int ex_prchars
__P((SCR
*, const char *, size_t *, size_t, u_int
, int));
36 * ex_list -- :[line [,line]] l[ist] [count] [flags]
38 * Display the addressed lines such that the output is unambiguous.
40 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
47 if (ex_print(sp
, cmdp
,
48 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_LIST
))
50 sp
->lno
= cmdp
->addr2
.lno
;
51 sp
->cno
= cmdp
->addr2
.cno
;
56 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
58 * Display the addressed lines with a leading line number.
60 * PUBLIC: int ex_number __P((SCR *, EXCMD *));
67 if (ex_print(sp
, cmdp
,
68 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_HASH
))
70 sp
->lno
= cmdp
->addr2
.lno
;
71 sp
->cno
= cmdp
->addr2
.cno
;
76 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
78 * Display the addressed lines.
80 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
87 if (ex_print(sp
, cmdp
, &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
))
89 sp
->lno
= cmdp
->addr2
.lno
;
90 sp
->cno
= cmdp
->addr2
.cno
;
96 * Print the selected lines.
98 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
101 ex_print(sp
, cmdp
, fp
, tp
, flags
)
115 for (from
= fp
->lno
, to
= tp
->lno
; from
<= to
; ++from
) {
119 * Display the line number. The %6 format is specified
120 * by POSIX 1003.2, and is almost certainly large enough.
121 * Check, though, just in case.
123 if (LF_ISSET(E_C_HASH
)) {
124 if (from
<= 999999) {
125 snprintf(buf
, sizeof(buf
), "%6lu ",
130 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
135 * Display the line. The format for E_C_PRINT isn't very good,
136 * especially in handling end-of-line tabs, but they're almost
137 * backward compatible.
139 if (db_get(sp
, from
, DBG_FATAL
, &p
, &len
))
142 if (len
== 0 && !LF_ISSET(E_C_LIST
))
143 (void)ex_puts(sp
, "\n");
144 else if (ex_ldisplay(sp
, p
, len
, col
, flags
))
155 * Display a line without any preceding number.
157 * PUBLIC: int ex_ldisplay __P((SCR *, const char *, size_t, size_t, u_int));
160 ex_ldisplay(sp
, p
, len
, col
, flags
)
166 if (len
> 0 && ex_prchars(sp
, p
, &col
, len
, LF_ISSET(E_C_LIST
), 0))
168 if (!INTERRUPTED(sp
) && LF_ISSET(E_C_LIST
)) {
170 if (ex_prchars(sp
, p
, &col
, 1, LF_ISSET(E_C_LIST
), 0))
173 if (!INTERRUPTED(sp
))
174 (void)ex_puts(sp
, "\n");
180 * Display a line for the substitute with confirmation routine.
182 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
185 ex_scprint(sp
, fp
, tp
)
193 if (O_ISSET(sp
, O_NUMBER
)) {
195 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
199 if (db_get(sp
, fp
->lno
, DBG_FATAL
, (char **)&p
, &len
))
202 if (ex_prchars(sp
, p
, &col
, fp
->cno
, 0, ' '))
206 p
, &col
, tp
->cno
== fp
->cno
? 1 : tp
->cno
- fp
->cno
, 0, '^'))
210 p
= "[ynq]"; /* XXX: should be msg_cat. */
211 if (ex_prchars(sp
, p
, &col
, 5, 0, 0))
219 * Local routine to dump characters to the screen.
222 ex_prchars(sp
, p
, colp
, len
, flags
, repeatc
)
231 size_t col
, tlen
, ts
;
233 if (O_ISSET(sp
, O_LIST
))
236 ts
= O_VAL(sp
, O_TABSTOP
);
237 for (col
= *colp
; len
--;)
238 if ((ch
= *p
++) == '\t' && !LF_ISSET(E_C_LIST
))
239 for (tlen
= ts
- col
% ts
;
240 col
< sp
->cols
&& tlen
--; ++col
) {
242 "%c", repeatc
? repeatc
: ' ');
247 kp
= KEY_NAME(sp
, ch
);
248 tlen
= KEY_LEN(sp
, ch
);
249 if (!repeatc
&& col
+ tlen
< sp
->cols
) {
250 (void)ex_puts(sp
, kp
);
253 for (; tlen
--; ++kp
, ++col
) {
254 if (col
== sp
->cols
) {
256 (void)ex_puts(sp
, "\n");
259 "%c", repeatc
? repeatc
: *kp
);
270 * Ex's version of printf.
272 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
276 ex_printf(SCR
*sp
, const char *fmt
, ...)
278 ex_printf(sp
, fmt
, va_alist
)
295 exp
->obp_len
+= n
= vsnprintf(exp
->obp
+ exp
->obp_len
,
296 sizeof(exp
->obp
) - exp
->obp_len
, fmt
, ap
);
299 /* Flush when reach a <newline> or half the buffer. */
300 if (exp
->obp
[exp
->obp_len
- 1] == '\n' ||
301 exp
->obp_len
> sizeof(exp
->obp
) / 2)
308 * Ex's version of puts.
310 * PUBLIC: int ex_puts __P((SCR *, const char *));
322 /* Flush when reach a <newline> or the end of the buffer. */
323 for (doflush
= n
= 0; *str
!= '\0'; ++n
) {
324 if (exp
->obp_len
> sizeof(exp
->obp
))
326 if ((exp
->obp
[exp
->obp_len
++] = *str
++) == '\n')
336 * Ex's version of fflush.
338 * PUBLIC: int ex_fflush __P((SCR *sp));
348 if (exp
->obp_len
!= 0) {
349 sp
->gp
->scr_msg(sp
, M_NONE
, exp
->obp
, exp
->obp_len
);