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