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