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