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