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