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