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