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