1 /* $NetBSD: ex_print.c,v 1.1.1.2 2008/05/18 14:31:17 aymeric Exp $ */
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.
9 * See the LICENSE file for redistribution information.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
33 #include "../common/common.h"
35 static int ex_prchars
__P((SCR
*, const CHAR_T
*, size_t *, size_t,
39 * ex_list -- :[line [,line]] l[ist] [count] [flags]
41 * Display the addressed lines such that the output is unambiguous.
43 * PUBLIC: int ex_list __P((SCR *, EXCMD *));
46 ex_list(SCR
*sp
, EXCMD
*cmdp
)
48 if (ex_print(sp
, cmdp
,
49 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_LIST
))
51 sp
->lno
= cmdp
->addr2
.lno
;
52 sp
->cno
= cmdp
->addr2
.cno
;
57 * ex_number -- :[line [,line]] nu[mber] [count] [flags]
59 * Display the addressed lines with a leading line number.
61 * PUBLIC: int ex_number __P((SCR *, EXCMD *));
64 ex_number(SCR
*sp
, EXCMD
*cmdp
)
66 if (ex_print(sp
, cmdp
,
67 &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
| E_C_HASH
))
69 sp
->lno
= cmdp
->addr2
.lno
;
70 sp
->cno
= cmdp
->addr2
.cno
;
75 * ex_pr -- :[line [,line]] p[rint] [count] [flags]
77 * Display the addressed lines.
79 * PUBLIC: int ex_pr __P((SCR *, EXCMD *));
82 ex_pr(SCR
*sp
, EXCMD
*cmdp
)
84 if (ex_print(sp
, cmdp
, &cmdp
->addr1
, &cmdp
->addr2
, cmdp
->iflags
))
86 sp
->lno
= cmdp
->addr2
.lno
;
87 sp
->cno
= cmdp
->addr2
.cno
;
93 * Print the selected lines.
95 * PUBLIC: int ex_print __P((SCR *, EXCMD *, MARK *, MARK *, u_int32_t));
98 ex_print(SCR
*sp
, EXCMD
*cmdp
, MARK
*fp
, MARK
*tp
, u_int32_t flags
)
110 for (from
= fp
->lno
, to
= tp
->lno
; from
<= to
; ++from
) {
114 * Display the line number. The %6 format is specified
115 * by POSIX 1003.2, and is almost certainly large enough.
116 * Check, though, just in case.
118 if (LF_ISSET(E_C_HASH
)) {
119 if (from
<= 999999) {
120 SPRINTF(buf
, SIZE(buf
), L("%6ld "), from
);
124 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
129 * Display the line. The format for E_C_PRINT isn't very good,
130 * especially in handling end-of-line tabs, but they're almost
131 * backward compatible.
133 if (db_get(sp
, from
, DBG_FATAL
, &q
, &len
))
137 if (len
== 0 && !LF_ISSET(E_C_LIST
))
138 (void)ex_puts(sp
, "\n");
139 else if (ex_ldisplay(sp
, p
, len
, col
, flags
))
150 * Display a line without any preceding number.
152 * PUBLIC: int ex_ldisplay __P((SCR *, const CHAR_T *, size_t, size_t, u_int));
155 ex_ldisplay(SCR
*sp
, const CHAR_T
*p
, size_t len
, size_t col
, u_int flags
)
157 if (len
> 0 && ex_prchars(sp
, p
, &col
, len
, LF_ISSET(E_C_LIST
), 0))
159 if (!INTERRUPTED(sp
) && LF_ISSET(E_C_LIST
)) {
161 if (ex_prchars(sp
, p
, &col
, 1, LF_ISSET(E_C_LIST
), 0))
164 if (!INTERRUPTED(sp
))
165 (void)ex_puts(sp
, "\n");
171 * Display a line for the substitute with confirmation routine.
173 * PUBLIC: int ex_scprint __P((SCR *, MARK *, MARK *));
176 ex_scprint(SCR
*sp
, MARK
*fp
, MARK
*tp
)
183 if (O_ISSET(sp
, O_NUMBER
)) {
185 if (ex_prchars(sp
, p
, &col
, 8, 0, 0))
189 if (db_get(sp
, fp
->lno
, DBG_FATAL
, &q
, &len
))
193 if (ex_prchars(sp
, p
, &col
, fp
->cno
, 0, ' '))
197 p
, &col
, tp
->cno
== fp
->cno
? 1 : tp
->cno
- fp
->cno
, 0, '^'))
201 p
= L("[ynq]"); /* XXX: should be msg_cat. */
202 if (ex_prchars(sp
, p
, &col
, 5, 0, 0))
210 * Local routine to dump characters to the screen.
213 ex_prchars(SCR
*sp
, const CHAR_T
*p
, size_t *colp
, size_t len
,
214 u_int flags
, int repeatc
)
219 size_t col
, tlen
, ts
;
221 if (O_ISSET(sp
, O_LIST
))
224 ts
= O_VAL(sp
, O_TABSTOP
);
225 for (col
= *colp
; len
--;)
226 if ((ch
= *p
++) == L('\t') && !LF_ISSET(E_C_LIST
))
227 for (tlen
= ts
- col
% ts
;
228 col
< sp
->cols
&& tlen
--; ++col
) {
230 "%c", repeatc
? repeatc
: ' ');
237 CHAR_T str
[2] = {0, 0};
239 INT2CHAR(sp
, str
, 2, kp
, tlen
);
241 kp
= (char *)KEY_NAME(sp
, ch
);
242 tlen
= KEY_LEN(sp
, ch
);
244 if (!repeatc
&& col
+ tlen
< sp
->cols
) {
245 (void)ex_puts(sp
, kp
);
248 for (; tlen
--; ++kp
, ++col
) {
249 if (col
== sp
->cols
) {
251 (void)ex_puts(sp
, "\n");
254 "%c", repeatc
? repeatc
: *kp
);
265 * Ex's version of printf.
267 * PUBLIC: int ex_printf __P((SCR *, const char *, ...));
271 ex_printf(SCR
*sp
, const char *fmt
, ...)
273 ex_printf(sp
, fmt
, va_alist
)
290 exp
->obp_len
+= n
= vsnprintf(exp
->obp
+ exp
->obp_len
,
291 sizeof(exp
->obp
) - exp
->obp_len
, fmt
, ap
);
294 /* Flush when reach a <newline> or half the buffer. */
295 if (exp
->obp
[exp
->obp_len
- 1] == '\n' ||
296 exp
->obp_len
> sizeof(exp
->obp
) / 2)
303 * Ex's version of puts.
305 * PUBLIC: int ex_puts __P((SCR *, const char *));
308 ex_puts(SCR
*sp
, const char *str
)
315 /* Flush when reach a <newline> or the end of the buffer. */
316 for (doflush
= n
= 0; *str
!= '\0'; ++n
) {
317 if (exp
->obp_len
> sizeof(exp
->obp
))
319 if ((exp
->obp
[exp
->obp_len
++] = *str
++) == '\n')
329 * Ex's version of fflush.
331 * PUBLIC: int ex_fflush __P((SCR *sp));
340 if (exp
->obp_len
!= 0) {
341 sp
->wp
->scr_msg(sp
, M_NONE
, exp
->obp
, exp
->obp_len
);