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