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