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