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