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