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