1 /* $NetBSD: ex_print.c,v 1.3 2013/11/26 16:32:04 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <bitstring.h>
32 #include "../common/common.h"
34 static int ex_prchars
__P((SCR
*, const CHAR_T
*, size_t *, size_t,
38 * ex_list -- :[line [,line]] l[ist] [count] [flags]
40 * Display the addressed lines such that the output is unambiguous.
42 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
45 ex_list(SCR
*sp
, EXCMD
*cmdp
)
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 *));
63 ex_number(SCR
*sp
, EXCMD
*cmdp
)
65 if (ex_print(sp
, cmdp
,
66 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_HASH
))
68 sp
->lno
= cmdp
->addr2
.lno
;
69 sp
->cno
= cmdp
->addr2
.cno
;
74 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
76 * Display the addressed lines.
78 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
81 ex_pr(SCR
*sp
, EXCMD
*cmdp
)
83 if (ex_print(sp
, cmdp
, &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
))
85 sp
->lno
= cmdp
->addr2
.lno
;
86 sp
->cno
= cmdp
->addr2
.cno
;
92 * Print the selected lines.
94 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
97 ex_print(SCR
*sp
, EXCMD
*cmdp
, MARK
*fp
, MARK
*tp
, u_int32_t flags
)
107 for (from
= fp
->lno
, to
= tp
->lno
; from
<= to
; ++from
) {
111 * Display the line number. The %6 format is specified
112 * by POSIX 1003.2, and is almost certainly large enough.
113 * Check, though, just in case.
115 if (LF_ISSET(E_C_HASH
)) {
116 if (from
<= 999999) {
117 SPRINTF(buf
, SIZE(buf
), L("%6ld "), from
);
121 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
126 * Display the line. The format for E_C_PRINT isn't very good,
127 * especially in handling end-of-line tabs, but they're almost
128 * backward compatible.
130 if (db_get(sp
, from
, DBG_FATAL
, &q
, &len
))
134 if (len
== 0 && !LF_ISSET(E_C_LIST
))
135 (void)ex_puts(sp
, "\n");
136 else if (ex_ldisplay(sp
, p
, len
, col
, flags
))
147 * Display a line without any preceding number.
149 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
152 ex_ldisplay(SCR
*sp
, const CHAR_T
*p
, size_t len
, size_t col
, u_int flags
)
154 if (len
> 0 && ex_prchars(sp
, p
, &col
, len
, LF_ISSET(E_C_LIST
), 0))
156 if (!INTERRUPTED(sp
) && LF_ISSET(E_C_LIST
)) {
158 if (ex_prchars(sp
, p
, &col
, 1, LF_ISSET(E_C_LIST
), 0))
161 if (!INTERRUPTED(sp
))
162 (void)ex_puts(sp
, "\n");
168 * Display a line for the substitute with confirmation routine.
170 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
173 ex_scprint(SCR
*sp
, MARK
*fp
, MARK
*tp
)
180 if (O_ISSET(sp
, O_NUMBER
)) {
182 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
186 if (db_get(sp
, fp
->lno
, DBG_FATAL
, &q
, &len
))
190 if (ex_prchars(sp
, p
, &col
, fp
->cno
, 0, ' '))
194 p
, &col
, tp
->cno
== fp
->cno
? 1 : tp
->cno
- fp
->cno
, 0, '^'))
198 p
= L("[ynq]"); /* XXX: should be msg_cat. */
199 if (ex_prchars(sp
, p
, &col
, 5, 0, 0))
207 * Local routine to dump characters to the screen.
210 ex_prchars(SCR
*sp
, const CHAR_T
*p
, size_t *colp
, size_t len
,
211 u_int flags
, int repeatc
)
215 size_t col
, tlen
, ts
;
217 if (O_ISSET(sp
, O_LIST
))
219 ts
= O_VAL(sp
, O_TABSTOP
);
220 for (col
= *colp
; len
--;)
221 if ((ch
= *p
++) == L('\t') && !LF_ISSET(E_C_LIST
))
222 for (tlen
= ts
- col
% ts
;
223 col
< sp
->cols
&& tlen
--; ++col
) {
225 "%c", repeatc
? repeatc
: ' ');
232 CHAR_T str
[2] = {0, 0};
234 INT2CHAR(sp
, str
, 2, kp
, tlen
);
236 kp
= (char *)KEY_NAME(sp
, ch
);
237 tlen
= KEY_LEN(sp
, ch
);
239 if (!repeatc
&& col
+ tlen
< sp
->cols
) {
240 (void)ex_puts(sp
, kp
);
243 for (; tlen
--; ++kp
, ++col
) {
244 if (col
== sp
->cols
) {
246 (void)ex_puts(sp
, "\n");
249 "%c", repeatc
? repeatc
: *kp
);
260 * Ex's version of printf.
262 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
266 ex_printf(SCR
*sp
, const char *fmt
, ...)
268 ex_printf(sp
, fmt
, va_alist
)
285 exp
->obp_len
+= n
= vsnprintf(exp
->obp
+ exp
->obp_len
,
286 sizeof(exp
->obp
) - exp
->obp_len
, fmt
, ap
);
289 /* Flush when reach a <newline> or half the buffer. */
290 if (exp
->obp
[exp
->obp_len
- 1] == '\n' ||
291 exp
->obp_len
> sizeof(exp
->obp
) / 2)
298 * Ex's version of puts.
300 * PUBLIC: int ex_puts __P((SCR *, const char *));
303 ex_puts(SCR
*sp
, const char *str
)
310 /* Flush when reach a <newline> or the end of the buffer. */
311 for (doflush
= n
= 0; *str
!= '\0'; ++n
) {
312 if (exp
->obp_len
> sizeof(exp
->obp
))
314 if ((exp
->obp
[exp
->obp_len
++] = *str
++) == '\n')
324 * Ex's version of fflush.
326 * PUBLIC: int ex_fflush __P((SCR *sp));
335 if (exp
&& exp
->obp_len
!= 0) {
336 sp
->wp
->scr_msg(sp
, M_NONE
, exp
->obp
, exp
->obp_len
);