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
358
359
360
361
362
363
364
365
366
367
368
369
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2009 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfdchdr.h"
#if _PACKAGE_ast
#include <ast_tty.h>
#include <signal.h>
#endif
/*
* a simple but fast more style pager discipline
* if on sfstdout then sfstdin ops reset the page state
*
* Glenn Fowler
* AT&T Research
*
* @(#)$Id: sfdcmore (AT&T Research) 1998-06-25 $
*/
typedef struct
{
Sfdisc_t disc; /* sfio discipline */
Sfio_t* input; /* tied with this input stream */
Sfio_t* error; /* tied with this error stream */
int rows; /* max rows */
int cols; /* max cols */
int row; /* current row */
int col; /* current col */
int match; /* match length, 0 if none */
char pattern[128]; /* match pattern */
char prompt[1]; /* prompt string */
} More_t;
/*
* more read
* we assume line-at-a-time input
*/
#if __STD_C
static ssize_t moreread(Sfio_t* f, void* buf, size_t n, Sfdisc_t* dp)
#else
static ssize_t moreread(f, buf, n, dp)
Sfio_t* f;
void* buf;
size_t n;
Sfdisc_t* dp;
#endif
{
register More_t* more = (More_t*)dp;
more->match = 0;
more->row = 2;
more->col = 1;
return sfrd(f, buf, n, dp);
}
/*
* output label on wfd and return next char on rfd with no echo
* return < -1 is -(signal + 1)
*/
#if __STD_C
static int ttyquery(Sfio_t* rp, Sfio_t* wp, const char* label, Sfdisc_t* dp)
#else
static int ttyquery(rp, wp, label, dp)
Sfio_t* rp;
Sfio_t* wp;
char* label;
Sfdisc_t* dp;
#endif
{
register int r;
int n;
#ifdef TCSADRAIN
unsigned char c;
struct termios old;
struct termios tty;
int rfd = sffileno(rp);
int wfd = sffileno(rp);
if (!label)
n = 0;
else if (n = strlen(label))
write(wfd, label, n);
tcgetattr(rfd, &old);
tty = old;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_lflag &= ~(ICANON|ECHO|ECHOK|ISIG);
tcsetattr(rfd, TCSADRAIN, &tty);
if ((r = read(rfd, &c, 1)) == 1)
{
if (c == old.c_cc[VEOF])
r = -1;
else if (c == old.c_cc[VINTR])
r = -(SIGINT + 1);
else if (c == old.c_cc[VQUIT])
r = -(SIGQUIT + 1);
else if (c == '\r')
r = '\n';
else
r = c;
}
tcsetattr(rfd, TCSADRAIN, &old);
if (n)
{
write(wfd, "\r", 1);
while (n-- > 0)
write(wfd, " ", 1);
write(wfd, "\r", 1);
}
#else
register char* s;
if (label && (n = strlen(label)))
sfwr(wp, label, n, dp);
r = (s = sfgetr(rp, '\n', 0)) ? *s : -1;
#endif
return r;
}
/*
* more write
*/
#if __STD_C
static ssize_t morewrite(Sfio_t* f, const Void_t* buf, register size_t n, Sfdisc_t* dp)
#else
static ssize_t morewrite(f, buf, n, dp)
Sfio_t* f;
Void_t* buf;
register size_t n;
Sfdisc_t* dp;
#endif
{
register More_t* more = (More_t*)dp;
register char* b;
register char* s;
register char* e;
register ssize_t w;
register int r;
if (!more->row)
return n;
if (!more->col)
return sfwr(f, buf, n, dp);
w = 0;
b = (char*)buf;
s = b;
e = s + n;
if (more->match)
{
match:
for (r = more->pattern[0];; s++)
{
if (s >= e)
return n;
if (*s == '\n')
b = s + 1;
else if (*s == r && (e - s) >= more->match && !strncmp(s, more->pattern, more->match))
break;
}
s = b;
w += b - (char*)buf;
more->match = 0;
}
while (s < e)
{
switch (*s++)
{
case '\t':
more->col = ((more->col + 8) & ~7) - 1;
/*FALLTHROUGH*/
default:
if (++more->col <= more->cols || s < e && *s == '\n')
continue;
/*FALLTHROUGH*/
case '\n':
more->col = 1;
if (++more->row < more->rows)
continue;
break;
case '\b':
if (more->col > 1)
more->col--;
continue;
case '\r':
more->col = 1;
continue;
}
w += sfwr(f, b, s - b, dp);
b = s;
r = ttyquery(sfstdin, f, more->prompt, dp);
if (r == '/' || r == 'n')
{
if (r == '/')
{
sfwr(f, "/", 1, dp);
if ((s = sfgetr(sfstdin, '\n', 1)) && (n = sfvalue(sfstdin) - 1) > 0)
{
if (n >= sizeof(more->pattern))
n = sizeof(more->pattern) - 1;
memcpy(more->pattern, s, n);
more->pattern[n] = 0;
}
}
if (more->match = strlen(more->pattern))
{
more->row = 1;
more->col = 1;
goto match;
}
}
switch (r)
{
case '\n':
case '\r':
more->row--;
more->col = 1;
break;
case ' ':
more->row = 2;
more->col = 1;
break;
default:
more->row = 0;
return n;
}
}
if (s > b)
w += sfwr(f, b, s - b, dp);
return w;
}
/*
* remove the discipline on close
*/
#if __STD_C
static int moreexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* dp)
#else
static int moreexcept(f, type, data, dp)
Sfio_t* f;
int type;
Void_t* data;
Sfdisc_t* dp;
#endif
{
register More_t* more = (More_t*)dp;
if (type == SF_FINAL || type == SF_DPOP)
{
if (f = more->input)
{
more->input = 0;
sfdisc(f, SF_POPDISC);
}
else if (f = more->error)
{
more->error = 0;
sfdisc(f, SF_POPDISC);
}
else
free(dp);
}
else if (type == SF_SYNC)
{
more->match = 0;
more->row = 1;
more->col = 1;
}
return 0;
}
/*
* push the more discipline on f
* if prompt==0 then a default ansi prompt is used
* if rows==0 or cols==0 then they are deterimined from the tty
* if f==sfstdout then input on sfstdin also resets the state
*/
#if __STD_C
int sfdcmore(Sfio_t* f, const char* prompt, int rows, int cols)
#else
int sfdcmore(f, prompt, rows, cols)
Sfio_t* f;
char* prompt;
int rows;
int cols;
#endif
{
register More_t* more;
size_t n;
/*
* this is a writeonly discipline for interactive io
*/
if (!(sfset(f, 0, 0) & SF_WRITE) || !isatty(sffileno(sfstdin)) || !isatty(sffileno(sfstdout)))
return -1;
if (!prompt)
prompt = "\033[7m More\033[m";
n = strlen(prompt) + 1;
if (!(more = (More_t*)malloc(sizeof(More_t) + n)))
return -1;
memset(more, 0, sizeof(*more));
more->disc.readf = moreread;
more->disc.writef = morewrite;
more->disc.exceptf = moreexcept;
memcpy(more->prompt, prompt, n);
if (!rows || !cols)
{
#if _PACKAGE_ast
astwinsize(sffileno(sfstdin), &rows, &cols);
#endif
if (!rows)
rows = 24;
if (!cols)
cols = 80;
}
more->rows = rows;
more->cols = cols;
more->row = 1;
more->col = 1;
if (sfdisc(f, &more->disc) != &more->disc)
{
free(more);
return -1;
}
if (f == sfstdout)
{
if (sfdisc(sfstdin, &more->disc) != &more->disc)
{
sfdisc(f, SF_POPDISC);
return -1;
}
more->input = sfstdin;
if (sfdisc(sfstderr, &more->disc) != &more->disc)
{
sfdisc(f, SF_POPDISC);
return -1;
}
more->error = sfstdin;
}
return 0;
}
|