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