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