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