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