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