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