Home | History | Annotate | Line # | Download | only in dev
ite.c revision 1.49
      1 /*	$NetBSD: ite.c,v 1.49 2007/03/11 08:09:24 isaki 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.49 2007/03/11 08:09:24 isaki 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(u_char c)
    636 {
    637 	static u_char mod = 0;
    638 	struct key key;
    639 	u_char code, up, mask;
    640 	int s;
    641 
    642 	up = c & 0x80 ? 1 : 0;
    643 	c &= 0x7f;
    644 	code = 0;
    645 
    646 	s = spltty();
    647 
    648 	mask = 0;
    649 	if (c >= KBD_LEFT_ALT && !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
    650 		switch (c) {
    651 		case KBD_LEFT_SHIFT:
    652 			mask = KBD_MOD_SHIFT;
    653 			break;
    654 
    655 		case KBD_LEFT_ALT:
    656 			mask = KBD_MOD_LALT;
    657 			break;
    658 
    659 		case KBD_RIGHT_ALT:
    660 			mask = KBD_MOD_RALT;
    661 			break;
    662 
    663 		case KBD_LEFT_META:
    664 			mask = KBD_MOD_LMETA;
    665 			break;
    666 
    667 		case KBD_RIGHT_META:
    668 			mask = KBD_MOD_RMETA;
    669 			break;
    670 
    671 		case KBD_CAPS_LOCK:
    672 			/*
    673 			 * capslock already behaves `right', don't need to
    674 			 * keep track of the state in here.
    675 			 */
    676 			mask = KBD_MOD_CAPS;
    677 			break;
    678 
    679 		case KBD_CTRL:
    680 			mask = KBD_MOD_CTRL;
    681 			break;
    682 
    683 		case KBD_RECONNECT:
    684 			/* ite got 0xff */
    685 			if (up)
    686 				kbd_setLED();
    687 			break;
    688 		}
    689 		if (mask & KBD_MOD_CAPS) {
    690 			if (!up) {
    691 				mod ^= KBD_MOD_CAPS;
    692 				kbdled ^= LED_CAPS_LOCK;
    693 				kbd_setLED();
    694 			}
    695 		} else if (up)
    696 			mod &= ~mask;
    697 		else mod |= mask;
    698 		splx(s);
    699 		return -1;
    700 	}
    701 
    702 	if (up) {
    703 		splx(s);
    704 		return -1;
    705 	}
    706 
    707 	/* translate modifiers */
    708 	if (mod & KBD_MOD_SHIFT) {
    709 		if (mod & KBD_MOD_ALT)
    710 			key = kbdmap.alt_shift_keys[c];
    711 		else
    712 			key = kbdmap.shift_keys[c];
    713 	} else if (mod & KBD_MOD_ALT)
    714 		key = kbdmap.alt_keys[c];
    715 	else {
    716 		key = kbdmap.keys[c];
    717 		/* if CAPS and key is CAPable (no pun intended) */
    718 		if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
    719 			key = kbdmap.shift_keys[c];
    720 	}
    721 	code = key.code;
    722 
    723 	/* if string return */
    724 	if (key.mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
    725 		splx(s);
    726 		return -1;
    727 	}
    728 	/* handle dead keys */
    729 	if (key.mode & KBD_MODE_DEAD) {
    730 		splx(s);
    731 		return -1;
    732 	}
    733 	if (mod & KBD_MOD_CTRL)
    734 		code &= 0x1f;
    735 	if (mod & KBD_MOD_META)
    736 		code |= 0x80;
    737 
    738 	/* do console mapping. */
    739 	code = code == '\r' ? '\n' : code;
    740 
    741 	splx(s);
    742 	return (code);
    743 }
    744 
    745 /* And now the old stuff. */
    746 inline static void
    747 itesendch(int ch)
    748 {
    749 	(*kbd_tty->t_linesw->l_rint)(ch, kbd_tty);
    750 }
    751 
    752 
    753 void
    754 ite_filter(u_char c)
    755 {
    756 	static u_short mod = 0;
    757 	unsigned char code, *str;
    758 	u_short up, mask;
    759 	struct key key;
    760 	int s, i;
    761 
    762 	if (!kbd_ite || !(kbd_tty = ite_tty[device_unit(&kbd_ite->device)]))
    763 		return;
    764 
    765 	/* have to make sure we're at spltty in here */
    766 	s = spltty();
    767 
    768 	up = c & 0x80 ? 1 : 0;
    769 	c &= 0x7f;
    770 	code = 0;
    771 
    772 	mask = 0;
    773 	if (c >= KBD_LEFT_ALT &&
    774 	    !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
    775 		switch (c) {
    776 		case KBD_LEFT_SHIFT:
    777 			mask = KBD_MOD_SHIFT;
    778 			break;
    779 
    780 		case KBD_LEFT_ALT:
    781 			mask = KBD_MOD_LALT;
    782 			break;
    783 
    784 		case KBD_RIGHT_ALT:
    785 			mask = KBD_MOD_RALT;
    786 			break;
    787 
    788 		case KBD_LEFT_META:
    789 			mask = KBD_MOD_LMETA;
    790 			break;
    791 
    792 		case KBD_RIGHT_META:
    793 			mask = KBD_MOD_RMETA;
    794 			break;
    795 
    796 		case KBD_CAPS_LOCK:
    797 			/*
    798 			 * capslock already behaves `right', don't need to keep
    799 			 * track of the state in here.
    800 			 */
    801 			mask = KBD_MOD_CAPS;
    802 			break;
    803 
    804 		case KBD_CTRL:
    805 			mask = KBD_MOD_CTRL;
    806 			break;
    807 
    808 		case KBD_OPT1:
    809 			mask = KBD_MOD_OPT1;
    810 			break;
    811 
    812 		case KBD_OPT2:
    813 			mask = KBD_MOD_OPT2;
    814 			break;
    815 
    816 		case KBD_RECONNECT:
    817 			if (up) { /* ite got 0xff */
    818 				kbd_setLED();
    819 			}
    820 			break;
    821 		}
    822 
    823 		if (mask & KBD_MOD_CAPS) {
    824 			if (!up) {
    825 				mod ^= KBD_MOD_CAPS;
    826 				kbdled ^= LED_CAPS_LOCK;
    827 				kbd_setLED();
    828 			}
    829 		} else if (up) {
    830 			mod &= ~mask;
    831 		} else mod |= mask;
    832 
    833 		/*
    834 		 * return even if it wasn't a modifier key, the other
    835 		 * codes up here are either special (like reset warning),
    836 		 * or not yet defined
    837 		 */
    838 		splx(s);
    839 		return;
    840 	}
    841 
    842 	if (up) {
    843 		splx(s);
    844 		return;
    845 	}
    846 
    847 	/*
    848 	 * intercept LAlt-LMeta-F1 here to switch back to original ascii-keymap.
    849 	 * this should probably be configurable..
    850 	 */
    851 	if (mod == (KBD_MOD_LALT|KBD_MOD_LMETA) && c == 0x63) {
    852 		memcpy(&kbdmap, &ascii_kbdmap, sizeof(struct kbdmap));
    853 		splx(s);
    854 		return;
    855 	}
    856 
    857 	/* translate modifiers */
    858 	if (mod & KBD_MOD_SHIFT) {
    859 		if (mod & KBD_MOD_ALT)
    860 			key = kbdmap.alt_shift_keys[c];
    861 		else
    862 			key = kbdmap.shift_keys[c];
    863 	} else if (mod & KBD_MOD_ALT)
    864 		key = kbdmap.alt_keys[c];
    865 	else {
    866 		key = kbdmap.keys[c];
    867 		/* if CAPS and key is CAPable (no pun intended) */
    868 		if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
    869 			key = kbdmap.shift_keys[c];
    870 		else if ((mod & KBD_MOD_OPT2) && (key.mode & KBD_MODE_KPAD))
    871 			key = kbdmap.shift_keys[c];
    872 	}
    873 	code = key.code;
    874 
    875 	/* handle dead keys */
    876 	if (key.mode & KBD_MODE_DEAD) {
    877 		splx(s);
    878 		return;
    879 	}
    880   	/* if not string, apply META and CTRL modifiers */
    881 	if (! (key.mode & KBD_MODE_STRING)
    882 	    && (!(key.mode & KBD_MODE_KPAD) ||
    883 		(kbd_ite && !kbd_ite->keypad_appmode))) {
    884 		if ((mod & KBD_MOD_CTRL) &&
    885 		    (code == ' ' || (code >= '@' && code <= 'z')))
    886 			code &= 0x1f;
    887 		if (mod & KBD_MOD_META)
    888 			code |= 0x80;
    889 	} else if ((key.mode & KBD_MODE_KPAD) &&
    890 	       (kbd_ite && kbd_ite->keypad_appmode)) {
    891 		static const char * const in = "0123456789-+.\r()/*";
    892 		static const char * const out = "pqrstuvwxymlnMPQRS";
    893 		char *cp = strchr(in, code);
    894 
    895 		/*
    896 		 * keypad-appmode sends SS3 followed by the above
    897 		 * translated character
    898 		 */
    899 		(*kbd_tty->t_linesw->l_rint)(27, kbd_tty);
    900 		(*kbd_tty->t_linesw->l_rint)('O', kbd_tty);
    901 		(*kbd_tty->t_linesw->l_rint)(out[cp - in], kbd_tty);
    902 		splx(s);
    903 		return;
    904 	} else {
    905 		/* *NO* I don't like this.... */
    906 		static u_char app_cursor[] =
    907 		{
    908 		  3, 27, 'O', 'A',
    909 		  3, 27, 'O', 'B',
    910 		  3, 27, 'O', 'C',
    911 		  3, 27, 'O', 'D'};
    912 
    913 		str = kbdmap.strings + code;
    914 		/*
    915 		 * if this is a cursor key, AND it has the default
    916 		 * keymap setting, AND we're in app-cursor mode, switch
    917 		 * to the above table. This is *nasty* !
    918 		 */
    919 		if (c >= 0x3b && c <= 0x3e && kbd_ite->cursor_appmode
    920 		    && !memcmp(str, "\x03\x1b[", 3) &&
    921 		    strchr("ABCD", str[3]))
    922 			str = app_cursor + 4 * (str[3] - 'A');
    923 
    924 		/*
    925 		 * using a length-byte instead of 0-termination allows
    926 		 * to embed \0 into strings, although this is not used
    927 		 * in the default keymap
    928 		 */
    929 		for (i = *str++; i; i--)
    930 			(*kbd_tty->t_linesw->l_rint)(*str++, kbd_tty);
    931 		splx(s);
    932 		return;
    933 	}
    934 	(*kbd_tty->t_linesw->l_rint)(code, kbd_tty);
    935 
    936 	splx(s);
    937 	return;
    938 }
    939 
    940 /* helper functions, makes the code below more readable */
    941 inline static void
    942 ite_sendstr(struct ite_softc *ip, const char *str)
    943 {
    944 	while (*str)
    945 		itesendch(*str++);
    946 }
    947 
    948 inline static void
    949 alignment_display(struct ite_softc *ip)
    950 {
    951 	int i, j;
    952 
    953 	for (j = 0; j < ip->rows; j++)
    954 		for (i = 0; i < ip->cols; i++)
    955 			SUBR_PUTC(ip, 'E', j, i, ATTR_NOR);
    956 	attrclr(ip, 0, 0, ip->rows, ip->cols);
    957 }
    958 
    959 inline static void
    960 snap_cury(struct ite_softc *ip)
    961 {
    962 	if (ip->inside_margins) {
    963 		if (ip->cury < ip->top_margin)
    964 			ip->cury = ip->top_margin;
    965 		if (ip->cury > ip->bottom_margin)
    966 			ip->cury = ip->bottom_margin;
    967 	}
    968 }
    969 
    970 inline static void
    971 ite_dnchar(struct ite_softc *ip, int n)
    972 {
    973 	n = min(n, ip->cols - ip->curx);
    974 	if (n < ip->cols - ip->curx) {
    975 		SUBR_SCROLL(ip, ip->cury, ip->curx + n, n, SCROLL_LEFT);
    976 		attrmov(ip, ip->cury, ip->curx + n, ip->cury, ip->curx,
    977 			1, ip->cols - ip->curx - n);
    978 		attrclr(ip, ip->cury, ip->cols - n, 1, n);
    979 	}
    980 	while (n-- > 0)
    981 		SUBR_PUTC(ip, ' ', ip->cury, ip->cols - n - 1, ATTR_NOR);
    982 }
    983 
    984 static void
    985 ite_inchar(struct ite_softc *ip, int n)
    986 {
    987 	int c = ip->save_char;
    988 	ip->save_char = 0;
    989 	n = min(n, ip->cols - ip->curx);
    990 	if (n < ip->cols - ip->curx) {
    991 		SUBR_SCROLL(ip, ip->cury, ip->curx, n, SCROLL_RIGHT);
    992 		attrmov(ip, ip->cury, ip->curx, ip->cury, ip->curx + n,
    993 			1, ip->cols - ip->curx - n);
    994 		attrclr(ip, ip->cury, ip->curx, 1, n);
    995 	}
    996 	while (n--)
    997 		SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
    998 	ip->save_char = c;
    999 }
   1000 
   1001 inline static void
   1002 ite_clrtoeol(struct ite_softc *ip)
   1003 {
   1004 	int y = ip->cury, x = ip->curx;
   1005 	if (ip->cols - x > 0) {
   1006 		SUBR_CLEAR(ip, y, x, 1, ip->cols - x);
   1007 		attrclr(ip, y, x, 1, ip->cols - x);
   1008 	}
   1009 }
   1010 
   1011 inline static void
   1012 ite_clrtobol(struct ite_softc *ip)
   1013 {
   1014 	int y = ip->cury, x = min(ip->curx + 1, ip->cols);
   1015 	SUBR_CLEAR(ip, y, 0, 1, x);
   1016 	attrclr(ip, y, 0, 1, x);
   1017 }
   1018 
   1019 inline static void
   1020 ite_clrline(struct ite_softc *ip)
   1021 {
   1022 	int y = ip->cury;
   1023 	SUBR_CLEAR(ip, y, 0, 1, ip->cols);
   1024 	attrclr(ip, y, 0, 1, ip->cols);
   1025 }
   1026 
   1027 inline static void
   1028 ite_clrtoeos(struct ite_softc *ip)
   1029 {
   1030 	ite_clrtoeol(ip);
   1031 	if (ip->cury < ip->rows - 1) {
   1032 		SUBR_CLEAR(ip, ip->cury + 1, 0, ip->rows - 1 - ip->cury, ip->cols);
   1033 		attrclr(ip, ip->cury, 0, ip->rows - ip->cury, ip->cols);
   1034 	}
   1035 }
   1036 
   1037 inline static void
   1038 ite_clrtobos(struct ite_softc *ip)
   1039 {
   1040 	ite_clrtobol(ip);
   1041 	if (ip->cury > 0) {
   1042 		SUBR_CLEAR(ip, 0, 0, ip->cury, ip->cols);
   1043 		attrclr(ip, 0, 0, ip->cury, ip->cols);
   1044 	}
   1045 }
   1046 
   1047 inline static void
   1048 ite_clrscreen(struct ite_softc *ip)
   1049 {
   1050 	SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
   1051 	attrclr(ip, 0, 0, ip->rows, ip->cols);
   1052 }
   1053 
   1054 
   1055 
   1056 inline static void
   1057 ite_dnline(struct ite_softc *ip, int n)
   1058 {
   1059 	/*
   1060 	 * interesting.. if the cursor is outside the scrolling
   1061 	 * region, this command is simply ignored..
   1062 	 */
   1063 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
   1064 		return;
   1065 
   1066 	n = min(n, ip->bottom_margin + 1 - ip->cury);
   1067 	if (n <= ip->bottom_margin - ip->cury) {
   1068 		SUBR_SCROLL(ip, ip->cury + n, 0, n, SCROLL_UP);
   1069 		attrmov(ip, ip->cury + n, 0, ip->cury, 0,
   1070 			ip->bottom_margin + 1 - ip->cury - n, ip->cols);
   1071 	}
   1072 	SUBR_CLEAR(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
   1073 	attrclr(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
   1074 }
   1075 
   1076 inline static void
   1077 ite_inline(struct ite_softc *ip, int n)
   1078 {
   1079 	/*
   1080 	 * interesting.. if the cursor is outside the scrolling
   1081 	 * region, this command is simply ignored..
   1082 	 */
   1083 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
   1084 		return;
   1085 
   1086 	if (n <= 0)
   1087 		n = 1;
   1088 	else n = min(n, ip->bottom_margin + 1 - ip->cury);
   1089 	if (n <= ip->bottom_margin  - ip->cury) {
   1090 		SUBR_SCROLL(ip, ip->cury, 0, n, SCROLL_DOWN);
   1091 		attrmov(ip, ip->cury, 0, ip->cury + n, 0,
   1092 			ip->bottom_margin + 1 - ip->cury - n, ip->cols);
   1093 	}
   1094 	SUBR_CLEAR(ip, ip->cury, 0, n, ip->cols);
   1095 	attrclr(ip, ip->cury, 0, n, ip->cols);
   1096 	ip->curx = 0;
   1097 }
   1098 
   1099 inline static void
   1100 ite_index(struct ite_softc *ip)
   1101 {
   1102 	++ip->cury;
   1103 	if ((ip->cury == ip->bottom_margin+1) || (ip->cury == ip->rows)) {
   1104 		ip->cury--;
   1105 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   1106 		ite_clrline(ip);
   1107 	}
   1108 	/*clr_attr(ip, ATTR_INV);*/
   1109 }
   1110 
   1111 inline static void
   1112 ite_lf(struct ite_softc *ip)
   1113 {
   1114 	++ip->cury;
   1115 	if (ip->cury > ip->bottom_margin) {
   1116 		ip->cury--;
   1117 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   1118 		ite_clrline(ip);
   1119 	}
   1120 /*	SUBR_CURSOR(ip, MOVE_CURSOR);*/
   1121 	/*clr_attr(ip, ATTR_INV);*/
   1122 	/* reset character set ... thanks for mohta. */
   1123 	ip->G0 = CSET_ASCII;
   1124 	ip->G1 = CSET_JIS1983;
   1125 	ip->G2 = CSET_JISKANA;
   1126 	ip->G3 = CSET_JIS1990;
   1127 	ip->GL = &ip->G0;
   1128 	ip->GR = &ip->G1;
   1129 	ip->save_GL = 0;
   1130 	ip->save_char = 0;
   1131 }
   1132 
   1133 inline static void
   1134 ite_crlf(struct ite_softc *ip)
   1135 {
   1136 	ip->curx = 0;
   1137 	ite_lf (ip);
   1138 }
   1139 
   1140 inline static void
   1141 ite_cr(struct ite_softc *ip)
   1142 {
   1143 	if (ip->curx) {
   1144 		ip->curx = 0;
   1145 	}
   1146 }
   1147 
   1148 inline static void
   1149 ite_rlf(struct ite_softc *ip)
   1150 {
   1151 	ip->cury--;
   1152 	if ((ip->cury < 0) || (ip->cury == ip->top_margin - 1)) {
   1153 		ip->cury++;
   1154 		SUBR_SCROLL(ip, ip->top_margin, 0, 1, SCROLL_DOWN);
   1155 		ite_clrline(ip);
   1156 	}
   1157 	clr_attr(ip, ATTR_INV);
   1158 }
   1159 
   1160 inline static int
   1161 atoi(const char *cp)
   1162 {
   1163 	int n;
   1164 
   1165 	for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
   1166 		n = n * 10 + *cp - '0';
   1167 	return n;
   1168 }
   1169 
   1170 inline static int
   1171 ite_argnum(struct ite_softc *ip)
   1172 {
   1173 	char ch;
   1174 	int n;
   1175 
   1176 	/* convert argument string into number */
   1177 	if (ip->ap == ip->argbuf)
   1178 		return 1;
   1179 	ch = *ip->ap;
   1180 	*ip->ap = 0;
   1181 	n = atoi(ip->argbuf);
   1182 	*ip->ap = ch;
   1183 
   1184 	return n;
   1185 }
   1186 
   1187 inline static int
   1188 ite_zargnum(struct ite_softc *ip)
   1189 {
   1190 	char ch;
   1191 	int n;
   1192 
   1193 	/* convert argument string into number */
   1194 	if (ip->ap == ip->argbuf)
   1195 		return 0;
   1196 	ch = *ip->ap;
   1197 	*ip->ap = 0;	/* terminate string */
   1198 	n = atoi(ip->argbuf);
   1199 	*ip->ap = ch;
   1200 
   1201 	return n;	/* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
   1202 }
   1203 
   1204 void
   1205 ite_putstr(const u_char *s, int len, dev_t dev)
   1206 {
   1207 	struct ite_softc *ip;
   1208 	int i;
   1209 
   1210 	ip = getitesp(dev);
   1211 
   1212 	/* XXX avoid problems */
   1213 	if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
   1214 	  	return;
   1215 
   1216 	SUBR_CURSOR(ip, START_CURSOROPT);
   1217 	for (i = 0; i < len; i++)
   1218 		if (s[i])
   1219 			iteputchar(s[i], ip);
   1220 	SUBR_CURSOR(ip, END_CURSOROPT);
   1221 }
   1222 
   1223 void
   1224 iteputchar(int c, struct ite_softc *ip)
   1225 {
   1226 	int n, x, y;
   1227 	char *cp;
   1228 
   1229 	if (c >= 0x20 && ip->escape) {
   1230 		switch (ip->escape) {
   1231 
   1232 		case ESC:
   1233 			switch (c) {
   1234 				/* first 7bit equivalents for the 8bit control characters */
   1235 
   1236 			case 'D':
   1237 				c = IND;
   1238 				ip->escape = 0;
   1239 				break; /* and fall into the next switch below (same for all `break') */
   1240 
   1241 			case 'E':
   1242 				/* next line */
   1243 				c = NEL;
   1244 				ip->escape = 0;
   1245 				break;
   1246 
   1247 			case 'H':
   1248 				/* set TAB at current col */
   1249 				c = HTS;
   1250 				ip->escape = 0;
   1251 				break;
   1252 
   1253 			case 'M':
   1254 				/* reverse index */
   1255 				c = RI;
   1256 				ip->escape = 0;
   1257 				break;
   1258 
   1259 			case 'N':
   1260 				/* single shift G2 */
   1261 				c = SS2;
   1262 				ip->escape = 0;
   1263 				break;
   1264 
   1265 			case 'O':
   1266 				/* single shift G3 */
   1267 				c = SS3;
   1268 				ip->escape = 0;
   1269 				break;
   1270 
   1271 			case 'P':
   1272 				/* DCS detected */
   1273 				c = DCS;
   1274 				ip->escape = 0;
   1275 				break;
   1276 
   1277 			case '[':
   1278 				c = CSI;
   1279 				ip->escape = 0;
   1280 				break;
   1281 
   1282 			case '\\':
   1283 				/* String Terminator */
   1284 				c = ST;
   1285 				ip->escape = 0;
   1286 				break;
   1287 
   1288 			case ']':
   1289 				c = OSC;
   1290 				ip->escape = 0;
   1291 				break;
   1292 
   1293 			case '^':
   1294 				c = PM;
   1295 				ip->escape = 0;
   1296 				break;
   1297 
   1298 			case '_':
   1299 				c = APC;
   1300 				ip->escape = 0;
   1301 				break;
   1302 
   1303 
   1304 			/* introduces 7/8bit control */
   1305 			case ' ':
   1306 				/* can be followed by either F or G */
   1307 				ip->escape = ' ';
   1308 				break;
   1309 
   1310 
   1311 			/* a lot of character set selections, not yet used...
   1312 			   94-character sets: */
   1313 			case '(':	/* G0 */
   1314 			case ')':	/* G1 */
   1315 				ip->escape = c;
   1316 				return;
   1317 
   1318 			case '*':	/* G2 */
   1319 			case '+':	/* G3 */
   1320 			case 'B':	/* ASCII */
   1321 			case 'A':	/* ISO latin 1 */
   1322 			case '<':	/* user preferred suplemental */
   1323 			case '0':	/* dec special graphics */
   1324 
   1325 			/* 96-character sets: */
   1326 			case '-':	/* G1 */
   1327 			case '.':	/* G2 */
   1328 			case '/':	/* G3 */
   1329 
   1330 			/* national character sets: */
   1331 			case '4':	/* dutch */
   1332 			case '5':
   1333 			case 'C':	/* finnish */
   1334 			case 'R':	/* french */
   1335 			case 'Q':	/* french canadian */
   1336 			case 'K':	/* german */
   1337 			case 'Y':	/* italian */
   1338 			case '6':	/* norwegian/danish */
   1339 			/* note: %5 and %6 are not supported (two chars..) */
   1340 
   1341 				ip->escape = 0;
   1342 				/* just ignore for now */
   1343 				return;
   1344 
   1345 			/* 94-multibyte character sets designate */
   1346 			case '$':
   1347 				ip->escape = '$';
   1348 				return;
   1349 
   1350 			/* locking shift modes */
   1351 			case '`':
   1352 				ip->GR = &ip->G1;
   1353 				ip->escape = 0;
   1354 				return;
   1355 
   1356 			case 'n':
   1357 				ip->GL = &ip->G2;
   1358 				ip->escape = 0;
   1359 				return;
   1360 
   1361 			case '}':
   1362 				ip->GR = &ip->G2;
   1363 				ip->escape = 0;
   1364 				return;
   1365 
   1366 			case 'o':
   1367 				ip->GL = &ip->G3;
   1368 				ip->escape = 0;
   1369 				return;
   1370 
   1371 			case '|':
   1372 				ip->GR = &ip->G3;
   1373 				ip->escape = 0;
   1374 				return;
   1375 
   1376 			case '~':
   1377 				ip->GR = &ip->G1;
   1378 				ip->escape = 0;
   1379 				return;
   1380 
   1381 			/* font width/height control */
   1382 			case '#':
   1383 				ip->escape = '#';
   1384 				return;
   1385 
   1386 			case 'c':
   1387 				/* hard terminal reset .. */
   1388 				ite_reset(ip);
   1389 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1390 				ip->escape = 0;
   1391 				return;
   1392 
   1393 
   1394 			case '7':
   1395 				/* save cursor */
   1396 				ip->save_curx = ip->curx;
   1397 				ip->save_cury = ip->cury;
   1398 				ip->save_attribute = ip->attribute;
   1399 				ip->sc_om = ip->inside_margins;
   1400 				ip->sc_G0 = ip->G0;
   1401 				ip->sc_G1 = ip->G1;
   1402 				ip->sc_G2 = ip->G2;
   1403 				ip->sc_G3 = ip->G3;
   1404 				ip->sc_GL = ip->GL;
   1405 				ip->sc_GR = ip->GR;
   1406 				ip->escape = 0;
   1407 				return;
   1408 
   1409 			case '8':
   1410 				/* restore cursor */
   1411 				ip->curx = ip->save_curx;
   1412 				ip->cury = ip->save_cury;
   1413 				ip->attribute = ip->save_attribute;
   1414 				ip->inside_margins = ip->sc_om;
   1415 				ip->G0 = ip->sc_G0;
   1416 				ip->G1 = ip->sc_G1;
   1417 				ip->G2 = ip->sc_G2;
   1418 				ip->G3 = ip->sc_G3;
   1419 				ip->GL = ip->sc_GL;
   1420 				ip->GR = ip->sc_GR;
   1421 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1422 				ip->escape = 0;
   1423 				return;
   1424 
   1425 			case '=':
   1426 				/* keypad application mode */
   1427 				ip->keypad_appmode = 1;
   1428 				ip->escape = 0;
   1429 				return;
   1430 
   1431 			case '>':
   1432 				/* keypad numeric mode */
   1433 				ip->keypad_appmode = 0;
   1434 				ip->escape = 0;
   1435 				return;
   1436 
   1437 			case 'Z':	/* request ID */
   1438 				if (ip->emul_level == EMUL_VT100)
   1439 					ite_sendstr(ip, "\033[61;0c"); /* XXX not clean */
   1440 				else
   1441 					ite_sendstr(ip, "\033[63;0c"); /* XXX not clean */
   1442 				ip->escape = 0;
   1443 				return;
   1444 
   1445 			/* default catch all for not recognized ESC sequences */
   1446 			default:
   1447 				ip->escape = 0;
   1448 				return;
   1449 			}
   1450 			break;
   1451 
   1452 
   1453 		case '(': /* designate G0 */
   1454 			switch (c) {
   1455 			case 'B': /* USASCII */
   1456 				ip->G0 = CSET_ASCII;
   1457 				ip->escape = 0;
   1458 				return;
   1459 			case 'I':
   1460 				ip->G0 = CSET_JISKANA;
   1461 				ip->escape = 0;
   1462 				return;
   1463 			case 'J':
   1464 				ip->G0 = CSET_JISROMA;
   1465 				ip->escape = 0;
   1466 				return;
   1467 			case 'A': /* British or ISO-Latin-1 */
   1468 			case 'H': /* Swedish */
   1469 			case 'K': /* German */
   1470 			case 'R': /* French */
   1471 			case 'Y': /* Italian */
   1472 			case 'Z': /* Spanish */
   1473 			default:
   1474 				/* not supported */
   1475 				ip->escape = 0;
   1476 				return;
   1477 			}
   1478 
   1479 		case ')': /* designate G1 */
   1480 			ip->escape = 0;
   1481 			return;
   1482 
   1483 		case '$': /* 94-multibyte character set */
   1484 			switch (c) {
   1485 			case '@':
   1486 				ip->G0 = CSET_JIS1978;
   1487 				ip->escape = 0;
   1488 				return;
   1489 			case 'B':
   1490 				ip->G0 = CSET_JIS1983;
   1491 				ip->escape = 0;
   1492 				return;
   1493 			case 'D':
   1494 				ip->G0 = CSET_JIS1990;
   1495 				ip->escape = 0;
   1496 				return;
   1497 			default:
   1498 				/* not supported */
   1499 				ip->escape = 0;
   1500 				return;
   1501 			}
   1502 
   1503 		case ' ':
   1504 			switch (c) {
   1505 			case 'F':
   1506 				ip->eightbit_C1 = 0;
   1507 				ip->escape = 0;
   1508 				return;
   1509 
   1510 			case 'G':
   1511 				ip->eightbit_C1 = 1;
   1512 				ip->escape = 0;
   1513 				return;
   1514 
   1515 			default:
   1516 				/* not supported */
   1517 				ip->escape = 0;
   1518 				return;
   1519 			}
   1520 			break;
   1521 
   1522 		case '#':
   1523 			switch (c) {
   1524 			case '5':
   1525 				/* single height, single width */
   1526 				ip->escape = 0;
   1527 				return;
   1528 
   1529 			case '6':
   1530 				/* double width, single height */
   1531 				ip->escape = 0;
   1532 				return;
   1533 
   1534 			case '3':
   1535 				/* top half */
   1536 				ip->escape = 0;
   1537 				return;
   1538 
   1539 			case '4':
   1540 				/* bottom half */
   1541 				ip->escape = 0;
   1542 				return;
   1543 
   1544 			case '8':
   1545 				/* screen alignment pattern... */
   1546 				alignment_display(ip);
   1547 				ip->escape = 0;
   1548 				return;
   1549 
   1550 			default:
   1551 				ip->escape = 0;
   1552 				return;
   1553 			}
   1554 			break;
   1555 
   1556 
   1557 
   1558 		case CSI:
   1559 			/* the biggie... */
   1560 			switch (c) {
   1561 			case '0': case '1': case '2': case '3': case '4':
   1562 			case '5': case '6': case '7': case '8': case '9':
   1563 			case ';': case '\"': case '$': case '>':
   1564 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   1565 					*ip->ap++ = c;
   1566 				return;
   1567 
   1568 			case 'p':
   1569 				*ip->ap = 0;
   1570 				if (!strncmp(ip->argbuf, "61\"", 3))
   1571 					ip->emul_level = EMUL_VT100;
   1572 				else if (!strncmp(ip->argbuf, "63;1\"", 5)
   1573 					 || !strncmp(ip->argbuf, "62;1\"", 5))
   1574 					ip->emul_level = EMUL_VT300_7;
   1575 				else
   1576 					ip->emul_level = EMUL_VT300_8;
   1577 				ip->escape = 0;
   1578 				return;
   1579 
   1580 
   1581 			case '?':
   1582 				*ip->ap = 0;
   1583 				ip->escape = '?';
   1584 				ip->ap = ip->argbuf;
   1585 				return;
   1586 
   1587 
   1588 			case 'c':
   1589 				/* device attributes */
   1590 				*ip->ap = 0;
   1591 				if (ip->argbuf[0] == '>') {
   1592 					ite_sendstr(ip, "\033[>24;0;0;0c");
   1593 				} else
   1594 					switch (ite_zargnum(ip)) {
   1595 					case 0:
   1596 						/* primary DA request, send primary DA response */
   1597 						if (ip->emul_level == EMUL_VT100)
   1598 							ite_sendstr(ip, "\033[?1;1c");
   1599 						else
   1600 							ite_sendstr(ip, "\033[63;0c");
   1601 						break;
   1602 					}
   1603 				ip->escape = 0;
   1604 				return;
   1605 
   1606 			case 'n':
   1607 				switch (ite_zargnum(ip)) {
   1608 				case 5:
   1609 					ite_sendstr(ip, "\033[0n");	/* no malfunction */
   1610 					break;
   1611 				case 6:
   1612 					/* cursor position report */
   1613 					sprintf(ip->argbuf, "\033[%d;%dR",
   1614 						 ip->cury + 1, ip->curx + 1);
   1615 					ite_sendstr(ip, ip->argbuf);
   1616 					break;
   1617 				}
   1618 				ip->escape = 0;
   1619 				return;
   1620 
   1621 
   1622 			case 'x':
   1623 				switch (ite_zargnum(ip)) {
   1624 				case 0:
   1625 					/* Fake some terminal parameters.  */
   1626 					ite_sendstr(ip, "\033[2;1;1;112;112;1;0x");
   1627 					break;
   1628 				case 1:
   1629 					ite_sendstr(ip, "\033[3;1;1;112;112;1;0x");
   1630 					break;
   1631 				}
   1632 				ip->escape = 0;
   1633 				return;
   1634 
   1635 
   1636 			case 'g':
   1637 				/* clear tabs */
   1638 				switch (ite_zargnum(ip)) {
   1639 				case 0:
   1640 					if (ip->curx < ip->cols)
   1641 						ip->tabs[ip->curx] = 0;
   1642 					break;
   1643 				case 3:
   1644 					for (n = 0; n < ip->cols; n++)
   1645 						ip->tabs[n] = 0;
   1646 					break;
   1647 
   1648 				default:
   1649 					/* ignore */
   1650 					break;
   1651 				}
   1652 				ip->escape = 0;
   1653 				return;
   1654 
   1655 
   1656 			case 'h': /* set mode */
   1657 			case 'l': /* reset mode */
   1658 				n = ite_zargnum(ip);
   1659 				switch (n) {
   1660 				case 4:
   1661 					ip->imode = (c == 'h');	/* insert/replace mode */
   1662 					break;
   1663 				case 20:
   1664 					ip->linefeed_newline = (c == 'h');
   1665 					break;
   1666 				}
   1667 				ip->escape = 0;
   1668 				return;
   1669 
   1670 
   1671 			case 'M':
   1672 				/* delete line */
   1673 				ite_dnline(ip, ite_argnum(ip));
   1674 				ip->escape = 0;
   1675 				return;
   1676 
   1677 
   1678 			case 'L':
   1679 				/* insert line */
   1680 				ite_inline(ip, ite_argnum(ip));
   1681 				ip->escape = 0;
   1682 				return;
   1683 
   1684 
   1685 			case 'P':
   1686 				/* delete char */
   1687 				ite_dnchar(ip, ite_argnum(ip));
   1688 				ip->escape = 0;
   1689 				return;
   1690 
   1691 
   1692 			case '@':
   1693 				/* insert char(s) */
   1694 				ite_inchar(ip, ite_argnum(ip));
   1695 				ip->escape = 0;
   1696 				return;
   1697 
   1698 			case '!':
   1699 				/* soft terminal reset */
   1700 				ip->escape = 0; /* XXX */
   1701 				return;
   1702 
   1703 			case 'G':
   1704 				/* this one was *not* in my vt320 manual but in
   1705 				   a vt320 termcap entry.. who is right?
   1706 				   It's supposed to set the horizontal cursor position. */
   1707 				*ip->ap = 0;
   1708 				x = atoi(ip->argbuf);
   1709 				if (x)
   1710 					x--;
   1711 				ip->curx = min(x, ip->cols - 1);
   1712 				ip->escape = 0;
   1713 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1714 				clr_attr(ip, ATTR_INV);
   1715 				return;
   1716 
   1717 
   1718 			case 'd':
   1719 				/* same thing here, this one's for setting the absolute
   1720 				   vertical cursor position. Not documented... */
   1721 				*ip->ap = 0;
   1722 				y = atoi(ip->argbuf);
   1723 				if (y)
   1724 					y--;
   1725 				if (ip->inside_margins)
   1726 					y += ip->top_margin;
   1727 				ip->cury = min(y, ip->rows - 1);
   1728 				ip->escape = 0;
   1729 				snap_cury(ip);
   1730 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1731 				clr_attr(ip, ATTR_INV);
   1732 				return;
   1733 
   1734 
   1735 			case 'H':
   1736 			case 'f':
   1737 				*ip->ap = 0;
   1738 				y = atoi(ip->argbuf);
   1739 				x = 0;
   1740 				cp = strchr(ip->argbuf, ';');
   1741 				if (cp)
   1742 					x = atoi(cp + 1);
   1743 				if (x)
   1744 					x--;
   1745 				if (y)
   1746 					y--;
   1747 				if (ip->inside_margins)
   1748 					y += ip->top_margin;
   1749 				ip->cury = min(y, ip->rows - 1);
   1750 				ip->curx = min(x, ip->cols - 1);
   1751 				ip->escape = 0;
   1752 				snap_cury(ip);
   1753 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1754 				/*clr_attr(ip, ATTR_INV);*/
   1755 				return;
   1756 
   1757 			case 'A':
   1758 				/* cursor up */
   1759 				n = ite_argnum(ip);
   1760 				n = ip->cury - (n ? n : 1);
   1761 				if (n < 0)
   1762 					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