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