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