xref: /csrg-svn/old/tk/tk.c (revision 37944)
1*37944Sbostic static char *sccsid = "@(#)tk.c	4.3 (Berkeley) 05/11/89";
21129Sbill /*
31129Sbill  * optimize output for Tek 4014
41129Sbill  */
51129Sbill 
6*37944Sbostic #include <sys/signal.h>
71129Sbill #include <stdio.h>
8*37944Sbostic #include <paths.h>
91129Sbill 
101129Sbill #define MAXY 3071
111129Sbill #define LINE 47
121129Sbill #define XOFF 248
131129Sbill #define US 037
141129Sbill #define GS 035
151129Sbill #define ESC 033
161129Sbill #define CR 015
171129Sbill #define FF 014
181129Sbill #define SO 016
191129Sbill #define SI 017
201129Sbill 
211129Sbill int	pl	= 66*LINE;
221129Sbill int	yyll	= -1;
231129Sbill int	xx = XOFF;
241129Sbill int	xoff = XOFF;
251129Sbill int	coff = 0;
261129Sbill int	ncol = 0;
271129Sbill int	maxcol = 1;
281129Sbill int	yy = MAXY;
291129Sbill int	ohy = -1;
301129Sbill int	ohx = -1;
311129Sbill int	oxb = -1;
321129Sbill int	oly = -1;
331129Sbill int	olx = -1;
341129Sbill int	alpha;
351129Sbill int	ry;
361129Sbill FILE	*ttyin;
371129Sbill 
main(argc,argv)381129Sbill main(argc, argv)
391129Sbill int argc;
401129Sbill char **argv;
411129Sbill {
421129Sbill 	register i, j;
431129Sbill 	extern ex();
441129Sbill 
451129Sbill 	while (--argc > 0 && (++argv)[0][0]=='-')
461129Sbill 		switch(argv[0][1]) {
471129Sbill 			case 'p':
481129Sbill 				if (i = atoi(&argv[0][2]))
491129Sbill 					pl = i;
501129Sbill 					yyll = MAXY + 1 - pl;
511129Sbill 				break;
521129Sbill 			default:
531129Sbill 				if (i = atoi(&argv[0][1])) {
541129Sbill 					maxcol = i;
551129Sbill 					xx = xoff = 0;
561129Sbill 					coff = 4096/i;
571129Sbill 				}
581129Sbill 				break;
591129Sbill 		}
60*37944Sbostic 	if ((ttyin = fopen(_PATH_TTY, "r")) != NULL)
611129Sbill 		setbuf(ttyin, (char *)NULL);
621129Sbill 	if (argc) {
631129Sbill 		if (freopen(argv[0], "r", stdin) == NULL) {
641129Sbill 			fprintf(stderr, "tk: cannot open %s\n", argv[0]);
651129Sbill 			exit(1);
661129Sbill 		}
671129Sbill 	}
681129Sbill 	signal(SIGINT, ex);
691129Sbill 	ncol = maxcol;
701129Sbill 	init();
711129Sbill 	while ((i = getchar()) != EOF) {
721129Sbill 		switch(i) {
731129Sbill 
741129Sbill 		case FF:
751129Sbill 			yy = 0;
761129Sbill 		case '\n':
771129Sbill 			xx = xoff;
781129Sbill 			yy -= LINE;
791129Sbill 			alpha = 0;
801129Sbill 			if (yy < yyll) {
811129Sbill 				ncol++;
821129Sbill 				yy = 0;
831129Sbill 				sendpt(0);
841129Sbill 				putchar(US);
851129Sbill 				fflush(stdout);
861129Sbill 				if (ncol >= maxcol)
871129Sbill 					kwait();
881129Sbill 				init();
891129Sbill 			}
901129Sbill 			continue;
911129Sbill 
921129Sbill 		case CR:
931129Sbill 			xx = xoff;
941129Sbill 			alpha = 0;
951129Sbill 			continue;
961129Sbill 
971129Sbill 		case ' ':
981129Sbill 			xx += 31;
991129Sbill 			alpha = 0;
1001129Sbill 			continue;
1011129Sbill 
1021129Sbill 		case '\t': /*tabstops at 8*31=248*/
1031129Sbill 			j = ((xx-xoff)/248) + 1;
1041129Sbill 			xx += j*248 - (xx-xoff);
1051129Sbill 			alpha = 0;
1061129Sbill 			continue;
1071129Sbill 
1081129Sbill 		case '\b':
1091129Sbill 			xx -= 31;
1101129Sbill 			alpha = 0;
1111129Sbill 			continue;
1121129Sbill 
1131129Sbill 		case ESC:
1141129Sbill 			switch(i = getchar()) {
1151129Sbill 			case '7':
1161129Sbill 				yy += LINE;
1171129Sbill 				alpha = 0;
1181129Sbill 				continue;
1191129Sbill 			case '8':
1201129Sbill 				yy += (LINE + ry)/2;
1211129Sbill 				ry = (LINE + ry)%2;
1221129Sbill 				alpha = 0;
1231129Sbill 				continue;
1241129Sbill 			case '9':
1251129Sbill 				yy -= (LINE - ry)/2;
1261129Sbill 				ry = -(LINE - ry)%2;
1271129Sbill 				alpha = 0;
1281129Sbill 				continue;
1291129Sbill 			default:
1301129Sbill 				continue;
1311129Sbill 			}
1321129Sbill 
1331129Sbill 		default:
1341129Sbill 			sendpt(alpha);
1351129Sbill 			if (alpha==0) {
1361129Sbill 				putchar(US);
1371129Sbill 				alpha = 1;
1381129Sbill 			}
1391129Sbill 			putchar(i);
1401129Sbill 			if (i>' ')
1411129Sbill 				xx += 31;
1421129Sbill 			continue;
1431129Sbill 		}
1441129Sbill 	}
1451129Sbill 	xx = xoff;
1461129Sbill 	yy = 0;
1471129Sbill 	sendpt(0);
1481129Sbill 	putchar(US);
1491129Sbill 	kwait();
1501129Sbill 	ex();
1511129Sbill }
1521129Sbill 
init()1531129Sbill init()
1541129Sbill {
1551129Sbill 	ohx = oxb = olx = ohy = oly = -1;
1561129Sbill 	if (ncol >= maxcol) {
1571129Sbill 		ncol = 0;
1581129Sbill 		if (maxcol > 1)
1591129Sbill 			xoff = 0;
1601129Sbill 		else
1611129Sbill 			xoff = XOFF;
1621129Sbill 	} else
1631129Sbill 		xoff += coff;
1641129Sbill 	xx = xoff;
1651129Sbill 	yy = MAXY;
1661129Sbill 	if (ncol==0)
1671129Sbill 		fputs("\033\014\033;", stdout);
1681129Sbill 	sendpt(0);
1691129Sbill }
1701129Sbill 
ex()1711129Sbill ex()
1721129Sbill {
1731129Sbill 	yy = MAXY;
1741129Sbill 	xx = 0;
1751129Sbill 	fputs("\033;\037", stdout);
1761129Sbill 	sendpt(1);
1771129Sbill 	exit(0);
1781129Sbill }
1791129Sbill 
kwait()1801129Sbill kwait()
1811129Sbill {
1821129Sbill 	register c;
1831129Sbill 
1841129Sbill 	fflush(stdout);
1851129Sbill 	if (ttyin==NULL)
1861129Sbill 		return;
1871129Sbill 	while ((c=getc(ttyin))!='\n') {
1881129Sbill 		if (c=='!') {
1891129Sbill 			execom();
1901129Sbill 			printf("!\n");
1911129Sbill 			fflush(stdout);
1921129Sbill 			continue;
1931129Sbill 		}
1941129Sbill 		if (c==EOF)
1951129Sbill 			ex();
1961129Sbill 	}
1971129Sbill }
1981129Sbill 
execom()1991129Sbill execom()
2001129Sbill {
2011129Sbill 	int (*si)(), (*sq)();
2021129Sbill 
2031129Sbill 	if (fork() != 0) {
2041129Sbill 		si = signal(SIGINT, SIG_IGN);
2051129Sbill 		sq = signal(SIGQUIT, SIG_IGN);
2061129Sbill 		wait((int *)NULL);
2071129Sbill 		signal(SIGINT, si);
2081129Sbill 		signal(SIGQUIT, sq);
2091129Sbill 		return;
2101129Sbill 	}
2111129Sbill 	if (isatty(fileno(stdin)) == 0) {
212*37944Sbostic 		if (freopen(_PATH_TTY, "r", stdin)==NULL)
213*37944Sbostic 			freopen(_PATH_DEVNULL, "r", stdin);
2141129Sbill 	}
215*37944Sbostic 	execl(_PATH_BSHELL, "sh", "-t", 0);
2161129Sbill }
2171129Sbill 
sendpt(a)2181129Sbill sendpt(a)
2191129Sbill {
2201129Sbill 	register zz;
2211129Sbill 	int hy,xb,ly,hx,lx;
2221129Sbill 
2231129Sbill 	if (a)
2241129Sbill 		return;
2251129Sbill 	if ((zz = yy) < 0)
2261129Sbill 		zz = 0;
2271129Sbill 	hy = ((zz>>7) & 037);
2281129Sbill 	xb = ((xx & 03) + ((zz<<2) & 014) & 017);
2291129Sbill 	ly = ((zz>>2) & 037);
2301129Sbill 	hx = ((xx>>7) & 037);
2311129Sbill 	lx = ((xx>>2) & 037);
2321129Sbill 	putchar(GS);
2331129Sbill 	if (hy != ohy)
2341129Sbill 		putchar(hy | 040);
2351129Sbill 	if (xb != oxb)
2361129Sbill 		putchar(xb | 0140);
2371129Sbill 	if ((ly != oly) || (hx != ohx) || (xb != oxb))
2381129Sbill 		putchar(ly | 0140);
2391129Sbill 	if (hx != ohx)
2401129Sbill 		putchar(hx | 040);
2411129Sbill 	putchar(lx | 0100);
2421129Sbill 	ohy = hy;
2431129Sbill 	oxb = xb;
2441129Sbill 	oly = ly;
2451129Sbill 	ohx = hx;
2461129Sbill 	olx = lx;
2471129Sbill 	alpha = 0;
2481129Sbill }
249