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