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