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