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