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
[] = "$Id: ex_print.c,v 10.18 1996/05/12 18:07:42 bostic Exp $ (Berkeley) $Date: 1996/05/12 18:07:42 $";
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
), "%6ld ", from
);
129 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
134 * Display the line. The format for E_C_PRINT isn't very good,
135 * especially in handling end-of-line tabs, but they're almost
136 * backward compatible.
138 if (db_get(sp
, from
, DBG_FATAL
, &p
, &len
))
141 if (len
== 0 && !LF_ISSET(E_C_LIST
))
142 (void)ex_puts(sp
, "\n");
143 else if (ex_ldisplay(sp
, p
, len
, col
, flags
))
154 * Display a line without any preceding number.
156 * PUBLIC: int ex_ldisplay __P((SCR *, const char *, size_t, size_t, u_int));
159 ex_ldisplay(sp
, p
, len
, col
, flags
)
165 if (len
> 0 && ex_prchars(sp
, p
, &col
, len
, LF_ISSET(E_C_LIST
), 0))
167 if (!INTERRUPTED(sp
) && LF_ISSET(E_C_LIST
)) {
169 if (ex_prchars(sp
, p
, &col
, 1, LF_ISSET(E_C_LIST
), 0))
172 if (!INTERRUPTED(sp
))
173 (void)ex_puts(sp
, "\n");
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
)
192 if (O_ISSET(sp
, O_NUMBER
)) {
194 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
198 if (db_get(sp
, fp
->lno
, DBG_FATAL
, (char **)&p
, &len
))
201 if (ex_prchars(sp
, p
, &col
, fp
->cno
, 0, ' '))
205 p
, &col
, tp
->cno
== fp
->cno
? 1 : tp
->cno
- fp
->cno
, 0, '^'))
209 p
= "[ynq]"; /* XXX: should be msg_cat. */
210 if (ex_prchars(sp
, p
, &col
, 5, 0, 0))
218 * Local routine to dump characters to the screen.
221 ex_prchars(sp
, p
, colp
, len
, flags
, repeatc
)
230 size_t col
, tlen
, ts
;
232 if (O_ISSET(sp
, O_LIST
))
235 ts
= O_VAL(sp
, O_TABSTOP
);
236 for (col
= *colp
; len
--;)
237 if ((ch
= *p
++) == '\t' && !LF_ISSET(E_C_LIST
))
238 for (tlen
= ts
- col
% ts
;
239 col
< sp
->cols
&& tlen
--; ++col
) {
241 "%c", repeatc
? repeatc
: ' ');
246 kp
= KEY_NAME(sp
, ch
);
247 tlen
= KEY_LEN(sp
, ch
);
248 if (!repeatc
&& col
+ tlen
< sp
->cols
) {
249 (void)ex_puts(sp
, kp
);
252 for (; tlen
--; ++kp
, ++col
) {
253 if (col
== sp
->cols
) {
255 (void)ex_puts(sp
, "\n");
258 "%c", repeatc
? repeatc
: *kp
);
269 * Ex's version of printf.
271 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
275 ex_printf(SCR
*sp
, const char *fmt
, ...)
277 ex_printf(sp
, fmt
, va_alist
)
294 exp
->obp_len
+= n
= vsnprintf(exp
->obp
+ exp
->obp_len
,
295 sizeof(exp
->obp
) - exp
->obp_len
, fmt
, ap
);
298 /* Flush when reach a <newline> or half the buffer. */
299 if (exp
->obp
[exp
->obp_len
- 1] == '\n' ||
300 exp
->obp_len
> sizeof(exp
->obp
) / 2)
307 * Ex's version of puts.
309 * PUBLIC: int ex_puts __P((SCR *, const char *));
321 /* Flush when reach a <newline> or the end of the buffer. */
322 for (doflush
= n
= 0; *str
!= '\0'; ++n
) {
323 if (exp
->obp_len
> sizeof(exp
->obp
))
325 if ((exp
->obp
[exp
->obp_len
++] = *str
++) == '\n')
335 * Ex's version of fflush.
337 * PUBLIC: int ex_fflush __P((SCR *sp));
347 if (exp
->obp_len
!= 0) {
348 sp
->gp
->scr_msg(sp
, M_NONE
, exp
->obp
, exp
->obp_len
);