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