Annotation of src/usr.bin/window/ttzapple.c, Revision 1.3
1.3 ! downsj 1: /* $OpenBSD$ */
1.1 deraadt 2: /* $NetBSD: ttzapple.c,v 1.3 1995/09/28 10:34:57 tls Exp $ */
3:
4: /*
5: * Copyright (c) 1989, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Edward Wang at The University of California, Berkeley.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: #if 0
42: static char sccsid[] = "@(#)ttzapple.c 8.1 (Berkeley) 6/6/93";
43: #else
1.3 ! downsj 44: static char rcsid[] = "$OpenBSD$";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include "ww.h"
49: #include "tt.h"
50: #include "char.h"
51:
52: /*
53: zz|zapple|perfect apple:\
54: :am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
55: :ho=\E0:ll=\E1:cm=\E=%+ %+ :ch=\E<%+ :cv=\E>%+ :\
56: :cl=\E4:ce=\E2:cd=\E3:rp=\E@%.%+ :\
57: :so=\E+:se=\E-:\
58: :dc=\Ec:DC=\EC%+ :ic=\Ei:IC=\EI%+ :\
59: :al=\Ea:AL=\EA%+ :dl=\Ed:DL=\ED%+ :\
60: :sf=\Ef:SF=\EF%+ :sr=\Er:SR=\ER%+ :cs=\E?%+ %+ :\
61: :is=\E-\ET :
62: */
63:
64: #define NCOL 80
65: #define NROW 24
66: #define TOKEN_MAX 32
67:
68: extern short gen_frame[];
69:
70: /* for error correction */
71: int zz_ecc;
72: int zz_lastc;
73:
74: /* for checkpointing */
75: int zz_sum;
76:
77: zz_setmodes(new)
78: {
79: if (new & WWM_REV) {
80: if ((tt.tt_modes & WWM_REV) == 0)
81: ttesc('+');
82: } else
83: if (tt.tt_modes & WWM_REV)
84: ttesc('-');
85: tt.tt_modes = new;
86: }
87:
88: zz_insline(n)
89: {
90: if (n == 1)
91: ttesc('a');
92: else {
93: ttesc('A');
94: ttputc(n + ' ');
95: }
96: }
97:
98: zz_delline(n)
99: {
100: if (n == 1)
101: ttesc('d');
102: else {
103: ttesc('D');
104: ttputc(n + ' ');
105: }
106: }
107:
108: zz_putc(c)
109: char c;
110: {
111: if (tt.tt_nmodes != tt.tt_modes)
112: zz_setmodes(tt.tt_nmodes);
113: ttputc(c);
114: if (++tt.tt_col == NCOL)
115: tt.tt_col = 0, tt.tt_row++;
116: }
117:
118: zz_write(p, n)
119: register char *p;
120: register n;
121: {
122: if (tt.tt_nmodes != tt.tt_modes)
123: zz_setmodes(tt.tt_nmodes);
124: ttwrite(p, n);
125: tt.tt_col += n;
126: if (tt.tt_col == NCOL)
127: tt.tt_col = 0, tt.tt_row++;
128: }
129:
130: zz_move(row, col)
131: register row, col;
132: {
133: register x;
134:
135: if (tt.tt_row == row) {
136: same_row:
137: if ((x = col - tt.tt_col) == 0)
138: return;
139: if (col == 0) {
140: ttctrl('m');
141: goto out;
142: }
143: switch (x) {
144: case 2:
145: ttctrl('f');
146: case 1:
147: ttctrl('f');
148: goto out;
149: case -2:
150: ttctrl('h');
151: case -1:
152: ttctrl('h');
153: goto out;
154: }
155: if ((col & 7) == 0 && x > 0 && x <= 16) {
156: ttctrl('i');
157: if (x > 8)
158: ttctrl('i');
159: goto out;
160: }
161: ttesc('<');
162: ttputc(col + ' ');
163: goto out;
164: }
165: if (tt.tt_col == col) {
166: switch (row - tt.tt_row) {
167: case 2:
168: ttctrl('j');
169: case 1:
170: ttctrl('j');
171: goto out;
172: case -2:
173: ttctrl('k');
174: case -1:
175: ttctrl('k');
176: goto out;
177: }
178: if (col == 0) {
179: if (row == 0)
180: goto home;
181: if (row == NROW - 1)
182: goto ll;
183: }
184: ttesc('>');
185: ttputc(row + ' ');
186: goto out;
187: }
188: if (col == 0) {
189: if (row == 0) {
190: home:
191: ttesc('0');
192: goto out;
193: }
194: if (row == tt.tt_row + 1) {
195: /*
196: * Do newline first to match the sequence
197: * for scroll down and return
198: */
199: ttctrl('j');
200: ttctrl('m');
201: goto out;
202: }
203: if (row == NROW - 1) {
204: ll:
205: ttesc('1');
206: goto out;
207: }
208: }
209: /* favor local motion for better compression */
210: if (row == tt.tt_row + 1) {
211: ttctrl('j');
212: goto same_row;
213: }
214: if (row == tt.tt_row - 1) {
215: ttctrl('k');
216: goto same_row;
217: }
218: ttesc('=');
219: ttputc(' ' + row);
220: ttputc(' ' + col);
221: out:
222: tt.tt_col = col;
223: tt.tt_row = row;
224: }
225:
226: zz_start()
227: {
228: ttesc('T');
229: ttputc(TOKEN_MAX + ' ');
230: ttesc('U');
231: ttputc('!');
232: zz_ecc = 1;
233: zz_lastc = -1;
234: ttesc('v');
235: ttflush();
236: zz_sum = 0;
237: zz_setscroll(0, NROW - 1);
238: zz_clear();
239: zz_setmodes(0);
240: }
241:
242: zz_reset()
243: {
244: zz_setscroll(0, NROW - 1);
245: tt.tt_modes = WWM_REV;
246: zz_setmodes(0);
247: tt.tt_col = tt.tt_row = -10;
248: }
249:
250: zz_end()
251: {
252: ttesc('T');
253: ttputc(' ');
254: ttesc('U');
255: ttputc(' ');
256: zz_ecc = 0;
257: }
258:
259: zz_clreol()
260: {
261: ttesc('2');
262: }
263:
264: zz_clreos()
265: {
266: ttesc('3');
267: }
268:
269: zz_clear()
270: {
271: ttesc('4');
272: tt.tt_col = tt.tt_row = 0;
273: }
274:
275: zz_insspace(n)
276: {
277: if (n == 1)
278: ttesc('i');
279: else {
280: ttesc('I');
281: ttputc(n + ' ');
282: }
283: }
284:
285: zz_delchar(n)
286: {
287: if (n == 1)
288: ttesc('c');
289: else {
290: ttesc('C');
291: ttputc(n + ' ');
292: }
293: }
294:
295: zz_scroll_down(n)
296: {
297: if (n == 1)
298: if (tt.tt_row == NROW - 1)
299: ttctrl('j');
300: else
301: ttesc('f');
302: else {
303: ttesc('F');
304: ttputc(n + ' ');
305: }
306: }
307:
308: zz_scroll_up(n)
309: {
310: if (n == 1)
311: ttesc('r');
312: else {
313: ttesc('R');
314: ttputc(n + ' ');
315: }
316: }
317:
318: zz_setscroll(top, bot)
319: {
320: ttesc('?');
321: ttputc(top + ' ');
322: ttputc(bot + ' ');
323: tt.tt_scroll_top = top;
324: tt.tt_scroll_bot = bot;
325: }
326:
327: int zz_debug = 0;
328:
329: zz_set_token(t, s, n)
330: char *s;
331: {
332: if (tt.tt_nmodes != tt.tt_modes)
333: zz_setmodes(tt.tt_nmodes);
334: if (zz_debug) {
335: char buf[100];
336: zz_setmodes(WWM_REV);
337: (void) sprintf(buf, "%02x=", t);
338: ttputs(buf);
339: tt.tt_col += 3;
340: }
341: ttputc(0x80);
342: ttputc(t + 1);
343: s[n - 1] |= 0x80;
344: ttwrite(s, n);
345: s[n - 1] &= ~0x80;
346: }
347:
348: /*ARGSUSED*/
349: zz_put_token(t, s, n)
350: char *s;
351: {
352: if (tt.tt_nmodes != tt.tt_modes)
353: zz_setmodes(tt.tt_nmodes);
354: if (zz_debug) {
355: char buf[100];
356: zz_setmodes(WWM_REV);
357: (void) sprintf(buf, "%02x>", t);
358: ttputs(buf);
359: tt.tt_col += 3;
360: }
361: ttputc(t + 0x81);
362: }
363:
364: zz_rint(p, n)
365: char *p;
366: {
367: register i;
368: register char *q;
369:
370: if (!zz_ecc)
371: return n;
372: for (i = n, q = p; --i >= 0;) {
373: register c = (unsigned char) *p++;
374:
375: if (zz_lastc == 0) {
376: switch (c) {
377: case 0:
378: *q++ = 0;
379: zz_lastc = -1;
380: break;
381: case 1: /* start input ecc */
382: zz_ecc = 2;
383: zz_lastc = -1;
384: wwnreadstat++;
385: break;
386: case 2: /* ack checkpoint */
387: tt.tt_ack = 1;
388: zz_lastc = -1;
389: wwnreadack++;
390: break;
391: case 3: /* nack checkpoint */
392: tt.tt_ack = -1;
393: zz_lastc = -1;
394: wwnreadnack++;
395: break;
396: default:
397: zz_lastc = c;
398: wwnreadec++;
399: }
400: } else if (zz_ecc == 1) {
401: if (c)
402: *q++ = c;
403: else
404: zz_lastc = 0;
405: } else {
406: if (zz_lastc < 0) {
407: zz_lastc = c;
408: } else if (zz_lastc == c) {
409: *q++ = zz_lastc;
410: zz_lastc = -1;
411: } else {
412: wwnreadec++;
413: zz_lastc = c;
414: }
415: }
416: }
417: return q - (p - n);
418: }
419:
420: zz_checksum(p, n)
421: register char *p;
422: register n;
423: {
424: while (--n >= 0) {
425: register c = *p++ & 0x7f;
426: c ^= zz_sum;
427: zz_sum = c << 1 | c >> 11 & 1;
428: }
429: }
430:
431: zz_compress(flag)
432: {
433: if (flag)
434: tt.tt_checksum = 0;
435: else
436: tt.tt_checksum = zz_checksum;
437: }
438:
439: zz_checkpoint()
440: {
441: static char x[] = { ctrl('['), 'V', 0, 0 };
442:
443: zz_checksum(x, sizeof x);
444: ttesc('V');
445: ttputc(' ' + (zz_sum & 0x3f));
446: ttputc(' ' + (zz_sum >> 6 & 0x3f));
447: ttflush();
448: zz_sum = 0;
449: }
450:
451: tt_zapple()
452: {
453: tt.tt_insspace = zz_insspace;
454: tt.tt_delchar = zz_delchar;
455: tt.tt_insline = zz_insline;
456: tt.tt_delline = zz_delline;
457: tt.tt_clreol = zz_clreol;
458: tt.tt_clreos = zz_clreos;
459: tt.tt_scroll_down = zz_scroll_down;
460: tt.tt_scroll_up = zz_scroll_up;
461: tt.tt_setscroll = zz_setscroll;
462: tt.tt_availmodes = WWM_REV;
463: tt.tt_wrap = 1;
464: tt.tt_retain = 0;
465: tt.tt_ncol = NCOL;
466: tt.tt_nrow = NROW;
467: tt.tt_start = zz_start;
468: tt.tt_reset = zz_reset;
469: tt.tt_end = zz_end;
470: tt.tt_write = zz_write;
471: tt.tt_putc = zz_putc;
472: tt.tt_move = zz_move;
473: tt.tt_clear = zz_clear;
474: tt.tt_setmodes = zz_setmodes;
475: tt.tt_frame = gen_frame;
476: tt.tt_padc = TT_PADC_NONE;
477: tt.tt_ntoken = 127;
478: tt.tt_set_token = zz_set_token;
479: tt.tt_put_token = zz_put_token;
480: tt.tt_token_min = 1;
481: tt.tt_token_max = TOKEN_MAX;
482: tt.tt_set_token_cost = 2;
483: tt.tt_put_token_cost = 1;
484: tt.tt_compress = zz_compress;
485: tt.tt_checksum = zz_checksum;
486: tt.tt_checkpoint = zz_checkpoint;
487: tt.tt_reset = zz_reset;
488: tt.tt_rint = zz_rint;
489: return 0;
490: }