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