Home | History | Annotate | Line # | Download | only in dev
ite.c revision 1.26
      1 /*	$NetBSD: ite.c,v 1.26 1995/03/02 04:41:51 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 doesc:
   1279 		switch (ip->escape) {
   1280 		case ESC:
   1281 			switch (c) {
   1282 				/*
   1283 				 * first 7bit equivalents for the
   1284 				 * 8bit control characters
   1285 				 */
   1286 			case 'D':
   1287 				c = IND;
   1288 				ip->escape = 0;
   1289 				break;
   1290 				/*
   1291 				 * and fall into the next
   1292 				 * switch below (same for all `break')
   1293 				 */
   1294 			case 'E':
   1295 				c = NEL;
   1296 				ip->escape = 0;
   1297 				break;
   1298 			case 'H':
   1299 				c = HTS;
   1300 				ip->escape = 0;
   1301 				break;
   1302 			case 'M':
   1303 				c = RI;
   1304 				ip->escape = 0;
   1305 				break;
   1306 			case 'N':
   1307 				c = SS2;
   1308 				ip->escape = 0;
   1309 				break;
   1310 			case 'O':
   1311 				c = SS3;
   1312 				ip->escape = 0;
   1313 				break;
   1314 			case 'P':
   1315 				c = DCS;
   1316 				ip->escape = 0;
   1317 				break;
   1318 			case '[':
   1319 				c = CSI;
   1320 				ip->escape = 0;
   1321 				break;
   1322 			case '\\':
   1323 				c = ST;
   1324 				ip->escape = 0;
   1325 				break;
   1326 			case ']':
   1327 				c = OSC;
   1328 				ip->escape = 0;
   1329 				break;
   1330 			case '^':
   1331 				c = PM;
   1332 				ip->escape = 0;
   1333 				break;
   1334 			case '_':
   1335 				c = APC;
   1336 				ip->escape = 0;
   1337 				break;
   1338 				/* introduces 7/8bit control */
   1339 			case ' ':
   1340 				/* can be followed by either F or G */
   1341 				ip->escape = ' ';
   1342 				break;
   1343 			/*
   1344 			 * a lot of character set selections, not yet
   1345 			 * used... 94-character sets:
   1346 			 */
   1347 			case '(':	/* G0 */
   1348 			case ')':	/* G1 */
   1349 				ip->escape = c;
   1350 				return;
   1351 			case '*':	/* G2 */
   1352 			case '+':	/* G3 */
   1353 			case 'B':	/* ASCII */
   1354 			case 'A':	/* ISO latin 1 */
   1355 			case '<':	/* user preferred suplemental */
   1356 			case '0':	/* dec special graphics */
   1357 			/*
   1358 			 * 96-character sets:
   1359 			 */
   1360 			case '-':	/* G1 */
   1361 			case '.':	/* G2 */
   1362 			case '/':	/* G3 */
   1363 			/*
   1364 			 * national character sets:
   1365 			 */
   1366 			case '4':	/* dutch */
   1367 			case '5':
   1368 			case 'C':	/* finnish */
   1369 			case 'R':	/* french */
   1370 			case 'Q':	/* french canadian */
   1371 			case 'K':	/* german */
   1372 			case 'Y':	/* italian */
   1373 			case '6':	/* norwegian/danish */
   1374 				/*
   1375 				 * note: %5 and %6 are not supported (two
   1376 				 * chars..)
   1377 				 */
   1378 				ip->escape = 0;
   1379 				/* just ignore for now */
   1380 				return;
   1381 			/*
   1382 			 * locking shift modes (as you might guess, not
   1383 			 * yet supported..)
   1384 			 */
   1385 			case '`':
   1386 				ip->GR = ip->G1;
   1387 				ip->escape = 0;
   1388 				return;
   1389 			case 'n':
   1390 				ip->GL = ip->G2;
   1391 				ip->escape = 0;
   1392 				return;
   1393 			case '}':
   1394 				ip->GR = ip->G2;
   1395 				ip->escape = 0;
   1396 				return;
   1397 			case 'o':
   1398 				ip->GL = ip->G3;
   1399 				ip->escape = 0;
   1400 				return;
   1401 			case '|':
   1402 				ip->GR = ip->G3;
   1403 				ip->escape = 0;
   1404 				return;
   1405 			case '#':
   1406 				/* font width/height control */
   1407 				ip->escape = '#';
   1408 				return;
   1409 			case 'c':
   1410 				/* hard terminal reset .. */
   1411 				ite_reset(ip);
   1412 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1413 				ip->escape = 0;
   1414 				return;
   1415 			case '7':
   1416 				ip->save_curx = ip->curx;
   1417 				ip->save_cury = ip->cury;
   1418 				ip->save_attribute = ip->attribute;
   1419 				ip->escape = 0;
   1420 				return;
   1421 			case '8':
   1422 				ip->curx = ip->save_curx;
   1423 				ip->cury = ip->save_cury;
   1424 				ip->attribute = ip->save_attribute;
   1425 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1426 				ip->escape = 0;
   1427 				return;
   1428 			case '=':
   1429 				ip->keypad_appmode = 1;
   1430 				ip->escape = 0;
   1431 				return;
   1432 			case '>':
   1433 				ip->keypad_appmode = 0;
   1434 				ip->escape = 0;
   1435 				return;
   1436 			case 'Z':	/* request ID */
   1437 				/* XXX not clean */
   1438 				if (ip->emul_level == EMUL_VT100)
   1439 					ite_sendstr("\033[?61;0c");
   1440 				else
   1441 					ite_sendstr("\033[?63;0c");
   1442 				ip->escape = 0;
   1443 				return;
   1444 			default:
   1445 				/*
   1446 				 * default catch all for not recognized ESC
   1447 				 * sequences
   1448 				 */
   1449 				ip->escape = 0;
   1450 				return;
   1451 			}
   1452 			break;
   1453 		case '(':
   1454 		case ')':
   1455 			ip->escape = 0;
   1456 			return;
   1457 		case ' ':
   1458 			switch (c) {
   1459 			case 'F':
   1460 				ip->eightbit_C1 = 0;
   1461 				ip->escape = 0;
   1462 				return;
   1463 			case 'G':
   1464 				ip->eightbit_C1 = 1;
   1465 				ip->escape = 0;
   1466 				return;
   1467 			default:
   1468 				/* not supported */
   1469 				ip->escape = 0;
   1470 				return;
   1471 			}
   1472 			break;
   1473 		case '#':
   1474 			switch (c) {
   1475 			case '5':
   1476 				/* single height, single width */
   1477 				ip->escape = 0;
   1478 				return;
   1479 			case '6':
   1480 				/* double width, single height */
   1481 				ip->escape = 0;
   1482 				return;
   1483 			case '3':
   1484 				/* top half */
   1485 				ip->escape = 0;
   1486 				return;
   1487 			case '4':
   1488 				/* bottom half */
   1489 				ip->escape = 0;
   1490 				return;
   1491 			case '8':
   1492 				/* screen alignment pattern... */
   1493 				alignment_display(ip);
   1494 				ip->escape = 0;
   1495 				return;
   1496 			default:
   1497 				ip->escape = 0;
   1498 				return;
   1499 			}
   1500 			break;
   1501 		case CSI:
   1502 			/* the biggie... */
   1503 			switch (c) {
   1504 			case '0':
   1505 			case '1':
   1506 			case '2':
   1507 			case '3':
   1508 			case '4':
   1509 			case '5':
   1510 			case '6':
   1511 			case '7':
   1512 			case '8':
   1513 			case '9':
   1514 			case ';':
   1515 			case '\"':
   1516 			case '$':
   1517 			case '>':
   1518 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   1519 					*ip->ap++ = c;
   1520 				return;
   1521 			case BS:
   1522 				/*
   1523 				 * you wouldn't believe such perversion is
   1524 				 * possible? it is.. BS is allowed in between
   1525 				 * cursor sequences (at least), according to
   1526 				 * vttest..
   1527 				 */
   1528 				if (--ip->curx < 0)
   1529 					ip->curx = 0;
   1530 				else
   1531 					SUBR_CURSOR(ip, MOVE_CURSOR);
   1532 				break;
   1533 			case 'p':
   1534 				*ip->ap = 0;
   1535 				if (!strncmp(ip->argbuf, "61\"", 3))
   1536 					ip->emul_level = EMUL_VT100;
   1537 				else if (!strncmp(ip->argbuf, "63;1\"", 5)
   1538 				    || !strncmp(ip->argbuf, "62;1\"", 5))
   1539 					ip->emul_level = EMUL_VT300_7;
   1540 				else
   1541 					ip->emul_level = EMUL_VT300_8;
   1542 				ip->escape = 0;
   1543 				return;
   1544 			case '?':
   1545 				*ip->ap = 0;
   1546 				ip->escape = '?';
   1547 				ip->ap = ip->argbuf;
   1548 				return;
   1549 			case 'c':
   1550 				*ip->ap = 0;
   1551 				if (ip->argbuf[0] == '>') {
   1552 					ite_sendstr("\033[>24;0;0;0c");
   1553 				} else
   1554 					switch (ite_zargnum(ip)) {
   1555 					case 0:
   1556 						/*
   1557 						 * primary DA request, send
   1558 						 * primary DA response
   1559 						 */
   1560 						if (ip->emul_level
   1561 						    == EMUL_VT100)
   1562 							ite_sendstr(
   1563 							    "\033[?1;1c");
   1564 						else
   1565 							ite_sendstr(
   1566 							    "\033[?63;1c");
   1567 						break;
   1568 					}
   1569 				ip->escape = 0;
   1570 				return;
   1571 			case 'n':
   1572 				switch (ite_zargnum(ip)) {
   1573 				case 5:
   1574 					/* no malfunction */
   1575 					ite_sendstr("\033[0n");
   1576 					break;
   1577 				case 6:
   1578 					/* cursor position report */
   1579 					sprintf(ip->argbuf, "\033[%d;%dR",
   1580 					    ip->cury + 1, ip->curx + 1);
   1581 					ite_sendstr(ip->argbuf);
   1582 					break;
   1583 				}
   1584 				ip->escape = 0;
   1585 				return;
   1586 			case 'x':
   1587 				switch (ite_zargnum(ip)) {
   1588 				case 0:
   1589 					/* Fake some terminal parameters.  */
   1590 					ite_sendstr("\033[2;1;1;112;112;1;0x");
   1591 					break;
   1592 				case 1:
   1593 					ite_sendstr("\033[3;1;1;112;112;1;0x");
   1594 					break;
   1595 				}
   1596 				ip->escape = 0;
   1597 				return;
   1598 			case 'g':
   1599 				switch (ite_zargnum(ip)) {
   1600 				case 0:
   1601 					if (ip->curx < ip->cols)
   1602 						ip->tabs[ip->curx] = 0;
   1603 					break;
   1604 				case 3:
   1605 					for (n = 0; n < ip->cols; n++)
   1606 						ip->tabs[n] = 0;
   1607 					break;
   1608 				}
   1609 				ip->escape = 0;
   1610 				return;
   1611 			case 'h':
   1612 			case 'l':
   1613 				n = ite_zargnum(ip);
   1614 				switch (n) {
   1615 				case 4:
   1616 					/* insert/replace mode */
   1617 					ip->imode = (c == 'h');
   1618 					break;
   1619 				case 20:
   1620 					ip->linefeed_newline = (c == 'h');
   1621 					break;
   1622 				}
   1623 				ip->escape = 0;
   1624 				return;
   1625 			case 'M':
   1626 				ite_dnline(ip, ite_argnum(ip));
   1627 				ip->escape = 0;
   1628 				return;
   1629 			case 'L':
   1630 				ite_inline(ip, ite_argnum(ip));
   1631 				ip->escape = 0;
   1632 				return;
   1633 			case 'P':
   1634 				ite_dnchar(ip, ite_argnum(ip));
   1635 				ip->escape = 0;
   1636 				return;
   1637 			case '@':
   1638 				ite_inchar(ip, ite_argnum(ip));
   1639 				ip->escape = 0;
   1640 				return;
   1641 			case 'G':
   1642 				/*
   1643 				 * this one was *not* in my vt320 manual but in
   1644 				 * a vt320 termcap entry.. who is right? It's
   1645 				 * supposed to set the horizontal cursor
   1646 				 * position.
   1647 				 */
   1648 				*ip->ap = 0;
   1649 				x = atoi(ip->argbuf);
   1650 				if (x)
   1651 					x--;
   1652 				ip->curx = min(x, ip->cols - 1);
   1653 				ip->escape = 0;
   1654 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1655 				clr_attr(ip, ATTR_INV);
   1656 				return;
   1657 			case 'd':
   1658 				/*
   1659 				 * same thing here, this one's for setting the
   1660 				 * absolute vertical cursor position. Not
   1661 				 * documented...
   1662 				 */
   1663 				*ip->ap = 0;
   1664 				y = atoi(ip->argbuf);
   1665 				if (y)
   1666 					y--;
   1667 				if (ip->inside_margins)
   1668 					y += ip->top_margin;
   1669 				ip->cury = min(y, ip->rows - 1);
   1670 				ip->escape = 0;
   1671 				snap_cury(ip);
   1672 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1673 				clr_attr(ip, ATTR_INV);
   1674 				return;
   1675 			case 'H':
   1676 			case 'f':
   1677 				*ip->ap = 0;
   1678 				y = atoi(ip->argbuf);
   1679 				x = 0;
   1680 				cp = index(ip->argbuf, ';');
   1681 				if (cp)
   1682 					x = atoi(cp + 1);
   1683 				if (x)
   1684 					x--;
   1685 				if (y)
   1686 					y--;
   1687 				if (ip->inside_margins)
   1688 					y += ip->top_margin;
   1689 				ip->cury = min(y, ip->rows - 1);
   1690 				ip->curx = min(x, ip->cols - 1);
   1691 				ip->escape = 0;
   1692 				snap_cury(ip);
   1693 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1694 				clr_attr(ip, ATTR_INV);
   1695 				return;
   1696 			case 'A':
   1697 				n = ite_argnum(ip);
   1698 				n = ip->cury - (n ? n : 1);
   1699 				if (n < 0)
   1700 					n = 0;
   1701 				if (ip->inside_margins)
   1702 					n = max(ip->top_margin, n);
   1703 				else if (n == ip->top_margin - 1)
   1704 					/*
   1705 					 * allow scrolling outside region, but
   1706 					 * don't scroll out of active region
   1707 					 * without explicit CUP
   1708 					 */
   1709 					n = ip->top_margin;
   1710 				ip->cury = n;
   1711 				ip->escape = 0;
   1712 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1713 				clr_attr(ip, ATTR_INV);
   1714 				return;
   1715 			case 'B':
   1716 				n = ite_argnum(ip);
   1717 				n = ip->cury + (n ? n : 1);
   1718 				n = min(ip->rows - 1, n);
   1719 				if (ip->inside_margins)
   1720 					n = min(ip->bottom_margin, n);
   1721 				else if (n == ip->bottom_margin + 1)
   1722 					/*
   1723 					 * allow scrolling outside region, but
   1724 					 * don't scroll out of active region
   1725 					 * without explicit CUP
   1726 					 */
   1727 					n = ip->bottom_margin;
   1728 				ip->cury = n;
   1729 				ip->escape = 0;
   1730 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1731 				clr_attr(ip, ATTR_INV);
   1732 				return;
   1733 			case 'C':
   1734 				n = ite_argnum(ip);
   1735 				n = n ? n : 1;
   1736 				ip->curx = min(ip->curx + n, ip->cols - 1);
   1737 				ip->escape = 0;
   1738 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1739 				clr_attr(ip, ATTR_INV);
   1740 				return;
   1741 			case 'D':
   1742 				n = ite_argnum(ip);
   1743 				n = n ? n : 1;
   1744 				n = ip->curx - n;
   1745 				ip->curx = n >= 0 ? n : 0;
   1746 				ip->escape = 0;
   1747 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1748 				clr_attr(ip, ATTR_INV);
   1749 				return;
   1750 			case 'J':
   1751 				*ip->ap = 0;
   1752 				n = ite_zargnum(ip);
   1753 				if (n == 0)
   1754 					ite_clrtoeos(ip);
   1755 				else if (n == 1)
   1756 					ite_clrtobos(ip);
   1757 				else if (n == 2)
   1758 					ite_clrscreen(ip);
   1759 				ip->escape = 0;
   1760 				return;
   1761 			case 'K':
   1762 				n = ite_zargnum(ip);
   1763 				if (n == 0)
   1764 					ite_clrtoeol(ip);
   1765 				else if (n == 1)
   1766 					ite_clrtobol(ip);
   1767 				else if (n == 2)
   1768 					ite_clrline(ip);
   1769 				ip->escape = 0;
   1770 				return;
   1771 			case 'X':
   1772 				n = ite_argnum(ip) - 1;
   1773 				n = min(n, ip->cols - 1 - ip->curx);
   1774 				for (; n >= 0; n--) {
   1775 					attrclr(ip, ip->cury, ip->curx + n, 1, 1);
   1776 					SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
   1777 				}
   1778 				ip->escape = 0;
   1779 				return;
   1780 			case '}':
   1781 			case '`':
   1782 				/* status line control */
   1783 				ip->escape = 0;
   1784 				return;
   1785 			case 'r':
   1786 				*ip->ap = 0;
   1787 				x = atoi(ip->argbuf);
   1788 				x = x ? x : 1;
   1789 				y = ip->rows;
   1790 				cp = index(ip->argbuf, ';');
   1791 				if (cp) {
   1792 					y = atoi(cp + 1);
   1793 					y = y ? y : ip->rows;
   1794 				}
   1795 				if (y - x < 2) {
   1796 					/*
   1797 					 * if illegal scrolling region, reset
   1798 					 * to defaults
   1799 					 */
   1800 					x = 1;
   1801 					y = ip->rows;
   1802 				}
   1803 				x--;
   1804 				y--;
   1805 				ip->top_margin = min(x, ip->rows - 1);
   1806 				ip->bottom_margin = min(y, ip->rows - 1);
   1807 				if (ip->inside_margins) {
   1808 					ip->cury = ip->top_margin;
   1809 					ip->curx = 0;
   1810 					SUBR_CURSOR(ip, MOVE_CURSOR);
   1811 				}
   1812 				ip->escape = 0;
   1813 				return;
   1814 			case 'm':
   1815 				/* big attribute setter/resetter */
   1816 				{ char *cp;
   1817 				*ip->ap = 0;
   1818 				/* kludge to make CSIm work (== CSI0m) */
   1819 				if (ip->ap == ip->argbuf)
   1820 					ip->ap++;
   1821 				for (cp = ip->argbuf; cp < ip->ap;) {
   1822 					switch (*cp) {
   1823 					case 0:
   1824 					case '0':
   1825 						clr_attr(ip, ATTR_ALL);
   1826 						cp++;
   1827 						break;
   1828 
   1829 					case '1':
   1830 						set_attr(ip, ATTR_BOLD);
   1831 						cp++;
   1832 						break;
   1833 
   1834 					case '2':
   1835 						switch (cp[1]) {
   1836 						case '2':
   1837 							clr_attr(ip, ATTR_BOLD);
   1838 							cp += 2;
   1839 							break;
   1840 
   1841 						case '4':
   1842 							clr_attr(ip, ATTR_UL);
   1843 							cp += 2;
   1844 							break;
   1845 
   1846 						case '5':
   1847 							clr_attr(ip, ATTR_BLINK);
   1848 							cp += 2;
   1849 							break;
   1850 
   1851 						case '7':
   1852 							clr_attr(ip, ATTR_INV);
   1853 							cp += 2;
   1854 							break;
   1855 
   1856 						default:
   1857 							cp++;
   1858 							break;
   1859 						}
   1860 						break;
   1861 
   1862 					case '4':
   1863 						set_attr(ip, ATTR_UL);
   1864 						cp++;
   1865 						break;
   1866 
   1867 					case '5':
   1868 						set_attr(ip, ATTR_BLINK);
   1869 						cp++;
   1870 						break;
   1871 
   1872 					case '7':
   1873 						set_attr(ip, ATTR_INV);
   1874 						cp++;
   1875 						break;
   1876 
   1877 					default:
   1878 						cp++;
   1879 						break;
   1880 					}
   1881 				}
   1882 				ip->escape = 0;
   1883 				return; }
   1884 			case 'u':
   1885 				/* DECRQTSR */
   1886 				ite_sendstr("\033P\033\\");
   1887 				ip->escape = 0;
   1888 				return;
   1889 			default:
   1890 				ip->escape = 0;
   1891 				return;
   1892 			}
   1893 			break;
   1894 		case '?':	/* CSI ? */
   1895 			switch (c) {
   1896 			case '0':
   1897 			case '1':
   1898 			case '2':
   1899 			case '3':
   1900 			case '4':
   1901 			case '5':
   1902 			case '6':
   1903 			case '7':
   1904 			case '8':
   1905 			case '9':
   1906 			case ';':
   1907 			case '\"':
   1908 			case '$':
   1909 				/*
   1910 				 * Don't fill the last character;
   1911 				 * it's needed.
   1912 				 * XXX yeah, where ??
   1913 				 */
   1914 				if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
   1915 					*ip->ap++ = c;
   1916 				return;
   1917 			case 'n':
   1918 				*ip->ap = 0;
   1919 				if (ip->ap == &ip->argbuf[2]) {
   1920 					if (!strncmp(ip->argbuf, "15", 2))
   1921 						/* printer status: no printer */
   1922 						ite_sendstr("\033[13n");
   1923 
   1924 					else if (!strncmp(ip->argbuf, "25", 2))
   1925 						/* udk status */
   1926 						ite_sendstr("\033[20n");
   1927 
   1928 					else if (!strncmp(ip->argbuf, "26", 2))
   1929 						/* keyboard dialect: US */
   1930 						ite_sendstr("\033[27;1n");
   1931 				}
   1932 				ip->escape = 0;
   1933 				return;
   1934 			case 'h':
   1935 			case 'l':
   1936 				n = ite_zargnum(ip);
   1937 				switch (n) {
   1938 				case 1:
   1939 					ip->cursor_appmode = (c == 'h');
   1940 					break;
   1941 				case 3:
   1942 					/* 132/80 columns (132 == 'h') */
   1943 					break;
   1944 				case 4:	/* smooth scroll */
   1945 					break;
   1946 				case 5:
   1947 					/*
   1948 					 * light background (=='h') /dark
   1949 					 * background(=='l')
   1950 					 */
   1951 					break;
   1952 				case 6:	/* origin mode */
   1953 					ip->inside_margins = (c == 'h');
   1954 					ip->curx = 0;
   1955 					ip->cury = ip->inside_margins ?
   1956 					    ip->top_margin : 0;
   1957 					SUBR_CURSOR(ip, MOVE_CURSOR);
   1958 					break;
   1959 				case 7:	/* auto wraparound */
   1960 					ip->auto_wrap = (c == 'h');
   1961 					break;
   1962 				case 8:	/* keyboard repeat */
   1963 					ip->key_repeat = (c == 'h');
   1964 					break;
   1965 				case 20:	/* newline mode */
   1966 					ip->linefeed_newline = (c == 'h');
   1967 					break;
   1968 				case 25:	/* cursor on/off */
   1969 					SUBR_CURSOR(ip, (c == 'h') ?
   1970 					    DRAW_CURSOR : ERASE_CURSOR);
   1971 					break;
   1972 				}
   1973 				ip->escape = 0;
   1974 				return;
   1975 			default:
   1976 				ip->escape = 0;
   1977 				return;
   1978 			}
   1979 			break;
   1980 		default:
   1981 			ip->escape = 0;
   1982 			return;
   1983 		}
   1984 	}
   1985 	switch (c) {
   1986 	case VT:		/* VT is treated like LF */
   1987 	case FF:		/* so is FF */
   1988 	case LF:
   1989 		/*
   1990 		 * cr->crlf distinction is done here, on output, not on input!
   1991 		 */
   1992 		if (ip->linefeed_newline)
   1993 			ite_crlf(ip);
   1994 		else
   1995 			ite_lf(ip);
   1996 		break;
   1997 	case CR:
   1998 		ite_cr(ip);
   1999 		break;
   2000 	case BS:
   2001 		if (--ip->curx < 0)
   2002 			ip->curx = 0;
   2003 		else
   2004 			SUBR_CURSOR(ip, MOVE_CURSOR);
   2005 		break;
   2006 	case HT:
   2007 		for (n = ip->curx + 1; n < ip->cols; n++) {
   2008 			if (ip->tabs[n]) {
   2009 				ip->curx = n;
   2010 				SUBR_CURSOR(ip, MOVE_CURSOR);
   2011 				break;
   2012 			}
   2013 		}
   2014 		break;
   2015 	case BEL:
   2016 		if (kbd_tty && kbd_ite && kbd_ite->tp == kbd_tty)
   2017 			kbdbell();
   2018 		break;
   2019 	case SO:
   2020 		ip->GL = ip->G1;
   2021 		break;
   2022 	case SI:
   2023 		ip->GL = ip->G0;
   2024 		break;
   2025 	case ENQ:
   2026 		/* send answer-back message !! */
   2027 		break;
   2028 	case CAN:
   2029 		ip->escape = 0;	/* cancel any escape sequence in progress */
   2030 		break;
   2031 	case SUB:
   2032 		ip->escape = 0;	/* dito, but see below */
   2033 		/* should also display a reverse question mark!! */
   2034 		break;
   2035 	case ESC:
   2036 		ip->escape = ESC;
   2037 		break;
   2038 	/*
   2039 	 * now it gets weird.. 8bit control sequences..
   2040 	 */
   2041 	case IND:
   2042 		/* index: move cursor down, scroll */
   2043 		ite_lf(ip);
   2044 		break;
   2045 	case NEL:
   2046 		/* next line. next line, first pos. */
   2047 		ite_crlf(ip);
   2048 		break;
   2049 	case HTS:
   2050 		/* set horizontal tab */
   2051 		if (ip->curx < ip->cols)
   2052 			ip->tabs[ip->curx] = 1;
   2053 		break;
   2054 	case RI:
   2055 		/* reverse index */
   2056 		ite_rlf(ip);
   2057 		break;
   2058 	case SS2:
   2059 		/* go into G2 for one character */
   2060 		/* not yet supported */
   2061 		break;
   2062 	case SS3:
   2063 		/* go into G3 for one character */
   2064 		break;
   2065 	case DCS:
   2066 		/* device control string introducer */
   2067 		ip->escape = DCS;
   2068 		ip->ap = ip->argbuf;
   2069 		break;
   2070 	case CSI:
   2071 		/* control sequence introducer */
   2072 		ip->escape = CSI;
   2073 		ip->ap = ip->argbuf;
   2074 		break;
   2075 	case ST:
   2076 		/* string terminator */
   2077 		/* ignore, if not used as terminator */
   2078 		break;
   2079 	case OSC:
   2080 		/*
   2081 		 * introduces OS command. Ignore everything
   2082 		 * upto ST
   2083 		 */
   2084 		ip->escape = OSC;
   2085 		break;
   2086 	case PM:
   2087 		/* privacy message, ignore everything upto ST */
   2088 		ip->escape = PM;
   2089 		break;
   2090 	case APC:
   2091 		/*
   2092 		 * application program command, ignore * everything upto ST
   2093 		 */
   2094 		ip->escape = APC;
   2095 		break;
   2096 	default:
   2097 		if ((c & 0x7f) < ' ' || c == DEL)
   2098 			break;
   2099 		if (ip->imode)
   2100 			ite_inchar(ip, 1);
   2101 		iteprecheckwrap(ip);
   2102 #ifdef DO_WEIRD_ATTRIBUTES
   2103 		if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
   2104 			attrset(ip, ATTR_INV);
   2105 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
   2106 		} else
   2107 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
   2108 #else
   2109 		SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
   2110 #endif
   2111 		SUBR_CURSOR(ip, DRAW_CURSOR);
   2112 		itecheckwrap(ip);
   2113 		break;
   2114 	}
   2115 }
   2116 
   2117 int
   2118 iteprecheckwrap(ip)
   2119 	struct ite_softc *ip;
   2120 {
   2121 	if (ip->auto_wrap && ip->curx == ip->cols) {
   2122 		ip->curx = 0;
   2123 		clr_attr(ip, ATTR_INV);
   2124 		if (++ip->cury >= ip->bottom_margin + 1) {
   2125 			ip->cury = ip->bottom_margin;
   2126 			SUBR_CURSOR(ip, MOVE_CURSOR);
   2127 			SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   2128 			ite_clrtoeol(ip);
   2129 		} else
   2130 			SUBR_CURSOR(ip, MOVE_CURSOR);
   2131 	}
   2132 }
   2133 
   2134 int
   2135 itecheckwrap(ip)
   2136 	struct ite_softc *ip;
   2137 {
   2138 #if 0
   2139 	if (++ip->curx == ip->cols) {
   2140 		if (ip->auto_wrap) {
   2141 			ip->curx = 0;
   2142 			clr_attr(ip, ATTR_INV);
   2143 			if (++ip->cury >= ip->bottom_margin + 1) {
   2144 				ip->cury = ip->bottom_margin;
   2145 				SUBR_CURSOR(ip, MOVE_CURSOR);
   2146 				SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   2147 				ite_clrtoeol(ip);
   2148 				return;
   2149 			}
   2150 		} else
   2151 			/* stay there if no autowrap.. */
   2152 			ip->curx--;
   2153 	}
   2154 #else
   2155 	if (ip->curx < ip->cols) {
   2156 		ip->curx++;
   2157 		SUBR_CURSOR(ip, MOVE_CURSOR);
   2158 	}
   2159 #endif
   2160 }
   2161