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