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