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