ite.c revision 1.14 1 /*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
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 * from: Utah Hdr: ite.c 1.1 90/07/09
39 * from: @(#)ite.c 7.6 (Berkeley) 5/16/91
40 * $Id: ite.c,v 1.14 1994/03/20 10:13:54 chopps Exp $
41 */
42
43 /*
44 * ite - bitmaped terminal.
45 * Supports VT200, a few terminal features will be unavailable until
46 * the system actually probes the device (i.e. not after consinit())
47 */
48
49 #include "ite.h"
50 #if NITE > 0
51
52 #include <sys/param.h>
53 #include <sys/conf.h>
54 #include <sys/malloc.h>
55 #include <sys/ioctl.h>
56 #include <sys/tty.h>
57 #include <sys/termios.h>
58 #include <sys/systm.h>
59 #include <sys/proc.h>
60 #include <dev/cons.h>
61
62 #include <amiga/amiga/kdassert.h>
63 #include <amiga/dev/iteioctl.h>
64 #include <amiga/dev/itevar.h>
65 #include <amiga/dev/kbdmap.h>
66
67 struct itesw itesw[] =
68 {
69 view_cnprobe, view_init, view_deinit, 0,
70 0, 0, 0,
71 retina_cnprobe, retina_init, retina_deinit, retina_clear,
72 retina_putc, retina_cursor, retina_scroll,
73 tiga_cnprobe, tiga_init, tiga_deinit, tiga_clear,
74 tiga_putc, tiga_cursor, tiga_scroll,
75
76 };
77
78 #undef NITE
79 #define NITE 3 /* always corrisponds to itesw */
80
81 struct ite_softc ite_softc[NITE]; /* device struct */
82 struct tty *ite_tty[NITE]; /* ttys associated with ite's */
83 int nunits = sizeof(itesw) / sizeof(struct itesw);
84
85 int start_repeat_timeo = 20; /* first repeat after. */
86 int next_repeat_timeo = 5; /* next repeat after. */
87
88 int ite_default_wrap = 1; /* you want vtxxx-nam, binpatch */
89
90 u_char cons_tabs[MAX_TABS];
91
92 struct ite_softc *kbd_ite;
93 int kbd_init;
94
95 static char *index __P((const char *, char));
96 static int inline atoi __P((const char *));
97 void iteputchar __P((int c, struct ite_softc *ip));
98 void ite_putstr __P((const char * s, int len, dev_t dev));
99
100 #define SUBR_CNPROBE(min) itesw[min].ite_cnprobe(min)
101 #define SUBR_INIT(ip) ip->itesw->ite_init(ip)
102 #define SUBR_DEINIT(ip) ip->itesw->ite_deinit(ip)
103 #define SUBR_PUTC(ip,c,dy,dx,m) ip->itesw->ite_putc(ip,c,dy,dx,m)
104 #define SUBR_CURSOR(ip,flg) ip->itesw->ite_cursor(ip,flg)
105 #define SUBR_CLEAR(ip,sy,sx,h,w) ip->itesw->ite_clear(ip,sy,sx,h,w)
106 #define SUBR_SCROLL(ip,sy,sx,count,dir) \
107 ip->itesw->ite_scroll(ip,sy,sx,count,dir)
108
109 #if defined (__STDC__)
110 #define GET_CHRDEV_MAJOR(dev, maj) { \
111 for(maj = 0; maj < nchrdev; maj++) \
112 if (cdevsw[maj].d_open == dev ## open) \
113 break; \
114 }
115 #else
116 #define GET_CHRDEV_MAJOR(dev, maj) { \
117 for(maj = 0; maj < nchrdev; maj++) \
118 if (cdevsw[maj].d_open == dev/**/open) \
119 break; \
120 }
121 #endif
122
123 /*
124 * cons.c entry points into ite device.
125 */
126
127 /*
128 * Return a priority in consdev->cn_pri field highest wins. This function
129 * is called before any devices have been probed.
130 */
131 void
132 ite_cnprobe(cd)
133 struct consdev *cd;
134 {
135 int unit, last, use, maj, cur;
136
137 use = -1;
138 last = 0;
139
140 /* XXX need to bring the graphics layer up. */
141 grfconfig();
142
143 GET_CHRDEV_MAJOR(ite, maj);
144
145 cd->cn_pri = CN_DEAD;
146 for (unit = 0; unit < nunits; unit++) {
147 cur = SUBR_CNPROBE(unit);
148 if (cur == CN_DEAD)
149 continue;
150 else if (last <= cur) {
151 last = cur;
152 use = unit;
153 }
154 ite_softc[unit].flags |= ITE_ALIVE;
155 }
156 if (use != -1) {
157 cd->cn_pri = last;
158 cd->cn_dev = makedev(maj, use);
159 }
160 }
161
162 void
163 ite_cninit(cd)
164 struct consdev *cd;
165 {
166 struct ite_softc *ip = &ite_softc[minor(cd->cn_dev)];
167
168 ip->tabs = cons_tabs;
169 iteinit(cd->cn_dev); /* init console unit */
170 ip->flags |= ITE_ACTIVE | ITE_ISCONS;
171 kbd_ite = ip;
172 }
173
174 /*
175 * ite_cnfinish() is called in ite_init() when the device is
176 * being probed in the normal fasion, thus we can finish setting
177 * up this ite now that the system is more functional.
178 */
179 void
180 ite_cnfinish(ip)
181 struct ite_softc *ip;
182 {
183 static int done;
184
185 if (done)
186 return;
187 done = 1;
188 }
189
190 int
191 ite_cngetc(dev)
192 dev_t dev;
193 {
194 int c;
195
196 /* XXX this should be moved */
197 if (!kbd_init) {
198 kbd_init = 1;
199 kbdenable();
200 }
201 do {
202 c = kbdgetcn();
203 c = ite_cnfilter(c, ITEFILT_CONSOLE);
204 } while (c == -1);
205 return (c);
206 }
207
208 void
209 ite_cnputc(dev, c)
210 dev_t dev;
211 int c;
212 {
213 static int paniced = 0;
214 struct ite_softc *ip = &ite_softc[minor(dev)];
215 char ch = c;
216
217 if (panicstr && !paniced &&
218 (ip->flags & (ITE_ACTIVE | ITE_INGRF)) != ITE_ACTIVE) {
219 (void)ite_on(dev, 3);
220 paniced = 1;
221 }
222 ite_putstr(&ch, 1, dev);
223 }
224
225 /*
226 * standard entry points to the device.
227 */
228
229 /*
230 * iteinit() is the standard entry point for initialization of
231 * an ite device, it is also called from ite_cninit().
232 */
233 void
234 iteinit(dev)
235 dev_t dev;
236 {
237 struct ite_softc *ip;
238
239 ip = &ite_softc[minor(dev)];
240 if (ip->flags & ITE_INITED)
241 return;
242 bcopy(&ascii_kbdmap, &kbdmap, sizeof(struct kbdmap));
243
244 ip->cursorx = 0;
245 ip->cursory = 0;
246 ip->itesw = &itesw[minor(dev)];
247 SUBR_INIT(ip);
248 SUBR_CURSOR(ip, DRAW_CURSOR);
249 if (!ip->tabs)
250 ip->tabs =
251 malloc(MAX_TABS * sizeof(u_char), M_DEVBUF, M_WAITOK);
252 ite_reset(ip);
253 /* draw cursor? */
254 ip->flags |= ITE_INITED;
255 }
256
257 int
258 iteopen(dev, mode, devtype, p)
259 dev_t dev;
260 int mode, devtype;
261 struct proc *p;
262 {
263 int unit = minor(dev);
264 struct ite_softc *ip = &ite_softc[unit];
265 struct tty *tp;
266 int error;
267 int first = 0;
268
269 if (unit >= NITE)
270 return (ENXIO);
271
272 if (!ite_tty[unit])
273 tp = ite_tty[unit] = ttymalloc();
274 else
275 tp = ite_tty[unit];
276 if ((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) == (TS_ISOPEN | TS_XCLUDE)
277 && p->p_ucred->cr_uid != 0)
278 return (EBUSY);
279 if ((ip->flags & ITE_ACTIVE) == 0) {
280 error = ite_on(dev, 0);
281 if (error)
282 return (error);
283 first = 1;
284 }
285 tp->t_oproc = itestart;
286 tp->t_param = ite_param;
287 tp->t_dev = dev;
288 if ((tp->t_state & TS_ISOPEN) == 0) {
289 ttychars(tp);
290 tp->t_iflag = TTYDEF_IFLAG;
291 tp->t_oflag = TTYDEF_OFLAG;
292 tp->t_cflag = TTYDEF_CFLAG;
293 tp->t_lflag = TTYDEF_LFLAG;
294 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
295 tp->t_state = TS_WOPEN | TS_CARR_ON;
296 ttsetwater(tp);
297 }
298 error = (*linesw[tp->t_line].l_open) (dev, tp);
299 if (error == 0) {
300 tp->t_winsize.ws_row = ip->rows;
301 tp->t_winsize.ws_col = ip->cols;
302 if (!kbd_init) {
303 kbd_init = 1;
304 kbdenable();
305 }
306 } else if (first)
307 ite_off(dev, 0);
308 return (error);
309 }
310
311 int
312 iteclose(dev, flag, mode, p)
313 dev_t dev;
314 int flag, mode;
315 struct proc *p;
316 {
317 struct tty *tp = ite_tty[minor(dev)];
318
319 KDASSERT(tp);
320 (*linesw[tp->t_line].l_close) (tp, flag);
321 ttyclose(tp);
322 ite_off(dev, 0);
323 return (0);
324 }
325
326 int
327 iteread(dev, uio, flag)
328 dev_t dev;
329 struct uio *uio;
330 int flag;
331 {
332 struct tty *tp = ite_tty[minor(dev)];
333
334 KDASSERT(tp);
335 return ((*linesw[tp->t_line].l_read) (tp, uio, flag));
336 }
337
338 int
339 itewrite(dev, uio, flag)
340 dev_t dev;
341 struct uio *uio;
342 int flag;
343 {
344 struct tty *tp = ite_tty[minor(dev)];
345
346 KDASSERT(tp);
347 return ((*linesw[tp->t_line].l_write) (tp, uio, flag));
348 }
349
350 int
351 iteioctl(dev, cmd, addr, flag, p)
352 dev_t dev;
353 int cmd, flag;
354 caddr_t addr;
355 struct proc *p;
356 {
357 struct ite_softc *ip = &ite_softc[minor(dev)];
358 struct tty *tp = ite_tty[minor(dev)];
359 int error;
360
361 KDASSERT(tp);
362
363 error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, addr, flag, p);
364 if (error >= 0)
365 return (error);
366 error = ttioctl(tp, cmd, addr, flag, p);
367 if (error >= 0)
368 return (error);
369
370 switch (cmd) {
371 case ITELOADKMAP:
372 if (addr) {
373 bcopy(addr, &kbdmap, sizeof(struct kbdmap));
374
375 return 0;
376 } else
377 return EFAULT;
378 case ITEGETKMAP:
379 if (addr) {
380 bcopy(&kbdmap, addr, sizeof(struct kbdmap));
381
382 return 0;
383 } else
384 return EFAULT;
385 }
386 /* XXX */
387 if (minor(dev) == 0) {
388 error = ite_grf_ioctl(ip, cmd, addr, flag, p);
389 if (error >= 0)
390 return (error);
391 }
392 return (ENOTTY);
393 }
394
395 void
396 itestart(tp)
397 struct tty *tp;
398 {
399 struct clist *rbp;
400 struct ite_softc *ip = &ite_softc[minor(tp->t_dev)];
401 u_char buf[ITEBURST];
402 int s, len, n;
403
404 KDASSERT(tp);
405
406 s = spltty(); {
407 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
408 goto out;
409
410 tp->t_state |= TS_BUSY;
411 rbp = &tp->t_outq;
412
413 len = q_to_b(rbp, buf, ITEBURST);
414 } splx(s);
415
416 /* Here is a really good place to implement pre/jumpscroll() */
417 ite_putstr(buf, len, tp->t_dev);
418
419 s = spltty(); {
420 tp->t_state &= ~TS_BUSY;
421 /* we have characters remaining. */
422 if (rbp->c_cc) {
423 tp->t_state |= TS_TIMEOUT;
424 timeout((timeout_t) ttrstrt, (caddr_t) tp, 1);
425 }
426 /* wakeup we are below */
427 if (rbp->c_cc <= tp->t_lowat) {
428 if (tp->t_state & TS_ASLEEP) {
429 tp->t_state &= ~TS_ASLEEP;
430 wakeup((caddr_t) rbp);
431 }
432 selwakeup(&tp->t_wsel);
433 }
434 out:
435 } splx(s);
436 }
437
438 int
439 ite_on(dev, flag)
440 dev_t dev;
441 int flag;
442 {
443 int unit = minor(dev);
444 struct ite_softc *ip = &ite_softc[unit];
445
446 if (unit < 0 || unit >= NITE || (ip->flags & ITE_ALIVE) == 0)
447 return (ENXIO);
448 /* force ite active, overriding graphics mode */
449 if (flag & 1) {
450 ip->flags |= ITE_ACTIVE;
451 ip->flags &= ~(ITE_INGRF | ITE_INITED);
452 }
453 /* leave graphics mode */
454 if (flag & 2) {
455 ip->flags &= ~ITE_INGRF;
456 if ((ip->flags & ITE_ACTIVE) == 0)
457 return (0);
458 }
459 ip->flags |= ITE_ACTIVE;
460 if (ip->flags & ITE_INGRF)
461 return (0);
462 iteinit(dev);
463 return (0);
464 }
465
466 int
467 ite_off(dev, flag)
468 dev_t dev;
469 int flag;
470 {
471 register struct ite_softc *ip = &ite_softc[minor(dev)];
472
473 if (flag & 2)
474 ip->flags |= ITE_INGRF;
475 if ((ip->flags & ITE_ACTIVE) == 0)
476 return;
477 if ((flag & 1) ||
478 (ip->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
479 SUBR_DEINIT(ip);
480 if ((flag & 2) == 0) /* XXX hmm grfon() I think wants this to go inactive. */
481 ip->flags &= ~ITE_ACTIVE;
482 }
483
484 /* XXX called after changes made in underlying grf layer. */
485 /* I want to nuke this */
486 void
487 ite_reinit(dev)
488 dev_t dev;
489 {
490 struct ite_softc *ip = &ite_softc[minor(dev)];
491
492 ip->flags &= ~ITE_INITED;
493 iteinit(dev);
494 }
495
496 int
497 ite_param(tp, t)
498 struct tty *tp;
499 struct termios *t;
500 {
501 tp->t_ispeed = t->c_ispeed;
502 tp->t_ospeed = t->c_ospeed;
503 tp->t_cflag = t->c_cflag;
504 return (0);
505 }
506
507 void
508 ite_reset(ip)
509 struct ite_softc *ip;
510 {
511 int i;
512
513 ip->curx = 0;
514 ip->cury = 0;
515 ip->attribute = ATTR_NOR;
516 ip->save_curx = 0;
517 ip->save_cury = 0;
518 ip->save_attribute = ATTR_NOR;
519 ip->ap = ip->argbuf;
520 ip->emul_level = 0;
521 ip->eightbit_C1 = 0;
522 ip->top_margin = 0;
523 ip->bottom_margin = ip->rows - 1;
524 ip->inside_margins = 0;
525 ip->linefeed_newline = 0;
526 ip->auto_wrap = ite_default_wrap;
527 ip->cursor_appmode = 0;
528 ip->keypad_appmode = 0;
529 ip->imode = 0;
530 ip->key_repeat = 1;
531 bzero(ip->tabs, ip->cols);
532 for (i = 0; i < ip->cols; i++)
533 ip->tabs[i] = ((i & 7) == 0);
534 }
535
536 /* Used in console at startup only */
537 int
538 ite_cnfilter(c, caller)
539 u_char c;
540 enum caller caller;
541 {
542 struct tty *kbd_tty;
543 static u_char mod = 0;
544 static u_char last_dead = 0;
545 struct key key;
546 u_char code, up, mask;
547 int s, i;
548
549 up = c & 0x80 ? 1 : 0;
550 c &= 0x7f;
551 code = 0;
552
553 s = spltty();
554
555 i = (int)c - KBD_LEFT_SHIFT;
556 if (i >= 0 && i <= (KBD_RIGHT_META - KBD_LEFT_SHIFT)) {
557 mask = 1 << i;
558 if (up)
559 mod &= ~mask;
560 else
561 mod |= mask;
562 splx(s);
563 return -1;
564 }
565
566 if (up) {
567 splx(s);
568 return -1;
569 }
570
571 /* translate modifiers */
572 if (mod & KBD_MOD_SHIFT) {
573 if (mod & KBD_MOD_ALT)
574 key = kbdmap.alt_shift_keys[c];
575 else
576 key = kbdmap.shift_keys[c];
577 } else if (mod & KBD_MOD_ALT)
578 key = kbdmap.alt_keys[c];
579 else {
580 key = kbdmap.keys[c];
581 /* if CAPS and key is CAPable (no pun intended) */
582 if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
583 key = kbdmap.shift_keys[c];
584 }
585 code = key.code;
586
587 /* if string return */
588 if (key.mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
589 splx(s);
590 return -1;
591 }
592 /* handle dead keys */
593 if (key.mode & KBD_MODE_DEAD) {
594 /* if entered twice, send accent itself */
595 if (last_dead == key.mode & KBD_MODE_ACCMASK)
596 last_dead = 0;
597 else {
598 last_dead = key.mode & KBD_MODE_ACCMASK;
599 splx(s);
600 return -1;
601 }
602 }
603 if (last_dead) {
604 /* can't apply dead flag to string-keys */
605 if (code >= '@' && code < 0x7f)
606 code =
607 acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
608 last_dead = 0;
609 }
610 if (mod & KBD_MOD_CTRL)
611 code &= 0x1f;
612 if (mod & KBD_MOD_META)
613 code |= 0x80;
614
615 /* do console mapping. */
616 code = code == '\r' ? '\n' : code;
617
618 splx(s);
619 return (code);
620 }
621
622 /* And now the old stuff. */
623
624 /* these are used to implement repeating keys.. */
625 static u_char last_char = 0;
626 static u_char tout_pending = 0;
627
628 static void
629 repeat_handler(a0, a1)
630 int a0, a1;
631 {
632 tout_pending = 0;
633 if (last_char)
634 add_sicallback(ite_filter, last_char, ITEFILT_REPEATER);
635 }
636
637 void
638 ite_filter(c, caller)
639 u_char c;
640 enum caller caller;
641 {
642 struct tty *kbd_tty;
643 static u_char mod = 0;
644 static u_char last_dead = 0;
645 u_char code, *str, up, mask;
646 struct key key;
647 int s, i;
648
649 if (!kbd_ite)
650 return;
651 kbd_tty = ite_tty[kbd_ite - ite_softc];
652
653 /* have to make sure we're at spltty in here */
654 s = spltty();
655
656 /*
657 * keyboard interrupts come at priority 2, while softint
658 * generated keyboard-repeat interrupts come at level 1. So,
659 * to not allow a key-up event to get thru before a repeat for
660 * the key-down, we remove any outstanding callout requests..
661 */
662 rem_sicallback(ite_filter);
663
664 up = c & 0x80 ? 1 : 0;
665 c &= 0x7f;
666 code = 0;
667
668 i = (int)c - KBD_LEFT_SHIFT;
669 if (i >= 0 && i <= (KBD_RIGHT_META - KBD_LEFT_SHIFT)) {
670 mask = 1 << i;
671 if (up)
672 mod &= ~mask;
673 else
674 mod |= mask;
675 splx(s);
676 return;
677 }
678 /* stop repeating on up event */
679 if (up) {
680 if (tout_pending) {
681 untimeout((timeout_t) repeat_handler, 0);
682 tout_pending = 0;
683 last_char = 0;
684 }
685 splx(s);
686 return;
687 } else if (tout_pending && last_char != c) {
688 /* different character, stop also */
689 untimeout((timeout_t) repeat_handler, 0);
690 tout_pending = 0;
691 last_char = 0;
692 }
693 /* Safety button, switch back to ascii keymap. */
694 if (mod == (KBD_MOD_LALT | KBD_MOD_LMETA) && c == 0x50) {
695 bcopy(&ascii_kbdmap, &kbdmap, sizeof(struct kbdmap));
696
697 splx(s);
698 return;
699 #ifdef DDB
700 } else if (mod == (KBD_MOD_LALT | KBD_MOD_LMETA) && c == 0x59) {
701 extern int Debugger();
702 Debugger();
703 splx(s);
704 return;
705 #endif
706 }
707 /* translate modifiers */
708 if (mod & KBD_MOD_SHIFT) {
709 if (mod & KBD_MOD_ALT)
710 key = kbdmap.alt_shift_keys[c];
711 else
712 key = kbdmap.shift_keys[c];
713 } else if (mod & KBD_MOD_ALT)
714 key = kbdmap.alt_keys[c];
715 else {
716 key = kbdmap.keys[c];
717 /* if CAPS and key is CAPable (no pun intended) */
718 if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
719 key = kbdmap.shift_keys[c];
720 }
721 code = key.code;
722
723 /*
724 * arrange to repeat the keystroke. By doing this at the level
725 * of scan-codes, we can have function keys, and keys that
726 * send strings, repeat too. This also entitles an additional
727 * overhead, since we have to do the conversion each time, but
728 * I guess that's ok.
729 */
730 if (!tout_pending && caller == ITEFILT_TTY && kbd_ite->key_repeat) {
731 tout_pending = 1;
732 last_char = c;
733 timeout((timeout_t) repeat_handler, 0, start_repeat_timeo);
734 } else if (!tout_pending && caller == ITEFILT_REPEATER &&
735 kbd_ite->key_repeat) {
736 tout_pending = 1;
737 last_char = c;
738 timeout((timeout_t) repeat_handler, 0, next_repeat_timeo);
739 }
740 /* handle dead keys */
741 if (key.mode & KBD_MODE_DEAD) {
742 /* if entered twice, send accent itself */
743 if (last_dead == key.mode & KBD_MODE_ACCMASK)
744 last_dead = 0;
745 else {
746 last_dead = key.mode & KBD_MODE_ACCMASK;
747 splx(s);
748 return;
749 }
750 }
751 if (last_dead) {
752 /* can't apply dead flag to string-keys */
753 if (!(key.mode & KBD_MODE_STRING) && code >= '@' &&
754 code < 0x7f)
755 code = acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
756 last_dead = 0;
757 }
758 /* if not string, apply META and CTRL modifiers */
759 if (!(key.mode & KBD_MODE_STRING)
760 && (!(key.mode & KBD_MODE_KPAD) ||
761 (kbd_ite && !kbd_ite->keypad_appmode))) {
762 if (mod & KBD_MOD_CTRL)
763 code &= 0x1f;
764 if (mod & KBD_MOD_META)
765 code |= 0x80;
766 } else if ((key.mode & KBD_MODE_KPAD) &&
767 (kbd_ite && kbd_ite->keypad_appmode)) {
768 static char *in = "0123456789-+.\r()/*";
769 static char *out = "pqrstuvwxymlnMPQRS";
770 char *cp;
771
772 /*
773 * keypad-appmode sends SS3 followed by the above
774 * translated character
775 */
776 (*linesw[kbd_tty->t_line].l_rint) (27, kbd_tty);
777 (*linesw[kbd_tty->t_line].l_rint) ('0', kbd_tty);
778 (*linesw[kbd_tty->t_line].l_rint) (out[cp - in], kbd_tty);
779 splx(s);
780 return;
781 } else {
782 /* *NO* I don't like this.... */
783 static u_char app_cursor[] =
784 {
785 3, 27, 'O', 'A',
786 3, 27, 'O', 'B',
787 3, 27, 'O', 'C',
788 3, 27, 'O', 'D'};
789
790 str = kbdmap.strings + code;
791 /*
792 * if this is a cursor key, AND it has the default
793 * keymap setting, AND we're in app-cursor mode, switch
794 * to the above table. This is *nasty* !
795 */
796 if (c >= 0x4c && c <= 0x4f && kbd_ite->cursor_appmode
797 && !bcmp(str, "\x03\x1b[", 3) &&
798 index("ABCD", str[3]))
799 str = app_cursor + 4 * (str[3] - 'A');
800
801 /*
802 * using a length-byte instead of 0-termination allows
803 * to embed \0 into strings, although this is not used
804 * in the default keymap
805 */
806 for (i = *str++; i; i--)
807 (*linesw[kbd_tty->t_line].l_rint) (*str++, kbd_tty);
808 splx(s);
809 return;
810 }
811 (*linesw[kbd_tty->t_line].l_rint) (code, kbd_tty);
812
813 splx(s);
814 return;
815 }
816
817 /* helper functions, makes the code below more readable */
818 static void inline
819 ite_sendstr(str)
820 char *str;
821 {
822 struct tty *kbd_tty = ite_tty[kbd_ite - ite_softc];
823
824 KDASSERT(kbd_tty);
825 while (*str)
826 (*linesw[kbd_tty->t_line].l_rint) (*str++, kbd_tty);
827 }
828
829 static void
830 alignment_display(ip)
831 struct ite_softc *ip;
832 {
833 int i, j;
834
835 for (j = 0; j < ip->rows; j++)
836 for (i = 0; i < ip->cols; i++)
837 SUBR_PUTC(ip, 'E', j, i, ATTR_NOR);
838 attrclr(ip, 0, 0, ip->rows, ip->cols);
839 SUBR_CURSOR(ip, DRAW_CURSOR);
840 }
841
842 static void inline
843 snap_cury(ip)
844 struct ite_softc *ip;
845 {
846 if (ip->inside_margins)
847 {
848 if (ip->cury < ip->top_margin)
849 ip->cury = ip->top_margin;
850 if (ip->cury > ip->bottom_margin)
851 ip->cury = ip->bottom_margin;
852 }
853 }
854
855 static void inline
856 ite_dnchar(ip, n)
857 struct ite_softc *ip;
858 int n;
859 {
860 n = MIN(n, ip->cols - ip->curx);
861 if (n < ip->cols - ip->curx)
862 {
863 SUBR_SCROLL(ip, ip->cury, ip->curx + n, n, SCROLL_LEFT);
864 attrmov(ip, ip->cury, ip->curx + n, ip->cury, ip->curx,
865 1, ip->cols - ip->curx - n);
866 attrclr(ip, ip->cury, ip->cols - n, 1, n);
867 }
868 while (n-- > 0)
869 SUBR_PUTC(ip, ' ', ip->cury, ip->cols - n - 1, ATTR_NOR);
870 SUBR_CURSOR(ip, DRAW_CURSOR);
871 }
872
873 static void inline
874 ite_inchar(ip, n)
875 struct ite_softc *ip;
876 int n;
877 {
878 n = MIN(n, ip->cols - ip->curx);
879 if (n < ip->cols - ip->curx)
880 {
881 SUBR_SCROLL(ip, ip->cury, ip->curx, n, SCROLL_RIGHT);
882 attrmov(ip, ip->cury, ip->curx, ip->cury, ip->curx + n,
883 1, ip->cols - ip->curx - n);
884 attrclr(ip, ip->cury, ip->curx, 1, n);
885 }
886 while (n--)
887 SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
888 SUBR_CURSOR(ip, DRAW_CURSOR);
889 }
890
891 static void inline
892 ite_clrtoeol(ip)
893 struct ite_softc *ip;
894 {
895 int y = ip->cury, x = ip->curx;
896 if (ip->cols - x > 0)
897 {
898 SUBR_CLEAR(ip, y, x, 1, ip->cols - x);
899 attrclr(ip, y, x, 1, ip->cols - x);
900 SUBR_CURSOR(ip, DRAW_CURSOR);
901 }
902 }
903
904 static void inline
905 ite_clrtobol(ip)
906 struct ite_softc *ip;
907 {
908 int y = ip->cury, x = MIN(ip->curx + 1, ip->cols);
909 SUBR_CLEAR(ip, y, 0, 1, x);
910 attrclr(ip, y, 0, 1, x);
911 SUBR_CURSOR(ip, DRAW_CURSOR);
912 }
913
914 static void inline
915 ite_clrline(ip)
916 struct ite_softc *ip;
917 {
918 int y = ip->cury;
919 SUBR_CLEAR(ip, y, 0, 1, ip->cols);
920 attrclr(ip, y, 0, 1, ip->cols);
921 SUBR_CURSOR(ip, DRAW_CURSOR);
922 }
923
924
925
926 static void inline
927 ite_clrtoeos(ip)
928 struct ite_softc *ip;
929 {
930 ite_clrtoeol(ip);
931 if (ip->cury < ip->rows - 1)
932 {
933 SUBR_CLEAR(ip, ip->cury + 1, 0, ip->rows - 1 - ip->cury, ip->cols);
934 attrclr(ip, ip->cury, 0, ip->rows - ip->cury, ip->cols);
935 SUBR_CURSOR(ip, DRAW_CURSOR);
936 }
937 }
938
939 static void inline
940 ite_clrtobos(ip)
941 struct ite_softc *ip;
942 {
943 ite_clrtobol(ip);
944 if (ip->cury > 0)
945 {
946 SUBR_CLEAR(ip, 0, 0, ip->cury, ip->cols);
947 attrclr(ip, 0, 0, ip->cury, ip->cols);
948 SUBR_CURSOR(ip, DRAW_CURSOR);
949 }
950 }
951
952 static void inline
953 ite_clrscreen(ip)
954 struct ite_softc *ip;
955 {
956 SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
957 attrclr(ip, 0, 0, ip->rows, ip->cols);
958 SUBR_CURSOR(ip, DRAW_CURSOR);
959 }
960
961
962
963 static void inline
964 ite_dnline(ip, n)
965 struct ite_softc *ip;
966 int n;
967 {
968 /* interesting.. if the cursor is outside the scrolling
969 region, this command is simply ignored.. */
970 if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
971 return;
972
973 n = MIN(n, ip->bottom_margin + 1 - ip->cury);
974 if (n <= ip->bottom_margin - ip->cury)
975 {
976 SUBR_SCROLL(ip, ip->cury + n, 0, n, SCROLL_UP);
977 attrmov(ip, ip->cury + n, 0, ip->cury, 0,
978 ip->bottom_margin + 1 - ip->cury - n, ip->cols);
979 }
980 SUBR_CLEAR(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
981 attrclr(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
982 SUBR_CURSOR(ip, DRAW_CURSOR);
983 }
984
985 static void inline
986 ite_inline(ip, n)
987 struct ite_softc *ip;
988 int n;
989 {
990 /* interesting.. if the cursor is outside the scrolling
991 region, this command is simply ignored.. */
992 if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
993 return;
994
995 n = MIN(n, ip->bottom_margin + 1 - ip->cury);
996 if (n <= ip->bottom_margin - ip->cury)
997 {
998 SUBR_SCROLL(ip, ip->cury, 0, n, SCROLL_DOWN);
999 attrmov(ip, ip->cury, 0, ip->cury + n, 0,
1000 ip->bottom_margin + 1 - ip->cury - n, ip->cols);
1001 }
1002 SUBR_CLEAR(ip, ip->cury, 0, n, ip->cols);
1003 attrclr(ip, ip->cury, 0, n, ip->cols);
1004 SUBR_CURSOR(ip, DRAW_CURSOR);
1005 }
1006
1007 static void inline
1008 ite_lf (ip)
1009 struct ite_softc *ip;
1010 {
1011 ++ip->cury;
1012 if ((ip->cury == ip->bottom_margin+1) || (ip->cury == ip->rows))
1013 {
1014 ip->cury--;
1015 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1016 ite_clrline(ip);
1017 }
1018 SUBR_CURSOR(ip, MOVE_CURSOR);
1019 clr_attr(ip, ATTR_INV);
1020 }
1021
1022 static void inline
1023 ite_crlf (ip)
1024 struct ite_softc *ip;
1025 {
1026 ip->curx = 0;
1027 ite_lf (ip);
1028 }
1029
1030 static void inline
1031 ite_cr (ip)
1032 struct ite_softc *ip;
1033 {
1034 if (ip->curx)
1035 {
1036 ip->curx = 0;
1037 SUBR_CURSOR(ip, MOVE_CURSOR);
1038 }
1039 }
1040
1041 static void inline
1042 ite_rlf (ip)
1043 struct ite_softc *ip;
1044 {
1045 ip->cury--;
1046 if ((ip->cury < 0) || (ip->cury == ip->top_margin - 1))
1047 {
1048 ip->cury++;
1049 SUBR_SCROLL(ip, ip->top_margin, 0, 1, SCROLL_DOWN);
1050 ite_clrline(ip);
1051 }
1052 SUBR_CURSOR(ip, MOVE_CURSOR);
1053 clr_attr(ip, ATTR_INV);
1054 }
1055
1056 static int inline
1057 atoi (cp)
1058 const char *cp;
1059 {
1060 int n;
1061
1062 for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
1063 n = n * 10 + *cp - '0';
1064
1065 return n;
1066 }
1067
1068 static char *
1069 index (cp, ch)
1070 const char *cp;
1071 char ch;
1072 {
1073 while (*cp && *cp != ch) cp++;
1074 return *cp ? (char *) cp : 0;
1075 }
1076
1077
1078
1079 static int inline
1080 ite_argnum (ip)
1081 struct ite_softc *ip;
1082 {
1083 char ch;
1084 int n;
1085
1086 /* convert argument string into number */
1087 if (ip->ap == ip->argbuf)
1088 return 1;
1089 ch = *ip->ap;
1090 *ip->ap = 0;
1091 n = atoi (ip->argbuf);
1092 *ip->ap = ch;
1093
1094 return n;
1095 }
1096
1097 static int inline
1098 ite_zargnum (ip)
1099 struct ite_softc *ip;
1100 {
1101 char ch, *cp;
1102 int n;
1103
1104 /* convert argument string into number */
1105 if (ip->ap == ip->argbuf)
1106 return 0;
1107 ch = *ip->ap;
1108 *ip->ap = 0;
1109 n = atoi (ip->argbuf);
1110 *ip->ap = ch;
1111
1112 return n; /* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
1113 }
1114
1115 static int inline
1116 strncmp (a, b, l)
1117 const char *a, *b;
1118 int l;
1119 {
1120 for (;l--; a++, b++)
1121 if (*a != *b)
1122 return *a - *b;
1123 return 0;
1124 }
1125
1126 void
1127 ite_putstr(s, len, dev)
1128 const char *s;
1129 int len;
1130 dev_t dev;
1131 {
1132 struct ite_softc *ip = &ite_softc[minor(dev)];
1133 int i;
1134
1135 /* XXX avoid problems */
1136 if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
1137 return;
1138
1139 SUBR_CURSOR(ip, START_CURSOROPT);
1140 for (i = 0; i < len; i++)
1141 if (s[i])
1142 iteputchar(s[i], ip);
1143 SUBR_CURSOR(ip, END_CURSOROPT);
1144 }
1145
1146
1147 void
1148 iteputchar(c, ip)
1149 register int c;
1150 struct ite_softc *ip;
1151 {
1152 struct tty *kbd_tty;
1153 int n, x, y;
1154 char *cp;
1155
1156 kbd_tty = ite_tty[kbd_ite - ite_softc];
1157
1158 if (ip->escape)
1159 {
1160 doesc:
1161 switch (ip->escape)
1162 {
1163 case ESC:
1164 switch (c)
1165 {
1166 /* first 7bit equivalents for the 8bit control characters */
1167
1168 case 'D':
1169 c = IND;
1170 ip->escape = 0;
1171 break; /* and fall into the next switch below (same for all `break') */
1172
1173 case 'E':
1174 c = NEL;
1175 ip->escape = 0;
1176 break;
1177
1178 case 'H':
1179 c = HTS;
1180 ip->escape = 0;
1181 break;
1182
1183 case 'M':
1184 c = RI;
1185 ip->escape = 0;
1186 break;
1187
1188 case 'N':
1189 c = SS2;
1190 ip->escape = 0;
1191 break;
1192
1193 case 'O':
1194 c = SS3;
1195 ip->escape = 0;
1196 break;
1197
1198 case 'P':
1199 c = DCS;
1200 ip->escape = 0;
1201 break;
1202
1203 case '[':
1204 c = CSI;
1205 ip->escape = 0;
1206 break;
1207
1208 case '\\':
1209 c = ST;
1210 ip->escape = 0;
1211 break;
1212
1213 case ']':
1214 c = OSC;
1215 ip->escape = 0;
1216 break;
1217
1218 case '^':
1219 c = PM;
1220 ip->escape = 0;
1221 break;
1222
1223 case '_':
1224 c = APC;
1225 ip->escape = 0;
1226 break;
1227
1228
1229 /* introduces 7/8bit control */
1230 case ' ':
1231 /* can be followed by either F or G */
1232 ip->escape = ' ';
1233 break;
1234
1235
1236 /* a lot of character set selections, not yet used...
1237 94-character sets: */
1238 case '(': /* G0 */
1239 case ')': /* G1 */
1240 ip->escape = c;
1241 return;
1242
1243 case '*': /* G2 */
1244 case '+': /* G3 */
1245 case 'B': /* ASCII */
1246 case 'A': /* ISO latin 1 */
1247 case '<': /* user preferred suplemental */
1248 case '0': /* dec special graphics */
1249
1250 /* 96-character sets: */
1251 case '-': /* G1 */
1252 case '.': /* G2 */
1253 case '/': /* G3 */
1254
1255 /* national character sets: */
1256 case '4': /* dutch */
1257 case '5':
1258 case 'C': /* finnish */
1259 case 'R': /* french */
1260 case 'Q': /* french canadian */
1261 case 'K': /* german */
1262 case 'Y': /* italian */
1263 case '6': /* norwegian/danish */
1264 /* note: %5 and %6 are not supported (two chars..) */
1265
1266 ip->escape = 0;
1267 /* just ignore for now */
1268 return;
1269
1270
1271 /* locking shift modes (as you might guess, not yet supported..) */
1272 case '`':
1273 ip->GR = ip->G1;
1274 ip->escape = 0;
1275 return;
1276
1277 case 'n':
1278 ip->GL = ip->G2;
1279 ip->escape = 0;
1280 return;
1281
1282 case '}':
1283 ip->GR = ip->G2;
1284 ip->escape = 0;
1285 return;
1286
1287 case 'o':
1288 ip->GL = ip->G3;
1289 ip->escape = 0;
1290 return;
1291
1292 case '|':
1293 ip->GR = ip->G3;
1294 ip->escape = 0;
1295 return;
1296
1297
1298 /* font width/height control */
1299 case '#':
1300 ip->escape = '#';
1301 return;
1302
1303
1304 /* hard terminal reset .. */
1305 case 'c':
1306 ite_reset (ip);
1307 SUBR_CURSOR(ip, MOVE_CURSOR);
1308 ip->escape = 0;
1309 return;
1310
1311
1312 case '7':
1313 ip->save_curx = ip->curx;
1314 ip->save_cury = ip->cury;
1315 ip->save_attribute = ip->attribute;
1316 ip->escape = 0;
1317 return;
1318
1319 case '8':
1320 ip->curx = ip->save_curx;
1321 ip->cury = ip->save_cury;
1322 ip->attribute = ip->save_attribute;
1323 SUBR_CURSOR(ip, MOVE_CURSOR);
1324 ip->escape = 0;
1325 return;
1326
1327 case '=':
1328 ip->keypad_appmode = 1;
1329 ip->escape = 0;
1330 return;
1331
1332 case '>':
1333 ip->keypad_appmode = 0;
1334 ip->escape = 0;
1335 return;
1336
1337 case 'Z': /* request ID */
1338 if (ip->emul_level == EMUL_VT100)
1339 ite_sendstr (ip, "\033[61;0c"); /* XXX not clean */
1340 else
1341 ite_sendstr (ip, "\033[63;0c"); /* XXX not clean */
1342 ip->escape = 0;
1343 return;
1344
1345 /* default catch all for not recognized ESC sequences */
1346 default:
1347 ip->escape = 0;
1348 return;
1349 }
1350 break;
1351
1352
1353 case '(':
1354 case ')':
1355 ip->escape = 0;
1356 return;
1357
1358
1359 case ' ':
1360 switch (c)
1361 {
1362 case 'F':
1363 ip->eightbit_C1 = 0;
1364 ip->escape = 0;
1365 return;
1366
1367 case 'G':
1368 ip->eightbit_C1 = 1;
1369 ip->escape = 0;
1370 return;
1371
1372 default:
1373 /* not supported */
1374 ip->escape = 0;
1375 return;
1376 }
1377 break;
1378
1379
1380 case '#':
1381 switch (c)
1382 {
1383 case '5':
1384 /* single height, single width */
1385 ip->escape = 0;
1386 return;
1387
1388 case '6':
1389 /* double width, single height */
1390 ip->escape = 0;
1391 return;
1392
1393 case '3':
1394 /* top half */
1395 ip->escape = 0;
1396 return;
1397
1398 case '4':
1399 /* bottom half */
1400 ip->escape = 0;
1401 return;
1402
1403 case '8':
1404 /* screen alignment pattern... */
1405 alignment_display (ip);
1406 ip->escape = 0;
1407 return;
1408
1409 default:
1410 ip->escape = 0;
1411 return;
1412 }
1413 break;
1414
1415
1416
1417 case CSI:
1418 /* the biggie... */
1419 switch (c)
1420 {
1421 case '0': case '1': case '2': case '3': case '4':
1422 case '5': case '6': case '7': case '8': case '9':
1423 case ';': case '\"': case '$': case '>':
1424 if (ip->ap < ip->argbuf + MAX_ARGSIZE)
1425 *ip->ap++ = c;
1426 return;
1427
1428 case BS:
1429 /* you wouldn't believe such perversion is possible?
1430 it is.. BS is allowed in between cursor sequences
1431 (at least), according to vttest.. */
1432 if (--ip->curx < 0)
1433 ip->curx = 0;
1434 else
1435 SUBR_CURSOR(ip, MOVE_CURSOR);
1436 break;
1437
1438 case 'p':
1439 *ip->ap = 0;
1440 if (! strncmp (ip->argbuf, "61\"", 3))
1441 ip->emul_level = EMUL_VT100;
1442 else if (! strncmp (ip->argbuf, "63;1\"", 5)
1443 || ! strncmp (ip->argbuf, "62;1\"", 5))
1444 ip->emul_level = EMUL_VT300_7;
1445 else
1446 ip->emul_level = EMUL_VT300_8;
1447 ip->escape = 0;
1448 return;
1449
1450
1451 case '?':
1452 *ip->ap = 0;
1453 ip->escape = '?';
1454 ip->ap = ip->argbuf;
1455 return;
1456
1457
1458 case 'c':
1459 *ip->ap = 0;
1460 if (ip->argbuf[0] == '>')
1461 {
1462 ite_sendstr (ip, "\033[>24;0;0;0c");
1463 }
1464 else switch (ite_zargnum(ip))
1465 {
1466 case 0:
1467 /* primary DA request, send primary DA response */
1468 if (ip->emul_level == EMUL_VT100)
1469 ite_sendstr (ip, "\033[?1;1c");
1470 else
1471 ite_sendstr (ip, "\033[63;0c");
1472 break;
1473 }
1474 ip->escape = 0;
1475 return;
1476
1477 case 'n':
1478 switch (ite_zargnum(ip))
1479 {
1480 case 5:
1481 ite_sendstr (ip, "\033[0n"); /* no malfunction */
1482 break;
1483 case 6:
1484 /* cursor position report */
1485 sprintf (ip->argbuf, "\033[%d;%dR",
1486 ip->cury + 1, ip->curx + 1);
1487 ite_sendstr (ip, ip->argbuf);
1488 break;
1489 }
1490 ip->escape = 0;
1491 return;
1492
1493
1494 case 'x':
1495 switch (ite_zargnum(ip))
1496 {
1497 case 0:
1498 /* Fake some terminal parameters. */
1499 ite_sendstr (ip, "\033[2;1;1;112;112;1;0x");
1500 break;
1501 case 1:
1502 ite_sendstr (ip, "\033[3;1;1;112;112;1;0x");
1503 break;
1504 }
1505 ip->escape = 0;
1506 return;
1507
1508
1509 case 'g':
1510 switch (ite_zargnum(ip))
1511 {
1512 case 0:
1513 if (ip->curx < ip->cols)
1514 ip->tabs[ip->curx] = 0;
1515 break;
1516 case 3:
1517 for (n = 0; n < ip->cols; n++)
1518 ip->tabs[n] = 0;
1519 break;
1520 }
1521 ip->escape = 0;
1522 return;
1523
1524
1525 case 'h': case 'l':
1526 n = ite_zargnum (ip);
1527 switch (n)
1528 {
1529 case 4:
1530 ip->imode = (c == 'h'); /* insert/replace mode */
1531 break;
1532 case 20:
1533 ip->linefeed_newline = (c == 'h');
1534 break;
1535 }
1536 ip->escape = 0;
1537 return;
1538
1539
1540 case 'M':
1541 ite_dnline (ip, ite_argnum (ip));
1542 ip->escape = 0;
1543 return;
1544
1545
1546 case 'L':
1547 ite_inline (ip, ite_argnum (ip));
1548 ip->escape = 0;
1549 return;
1550
1551
1552 case 'P':
1553 ite_dnchar (ip, ite_argnum (ip));
1554 ip->escape = 0;
1555 return;
1556
1557
1558 case '@':
1559 ite_inchar (ip, ite_argnum (ip));
1560 ip->escape = 0;
1561 return;
1562
1563
1564 case 'G':
1565 /* this one was *not* in my vt320 manual but in
1566 a vt320 termcap entry.. who is right?
1567 It's supposed to set the horizontal cursor position. */
1568 *ip->ap = 0;
1569 x = atoi (ip->argbuf);
1570 if (x) x--;
1571 ip->curx = MIN(x, ip->cols - 1);
1572 ip->escape = 0;
1573 SUBR_CURSOR(ip, MOVE_CURSOR);
1574 clr_attr (ip, ATTR_INV);
1575 return;
1576
1577
1578 case 'd':
1579 /* same thing here, this one's for setting the absolute
1580 vertical cursor position. Not documented... */
1581 *ip->ap = 0;
1582 y = atoi (ip->argbuf);
1583 if (y) y--;
1584 if (ip->inside_margins)
1585 y += ip->top_margin;
1586 ip->cury = MIN(y, ip->rows - 1);
1587 ip->escape = 0;
1588 snap_cury(ip);
1589 SUBR_CURSOR(ip, MOVE_CURSOR);
1590 clr_attr (ip, ATTR_INV);
1591 return;
1592
1593
1594 case 'H':
1595 case 'f':
1596 *ip->ap = 0;
1597 y = atoi (ip->argbuf);
1598 x = 0;
1599 cp = index (ip->argbuf, ';');
1600 if (cp)
1601 x = atoi (cp + 1);
1602 if (x) x--;
1603 if (y) y--;
1604 if (ip->inside_margins)
1605 y += ip->top_margin;
1606 ip->cury = MIN(y, ip->rows - 1);
1607 ip->curx = MIN(x, ip->cols - 1);
1608 ip->escape = 0;
1609 snap_cury(ip);
1610 SUBR_CURSOR(ip, MOVE_CURSOR);
1611 clr_attr (ip, ATTR_INV);
1612 return;
1613
1614 case 'A':
1615 n = ite_argnum (ip);
1616 n = ip->cury - (n ? n : 1);
1617 if (n < 0) n = 0;
1618 if (ip->inside_margins)
1619 n = MAX(ip->top_margin, n);
1620 else if (n == ip->top_margin - 1)
1621 /* allow scrolling outside region, but don't scroll out
1622 of active region without explicit CUP */
1623 n = ip->top_margin;
1624 ip->cury = n;
1625 ip->escape = 0;
1626 SUBR_CURSOR(ip, MOVE_CURSOR);
1627 clr_attr (ip, ATTR_INV);
1628 return;
1629
1630 case 'B':
1631 n = ite_argnum (ip);
1632 n = ip->cury + (n ? n : 1);
1633 n = MIN(ip->rows - 1, n);
1634 if (ip->inside_margins)
1635 n = MIN(ip->bottom_margin, n);
1636 else if (n == ip->bottom_margin + 1)
1637 /* allow scrolling outside region, but don't scroll out
1638 of active region without explicit CUP */
1639 n = ip->bottom_margin;
1640 ip->cury = n;
1641 ip->escape = 0;
1642 SUBR_CURSOR(ip, MOVE_CURSOR);
1643 clr_attr (ip, ATTR_INV);
1644 return;
1645
1646 case 'C':
1647 n = ite_argnum (ip);
1648 n = n ? n : 1;
1649 ip->curx = MIN(ip->curx + n, ip->cols - 1);
1650 ip->escape = 0;
1651 SUBR_CURSOR(ip, MOVE_CURSOR);
1652 clr_attr (ip, ATTR_INV);
1653 return;
1654
1655 case 'D':
1656 n = ite_argnum (ip);
1657 n = n ? n : 1;
1658 n = ip->curx - n;
1659 ip->curx = n >= 0 ? n : 0;
1660 ip->escape = 0;
1661 SUBR_CURSOR(ip, MOVE_CURSOR);
1662 clr_attr (ip, ATTR_INV);
1663 return;
1664
1665
1666
1667
1668 case 'J':
1669 *ip->ap = 0;
1670 n = ite_zargnum (ip);
1671 if (n == 0)
1672 ite_clrtoeos(ip);
1673 else if (n == 1)
1674 ite_clrtobos(ip);
1675 else if (n == 2)
1676 ite_clrscreen(ip);
1677 ip->escape = 0;
1678 return;
1679
1680
1681 case 'K':
1682 n = ite_zargnum (ip);
1683 if (n == 0)
1684 ite_clrtoeol(ip);
1685 else if (n == 1)
1686 ite_clrtobol(ip);
1687 else if (n == 2)
1688 ite_clrline(ip);
1689 ip->escape = 0;
1690 return;
1691
1692
1693 case 'X':
1694 n = ite_argnum(ip) - 1;
1695 n = MIN(n, ip->cols - 1 - ip->curx);
1696 for (; n >= 0; n--)
1697 {
1698 attrclr(ip, ip->cury, ip->curx + n, 1, 1);
1699 SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1700 }
1701 ip->escape = 0;
1702 return;
1703
1704
1705 case '}': case '`':
1706 /* status line control */
1707 ip->escape = 0;
1708 return;
1709
1710
1711 case 'r':
1712 *ip->ap = 0;
1713 x = atoi (ip->argbuf);
1714 x = x ? x : 1;
1715 y = ip->rows;
1716 cp = index (ip->argbuf, ';');
1717 if (cp)
1718 {
1719 y = atoi (cp + 1);
1720 y = y ? y : ip->rows;
1721 }
1722 if (y - x < 2)
1723 {
1724 /* if illegal scrolling region, reset to defaults */
1725 x = 1;
1726 y = ip->rows;
1727 }
1728 x--;
1729 y--;
1730 ip->top_margin = MIN(x, ip->rows - 1);
1731 ip->bottom_margin = MIN(y, ip->rows - 1);
1732 if (ip->inside_margins)
1733 {
1734 ip->cury = ip->top_margin;
1735 ip->curx = 0;
1736 SUBR_CURSOR(ip, MOVE_CURSOR);
1737 }
1738 ip->escape = 0;
1739 return;
1740
1741
1742 case 'm':
1743 /* big attribute setter/resetter */
1744 {
1745 char *cp;
1746 *ip->ap = 0;
1747 /* kludge to make CSIm work (== CSI0m) */
1748 if (ip->ap == ip->argbuf)
1749 ip->ap++;
1750 for (cp = ip->argbuf; cp < ip->ap; )
1751 {
1752 switch (*cp)
1753 {
1754 case 0:
1755 case '0':
1756 clr_attr (ip, ATTR_ALL);
1757 cp++;
1758 break;
1759
1760 case '1':
1761 set_attr (ip, ATTR_BOLD);
1762 cp++;
1763 break;
1764
1765 case '2':
1766 switch (cp[1])
1767 {
1768 case '2':
1769 clr_attr (ip, ATTR_BOLD);
1770 cp += 2;
1771 break;
1772
1773 case '4':
1774 clr_attr (ip, ATTR_UL);
1775 cp += 2;
1776 break;
1777
1778 case '5':
1779 clr_attr (ip, ATTR_BLINK);
1780 cp += 2;
1781 break;
1782
1783 case '7':
1784 clr_attr (ip, ATTR_INV);
1785 cp += 2;
1786 break;
1787
1788 default:
1789 cp++;
1790 break;
1791 }
1792 break;
1793
1794 case '4':
1795 set_attr (ip, ATTR_UL);
1796 cp++;
1797 break;
1798
1799 case '5':
1800 set_attr (ip, ATTR_BLINK);
1801 cp++;
1802 break;
1803
1804 case '7':
1805 set_attr (ip, ATTR_INV);
1806 cp++;
1807 break;
1808
1809 default:
1810 cp++;
1811 break;
1812 }
1813 }
1814
1815 }
1816 ip->escape = 0;
1817 return;
1818
1819
1820 case 'u':
1821 /* DECRQTSR */
1822 ite_sendstr (ip, "\033P\033\\");
1823 ip->escape = 0;
1824 return;
1825
1826
1827
1828 default:
1829 ip->escape = 0;
1830 return;
1831 }
1832 break;
1833
1834
1835
1836 case '?': /* CSI ? */
1837 switch (c)
1838 {
1839 case '0': case '1': case '2': case '3': case '4':
1840 case '5': case '6': case '7': case '8': case '9':
1841 case ';': case '\"': case '$':
1842 /* Don't fill the last character; it's needed. */
1843 /* XXX yeah, where ?? */
1844 if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
1845 *ip->ap++ = c;
1846 return;
1847
1848
1849 case 'n':
1850 *ip->ap = 0;
1851 if (ip->ap == &ip->argbuf[2])
1852 {
1853 if (! strncmp (ip->argbuf, "15", 2))
1854 /* printer status: no printer */
1855 ite_sendstr (ip, "\033[13n");
1856
1857 else if (! strncmp (ip->argbuf, "25", 2))
1858 /* udk status */
1859 ite_sendstr (ip, "\033[20n");
1860
1861 else if (! strncmp (ip->argbuf, "26", 2))
1862 /* keyboard dialect: US */
1863 ite_sendstr (ip, "\033[27;1n");
1864 }
1865 ip->escape = 0;
1866 return;
1867
1868
1869 case 'h': case 'l':
1870 n = ite_zargnum (ip);
1871 switch (n)
1872 {
1873 case 1:
1874 ip->cursor_appmode = (c == 'h');
1875 break;
1876
1877 case 3:
1878 /* 132/80 columns (132 == 'h') */
1879 break;
1880
1881 case 4: /* smooth scroll */
1882 break;
1883
1884 case 5:
1885 /* light background (=='h') /dark background(=='l') */
1886 break;
1887
1888 case 6: /* origin mode */
1889 ip->inside_margins = (c == 'h');
1890 ip->curx = 0;
1891 ip->cury = ip->inside_margins ? ip->top_margin : 0;
1892 SUBR_CURSOR(ip, MOVE_CURSOR);
1893 break;
1894
1895 case 7: /* auto wraparound */
1896 ip->auto_wrap = (c == 'h');
1897 break;
1898
1899 case 8: /* keyboard repeat */
1900 ip->key_repeat = (c == 'h');
1901 break;
1902
1903 case 20: /* newline mode */
1904 ip->linefeed_newline = (c == 'h');
1905 break;
1906
1907 case 25: /* cursor on/off */
1908 SUBR_CURSOR(ip, (c == 'h') ? DRAW_CURSOR : ERASE_CURSOR);
1909 break;
1910 }
1911 ip->escape = 0;
1912 return;
1913
1914 default:
1915 ip->escape = 0;
1916 return;
1917 }
1918 break;
1919
1920
1921 default:
1922 ip->escape = 0;
1923 return;
1924 }
1925 }
1926
1927 switch (c) {
1928
1929 case VT: /* VT is treated like LF */
1930 case FF: /* so is FF */
1931 case LF:
1932 /* cr->crlf distinction is done here, on output,
1933 not on input! */
1934 if (ip->linefeed_newline)
1935 ite_crlf (ip);
1936 else
1937 ite_lf (ip);
1938 break;
1939
1940 case CR:
1941 ite_cr (ip);
1942 break;
1943
1944 case BS:
1945 if (--ip->curx < 0)
1946 ip->curx = 0;
1947 else
1948 SUBR_CURSOR(ip, MOVE_CURSOR);
1949 break;
1950
1951 case HT:
1952 for (n = ip->curx + 1; n < ip->cols; n++) {
1953 if (ip->tabs[n]) {
1954 ip->curx = n;
1955 SUBR_CURSOR(ip, MOVE_CURSOR);
1956 break;
1957 }
1958 }
1959 break;
1960
1961 case BEL:
1962 if (kbd_tty && ite_tty[kbd_ite - ite_softc] == kbd_tty)
1963 kbdbell();
1964 break;
1965
1966 case SO:
1967 ip->GL = ip->G1;
1968 break;
1969
1970 case SI:
1971 ip->GL = ip->G0;
1972 break;
1973
1974 case ENQ:
1975 /* send answer-back message !! */
1976 break;
1977
1978 case CAN:
1979 ip->escape = 0; /* cancel any escape sequence in progress */
1980 break;
1981
1982 case SUB:
1983 ip->escape = 0; /* dito, but see below */
1984 /* should also display a reverse question mark!! */
1985 break;
1986
1987 case ESC:
1988 ip->escape = ESC;
1989 break;
1990
1991
1992 /* now it gets weird.. 8bit control sequences.. */
1993 case IND: /* index: move cursor down, scroll */
1994 ite_lf (ip);
1995 break;
1996
1997 case NEL: /* next line. next line, first pos. */
1998 ite_crlf (ip);
1999 break;
2000
2001 case HTS: /* set horizontal tab */
2002 if (ip->curx < ip->cols)
2003 ip->tabs[ip->curx] = 1;
2004 break;
2005
2006 case RI: /* reverse index */
2007 ite_rlf (ip);
2008 break;
2009
2010 case SS2: /* go into G2 for one character */
2011 /* not yet supported */
2012 break;
2013
2014 case SS3: /* go into G3 for one character */
2015 break;
2016
2017 case DCS: /* device control string introducer */
2018 ip->escape = DCS;
2019 ip->ap = ip->argbuf;
2020 break;
2021
2022 case CSI: /* control sequence introducer */
2023 ip->escape = CSI;
2024 ip->ap = ip->argbuf;
2025 break;
2026
2027 case ST: /* string terminator */
2028 /* ignore, if not used as terminator */
2029 break;
2030
2031 case OSC: /* introduces OS command. Ignore everything upto ST */
2032 ip->escape = OSC;
2033 break;
2034
2035 case PM: /* privacy message, ignore everything upto ST */
2036 ip->escape = PM;
2037 break;
2038
2039 case APC: /* application program command, ignore everything upto ST */
2040 ip->escape = APC;
2041 break;
2042
2043 default:
2044 if (c < ' ' || c == DEL)
2045 break;
2046 if (ip->imode)
2047 ite_inchar(ip, 1);
2048 iteprecheckwrap(ip);
2049 #ifdef DO_WEIRD_ATTRIBUTES
2050 if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
2051 attrset(ip, ATTR_INV);
2052 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
2053 }
2054 else
2055 SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
2056 #else
2057 SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
2058 #endif
2059 SUBR_CURSOR(ip, DRAW_CURSOR);
2060 itecheckwrap(ip);
2061 break;
2062 }
2063 }
2064
2065 iteprecheckwrap(ip)
2066 struct ite_softc *ip;
2067 {
2068 if (ip->auto_wrap && ip->curx == ip->cols) {
2069 ip->curx = 0;
2070 clr_attr(ip, ATTR_INV);
2071 if (++ip->cury >= ip->bottom_margin + 1) {
2072 ip->cury = ip->bottom_margin;
2073 SUBR_CURSOR(ip, MOVE_CURSOR);
2074 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
2075 ite_clrtoeol(ip);
2076 } else
2077 SUBR_CURSOR(ip, MOVE_CURSOR);
2078 }
2079 }
2080
2081 itecheckwrap(ip)
2082 struct ite_softc *ip;
2083 {
2084 #if 0
2085 if (++ip->curx == ip->cols) {
2086 if (ip->auto_wrap) {
2087 ip->curx = 0;
2088 clr_attr(ip, ATTR_INV);
2089 if (++ip->cury >= ip->bottom_margin + 1) {
2090 ip->cury = ip->bottom_margin;
2091 SUBR_CURSOR(ip, MOVE_CURSOR);
2092 SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
2093 ite_clrtoeol(ip);
2094 return;
2095 }
2096 } else
2097 /* stay there if no autowrap.. */
2098 ip->curx--;
2099 }
2100 #else
2101 if (ip->curx < ip->cols) {
2102 ip->curx++;
2103 SUBR_CURSOR(ip, MOVE_CURSOR);
2104 }
2105 #endif
2106 }
2107
2108 void
2109 iteattach()
2110 {
2111 }
2112
2113 #endif
2114