xref: /csrg-svn/old/tk/tk.c (revision 1129)
1*1129Sbill static char *sccsid = "@(#)tk.c	4.1 (Berkeley) 10/01/80";
2*1129Sbill /*
3*1129Sbill  * optimize output for Tek 4014
4*1129Sbill  */
5*1129Sbill 
6*1129Sbill #include <stdio.h>
7*1129Sbill #include <signal.h>
8*1129Sbill 
9*1129Sbill #define MAXY 3071
10*1129Sbill #define LINE 47
11*1129Sbill #define XOFF 248
12*1129Sbill #define US 037
13*1129Sbill #define GS 035
14*1129Sbill #define ESC 033
15*1129Sbill #define CR 015
16*1129Sbill #define FF 014
17*1129Sbill #define SO 016
18*1129Sbill #define SI 017
19*1129Sbill 
20*1129Sbill int	pl	= 66*LINE;
21*1129Sbill int	yyll	= -1;
22*1129Sbill char	obuf[BUFSIZ];
23*1129Sbill int	xx = XOFF;
24*1129Sbill int	xoff = XOFF;
25*1129Sbill int	coff = 0;
26*1129Sbill int	ncol = 0;
27*1129Sbill int	maxcol = 1;
28*1129Sbill int	yy = MAXY;
29*1129Sbill int	ohy = -1;
30*1129Sbill int	ohx = -1;
31*1129Sbill int	oxb = -1;
32*1129Sbill int	oly = -1;
33*1129Sbill int	olx = -1;
34*1129Sbill int	alpha;
35*1129Sbill int	ry;
36*1129Sbill FILE	*ttyin;
37*1129Sbill 
38*1129Sbill main(argc, argv)
39*1129Sbill int argc;
40*1129Sbill char **argv;
41*1129Sbill {
42*1129Sbill 	register i, j;
43*1129Sbill 	extern ex();
44*1129Sbill 
45*1129Sbill 	while (--argc > 0 && (++argv)[0][0]=='-')
46*1129Sbill 		switch(argv[0][1]) {
47*1129Sbill 			case 'p':
48*1129Sbill 				if (i = atoi(&argv[0][2]))
49*1129Sbill 					pl = i;
50*1129Sbill 					yyll = MAXY + 1 - pl;
51*1129Sbill 				break;
52*1129Sbill 			default:
53*1129Sbill 				if (i = atoi(&argv[0][1])) {
54*1129Sbill 					maxcol = i;
55*1129Sbill 					xx = xoff = 0;
56*1129Sbill 					coff = 4096/i;
57*1129Sbill 				}
58*1129Sbill 				break;
59*1129Sbill 		}
60*1129Sbill 	if ((ttyin = fopen("/dev/tty", "r")) != NULL)
61*1129Sbill 		setbuf(ttyin, (char *)NULL);
62*1129Sbill 	if (argc) {
63*1129Sbill 		if (freopen(argv[0], "r", stdin) == NULL) {
64*1129Sbill 			fprintf(stderr, "tk: cannot open %s\n", argv[0]);
65*1129Sbill 			exit(1);
66*1129Sbill 		}
67*1129Sbill 	}
68*1129Sbill 	signal(SIGINT, ex);
69*1129Sbill 	setbuf(stdout, obuf);
70*1129Sbill 	ncol = maxcol;
71*1129Sbill 	init();
72*1129Sbill 	while ((i = getchar()) != EOF) {
73*1129Sbill 		switch(i) {
74*1129Sbill 
75*1129Sbill 		case FF:
76*1129Sbill 			yy = 0;
77*1129Sbill 		case '\n':
78*1129Sbill 			xx = xoff;
79*1129Sbill 			yy -= LINE;
80*1129Sbill 			alpha = 0;
81*1129Sbill 			if (yy < yyll) {
82*1129Sbill 				ncol++;
83*1129Sbill 				yy = 0;
84*1129Sbill 				sendpt(0);
85*1129Sbill 				putchar(US);
86*1129Sbill 				fflush(stdout);
87*1129Sbill 				if (ncol >= maxcol)
88*1129Sbill 					kwait();
89*1129Sbill 				init();
90*1129Sbill 			}
91*1129Sbill 			continue;
92*1129Sbill 
93*1129Sbill 		case CR:
94*1129Sbill 			xx = xoff;
95*1129Sbill 			alpha = 0;
96*1129Sbill 			continue;
97*1129Sbill 
98*1129Sbill 		case ' ':
99*1129Sbill 			xx += 31;
100*1129Sbill 			alpha = 0;
101*1129Sbill 			continue;
102*1129Sbill 
103*1129Sbill 		case '\t': /*tabstops at 8*31=248*/
104*1129Sbill 			j = ((xx-xoff)/248) + 1;
105*1129Sbill 			xx += j*248 - (xx-xoff);
106*1129Sbill 			alpha = 0;
107*1129Sbill 			continue;
108*1129Sbill 
109*1129Sbill 		case '\b':
110*1129Sbill 			xx -= 31;
111*1129Sbill 			alpha = 0;
112*1129Sbill 			continue;
113*1129Sbill 
114*1129Sbill 		case ESC:
115*1129Sbill 			switch(i = getchar()) {
116*1129Sbill 			case '7':
117*1129Sbill 				yy += LINE;
118*1129Sbill 				alpha = 0;
119*1129Sbill 				continue;
120*1129Sbill 			case '8':
121*1129Sbill 				yy += (LINE + ry)/2;
122*1129Sbill 				ry = (LINE + ry)%2;
123*1129Sbill 				alpha = 0;
124*1129Sbill 				continue;
125*1129Sbill 			case '9':
126*1129Sbill 				yy -= (LINE - ry)/2;
127*1129Sbill 				ry = -(LINE - ry)%2;
128*1129Sbill 				alpha = 0;
129*1129Sbill 				continue;
130*1129Sbill 			default:
131*1129Sbill 				continue;
132*1129Sbill 			}
133*1129Sbill 
134*1129Sbill 		default:
135*1129Sbill 			sendpt(alpha);
136*1129Sbill 			if (alpha==0) {
137*1129Sbill 				putchar(US);
138*1129Sbill 				alpha = 1;
139*1129Sbill 			}
140*1129Sbill 			putchar(i);
141*1129Sbill 			if (i>' ')
142*1129Sbill 				xx += 31;
143*1129Sbill 			continue;
144*1129Sbill 		}
145*1129Sbill 	}
146*1129Sbill 	xx = xoff;
147*1129Sbill 	yy = 0;
148*1129Sbill 	sendpt(0);
149*1129Sbill 	putchar(US);
150*1129Sbill 	kwait();
151*1129Sbill 	ex();
152*1129Sbill }
153*1129Sbill 
154*1129Sbill init()
155*1129Sbill {
156*1129Sbill 	ohx = oxb = olx = ohy = oly = -1;
157*1129Sbill 	if (ncol >= maxcol) {
158*1129Sbill 		ncol = 0;
159*1129Sbill 		if (maxcol > 1)
160*1129Sbill 			xoff = 0;
161*1129Sbill 		else
162*1129Sbill 			xoff = XOFF;
163*1129Sbill 	} else
164*1129Sbill 		xoff += coff;
165*1129Sbill 	xx = xoff;
166*1129Sbill 	yy = MAXY;
167*1129Sbill 	if (ncol==0)
168*1129Sbill 		fputs("\033\014\033;", stdout);
169*1129Sbill 	sendpt(0);
170*1129Sbill }
171*1129Sbill 
172*1129Sbill ex()
173*1129Sbill {
174*1129Sbill 	yy = MAXY;
175*1129Sbill 	xx = 0;
176*1129Sbill 	fputs("\033;\037", stdout);
177*1129Sbill 	sendpt(1);
178*1129Sbill 	exit(0);
179*1129Sbill }
180*1129Sbill 
181*1129Sbill kwait()
182*1129Sbill {
183*1129Sbill 	register c;
184*1129Sbill 
185*1129Sbill 	fflush(stdout);
186*1129Sbill 	if (ttyin==NULL)
187*1129Sbill 		return;
188*1129Sbill 	while ((c=getc(ttyin))!='\n') {
189*1129Sbill 		if (c=='!') {
190*1129Sbill 			execom();
191*1129Sbill 			printf("!\n");
192*1129Sbill 			fflush(stdout);
193*1129Sbill 			continue;
194*1129Sbill 		}
195*1129Sbill 		if (c==EOF)
196*1129Sbill 			ex();
197*1129Sbill 	}
198*1129Sbill }
199*1129Sbill 
200*1129Sbill execom()
201*1129Sbill {
202*1129Sbill 	int (*si)(), (*sq)();
203*1129Sbill 
204*1129Sbill 	if (fork() != 0) {
205*1129Sbill 		si = signal(SIGINT, SIG_IGN);
206*1129Sbill 		sq = signal(SIGQUIT, SIG_IGN);
207*1129Sbill 		wait((int *)NULL);
208*1129Sbill 		signal(SIGINT, si);
209*1129Sbill 		signal(SIGQUIT, sq);
210*1129Sbill 		return;
211*1129Sbill 	}
212*1129Sbill 	if (isatty(fileno(stdin)) == 0) {
213*1129Sbill 		if (freopen("/dev/tty", "r", stdin)==NULL)
214*1129Sbill 			freopen("/dev/null", "r", stdin);
215*1129Sbill 	}
216*1129Sbill 	execl("/bin/sh", "sh", "-t", 0);
217*1129Sbill }
218*1129Sbill 
219*1129Sbill sendpt(a)
220*1129Sbill {
221*1129Sbill 	register zz;
222*1129Sbill 	int hy,xb,ly,hx,lx;
223*1129Sbill 
224*1129Sbill 	if (a)
225*1129Sbill 		return;
226*1129Sbill 	if ((zz = yy) < 0)
227*1129Sbill 		zz = 0;
228*1129Sbill 	hy = ((zz>>7) & 037);
229*1129Sbill 	xb = ((xx & 03) + ((zz<<2) & 014) & 017);
230*1129Sbill 	ly = ((zz>>2) & 037);
231*1129Sbill 	hx = ((xx>>7) & 037);
232*1129Sbill 	lx = ((xx>>2) & 037);
233*1129Sbill 	putchar(GS);
234*1129Sbill 	if (hy != ohy)
235*1129Sbill 		putchar(hy | 040);
236*1129Sbill 	if (xb != oxb)
237*1129Sbill 		putchar(xb | 0140);
238*1129Sbill 	if ((ly != oly) || (hx != ohx) || (xb != oxb))
239*1129Sbill 		putchar(ly | 0140);
240*1129Sbill 	if (hx != ohx)
241*1129Sbill 		putchar(hx | 040);
242*1129Sbill 	putchar(lx | 0100);
243*1129Sbill 	ohy = hy;
244*1129Sbill 	oxb = xb;
245*1129Sbill 	oly = ly;
246*1129Sbill 	ohx = hx;
247*1129Sbill 	olx = lx;
248*1129Sbill 	alpha = 0;
249*1129Sbill }
250