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