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