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