|
|
1.1 root 1: /*
2: * optimize output for Tek 4014
3: */
4:
5: char x4014vers[] = "@(#)4014.c 1.5";
6:
7: #include <stdio.h>
8: #include <signal.h>
9: #include <sgtty.h>
10:
11: #define MAXY 3071
12: #define LINE 47
13: #define INCH 282
14: #define XOFF 248
15: #define US 037
16: #define GS 035
17: #define ESC 033
18: #define CR 015
19: #define FF 014
20: #define SO 016
21: #define SI 017
22: #define VALID 01777
23: #define HALF 5
24: #define HALFMASK 037
25:
26: int pl = 66*LINE;
27: int yyll = -1;
28: char obuf[BUFSIZ];
29: int xx = XOFF;
30: int xoff = XOFF;
31: int coff = 0;
32: int ncol = 0;
33: int maxcol = 1;
34: int yy = MAXY;
35: int ohy = -1;
36: int ohx = -1;
37: int oxb = -1;
38: int oly = -1;
39: int olx = -1;
40: int alpha;
41: int ry;
42: int erase = 1;
43: int tflg;
44: FILE *ttyin;
45:
46: main(argc, argv)
47: int argc;
48: char **argv;
49: {
50: register i, j;
51: register c;
52: char *argptr;
53: extern ex();
54: extern char *optarg;
55: extern int optind;
56:
57: while ((c = getopt(argc, argv, "tnp:c:")) != EOF)
58: switch(c) {
59: case 'p':
60: argptr = optarg;
61: if (i = tekpoints(argptr)) {
62: pl = i;
63: yyll = MAXY + 1 - pl;
64: }
65: break;
66: case 'n':
67: erase = 0;
68: break;
69:
70: case 't':
71: tflg ++;
72: break;
73:
74: case 'c':
75: argptr = optarg;
76: if (i = atoi(argptr)) {
77: maxcol = i;
78: xx = xoff = 0;
79: coff = 4096/i;
80: }
81: break;
82:
83: case '?':
84: fprintf(stderr, "usage: 4014 [ -tn ] [ -cN ] [ -pL ] [ file ]\n",(char *)NULL);
85: exit(1);
86: }
87: if ((ttyin = fopen("/dev/tty", "r")) != NULL)
88: setbuf(ttyin, (char *)NULL);
89: if ( optind < argc ) {
90: if (freopen(argv[optind], "r", stdin) == NULL) {
91: fprintf(stderr, "4014: cannot open %s\n", argv[optind]);
92: exit(1);
93: }
94: }
95: signal(SIGINT, ex);
96: setbuf(stdout, obuf);
97: ncol = maxcol;
98: init();
99: while ((i = getchar()) != EOF) {
100: switch(i) {
101:
102: case FF:
103: yy = 0;
104: case '\n':
105: xx = xoff;
106: yy -= LINE;
107: alpha = 0;
108: if (yy < yyll) {
109: ncol++;
110: yy = 0;
111: sendpt(0);
112: putchar(US);
113: fflush(stdout);
114: if (ncol >= maxcol)
115: kwait();
116: init();
117: }
118: continue;
119:
120: case CR:
121: xx = xoff;
122: alpha = 0;
123: continue;
124:
125: case ' ':
126: xx += 31;
127: alpha = 0;
128: continue;
129:
130: case '\t': /*tabstops at 8*31=248*/
131: j = ((xx-xoff)/248) + 1;
132: xx += j*248 - (xx-xoff);
133: alpha = 0;
134: continue;
135:
136: case '\b':
137: xx -= 31;
138: alpha = 0;
139: continue;
140:
141: case ESC:
142: switch(i = getchar()) {
143: case '7':
144: yy += LINE;
145: alpha = 0;
146: continue;
147: case '8':
148: yy += (LINE + ry)/2;
149: ry = (LINE + ry)%2;
150: alpha = 0;
151: continue;
152: case '9':
153: yy -= (LINE - ry)/2;
154: ry = -(LINE - ry)%2;
155: alpha = 0;
156: continue;
157: default:
158: continue;
159: }
160:
161: default:
162: sendpt(alpha);
163: if (alpha==0) {
164: putchar(US);
165: alpha = 1;
166: }
167: putchar(i);
168: if (i>' ')
169: xx += 31;
170: continue;
171: }
172: }
173: xx = xoff;
174: yy = 0;
175: sendpt(0);
176: putchar(US);
177: kwait();
178: ex();
179: }
180:
181: init()
182: {
183: static flags;
184: ohx = oxb = olx = ohy = oly = -1;
185: if (ncol >= maxcol) {
186: ncol = 0;
187: if (maxcol > 1)
188: xoff = 0;
189: else
190: xoff = XOFF;
191: } else
192: xoff += coff;
193: xx = xoff;
194: yy = MAXY;
195: if (ncol==0 && erase)
196: fputs("\033\014\033;", stdout);
197: sendpt(!erase);
198: if(!erase && !flags && isatty(fileno(stdout))) {
199: struct sgttyb statb;
200:
201: gtty(fileno(ttyin), &statb);
202: flags = statb.sg_flags;
203: statb.sg_flags = (flags & ~ECHO) | CBREAK;
204: stty(fileno(ttyin), &statb);
205: fputs("\033\005", stdout);
206: fflush(stdout);
207: getc(ttyin);
208: xx = (getc(ttyin) << HALF) & VALID;
209: xx |= getc(ttyin) & HALFMASK;
210: yy = (getc(ttyin) << HALF) & VALID;
211: yy |= getc(ttyin) & HALFMASK;
212: xx <<= 2, yy <<= 2;
213: statb.sg_flags = flags;
214: stty(fileno(ttyin), &statb);
215: }
216: }
217:
218: ex()
219: {
220: yy = MAXY;
221: xx = 0;
222: fputs("\033;\037", stdout);
223: sendpt(1);
224: exit(0);
225: }
226:
227: kwait()
228: {
229: register c;
230:
231: fflush(stdout);
232: if (ttyin==NULL || tflg )
233: return;
234: while ((c=getc(ttyin))!='\n') {
235: if (c=='!') {
236: execom();
237: printf("!\n");
238: fflush(stdout);
239: continue;
240: }
241: if (c==EOF)
242: ex();
243: }
244: }
245:
246: execom()
247: {
248: int (*si)(), (*sq)();
249:
250: if (fork() != 0) {
251: si = signal(SIGINT, SIG_IGN);
252: sq = signal(SIGQUIT, SIG_IGN);
253: wait((int *)NULL);
254: signal(SIGINT, si);
255: signal(SIGQUIT, sq);
256: return;
257: }
258: if (isatty(fileno(stdin)) == 0) {
259: if (freopen("/dev/tty", "r", stdin)==NULL)
260: freopen("/dev/null", "r", stdin);
261: }
262: execl("/bin/sh", "sh", "-t", 0);
263: }
264:
265: sendpt(a)
266: {
267: register zz;
268: int hy,xb,ly,hx,lx;
269:
270: if (a)
271: return;
272: if ((zz = yy) < 0)
273: zz = 0;
274: hy = ((zz>>7) & 037);
275: xb = ((xx & 03) + ((zz<<2) & 014) & 017);
276: ly = ((zz>>2) & 037);
277: hx = ((xx>>7) & 037);
278: lx = ((xx>>2) & 037);
279: putchar(GS);
280: if (hy != ohy)
281: putchar(hy | 040);
282: if (xb != oxb)
283: putchar(xb | 0140);
284: if ((ly != oly) || (hx != ohx) || (xb != oxb))
285: putchar(ly | 0140);
286: if (hx != ohx)
287: putchar(hx | 040);
288: putchar(lx | 0100);
289: ohy = hy;
290: oxb = xb;
291: oly = ly;
292: ohx = hx;
293: olx = lx;
294: alpha = 0;
295: }
296: tekpoints(str)
297: register char *str;
298: {
299: register c,s = 0;
300: while((c = *str++) && ( c >= '0' && c <= '9'))
301: s = 10 * s + c - '0';
302: switch(c) {
303: case 'l':
304: case '\0':
305: return(s * LINE);
306: case 'i':
307: return(s*INCH);
308: default:
309: fprintf(stderr,"illegal scale factor: %s\n", str-1);
310: exit(1);
311: }
312: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.