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