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