xref: /original-bsd/usr.bin/window/ttzapple.c (revision 9e0a9e1b)
194888b1cSedward /*
2*9e0a9e1bSbostic  * Copyright (c) 1989, 1993
3*9e0a9e1bSbostic  *	The Regents of the University of California.  All rights reserved.
494888b1cSedward  *
58e751acfSbostic  * This code is derived from software contributed to Berkeley by
68e751acfSbostic  * Edward Wang at The University of California, Berkeley.
78e751acfSbostic  *
8122a1d9eSbostic  * %sccs.include.redist.c%
994888b1cSedward  */
1094888b1cSedward 
1194888b1cSedward #ifndef lint
12*9e0a9e1bSbostic static char sccsid[] = "@(#)ttzapple.c	8.1 (Berkeley) 06/06/93";
1394888b1cSedward #endif /* not lint */
1494888b1cSedward 
1594888b1cSedward #include "ww.h"
1694888b1cSedward #include "tt.h"
1794888b1cSedward #include "char.h"
1894888b1cSedward 
1994888b1cSedward /*
209fbe1aabSedward zz|zapple|perfect apple:\
2194888b1cSedward 	:am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
229fbe1aabSedward 	:ho=\E0:ll=\E1:cm=\E=%+ %+ :ch=\E<%+ :cv=\E>%+ :\
239fbe1aabSedward 	:cl=\E4:ce=\E2:cd=\E3:rp=\E@%.%+ :\
249fbe1aabSedward 	:so=\E+:se=\E-:\
259fbe1aabSedward 	:dc=\Ec:DC=\EC%+ :ic=\Ei:IC=\EI%+ :\
269fbe1aabSedward 	:al=\Ea:AL=\EA%+ :dl=\Ed:DL=\ED%+ :\
279fbe1aabSedward 	:sf=\Ef:SF=\EF%+ :sr=\Er:SR=\ER%+ :cs=\E?%+ %+ :\
289fbe1aabSedward 	:is=\E-\ET :
2994888b1cSedward */
3094888b1cSedward 
3194888b1cSedward #define NCOL		80
3294888b1cSedward #define NROW		24
335ec5e80aSedward #define TOKEN_MAX	32
3494888b1cSedward 
3594888b1cSedward extern short gen_frame[];
3694888b1cSedward 
374277bd53Sedward 	/* for error correction */
384277bd53Sedward int zz_ecc;
393b2f06aaSedward int zz_lastc;
403b2f06aaSedward 
413b2f06aaSedward 	/* for checkpointing */
423b2f06aaSedward int zz_sum;
434277bd53Sedward 
zz_setmodes(new)4494888b1cSedward zz_setmodes(new)
4594888b1cSedward {
4694888b1cSedward 	if (new & WWM_REV) {
479fbe1aabSedward 		if ((tt.tt_modes & WWM_REV) == 0)
4853c9470aSedward 			ttesc('+');
4994888b1cSedward 	} else
509fbe1aabSedward 		if (tt.tt_modes & WWM_REV)
5153c9470aSedward 			ttesc('-');
5294888b1cSedward 	tt.tt_modes = new;
5394888b1cSedward }
5494888b1cSedward 
zz_insline(n)5564a3ee6bSedward zz_insline(n)
5694888b1cSedward {
579fbe1aabSedward 	if (n == 1)
5853c9470aSedward 		ttesc('a');
599fbe1aabSedward 	else {
6053c9470aSedward 		ttesc('A');
6153c9470aSedward 		ttputc(n + ' ');
6264a3ee6bSedward 	}
6394888b1cSedward }
6494888b1cSedward 
zz_delline(n)6564a3ee6bSedward zz_delline(n)
6694888b1cSedward {
679fbe1aabSedward 	if (n == 1)
6853c9470aSedward 		ttesc('d');
699fbe1aabSedward 	else {
7053c9470aSedward 		ttesc('D');
7153c9470aSedward 		ttputc(n + ' ');
7264a3ee6bSedward 	}
7394888b1cSedward }
7494888b1cSedward 
zz_putc(c)7594888b1cSedward zz_putc(c)
7694888b1cSedward 	char c;
7794888b1cSedward {
7894888b1cSedward 	if (tt.tt_nmodes != tt.tt_modes)
7994888b1cSedward 		zz_setmodes(tt.tt_nmodes);
8094888b1cSedward 	ttputc(c);
8194888b1cSedward 	if (++tt.tt_col == NCOL)
8294888b1cSedward 		tt.tt_col = 0, tt.tt_row++;
8394888b1cSedward }
8494888b1cSedward 
zz_write(p,n)8594888b1cSedward zz_write(p, n)
8694888b1cSedward 	register char *p;
8794888b1cSedward 	register n;
8894888b1cSedward {
8994888b1cSedward 	if (tt.tt_nmodes != tt.tt_modes)
9094888b1cSedward 		zz_setmodes(tt.tt_nmodes);
9194888b1cSedward 	ttwrite(p, n);
9294888b1cSedward 	tt.tt_col += n;
9394888b1cSedward 	if (tt.tt_col == NCOL)
9494888b1cSedward 		tt.tt_col = 0, tt.tt_row++;
9594888b1cSedward }
9694888b1cSedward 
zz_move(row,col)9794888b1cSedward zz_move(row, col)
9894888b1cSedward 	register row, col;
9994888b1cSedward {
1005ec5e80aSedward 	register x;
1015ec5e80aSedward 
10294888b1cSedward 	if (tt.tt_row == row) {
1037175ad38Sedward same_row:
1045ec5e80aSedward 		if ((x = col - tt.tt_col) == 0)
10594888b1cSedward 			return;
10694888b1cSedward 		if (col == 0) {
10753c9470aSedward 			ttctrl('m');
10894888b1cSedward 			goto out;
10994888b1cSedward 		}
1105ec5e80aSedward 		switch (x) {
1115ec5e80aSedward 		case 2:
11253c9470aSedward 			ttctrl('f');
1135ec5e80aSedward 		case 1:
11453c9470aSedward 			ttctrl('f');
11594888b1cSedward 			goto out;
1165ec5e80aSedward 		case -2:
11753c9470aSedward 			ttctrl('h');
1185ec5e80aSedward 		case -1:
11953c9470aSedward 			ttctrl('h');
1205ec5e80aSedward 			goto out;
1215ec5e80aSedward 		}
122dddaa135Sedward 		if ((col & 7) == 0 && x > 0 && x <= 16) {
12353c9470aSedward 			ttctrl('i');
1245ec5e80aSedward 			if (x > 8)
12553c9470aSedward 				ttctrl('i');
12694888b1cSedward 			goto out;
12794888b1cSedward 		}
12853c9470aSedward 		ttesc('<');
12953c9470aSedward 		ttputc(col + ' ');
13094888b1cSedward 		goto out;
13194888b1cSedward 	}
13294888b1cSedward 	if (tt.tt_col == col) {
1335ec5e80aSedward 		switch (row - tt.tt_row) {
1345ec5e80aSedward 		case 2:
13553c9470aSedward 			ttctrl('j');
1365ec5e80aSedward 		case 1:
13753c9470aSedward 			ttctrl('j');
1385ec5e80aSedward 			goto out;
1395ec5e80aSedward 		case -2:
14053c9470aSedward 			ttctrl('k');
1415ec5e80aSedward 		case -1:
14253c9470aSedward 			ttctrl('k');
14394888b1cSedward 			goto out;
14494888b1cSedward 		}
14594888b1cSedward 		if (col == 0) {
14694888b1cSedward 			if (row == 0)
14794888b1cSedward 				goto home;
14894888b1cSedward 			if (row == NROW - 1)
14994888b1cSedward 				goto ll;
15094888b1cSedward 		}
15153c9470aSedward 		ttesc('>');
15253c9470aSedward 		ttputc(row + ' ');
15394888b1cSedward 		goto out;
15494888b1cSedward 	}
15594888b1cSedward 	if (col == 0) {
15694888b1cSedward 		if (row == 0) {
15794888b1cSedward home:
15853c9470aSedward 			ttesc('0');
15994888b1cSedward 			goto out;
16094888b1cSedward 		}
1615ec5e80aSedward 		if (row == tt.tt_row + 1) {
1627175ad38Sedward 			/*
1637175ad38Sedward 			 * Do newline first to match the sequence
1647175ad38Sedward 			 * for scroll down and return
1657175ad38Sedward 			 */
16653c9470aSedward 			ttctrl('j');
1677175ad38Sedward 			ttctrl('m');
1685ec5e80aSedward 			goto out;
1695ec5e80aSedward 		}
17094888b1cSedward 		if (row == NROW - 1) {
17194888b1cSedward ll:
17253c9470aSedward 			ttesc('1');
17394888b1cSedward 			goto out;
17494888b1cSedward 		}
17594888b1cSedward 	}
1767175ad38Sedward 	/* favor local motion for better compression */
1777175ad38Sedward 	if (row == tt.tt_row + 1) {
1787175ad38Sedward 		ttctrl('j');
1797175ad38Sedward 		goto same_row;
1807175ad38Sedward 	}
1817175ad38Sedward 	if (row == tt.tt_row - 1) {
1827175ad38Sedward 		ttctrl('k');
1837175ad38Sedward 		goto same_row;
1847175ad38Sedward 	}
18553c9470aSedward 	ttesc('=');
18653c9470aSedward 	ttputc(' ' + row);
18753c9470aSedward 	ttputc(' ' + col);
18894888b1cSedward out:
18994888b1cSedward 	tt.tt_col = col;
19094888b1cSedward 	tt.tt_row = row;
19194888b1cSedward }
19294888b1cSedward 
zz_start()1935ec5e80aSedward zz_start()
19494888b1cSedward {
19553c9470aSedward 	ttesc('T');
19653c9470aSedward 	ttputc(TOKEN_MAX + ' ');
1974277bd53Sedward 	ttesc('U');
1984277bd53Sedward 	ttputc('!');
1994277bd53Sedward 	zz_ecc = 1;
2003b2f06aaSedward 	zz_lastc = -1;
2013b2f06aaSedward 	ttesc('v');
2023b2f06aaSedward 	ttflush();
2033b2f06aaSedward 	zz_sum = 0;
2043b2f06aaSedward 	zz_setscroll(0, NROW - 1);
2053b2f06aaSedward 	zz_clear();
2063b2f06aaSedward 	zz_setmodes(0);
2073b2f06aaSedward }
2083b2f06aaSedward 
zz_reset()2093b2f06aaSedward zz_reset()
2103b2f06aaSedward {
2113b2f06aaSedward 	zz_setscroll(0, NROW - 1);
2123b2f06aaSedward 	tt.tt_modes = WWM_REV;
2133b2f06aaSedward 	zz_setmodes(0);
2143b2f06aaSedward 	tt.tt_col = tt.tt_row = -10;
21594888b1cSedward }
21694888b1cSedward 
zz_end()21794888b1cSedward zz_end()
21894888b1cSedward {
21953c9470aSedward 	ttesc('T');
22053c9470aSedward 	ttputc(' ');
2214277bd53Sedward 	ttesc('U');
2224277bd53Sedward 	ttputc(' ');
2234277bd53Sedward 	zz_ecc = 0;
22494888b1cSedward }
22594888b1cSedward 
zz_clreol()22694888b1cSedward zz_clreol()
22794888b1cSedward {
22853c9470aSedward 	ttesc('2');
22994888b1cSedward }
23094888b1cSedward 
zz_clreos()23194888b1cSedward zz_clreos()
23294888b1cSedward {
23353c9470aSedward 	ttesc('3');
23494888b1cSedward }
23594888b1cSedward 
zz_clear()23694888b1cSedward zz_clear()
23794888b1cSedward {
23853c9470aSedward 	ttesc('4');
23994888b1cSedward 	tt.tt_col = tt.tt_row = 0;
24094888b1cSedward }
24194888b1cSedward 
zz_insspace(n)242add27573Sedward zz_insspace(n)
24394888b1cSedward {
2449fbe1aabSedward 	if (n == 1)
24553c9470aSedward 		ttesc('i');
2469fbe1aabSedward 	else {
24753c9470aSedward 		ttesc('I');
24853c9470aSedward 		ttputc(n + ' ');
24964a3ee6bSedward 	}
25064a3ee6bSedward }
25164a3ee6bSedward 
zz_delchar(n)25264a3ee6bSedward zz_delchar(n)
25364a3ee6bSedward {
2549fbe1aabSedward 	if (n == 1)
25553c9470aSedward 		ttesc('c');
2569fbe1aabSedward 	else {
25753c9470aSedward 		ttesc('C');
25853c9470aSedward 		ttputc(n + ' ');
25994888b1cSedward 	}
26064a3ee6bSedward }
26194888b1cSedward 
zz_scroll_down(n)26264a3ee6bSedward zz_scroll_down(n)
26394888b1cSedward {
2649fbe1aabSedward 	if (n == 1)
2659fbe1aabSedward 		if (tt.tt_row == NROW - 1)
26653c9470aSedward 			ttctrl('j');
2679fbe1aabSedward 		else
26853c9470aSedward 			ttesc('f');
26994888b1cSedward 	else {
27053c9470aSedward 		ttesc('F');
27153c9470aSedward 		ttputc(n + ' ');
27294888b1cSedward 	}
27394888b1cSedward }
27494888b1cSedward 
zz_scroll_up(n)27564a3ee6bSedward zz_scroll_up(n)
27694888b1cSedward {
2779fbe1aabSedward 	if (n == 1)
27853c9470aSedward 		ttesc('r');
2799fbe1aabSedward 	else {
28053c9470aSedward 		ttesc('R');
28153c9470aSedward 		ttputc(n + ' ');
28264a3ee6bSedward 	}
28394888b1cSedward }
28494888b1cSedward 
zz_setscroll(top,bot)28594888b1cSedward zz_setscroll(top, bot)
28694888b1cSedward {
28753c9470aSedward 	ttesc('?');
28853c9470aSedward 	ttputc(top + ' ');
28953c9470aSedward 	ttputc(bot + ' ');
29094888b1cSedward 	tt.tt_scroll_top = top;
29194888b1cSedward 	tt.tt_scroll_bot = bot;
29294888b1cSedward }
29394888b1cSedward 
2945ec5e80aSedward int zz_debug = 0;
2955ec5e80aSedward 
zz_set_token(t,s,n)2965ec5e80aSedward zz_set_token(t, s, n)
29764a3ee6bSedward 	char *s;
29894888b1cSedward {
2995ec5e80aSedward 	if (tt.tt_nmodes != tt.tt_modes)
3005ec5e80aSedward 		zz_setmodes(tt.tt_nmodes);
3015ec5e80aSedward 	if (zz_debug) {
3025ec5e80aSedward 		char buf[100];
3035ec5e80aSedward 		zz_setmodes(WWM_REV);
3045ec5e80aSedward 		(void) sprintf(buf, "%02x=", t);
3055ec5e80aSedward 		ttputs(buf);
3065ec5e80aSedward 		tt.tt_col += 3;
30764a3ee6bSedward 	}
30853c9470aSedward 	ttputc(0x80);
30953c9470aSedward 	ttputc(t + 1);
3105ec5e80aSedward 	s[n - 1] |= 0x80;
3115ec5e80aSedward 	ttwrite(s, n);
3125ec5e80aSedward 	s[n - 1] &= ~0x80;
31394888b1cSedward }
31494888b1cSedward 
3155ec5e80aSedward /*ARGSUSED*/
zz_put_token(t,s,n)3165ec5e80aSedward zz_put_token(t, s, n)
3175ec5e80aSedward 	char *s;
31864a3ee6bSedward {
3195ec5e80aSedward 	if (tt.tt_nmodes != tt.tt_modes)
3205ec5e80aSedward 		zz_setmodes(tt.tt_nmodes);
3215ec5e80aSedward 	if (zz_debug) {
3225ec5e80aSedward 		char buf[100];
3235ec5e80aSedward 		zz_setmodes(WWM_REV);
3245ec5e80aSedward 		(void) sprintf(buf, "%02x>", t);
3255ec5e80aSedward 		ttputs(buf);
3265ec5e80aSedward 		tt.tt_col += 3;
32764a3ee6bSedward 	}
32853c9470aSedward 	ttputc(t + 0x81);
32994888b1cSedward }
33094888b1cSedward 
zz_rint(p,n)3314277bd53Sedward zz_rint(p, n)
3324277bd53Sedward 	char *p;
3334277bd53Sedward {
3344277bd53Sedward 	register i;
3354277bd53Sedward 	register char *q;
3364277bd53Sedward 
3374277bd53Sedward 	if (!zz_ecc)
3384277bd53Sedward 		return n;
3394277bd53Sedward 	for (i = n, q = p; --i >= 0;) {
3404277bd53Sedward 		register c = (unsigned char) *p++;
3413b2f06aaSedward 
3423b2f06aaSedward 		if (zz_lastc == 0) {
3433b2f06aaSedward 			switch (c) {
3443b2f06aaSedward 			case 0:
3453b2f06aaSedward 				*q++ = 0;
3463b2f06aaSedward 				zz_lastc = -1;
3473b2f06aaSedward 				break;
3483b2f06aaSedward 			case 1:		/* start input ecc */
3493b2f06aaSedward 				zz_ecc = 2;
3503b2f06aaSedward 				zz_lastc = -1;
3513b2f06aaSedward 				wwnreadstat++;
3523b2f06aaSedward 				break;
3533b2f06aaSedward 			case 2:		/* ack checkpoint */
3543b2f06aaSedward 				tt.tt_ack = 1;
3553b2f06aaSedward 				zz_lastc = -1;
3563b2f06aaSedward 				wwnreadack++;
3573b2f06aaSedward 				break;
3583b2f06aaSedward 			case 3:		/* nack checkpoint */
3593b2f06aaSedward 				tt.tt_ack = -1;
3603b2f06aaSedward 				zz_lastc = -1;
3613b2f06aaSedward 				wwnreadnack++;
3623b2f06aaSedward 				break;
3633b2f06aaSedward 			default:
3643b2f06aaSedward 				zz_lastc = c;
3653b2f06aaSedward 				wwnreadec++;
3663b2f06aaSedward 			}
3673b2f06aaSedward 		} else if (zz_ecc == 1) {
3684277bd53Sedward 			if (c)
3694277bd53Sedward 				*q++ = c;
3703b2f06aaSedward 			else
3713b2f06aaSedward 				zz_lastc = 0;
3724277bd53Sedward 		} else {
3733b2f06aaSedward 			if (zz_lastc < 0) {
3743b2f06aaSedward 				zz_lastc = c;
3753b2f06aaSedward 			} else if (zz_lastc == c) {
3763b2f06aaSedward 				*q++ = zz_lastc;
3773b2f06aaSedward 				zz_lastc = -1;
3784277bd53Sedward 			} else {
3794277bd53Sedward 				wwnreadec++;
3803b2f06aaSedward 				zz_lastc = c;
3814277bd53Sedward 			}
3824277bd53Sedward 		}
3834277bd53Sedward 	}
3844277bd53Sedward 	return q - (p - n);
3854277bd53Sedward }
3864277bd53Sedward 
zz_checksum(p,n)3873b2f06aaSedward zz_checksum(p, n)
3883b2f06aaSedward 	register char *p;
3893b2f06aaSedward 	register n;
3903b2f06aaSedward {
3913b2f06aaSedward 	while (--n >= 0) {
3923b2f06aaSedward 		register c = *p++ & 0x7f;
3933b2f06aaSedward 		c ^= zz_sum;
3943b2f06aaSedward 		zz_sum = c << 1 | c >> 11 & 1;
3953b2f06aaSedward 	}
3963b2f06aaSedward }
3973b2f06aaSedward 
zz_compress(flag)3983b2f06aaSedward zz_compress(flag)
3993b2f06aaSedward {
4003b2f06aaSedward 	if (flag)
4013b2f06aaSedward 		tt.tt_checksum = 0;
4023b2f06aaSedward 	else
4033b2f06aaSedward 		tt.tt_checksum = zz_checksum;
4043b2f06aaSedward }
4053b2f06aaSedward 
zz_checkpoint()4063b2f06aaSedward zz_checkpoint()
4073b2f06aaSedward {
4083b2f06aaSedward 	static char x[] = { ctrl('['), 'V', 0, 0 };
4093b2f06aaSedward 
4103b2f06aaSedward 	zz_checksum(x, sizeof x);
4113b2f06aaSedward 	ttesc('V');
4123b2f06aaSedward 	ttputc(' ' + (zz_sum & 0x3f));
4133b2f06aaSedward 	ttputc(' ' + (zz_sum >> 6 & 0x3f));
4143b2f06aaSedward 	ttflush();
4153b2f06aaSedward 	zz_sum = 0;
4163b2f06aaSedward }
4173b2f06aaSedward 
tt_zapple()41894888b1cSedward tt_zapple()
41994888b1cSedward {
420add27573Sedward 	tt.tt_insspace = zz_insspace;
42194888b1cSedward 	tt.tt_delchar = zz_delchar;
42294888b1cSedward 	tt.tt_insline = zz_insline;
42394888b1cSedward 	tt.tt_delline = zz_delline;
42494888b1cSedward 	tt.tt_clreol = zz_clreol;
42594888b1cSedward 	tt.tt_clreos = zz_clreos;
42694888b1cSedward 	tt.tt_scroll_down = zz_scroll_down;
42794888b1cSedward 	tt.tt_scroll_up = zz_scroll_up;
42894888b1cSedward 	tt.tt_setscroll = zz_setscroll;
42994888b1cSedward 	tt.tt_availmodes = WWM_REV;
43094888b1cSedward 	tt.tt_wrap = 1;
43194888b1cSedward 	tt.tt_retain = 0;
4325ec5e80aSedward 	tt.tt_ncol = NCOL;
4335ec5e80aSedward 	tt.tt_nrow = NROW;
4345ec5e80aSedward 	tt.tt_start = zz_start;
4353b2f06aaSedward 	tt.tt_reset = zz_reset;
43694888b1cSedward 	tt.tt_end = zz_end;
43794888b1cSedward 	tt.tt_write = zz_write;
43894888b1cSedward 	tt.tt_putc = zz_putc;
43994888b1cSedward 	tt.tt_move = zz_move;
44094888b1cSedward 	tt.tt_clear = zz_clear;
44194888b1cSedward 	tt.tt_setmodes = zz_setmodes;
44294888b1cSedward 	tt.tt_frame = gen_frame;
4437175ad38Sedward 	tt.tt_padc = TT_PADC_NONE;
444df0f96bcSedward 	tt.tt_ntoken = 127;
4455ec5e80aSedward 	tt.tt_set_token = zz_set_token;
4465ec5e80aSedward 	tt.tt_put_token = zz_put_token;
4475ec5e80aSedward 	tt.tt_token_min = 1;
4485ec5e80aSedward 	tt.tt_token_max = TOKEN_MAX;
4495ec5e80aSedward 	tt.tt_set_token_cost = 2;
4505ec5e80aSedward 	tt.tt_put_token_cost = 1;
4513b2f06aaSedward 	tt.tt_compress = zz_compress;
4523b2f06aaSedward 	tt.tt_checksum = zz_checksum;
4533b2f06aaSedward 	tt.tt_checkpoint = zz_checkpoint;
4543b2f06aaSedward 	tt.tt_reset = zz_reset;
4554277bd53Sedward 	tt.tt_rint = zz_rint;
45694888b1cSedward 	return 0;
45794888b1cSedward }
458