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