date and time created 84/05/12 21:01:48 by sam
[unix-history] / usr / src / local / local.cmd / dis.c
CommitLineData
35f72e44
SL
1#ifndef lint
2static char sccsid[] = "@(#)dis.c 1.1 (Berkeley) %G%";
3#endif
4
5/*
6** Dis -- VDU page display program
7**
8** "dis [-t<timeout>] [-c<refresh count>] [-u]"
9**
10** Bugs and comments to: Piers Lauder
11** Dept of Comp Sci
12** Sydney University
13** May '80.
14*/
15
16/*
17#include <local-system>
18*/
19#include <signal.h>
20#include <setjmp.h>
21#include <sgtty.h>
22#include <stdio.h>
23
24/*
25** Parameters
26*/
27
28#define MAXWID 132
29#define MAXLEN 80
30
31
32/*
33** Screen buffer
34*/
35
36char * Buf;
37
38/*
39** Term Cap
40*/
41
42char *CM, *CL;
43short amflag;
44
45extern short ospeed;
46extern char PC, *BC, *UP;
47
48extern char * tgoto();
49extern char * tgetstr();
50
51/*
52** Screen macro
53*/
54
55#define putcm(cp,p,c) if(*cp++!=c){\
56 if(move((--cp)-p))\
57 putc(c,stdout);\
58 *cp++=c;\
59 }
60
61/*
62** Miscellaneous
63*/
64
65jmp_buf alrmbuf;
66short width;
67short Width; /* width - 1 */
68short length;
69short Length; /* length - 1 */
70char * name;
71unsigned timeout;
72short rcount;
73
74void
75 tcinit(),
76 dis(),
77 warn(),
78 terror(),
79 outc();
80
81int alrmcatch();
82
83/*
84** Externals
85*/
86
87extern char _sobuf[];
88extern char * tgetstr();
89extern char * malloc();
90extern char * getenv();
91
92
93
94main(argc, argv)
95 register int argc;
96 register char * argv[];
97{
98 register char * cp;
99 register unsigned size;
100
101 name = *argv++;
102 argc--;
103
104 while ( argc )
105 {
106 switch( argv[0][0] )
107 {
108 case '-': argv[0]++;
109 continue;
110 case 't': timeout = atoi(&argv[0][1]);
111 break;
112 case 'c': rcount = atoi(&argv[0][1]);
113 break;
114 case 'u': setbuf(stdin, NULL);
115 break;
116 default: fprintf(stderr, "%s: bad arg - %s\n", name, argv[0] );
117 return 1;
118 }
119 argc--;
120 argv++;
121 }
122
123 setbuf(stdout, _sobuf);
124 tcinit();
125 size = length * width;
126 if ( (Buf = malloc(size)) == (char *)0 )
127 {
128 fprintf(stderr, "No memory\n");
129 exit(2);
130 }
131 bzero(Buf, size);
132
133 Length = length - 1;
134 Width = width - 1;
135 dis(size);
136
137 return 0;
138}
139
140
141
142
143void
144dis(size)
145 unsigned size;
146{
147 register char * ep;
148 register char * p = Buf;
149 register int c;
150 register int line;
151 /** on stack to avoid setjmp **/
152 char * cp;
153 char * lastend;
154 int rc;
155
156 lastend = p;
157 rc = rcount;
158
159 do
160 {
161 line = 0;
162 cp = p;
163 ep = cp+width;
164
165 if ( timeout == 0 || setjmp(alrmbuf) == 0 )
166 {
167 if ( timeout )
168 {
169 signal(SIGALRM, alrmcatch);
170 alarm(timeout);
171 }
172 while ( (c = getchar()) != EOF )
173 {
174 if ( rcount && !rc )
175 {
176 tputs(CL, 1, outc);
177 bzero(p, size);
178 rc = rcount;
179 }
180 if ( c < ' ' )
181 {
182 switch ( c )
183 {
184 case '\f': if ( cp != p )
185 break;
186 continue;
187 case '\t': c = cp - &p[line*width] + 1;
188 putcm(cp, p, ' ');
189 while ( c++&7 )
190 putcm(cp, p, ' ');
191 continue;
192 case '\n': while ( cp < ep )
193 putcm(cp, p, ' ');
194 if ( line < Length )
195 {
196 cp = &p[(++line)*width];
197 ep = cp+width;
198 }
199 continue;
200 default:
201 if ( cp < ep )
202 putcm(cp, p, '?');
203 continue;
204 }
205 }
206 else
207 {
208 if ( cp < ep )
209 putcm(cp, p, c);
210 continue;
211 }
212 break;
213 }
214 if ( timeout )
215 alarm(0);
216 }
217 ep = cp - 1;
218 while ( cp < lastend )
219 putcm(cp, p, ' ');
220 lastend = ep;
221 if ( (line = (ep-p)/width) < Length )
222 line++;
223 (void)move(line*width);
224 fflush(stdout);
225 if ( rcount )
226 --rc;
227 }
228 while
229 ( c != EOF );
230}
231
232
233
234
235int
236move(pos)
237 register int pos;
238{
239 register int x = pos%width;
240 register int y = pos/width;
241 register int i;
242 static int oy, ox = -1;
243
244 if ( oy == y )
245 {
246 if ( (i = x - ox) != 1 )
247 if ( i <= 3 && i > 0 )
248 {
249 i--;
250 pos -= i;
251 do
252 putc(Buf[pos++], stdout);
253 while
254 ( --i > 0 );
255 }
256 else
257 tputs(tgoto(CM, x, y), 1, outc);
258 }
259 else
260 if ( oy == (y-1) && x == 0 )
261 {
262 if ( ox != Width || !amflag )
263 putc('\n', stdout);
264 }
265 else
266 tputs(tgoto(CM, x, y), oy<y?y-oy:oy-y, outc);
267
268 ox = x; oy = y;
269 if ( y==Length && x==Width && amflag )
270 return 0;
271 return 1;
272}
273
274
275
276
277int
278alrmcatch()
279{
280 longjmp(alrmbuf, 1);
281}
282
283
284
285
286void
287tcinit()
288{
289 register char * cp;
290 struct sgttyb gb;
291 char bp[1024];
292 static char buf[100];
293 char *area = buf;
294
295 if ( tgetent(bp, getenv("TERM")) != 1 )
296 terror("no \"termcap\" entry");
297 if ( (CL = tgetstr("cl", &area)) == (char *)0 )
298 terror("not VDU");
299 if ( (CM = tgetstr("cm", &area)) == (char *)0 )
300 terror("no cursor addressing");
301 UP = tgetstr("up", &area);
302 BC = tgetstr("bc", &area);
303 if ( tgetflag("am") == 1 )
304 amflag++;
305 if ( (cp = getenv("WIDTH")) != (char *)0 )
306 width = atoi(cp);
307 else
308 width = tgetnum("co");
309 if ( width > MAXWID )
310 {
311 width = MAXWID;
312 warn("width truncated");
313 }
314 if ( (length = tgetnum("li")) > MAXLEN )
315 {
316 length = MAXLEN;
317 warn("length truncated");
318 }
319 if ( (cp = tgetstr("pc", &area)) != (char *)0 )
320 PC = *cp;
321 gtty(1, &gb);
322 ospeed = gb.sg_ospeed;
323
324 tputs(CL, 1, outc);
325 fflush(stdout);
326}
327
328
329
330
331void
332outc(c)
333{
334 putc(c, stdout);
335}
336
337
338
339
340void
341warn(s)
342 char * s;
343{
344 fprintf(stderr, "Warning: %s\n", s);
345 sleep(2);
346}
347
348
349
350
351void
352terror(s)
353 char * s;
354{
355 fprintf(stderr, "Terminal capability error - %s\n", s);
356 exit(1);
357}