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