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