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