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