Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: ite.c,v 1.75 2026/11/23 05:31:08 isaki 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 - bitmapped 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.75 2026/11/23 05:31:08 isaki 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 #include <sys/device_impl.h>	/* XXX autoconf abuse */
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/kbio.h>
     71 #include <machine/bus.h>
     72 #include <machine/autoconf.h>
     73 #include <machine/grfioctl.h>
     74 #include <machine/iteioctl.h>
     75 
     76 #include <arch/x68k/dev/grfvar.h>
     77 #include <arch/x68k/dev/itevar.h>
     78 #include <arch/x68k/dev/kbdmap.h>
     79 #include <arch/x68k/dev/mfp.h>
     80 
     81 #include "ioconf.h"
     82 
     83 #if NBELL > 0
     84 void opm_bell(void);
     85 #endif
     86 
     87 #define SUBR_INIT(ip)		ip->isw->ite_init(ip)
     88 #define SUBR_DEINIT(ip)		ip->isw->ite_deinit(ip)
     89 #define SUBR_PUTC(ip,c,dy,dx,m)	ip->isw->ite_putc(ip,c,dy,dx,m)
     90 #define SUBR_CURSOR(ip,flg)	ip->isw->ite_cursor(ip,flg)
     91 #define SUBR_CLEAR(ip,sy,sx,h,w)	ip->isw->ite_clear(ip,sy,sx,h,w)
     92 #define SUBR_SCROLL(ip,sy,sx,count,dir)	\
     93     ip->isw->ite_scroll(ip,sy,sx,count,dir)
     94 #if defined(ITE_SIXEL)
     95 #define SUBR_SIXEL(ip,sy,sx)	ip->isw->ite_sixel(ip,sy,sx)
     96 #endif
     97 
     98 struct consdev;
     99 
    100 static inline void itesendch(int);
    101 static inline void alignment_display(struct ite_softc *);
    102 static inline void snap_cury(struct ite_softc *);
    103 static inline void ite_dnchar(struct ite_softc *, int);
    104 static void ite_inchar(struct ite_softc *,	int);
    105 static inline void ite_clrtoeol(struct ite_softc *);
    106 static inline void ite_clrtobol(struct ite_softc *);
    107 static inline void ite_clrline(struct ite_softc *);
    108 static inline void ite_clrtoeos(struct ite_softc *);
    109 static inline void ite_clrtobos(struct ite_softc *);
    110 static inline void ite_clrscreen(struct ite_softc *);
    111 static inline void ite_dnline(struct ite_softc *, int);
    112 static inline void ite_inline(struct ite_softc *, int);
    113 static inline void ite_index(struct ite_softc *);
    114 static inline void ite_lf(struct ite_softc *);
    115 static inline void ite_crlf(struct ite_softc *);
    116 static inline void ite_cr(struct ite_softc *);
    117 static inline void ite_rlf(struct ite_softc *);
    118 static inline void ite_sgr(struct ite_softc *);
    119 static void iteprecheckwrap(struct ite_softc *);
    120 static void itecheckwrap(struct ite_softc *);
    121 static int ite_argnum(struct ite_softc *);
    122 static int ite_zargnum(struct ite_softc *);
    123 static void ite_sendstr(struct ite_softc *, const char *);
    124 static inline int atoi(const char *);
    125 static struct ite_softc *getitesp(dev_t);
    126 
    127 static struct itesw itesw[] = {
    128 	{0,	tv_init,	tv_deinit,	0,
    129 	 0,	0,		0}
    130 };
    131 
    132 /*
    133  * # of chars are output in a single itestart() call.
    134  * If this is too big, user processes will be blocked out for
    135  * long periods of time while we are emptying the queue in itestart().
    136  * If it is too small, console output will be very ragged.
    137  */
    138 #define ITEBURST 64
    139 
    140 struct	tty *ite_tty[NITE];
    141 
    142 static struct ite_softc *kbd_ite = NULL;
    143 static struct ite_softc con_itesoftc;
    144 static struct device con_itedev;
    145 
    146 static struct tty *kbd_tty = NULL;
    147 
    148 static int start_repeat_timeo = 20;	/* /100: initial timeout till pressed
    149 						 key repeats */
    150 static int next_repeat_timeo  = 3;	/* /100: timeout when repeating for
    151 						 next char */
    152 
    153 static u_char cons_tabs[MAX_TABS];
    154 
    155 static void itestart(struct tty *);
    156 
    157 static void iteputchar(int, struct ite_softc *);
    158 #if defined(ITE_SIXEL)
    159 static int ite_dcs(const int, struct ite_softc *);
    160 #endif
    161 static void ite_putstr(const u_char *, int, dev_t);
    162 
    163 static int itematch(device_t, cfdata_t, void *);
    164 static void iteattach(device_t, device_t, void *);
    165 
    166 CFATTACH_DECL_NEW(ite, sizeof(struct ite_softc),
    167     itematch, iteattach, NULL, NULL);
    168 
    169 static dev_type_open(iteopen);
    170 static dev_type_close(iteclose);
    171 static dev_type_read(iteread);
    172 static dev_type_write(itewrite);
    173 static dev_type_ioctl(iteioctl);
    174 static dev_type_tty(itetty);
    175 static dev_type_poll(itepoll);
    176 
    177 const struct cdevsw ite_cdevsw = {
    178 	.d_open = iteopen,
    179 	.d_close = iteclose,
    180 	.d_read = iteread,
    181 	.d_write = itewrite,
    182 	.d_ioctl = iteioctl,
    183 	.d_stop = nostop,
    184 	.d_tty = itetty,
    185 	.d_poll = itepoll,
    186 	.d_mmap = nommap,
    187 	.d_kqfilter = ttykqfilter,
    188 	.d_discard = nodiscard,
    189 	.d_flag = D_TTY
    190 };
    191 
    192 static int
    193 itematch(device_t parent, cfdata_t cf, void *aux)
    194 {
    195 	struct grf_softc *gp;
    196 
    197 	gp = aux;
    198 	if (cf->cf_loc[GRFCF_GRFADDR] != gp->g_cfaddr)
    199 		return 0;
    200 
    201 	return 1;
    202 }
    203 
    204 /*
    205  * iteinit() is the standard entry point for initialization of
    206  * an ite device, it is also called from ite_cninit().
    207  */
    208 static void
    209 iteattach(device_t parent, device_t self, void *aux)
    210 {
    211 	struct ite_softc *ip;
    212 	struct grf_softc *gp;
    213 
    214 	gp = aux;
    215 	ip = device_private(self);
    216 	ip->device = self;
    217 	if(con_itesoftc.grf != NULL
    218 		/*&& con_itesoftc.grf->g_unit == gp->g_unit*/) {
    219 		/*
    220 		 * console reinit copy params over.
    221 		 * and console always gets keyboard
    222 		 */
    223 		memcpy(&ip->grf, &con_itesoftc.grf,
    224 		    (char *)&ip[1] - (char *)&ip->grf);
    225 		con_itesoftc.grf = NULL;
    226 		kbd_ite = ip;
    227 	}
    228 	ip->grf = gp;
    229 	iteinit(device_unit(self)); /* XXX */
    230 	aprint_normal(": rows %d cols %d", ip->rows, ip->cols);
    231 	if (kbd_ite == NULL)
    232 		kbd_ite = ip;
    233 	aprint_normal("\n");
    234 }
    235 
    236 static struct ite_softc *
    237 getitesp(dev_t dev)
    238 {
    239 
    240 	if (x68k_realconfig && con_itesoftc.grf == NULL)
    241 		return device_lookup_private(&ite_cd, UNIT(dev));
    242 
    243 	if (con_itesoftc.grf == NULL)
    244 		panic("no ite_softc for console");
    245 	return &con_itesoftc;
    246 }
    247 
    248 void
    249 iteinit(dev_t dev)
    250 {
    251 	struct ite_softc *ip;
    252 
    253 	ip = getitesp(dev);
    254 
    255 	if (ip->flags & ITE_INITED)
    256 		return;
    257 	memcpy(&kbdmap, &ascii_kbdmap, sizeof(struct kbdmap));
    258 
    259 	ip->curx = 0;
    260 	ip->cury = 0;
    261 	ip->cursorx = 0;
    262 	ip->cursory = 0;
    263 
    264 	ip->isw = &itesw[device_unit(ip->device)]; /* XXX */
    265 	SUBR_INIT(ip);
    266 	SUBR_CURSOR(ip, DRAW_CURSOR);
    267 	if (ip->tabs == NULL)
    268 		ip->tabs = malloc(MAX_TABS * sizeof(u_char),
    269 		    M_DEVBUF, M_WAITOK);
    270 	ite_reset(ip);
    271 	ip->flags |= ITE_INITED;
    272 }
    273 
    274 void
    275 ite_config_console(void)
    276 {
    277 	struct grf_softc *gp = &congrf;
    278 
    279 	if (con_itesoftc.grf != NULL)
    280 		return;
    281 	con_itesoftc.grf = gp;
    282 	con_itesoftc.tabs = cons_tabs;
    283 	con_itesoftc.device = &con_itedev;
    284 }
    285 
    286 /*
    287  * Perform functions necessary to setup device as a terminal emulator.
    288  */
    289 int
    290 iteon(dev_t dev, int flag)
    291 {
    292 	int unit = UNIT(dev);
    293 	struct ite_softc *ip;
    294 
    295 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
    296 	    (ip = getitesp(dev)) == NULL || (ip->flags & ITE_ALIVE) == 0)
    297 		return ENXIO;
    298 	/* force ite active, overriding graphics mode */
    299 	if ((flag & 1) != 0) {
    300 		ip->flags |= ITE_ACTIVE;
    301 		ip->flags &= ~(ITE_INGRF|ITE_INITED);
    302 	}
    303 	/* leave graphics mode */
    304 	if ((flag & 2) != 0) {
    305 		ip->flags &= ~ITE_INGRF;
    306 		if ((ip->flags & ITE_ACTIVE) == 0)
    307 			return 0;
    308 	}
    309 	ip->flags |= ITE_ACTIVE;
    310 	if ((ip->flags & ITE_INGRF) != 0)
    311 		return 0;
    312 	iteinit(dev);
    313 	if ((flag & 2) != 0)
    314 		ite_reset(ip);
    315 #if NKBD > 0
    316 	mfp_send_usart(0x49);	/* XXX */
    317 #endif
    318 	return 0;
    319 }
    320 
    321 /*
    322  * "Shut down" device as terminal emulator.
    323  * Note that we do not deinit the console device unless forced.
    324  * Deinit'ing the console every time leads to a very active
    325  * screen when processing /etc/rc.
    326  */
    327 void
    328 iteoff(dev_t dev, int flag)
    329 {
    330 	int unit = UNIT(dev);
    331 	struct ite_softc *ip;
    332 
    333 	/* XXX check whether when call from grf.c */
    334 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
    335 	    (ip = getitesp(dev)) == NULL || (ip->flags & ITE_ALIVE) == 0)
    336 		return;
    337 	if ((flag & 2) != 0)
    338 		ip->flags |= ITE_INGRF;
    339 
    340 	if ((ip->flags & ITE_ACTIVE) == 0)
    341 		return;
    342 	if ((flag & 1) != 0 ||
    343 	    (ip->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
    344 		SUBR_DEINIT(ip);
    345 
    346 	/*
    347 	 * XXX When the system is rebooted with "reboot", init(8)
    348 	 * kills the last process to have the console open.
    349 	 * If we don't revent the ITE_ACTIVE bit from being
    350 	 * cleared, we will never see messages printed during
    351 	 * the process of rebooting.
    352 	 */
    353 	if ((flag & 2) == 0 && (ip->flags & ITE_ISCONS) == 0) {
    354 		ip->flags &= ~ITE_ACTIVE;
    355 #if NKBD > 0
    356 		mfp_send_usart(0x48);	/* XXX */
    357 #endif
    358 	}
    359 }
    360 
    361 /*
    362  * standard entry points to the device.
    363  */
    364 
    365 /* ARGSUSED */
    366 static int
    367 iteopen(dev_t dev, int mode, int devtype, struct lwp *l)
    368 {
    369 	int unit = UNIT(dev);
    370 	struct tty *tp;
    371 	struct ite_softc *ip;
    372 	int error;
    373 	int first = 0;
    374 
    375 	if (unit >= ite_cd.cd_ndevs || (ip = getitesp(dev)) == NULL)
    376 		return ENXIO;
    377 	if (ite_tty[unit] == NULL) {
    378 		tp = ite_tty[unit] = tty_alloc();
    379 		tty_attach(tp);
    380 	} else
    381 		tp = ite_tty[unit];
    382 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    383 		return EBUSY;
    384 	if ((ip->flags & ITE_ACTIVE) == 0) {
    385 		error = iteon(dev, 0);
    386 		if (error)
    387 			return error;
    388 		first = 1;
    389 	}
    390 	tp->t_oproc = itestart;
    391 	tp->t_param = NULL;
    392 	tp->t_dev = dev;
    393 	if ((tp->t_state&TS_ISOPEN) == 0) {
    394 		ttychars(tp);
    395 		tp->t_iflag = TTYDEF_IFLAG;
    396 		tp->t_oflag = TTYDEF_OFLAG;
    397 		tp->t_cflag = TTYDEF_CFLAG;
    398 		tp->t_lflag = TTYDEF_LFLAG;
    399 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    400 		tp->t_state = TS_ISOPEN|TS_CARR_ON;
    401 		ttsetwater(tp);
    402 	}
    403 	error = (*tp->t_linesw->l_open)(dev, tp);
    404 	if (error == 0) {
    405 		tp->t_winsize.ws_row = ip->rows;
    406 		tp->t_winsize.ws_col = ip->cols;
    407 	} else if (first)
    408 		iteoff(dev, 0);
    409 	return error;
    410 }
    411 
    412 /*ARGSUSED*/
    413 static int
    414 iteclose(dev_t dev, int flag, int mode, struct lwp *l)
    415 {
    416 	struct tty *tp = ite_tty[UNIT(dev)];
    417 
    418 	(*tp->t_linesw->l_close)(tp, flag);
    419 	ttyclose(tp);
    420 	iteoff(dev, 0);
    421 #if 0
    422 	tty_free(tp);
    423 	ite_tty[UNIT(dev)] = NULL;
    424 #endif
    425 	return 0;
    426 }
    427 
    428 static int
    429 iteread(dev_t dev, struct uio *uio, int flag)
    430 {
    431 	struct tty *tp = ite_tty[UNIT(dev)];
    432 
    433 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    434 }
    435 
    436 static int
    437 itewrite(dev_t dev, struct uio *uio, int flag)
    438 {
    439 	struct tty *tp = ite_tty[UNIT(dev)];
    440 
    441 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    442 }
    443 
    444 static int
    445 itepoll(dev_t dev, int events, struct lwp *l)
    446 {
    447 	struct tty *tp = ite_tty[UNIT(dev)];
    448 
    449 	return (*tp->t_linesw->l_poll)(tp, events, l);
    450 }
    451 
    452 static struct tty *
    453 itetty(dev_t dev)
    454 {
    455 
    456 	return ite_tty[UNIT(dev)];
    457 }
    458 
    459 static int
    460 iteioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    461 {
    462 	struct iterepeat *irp;
    463 	struct tty *tp = ite_tty[UNIT(dev)];
    464 	int error;
    465 
    466 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, addr, flag, l);
    467 	if (error != EPASSTHROUGH)
    468 		return error;
    469 
    470 	error = ttioctl(tp, cmd, addr, flag, l);
    471 	if (error != EPASSTHROUGH)
    472 		return error;
    473 
    474 	switch (cmd) {
    475 	case ITEIOCSKMAP:
    476 		if (addr == NULL)
    477 			return EFAULT;
    478 		memcpy(&kbdmap, addr, sizeof(struct kbdmap));
    479 		return 0;
    480 
    481 	case ITEIOCGKMAP:
    482 		if (addr == NULL)
    483 			return EFAULT;
    484 		memcpy(addr, &kbdmap, sizeof(struct kbdmap));
    485 		return 0;
    486 
    487 	case ITEIOCGREPT:
    488 		irp = (struct iterepeat *)addr;
    489 		irp->start = start_repeat_timeo;
    490 		irp->next = next_repeat_timeo;
    491 
    492 	case ITEIOCSREPT:
    493 		irp = (struct iterepeat *)addr;
    494 		if (irp->start < ITEMINREPEAT && irp->next < ITEMINREPEAT)
    495 			return(EINVAL);
    496 		start_repeat_timeo = irp->start;
    497 		next_repeat_timeo = irp->next;
    498 #if x68k
    499 	case ITELOADFONT:
    500 		if (addr != NULL) {
    501 			memcpy(kern_font, addr, 4096 /*sizeof(kernel_font)*/);
    502 			ite_set_glyph();
    503 			return 0;
    504 		} else
    505 			return EFAULT;
    506 
    507 	case ITETVCTRL:
    508 		if (addr != NULL && *(uint8_t *)addr < 0x40) {
    509 			return mfp_send_usart(*(uint8_t *)addr);
    510 		} else {
    511 			return EFAULT;
    512 		}
    513 #endif
    514 	}
    515 	return EPASSTHROUGH;
    516 }
    517 
    518 static void
    519 itestart(struct tty *tp)
    520 {
    521 	struct clist *rbp;
    522 	uint8_t buf[ITEBURST];
    523 	int s, len;
    524 
    525 	getitesp(tp->t_dev);
    526 	/*
    527 	 * (Potentially) lower priority.  We only need to protect ourselves
    528 	 * from keyboard interrupts since that is all that can affect the
    529 	 * state of our tty (kernel printf doesn't go through this routine).
    530 	 */
    531 	s = spltty();
    532 	if ((tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) != 0)
    533 		goto out;
    534 	tp->t_state |= TS_BUSY;
    535 	rbp = &tp->t_outq;
    536 	len = q_to_b(rbp, buf, ITEBURST);
    537 	/*splx(s);*/
    538 
    539 	/* Here is a really good place to implement pre/jumpscroll() */
    540 	ite_putstr(buf, len, tp->t_dev);
    541 
    542 	/*s = spltty();*/
    543 	tp->t_state &= ~TS_BUSY;
    544 	/* we have characters remaining. */
    545 	if (ttypull(tp)) {
    546 		tp->t_state |= TS_TIMEOUT;
    547 		callout_schedule(&tp->t_rstrt_ch, 1);
    548 	}
    549  out:
    550 	splx(s);
    551 }
    552 
    553 /* XXX called after changes made in underlying grf layer. */
    554 /* I want to nuke this */
    555 void
    556 ite_reinit(dev_t dev)
    557 {
    558 	struct ite_softc *ip;
    559 	int unit = UNIT(dev);
    560 
    561 	/* XXX check whether when call from grf.c */
    562 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
    563 	    (ip = getitesp(dev)) == NULL)
    564 		return;
    565 
    566 	ip->flags &= ~ITE_INITED;
    567 	iteinit(dev);
    568 }
    569 
    570 void
    571 ite_reset(struct ite_softc *ip)
    572 {
    573 	int i;
    574 
    575 	ip->curx = 0;
    576 	ip->cury = 0;
    577 	ip->attribute = 0;
    578 	ip->save_curx = 0;
    579 	ip->save_cury = 0;
    580 	ip->save_attribute = 0;
    581 	ip->ap = ip->argbuf;
    582 	ip->emul_level = EMUL_VT300_8;
    583 	ip->eightbit_C1 = 0;
    584 	ip->top_margin = 0;
    585 	ip->bottom_margin = ip->rows - 1;
    586 	ip->inside_margins = 0; /* origin mode == absolute */
    587 	ip->linefeed_newline = 0;
    588 	ip->auto_wrap = 1;
    589 	ip->cursor_appmode = 0;
    590 	ip->keypad_appmode = 0;
    591 	ip->imode = 0;
    592 	ip->key_repeat = 1;
    593 	ip->G0 = CSET_ASCII;
    594 	ip->G1 = CSET_JIS1983;
    595 	ip->G2 = CSET_JISKANA;
    596 	ip->G3 = CSET_JIS1990;
    597 	ip->GL = &ip->G0;
    598 	ip->GR = &ip->G1;
    599 	ip->save_GL = 0;
    600 	ip->save_char = 0;
    601 	ip->fgcolor = 7;
    602 	ip->bgcolor = 0;
    603 	for (i = 0; i < ip->cols; i++)
    604 		ip->tabs[i] = ((i & 7) == 0);
    605 	/* XXX clear screen */
    606 	SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
    607 	attrclr(ip, 0, 0, ip->rows, ip->cols);
    608 }
    609 
    610 /* Used in console at startup only */
    611 int
    612 ite_cnfilter(u_char c)
    613 {
    614 	static u_char mod = 0;
    615 	struct key key;
    616 	uint8_t code, up, mask;
    617 	int s;
    618 
    619 	up = c & 0x80 ? 1 : 0;
    620 	c &= 0x7f;
    621 	code = 0;
    622 
    623 	s = spltty();
    624 
    625 	mask = 0;
    626 	if (c >= KBD_LEFT_ALT &&
    627 	    !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
    628 		switch (c) {
    629 		case KBD_LEFT_SHIFT:
    630 			mask = KBD_MOD_SHIFT;
    631 			break;
    632 
    633 		case KBD_LEFT_ALT:
    634 			mask = KBD_MOD_LALT;
    635 			break;
    636 
    637 		case KBD_RIGHT_ALT:
    638 			mask = KBD_MOD_RALT;
    639 			break;
    640 
    641 		case KBD_LEFT_META:
    642 			mask = KBD_MOD_LMETA;
    643 			break;
    644 
    645 		case KBD_RIGHT_META:
    646 			mask = KBD_MOD_RMETA;
    647 			break;
    648 
    649 		case KBD_CAPS_LOCK:
    650 			/*
    651 			 * capslock already behaves `right', don't need to
    652 			 * keep track of the state in here.
    653 			 */
    654 			mask = KBD_MOD_CAPS;
    655 			break;
    656 
    657 		case KBD_CTRL:
    658 			mask = KBD_MOD_CTRL;
    659 			break;
    660 
    661 		case KBD_RECONNECT:
    662 			/* ite got 0xff */
    663 			if (up)
    664 				kbd_setLED();
    665 			break;
    666 		}
    667 		if (mask & KBD_MOD_CAPS) {
    668 			if (!up) {
    669 				mod ^= KBD_MOD_CAPS;
    670 				kbdled ^= LED_CAPS_LOCK;
    671 				kbd_setLED();
    672 			}
    673 		} else if (up)
    674 			mod &= ~mask;
    675 		else
    676 			mod |= mask;
    677 		splx(s);
    678 		return -1;
    679 	}
    680 
    681 	if (up) {
    682 		splx(s);
    683 		return -1;
    684 	}
    685 
    686 	/* translate modifiers */
    687 	if ((mod & KBD_MOD_SHIFT) != 0) {
    688 		if ((mod & KBD_MOD_ALT) != 0)
    689 			key = kbdmap.alt_shift_keys[c];
    690 		else
    691 			key = kbdmap.shift_keys[c];
    692 	} else if ((mod & KBD_MOD_ALT) != 0)
    693 		key = kbdmap.alt_keys[c];
    694 	else {
    695 		key = kbdmap.keys[c];
    696 		/* if CAPS and key is CAPable (no pun intended) */
    697 		if ((mod & KBD_MOD_CAPS) != 0 &&
    698 		    (key.mode & KBD_MODE_CAPS) != 0)
    699 			key = kbdmap.shift_keys[c];
    700 	}
    701 	code = key.code;
    702 
    703 	/* if string return */
    704 	if ((key.mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) != 0) {
    705 		splx(s);
    706 		return -1;
    707 	}
    708 	/* handle dead keys */
    709 	if ((key.mode & KBD_MODE_DEAD) != 0) {
    710 		splx(s);
    711 		return -1;
    712 	}
    713 	if ((mod & KBD_MOD_CTRL) != 0)
    714 		code &= 0x1f;
    715 	if ((mod & KBD_MOD_META) != 0)
    716 		code |= 0x80;
    717 
    718 	/* do console mapping. */
    719 	code = code == '\r' ? '\n' : code;
    720 
    721 	splx(s);
    722 	return code;
    723 }
    724 
    725 /* And now the old stuff. */
    726 static inline void
    727 itesendch(int ch)
    728 {
    729 
    730 	(*kbd_tty->t_linesw->l_rint)(ch, kbd_tty);
    731 }
    732 
    733 void
    734 ite_filter(u_char c)
    735 {
    736 	static uint16_t mod = 0;
    737 	uint8_t code, *str;
    738 	uint16_t up, mask;
    739 	struct key key;
    740 	int s, i;
    741 
    742 	if (kbd_ite == NULL ||
    743 	    (kbd_tty = ite_tty[device_unit(kbd_ite->device)]) == NULL)
    744 		return;
    745 
    746 	/* have to make sure we're at spltty in here */
    747 	s = spltty();
    748 
    749 	up = c & 0x80 ? 1 : 0;
    750 	c &= 0x7f;
    751 	code = 0;
    752 
    753 	mask = 0;
    754 	if (c >= KBD_LEFT_ALT &&
    755 	    !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
    756 		switch (c) {
    757 		case KBD_LEFT_SHIFT:
    758 			mask = KBD_MOD_SHIFT;
    759 			break;
    760 
    761 		case KBD_LEFT_ALT:
    762 			mask = KBD_MOD_LALT;
    763 			break;
    764 
    765 		case KBD_RIGHT_ALT:
    766 			mask = KBD_MOD_RALT;
    767 			break;
    768 
    769 		case KBD_LEFT_META:
    770 			mask = KBD_MOD_LMETA;
    771 			break;
    772 
    773 		case KBD_RIGHT_META:
    774 			mask = KBD_MOD_RMETA;
    775 			break;
    776 
    777 		case KBD_CAPS_LOCK:
    778 			/*
    779 			 * capslock already behaves `right', don't need to keep
    780 			 * track of the state in here.
    781 			 */
    782 			mask = KBD_MOD_CAPS;
    783 			break;
    784 
    785 		case KBD_CTRL:
    786 			mask = KBD_MOD_CTRL;
    787 			break;
    788 
    789 		case KBD_OPT1:
    790 			mask = KBD_MOD_OPT1;
    791 			break;
    792 
    793 		case KBD_OPT2:
    794 			mask = KBD_MOD_OPT2;
    795 			break;
    796 
    797 		case KBD_RECONNECT:
    798 			if (up) { /* ite got 0xff */
    799 				kbd_setLED();
    800 			}
    801 			break;
    802 		}
    803 
    804 		if ((mask & KBD_MOD_CAPS) != 0) {
    805 			if (!up) {
    806 				mod ^= KBD_MOD_CAPS;
    807 				kbdled ^= LED_CAPS_LOCK;
    808 				kbd_setLED();
    809 			}
    810 		} else if (up) {
    811 			mod &= ~mask;
    812 		} else
    813 			mod |= mask;
    814 
    815 		/*
    816 		 * return even if it wasn't a modifier key, the other
    817 		 * codes up here are either special (like reset warning),
    818 		 * or not yet defined
    819 		 */
    820 		splx(s);
    821 		return;
    822 	}
    823 
    824 	if (up) {
    825 		splx(s);
    826 		return;
    827 	}
    828 
    829 	/*
    830 	 * intercept LAlt-LMeta-F1 here to switch back to original ascii-keymap.
    831 	 * this should probably be configurable..
    832 	 */
    833 	if (mod == (KBD_MOD_LALT|KBD_MOD_LMETA) && c == 0x63) {
    834 		memcpy(&kbdmap, &ascii_kbdmap, sizeof(struct kbdmap));
    835 		splx(s);
    836 		return;
    837 	}
    838 
    839 	/* translate modifiers */
    840 	if ((mod & KBD_MOD_SHIFT) != 0) {
    841 		if ((mod & KBD_MOD_ALT) != 0)
    842 			key = kbdmap.alt_shift_keys[c];
    843 		else
    844 			key = kbdmap.shift_keys[c];
    845 	} else if ((mod & KBD_MOD_ALT) != 0)
    846 		key = kbdmap.alt_keys[c];
    847 	else {
    848 		key = kbdmap.keys[c];
    849 		/* if CAPS and key is CAPable (no pun intended) */
    850 		if ((mod & KBD_MOD_CAPS) != 0 &&
    851 		    (key.mode & KBD_MODE_CAPS) != 0)
    852 			key = kbdmap.shift_keys[c];
    853 		else if ((mod & KBD_MOD_OPT2) != 0 &&
    854 		         (key.mode & KBD_MODE_KPAD) != 0)
    855 			key = kbdmap.shift_keys[c];
    856 	}
    857 	code = key.code;
    858 
    859 	/* handle dead keys */
    860 	if ((key.mode & KBD_MODE_DEAD) != 0) {
    861 		splx(s);
    862 		return;
    863 	}
    864 	/* if not string, apply META and CTRL modifiers */
    865 	if ((key.mode & KBD_MODE_STRING) == 0 &&
    866 	    ((key.mode & KBD_MODE_KPAD) == 0 ||
    867 	     (kbd_ite != NULL && kbd_ite->keypad_appmode == 0))) {
    868 		if ((mod & KBD_MOD_CTRL) != 0 &&
    869 		    (code == ' ' || (code >= '@' && code <= 'z')))
    870 			code &= 0x1f;
    871 		if ((mod & KBD_MOD_META) != 0)
    872 			code |= 0x80;
    873 	} else if ((key.mode & KBD_MODE_KPAD) != 0 &&
    874 	       (kbd_ite != NULL && kbd_ite->keypad_appmode != 0)) {
    875 		static const char * const in = "0123456789-+.\r()/*";
    876 		static const char * const out = "pqrstuvwxymlnMPQRS";
    877 		char *cp = strchr(in, code);
    878 
    879 		/*
    880 		 * keypad-appmode sends SS3 followed by the above
    881 		 * translated character
    882 		 */
    883 		(*kbd_tty->t_linesw->l_rint)(27, kbd_tty);
    884 		(*kbd_tty->t_linesw->l_rint)('O', kbd_tty);
    885 		(*kbd_tty->t_linesw->l_rint)(out[cp - in], kbd_tty);
    886 		splx(s);
    887 		return;
    888 	} else {
    889 		/* *NO* I don't like this.... */
    890 		static u_char app_cursor[] = {
    891 		  3, 27, 'O', 'A',
    892 		  3, 27, 'O', 'B',
    893 		  3, 27, 'O', 'C',
    894 		  3, 27, 'O', 'D'
    895 		};
    896 
    897 		str = kbdmap.strings + code;
    898 		/*
    899 		 * if this is a cursor key, AND it has the default
    900 		 * keymap setting, AND we're in app-cursor mode, switch
    901 		 * to the above table. This is *nasty* !
    902 		 */
    903 		if (c >= 0x3b && c <= 0x3e && kbd_ite->cursor_appmode != 0 &&
    904 		    memcmp(str, "\x03\x1b[", 3) == 0 &&
    905 		    strchr("ABCD", str[3]) != 0)
    906 			str = app_cursor + 4 * (str[3] - 'A');
    907 
    908 		/*
    909 		 * using a length-byte instead of 0-termination allows
    910 		 * to embed \0 into strings, although this is not used
    911 		 * in the default keymap
    912 		 */
    913 		for (i = *str++; i; i--)
    914 			(*kbd_tty->t_linesw->l_rint)(*str++, kbd_tty);
    915 		splx(s);
    916 		return;
    917 	}
    918 	(*kbd_tty->t_linesw->l_rint)(code, kbd_tty);
    919 
    920 	splx(s);
    921 	return;
    922 }
    923 
    924 /* helper functions, makes the code below more readable */
    925 static inline void
    926 ite_sendstr(struct ite_softc *ip, const char *str)
    927 {
    928 
    929 	while (*str != 0)
    930 		itesendch(*str++);
    931 }
    932 
    933 static inline void
    934 alignment_display(struct ite_softc *ip)
    935 {
    936 	int i, j;
    937 
    938 	for (j = 0; j < ip->rows; j++)
    939 		for (i = 0; i < ip->cols; i++)
    940 			SUBR_PUTC(ip, 'E', j, i, ATTR_NOR);
    941 	attrclr(ip, 0, 0, ip->rows, ip->cols);
    942 }
    943 
    944 static inline void
    945 snap_cury(struct ite_softc *ip)
    946 {
    947 
    948 	if (ip->inside_margins != 0) {
    949 		if (ip->cury < ip->top_margin)
    950 			ip->cury = ip->top_margin;
    951 		if (ip->cury > ip->bottom_margin)
    952 			ip->cury = ip->bottom_margin;
    953 	}
    954 }
    955 
    956 static inline void
    957 ite_dnchar(struct ite_softc *ip, int n)
    958 {
    959 
    960 	n = uimin(n, ip->cols - ip->curx);
    961 	if (n < ip->cols - ip->curx) {
    962 		SUBR_SCROLL(ip, ip->cury, ip->curx + n, n, SCROLL_LEFT);
    963 		attrmov(ip, ip->cury, ip->curx + n, ip->cury, ip->curx,
    964 		    1, ip->cols - ip->curx - n);
    965 		attrclr(ip, ip->cury, ip->cols - n, 1, n);
    966 	}
    967 	while (n-- > 0)
    968 		SUBR_PUTC(ip, ' ', ip->cury, ip->cols - n - 1, ATTR_NOR);
    969 }
    970 
    971 static void
    972 ite_inchar(struct ite_softc *ip, int n)
    973 {
    974 	int c = ip->save_char;
    975 
    976 	ip->save_char = 0;
    977 	n = uimin(n, ip->cols - ip->curx);
    978 	if (n < ip->cols - ip->curx) {
    979 		SUBR_SCROLL(ip, ip->cury, ip->curx, n, SCROLL_RIGHT);
    980 		attrmov(ip, ip->cury, ip->curx, ip->cury, ip->curx + n,
    981 		    1, ip->cols - ip->curx - n);
    982 		attrclr(ip, ip->cury, ip->curx, 1, n);
    983 	}
    984 	while (n-- != 0)
    985 		SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
    986 	ip->save_char = c;
    987 }
    988 
    989 static inline void
    990 ite_clrtoeol(struct ite_softc *ip)
    991 {
    992 	int y = ip->cury, x = ip->curx;
    993 
    994 	if (ip->cols - x > 0) {
    995 		SUBR_CLEAR(ip, y, x, 1, ip->cols - x);
    996 		attrclr(ip, y, x, 1, ip->cols - x);
    997 	}
    998 }
    999 
   1000 static inline void
   1001 ite_clrtobol(struct ite_softc *ip)
   1002 {
   1003 	int y = ip->cury, x = uimin(ip->curx + 1, ip->cols);
   1004 
   1005 	SUBR_CLEAR(ip, y, 0, 1, x);
   1006 	attrclr(ip, y, 0, 1, x);
   1007 }
   1008 
   1009 static inline void
   1010 ite_clrline(struct ite_softc *ip)
   1011 {
   1012 	int y = ip->cury;
   1013 
   1014 	SUBR_CLEAR(ip, y, 0, 1, ip->cols);
   1015 	attrclr(ip, y, 0, 1, ip->cols);
   1016 }
   1017 
   1018 static inline void
   1019 ite_clrtoeos(struct ite_softc *ip)
   1020 {
   1021 
   1022 	ite_clrtoeol(ip);
   1023 	if (ip->cury < ip->rows - 1) {
   1024 		SUBR_CLEAR(ip, ip->cury + 1, 0,
   1025 		    ip->rows - 1 - ip->cury, ip->cols);
   1026 		attrclr(ip, ip->cury, 0, ip->rows - ip->cury, ip->cols);
   1027 	}
   1028 }
   1029 
   1030 static inline void
   1031 ite_clrtobos(struct ite_softc *ip)
   1032 {
   1033 
   1034 	ite_clrtobol(ip);
   1035 	if (ip->cury > 0) {
   1036 		SUBR_CLEAR(ip, 0, 0, ip->cury, ip->cols);
   1037 		attrclr(ip, 0, 0, ip->cury, ip->cols);
   1038 	}
   1039 }
   1040 
   1041 static inline void
   1042 ite_clrscreen(struct ite_softc *ip)
   1043 {
   1044 
   1045 	SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
   1046 	attrclr(ip, 0, 0, ip->rows, ip->cols);
   1047 }
   1048 
   1049 static inline void
   1050 ite_dnline(struct ite_softc *ip, int n)
   1051 {
   1052 
   1053 	/*
   1054 	 * interesting.. if the cursor is outside the scrolling
   1055 	 * region, this command is simply ignored..
   1056 	 */
   1057 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
   1058 		return;
   1059 
   1060 	n = uimin(n, ip->bottom_margin + 1 - ip->cury);
   1061 	if (n <= ip->bottom_margin - ip->cury) {
   1062 		SUBR_SCROLL(ip, ip->cury + n, 0, n, SCROLL_UP);
   1063 		attrmov(ip, ip->cury + n, 0, ip->cury, 0,
   1064 		    ip->bottom_margin + 1 - ip->cury - n, ip->cols);
   1065 	}
   1066 	SUBR_CLEAR(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
   1067 	attrclr(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
   1068 }
   1069 
   1070 static inline void
   1071 ite_inline(struct ite_softc *ip, int n)
   1072 {
   1073 
   1074 	/*
   1075 	 * interesting.. if the cursor is outside the scrolling
   1076 	 * region, this command is simply ignored..
   1077 	 */
   1078 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
   1079 		return;
   1080 
   1081 	if (n <= 0)
   1082 		n = 1;
   1083 	else
   1084 		n = uimin(n, ip->bottom_margin + 1 - ip->cury);
   1085 	if (n <= ip->bottom_margin  - ip->cury) {
   1086 		SUBR_SCROLL(ip, ip->cury, 0, n, SCROLL_DOWN);
   1087 		attrmov(ip, ip->cury, 0, ip->cury + n, 0,
   1088 		    ip->bottom_margin + 1 - ip->cury - n, ip->cols);
   1089 	}
   1090 	SUBR_CLEAR(ip, ip->cury, 0, n, ip->cols);
   1091 	attrclr(ip, ip->cury, 0, n, ip->cols);
   1092 	ip->curx = 0;
   1093 }
   1094 
   1095 static inline void
   1096 ite_index(struct ite_softc *ip)
   1097 {
   1098 
   1099 	++ip->cury;
   1100 	if ((ip->cury == ip->bottom_margin+1) || (ip->cury == ip->rows)) {
   1101 		ip->cury--;
   1102 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   1103 		ite_clrline(ip);
   1104 	}
   1105 	/*clr_attr(ip, ATTR_INV);*/
   1106 }
   1107 
   1108 static inline void
   1109 ite_lf(struct ite_softc *ip)
   1110 {
   1111 
   1112 	++ip->cury;
   1113 	if (ip->cury > ip->bottom_margin) {
   1114 		ip->cury--;
   1115 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   1116 		ite_clrline(ip);
   1117 	}
   1118 	/*SUBR_CURSOR(ip, MOVE_CURSOR);*/
   1119 	/*clr_attr(ip, ATTR_INV);*/
   1120 	/* reset character set ... thanks for mohta. */
   1121 	ip->G0 = CSET_ASCII;
   1122 	ip->G1 = CSET_JIS1983;
   1123 	ip->G2 = CSET_JISKANA;
   1124 	ip->G3 = CSET_JIS1990;
   1125 	ip->GL = &ip->G0;
   1126 	ip->GR = &ip->G1;
   1127 	ip->save_GL = 0;
   1128 	ip->save_char = 0;
   1129 }
   1130 
   1131 static inline void
   1132 ite_crlf(struct ite_softc *ip)
   1133 {
   1134 
   1135 	ip->curx = 0;
   1136 	ite_lf(ip);
   1137 }
   1138 
   1139 static inline void
   1140 ite_cr(struct ite_softc *ip)
   1141 {
   1142 
   1143 	if (ip->curx != 0) {
   1144 		ip->curx = 0;
   1145 	}
   1146 }
   1147 
   1148 static inline void
   1149 ite_rlf(struct ite_softc *ip)
   1150 {
   1151 
   1152 	ip->cury--;
   1153 	if ((ip->cury < 0) || (ip->cury == ip->top_margin - 1)) {
   1154 		ip->cury++;
   1155 		SUBR_SCROLL(ip, ip->top_margin, 0, 1, SCROLL_DOWN);
   1156 		ite_clrline(ip);
   1157 	}
   1158 	clr_attr(ip, ATTR_INV);
   1159 }
   1160 
   1161 static inline int
   1162 atoi(const char *cp)
   1163 {
   1164 	int n;
   1165 
   1166 	for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
   1167 		n = n * 10 + *cp - '0';
   1168 	return n;
   1169 }
   1170 
   1171 static inline int
   1172 ite_argnum(struct ite_softc *ip)
   1173 {
   1174 	char ch;
   1175 	int n;
   1176 
   1177 	/* convert argument string into number */
   1178 	if (ip->ap == ip->argbuf)
   1179 		return 1;
   1180 	ch = *ip->ap;
   1181 	*ip->ap = 0;
   1182 	n = atoi(ip->argbuf);
   1183 	*ip->ap = ch;
   1184 
   1185 	return n;
   1186 }
   1187 
   1188 static inline int
   1189 ite_zargnum(struct ite_softc *ip)
   1190 {
   1191 	char ch;
   1192 	int n;
   1193 
   1194 	/* convert argument string into number */
   1195 	if (ip->ap == ip->argbuf)
   1196 		return 0;
   1197 	ch = *ip->ap;
   1198 	*ip->ap = 0;	/* terminate string */
   1199 	n = atoi(ip->argbuf);
   1200 	*ip->ap = ch;
   1201 
   1202 	return n;	/* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
   1203 }
   1204 
   1205 static void
   1206 ite_putstr(const u_char *s, int len, dev_t dev)
   1207 {
   1208 	struct ite_softc *ip;
   1209 	int i;
   1210 
   1211 	ip = getitesp(dev);
   1212 
   1213 	/* XXX avoid problems */
   1214 	if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
   1215 		return;
   1216 
   1217 #if defined(ITE_SIXEL)
   1218 	/* avoid flicking cursor */
   1219 	if (ip->escape == 0)
   1220 #endif
   1221 	SUBR_CURSOR(ip, START_CURSOROPT);
   1222 	for (i = 0; i < len; i++)
   1223 		if (s[i] != 0)
   1224 			iteputchar(s[i], ip);
   1225 #if defined(ITE_SIXEL)
   1226 	/* avoid flicking cursor */
   1227 	if (ip->escape == 0)
   1228 #endif
   1229 	SUBR_CURSOR(ip, END_CURSOROPT);
   1230 }
   1231 
   1232 static void
   1233 iteputchar(int c, struct ite_softc *ip)
   1234 {
   1235 	int n, x, y;
   1236 	char *cp;
   1237 
   1238 #if defined(ITE_SIXEL)
   1239 	if ((c >= 0x20 && ip->escape != 0) || ip->escape == DCS) {
   1240 #else
   1241 	if (c >= 0x20 && ip->escape != 0) {
   1242 #endif
   1243 		switch (ip->escape) {
   1244 
   1245 		case ESC:
   1246 			switch (c) {
   1247 			/*
   1248 			 * first 7bit equivalents for the 8bit control
   1249 			 * characters
   1250 			 */
   1251 
   1252 			case 'D':
   1253 				c = IND;
   1254 				ip->escape = 0;
   1255 				break;
   1256 				/*
   1257 				 * and fall into the next switch below
   1258 				 * (same for all `break')
   1259 				 */
   1260 
   1261 			case 'E':
   1262 				/* next line */
   1263 				c = NEL;
   1264 				ip->escape = 0;
   1265 				break;
   1266 
   1267 			case 'H':
   1268 				/* set TAB at current col */
   1269 				c = HTS;
   1270 				ip->escape = 0;
   1271 				break;
   1272 
   1273 			case 'M':
   1274 				/* reverse index */
   1275 				c = RI;
   1276 				ip->escape = 0;
   1277 				break;
   1278 
   1279 			case 'N':
   1280 				/* single shift G2 */
   1281 				c = SS2;
   1282 				ip->escape = 0;
   1283 				break;
   1284 
   1285 			case 'O':
   1286 				/* single shift G3 */
   1287 				c = SS3;
   1288 				ip->escape = 0;
   1289 				break;
   1290 
   1291 			case 'P':
   1292 				/* DCS detected */
   1293 				c = DCS;
   1294 				ip->escape = 0;
   1295 				break;
   1296 
   1297 			case '[':
   1298 				c = CSI;
   1299 				ip->escape = 0;
   1300 				break;
   1301 
   1302 			case '\\':
   1303 				/* String Terminator */
   1304 				c = ST;
   1305 				ip->escape = 0;
   1306 #if defined(ITE_SIXEL)
   1307 				if (ip->decsixel_y != 0) {
   1308 					ite_lf(ip);
   1309 				}
   1310 #endif
   1311 				break;
   1312 
   1313 			case ']':
   1314 				c = OSC;
   1315 				ip->escape = 0;
   1316 				break;
   1317 
   1318 			case '^':
   1319 				c = PM;
   1320 				ip->escape = 0;
   1321 				break;
   1322 
   1323 			case '_':
   1324 				c = APC;
   1325 				ip->escape = 0;
   1326 				break;
   1327 
   1328 			/* introduces 7/8bit control */
   1329 			case ' ':
   1330 				/* can be followed by either F or G */
   1331 				ip->escape = ' ';
   1332 				break;
   1333 
   1334 			/* a lot of character set selections, not yet used... */
   1335 			/* 94-character sets: */
   1336 			case '(':	/* G0 */
   1337 			case ')':	/* G1 */
   1338 				ip->escape = c;
   1339 				return;
   1340 
   1341 			case '*':	/* G2 */
   1342 			case '+':	/* G3 */
   1343 			case 'B':	/* ASCII */
   1344 			case 'A':	/* ISO latin 1 */
   1345 			case '<':	/* user preferred suplemental */
   1346 			case '0':	/* DEC special graphics */
   1347 
   1348 			/* 96-character sets: */
   1349 			case '-':	/* G1 */
   1350 			case '.':	/* G2 */
   1351 			case '/':	/* G3 */
   1352 
   1353 			/* national character sets: */
   1354 			case '4':	/* dutch */
   1355 			case '5':
   1356 			case 'C':	/* finnish */
   1357 			case 'R':	/* french */
   1358 			case 'Q':	/* french canadian */
   1359 			case 'K':	/* german */
   1360 			case 'Y':	/* italian */
   1361 			case '6':	/* norwegian/danish */
   1362 			/* note: %5 and %6 are not supported (two chars..) */
   1363 
   1364 				ip->escape = 0;
   1365 				/* just ignore for now */
   1366 				return;
   1367 
   1368 			/* 94-multibyte character sets designate */
   1369 			case '$':
   1370 				ip->escape = '$';
   1371 				return;
   1372 
   1373 			/* locking shift modes */
   1374 			case '`':
   1375 				ip->GR = &ip->G1;
   1376 				ip->escape = 0;
   1377 				return;
   1378 
   1379 			case 'n':
   1380 				ip->GL = &ip->G2;
   1381 				ip->escape = 0;
   1382 				return;
   1383 
   1384 			case '}':
   1385 				ip->GR = &ip->G2;
   1386 				ip->escape = 0;
   1387 				return;
   1388 
   1389 			case 'o':
   1390 				ip->GL = &ip->G3;
   1391 				ip->escape = 0;
   1392 				return;
   1393 
   1394 			case '|':
   1395 				ip->GR = &ip->G3;
   1396 				ip->escape = 0;
   1397 				return;
   1398 
   1399 			case '~':
   1400 				ip->GR = &ip->G1;
   1401 				ip->escape = 0;
   1402 				return;
   1403 
   1404 			/* font width/height control */
   1405 			case '#':
   1406 				ip->escape = '#';
   1407 				return;
   1408 
   1409 			case 'c':
   1410 				/* hard terminal reset .. */
   1411 				ite_reset(ip);
   1412 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1413 				ip->escape = 0;
   1414 				return;
   1415 
   1416 			case '7':
   1417 				/* save cursor */
   1418 				ip->save_curx = ip->curx;
   1419 				ip->save_cury = ip->cury;
   1420 				ip->save_attribute = ip->attribute;
   1421 				ip->sc_om = ip->inside_margins;
   1422 				ip->sc_G0 = ip->G0;
   1423 				ip->sc_G1 = ip->G1;
   1424 				ip->sc_G2 = ip->G2;
   1425 				ip->sc_G3 = ip->G3;
   1426 				ip->sc_GL = ip->GL;
   1427 				ip->sc_GR = ip->GR;
   1428 				ip->escape = 0;
   1429 				return;
   1430 
   1431 			case '8':
   1432 				/* restore cursor */
   1433 				ip->curx = ip->save_curx;
   1434 				ip->cury = ip->save_cury;
   1435 				ip->attribute = ip->save_attribute;
   1436 				ip->inside_margins = ip->sc_om;
   1437 				ip->G0 = ip->sc_G0;
   1438 				ip->G1 = ip->sc_G1;
   1439 				ip->G2 = ip->sc_G2;
   1440 				ip->G3 = ip->sc_G3;
   1441 				ip->GL = ip->sc_GL;
   1442 				ip->GR = ip->sc_GR;
   1443 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1444 				ip->escape = 0;
   1445 				return;
   1446 
   1447 			case '=':
   1448 				/* keypad application mode */
   1449 				ip->keypad_appmode = 1;
   1450 				ip->escape = 0;
   1451 				return;
   1452 
   1453 			case '>':
   1454 				/* keypad numeric mode */
   1455 				ip->keypad_appmode = 0;
   1456 				ip->escape = 0;
   1457 				return;
   1458 
   1459 			case 'Z':	/* request ID */
   1460 				if (ip->emul_level == EMUL_VT100) {
   1461 					/* XXX not clean */
   1462 					ite_sendstr(ip, "\033[61;0c");
   1463 				} else {
   1464 					/* XXX not clean */
   1465 					ite_sendstr(ip, "\033[63;0c");
   1466 				}
   1467 				ip->escape = 0;
   1468 				return;
   1469 
   1470 			/* default catch all for not recognized ESC sequences */
   1471 			default:
   1472 				ip->escape = 0;
   1473 				return;
   1474 			}
   1475 			break;
   1476 
   1477 		case '(': /* designate G0 */
   1478 			switch (c) {
   1479 			case 'B': /* USASCII */
   1480 				ip->G0 = CSET_ASCII;
   1481 				ip->escape = 0;
   1482 				return;
   1483 			case 'I':
   1484 				ip->G0 = CSET_JISKANA;
   1485 				ip->escape = 0;
   1486 				return;
   1487 			case 'J':
   1488 				ip->G0 = CSET_JISROMA;
   1489 				ip->escape = 0;
   1490 				return;
   1491 			case '0': /* DEC special graphics */
   1492 				ip->G0 = CSET_DECGRAPH;
   1493 				ip->escape = 0;
   1494 				return;
   1495 			case 'A': /* British or ISO-Latin-1 */
   1496 			case 'H': /* Swedish */
   1497 			case 'K': /* German */
   1498 			case 'R': /* French */
   1499 			case 'Y': /* Italian */
   1500 			case 'Z': /* Spanish */
   1501 			default:
   1502 				/* not supported */
   1503 				ip->escape = 0;
   1504 				return;
   1505 			}
   1506 
   1507 		case ')': /* designate G1 */
   1508 			ip->escape = 0;
   1509 			return;
   1510 
   1511 		case '$': /* 94-multibyte character set */
   1512 			switch (c) {
   1513 			case '@':
   1514 				ip->G0 = CSET_JIS1978;
   1515 				ip->escape = 0;
   1516 				return;
   1517 			case 'B':
   1518 				ip->G0 = CSET_JIS1983;
   1519 				ip->escape = 0;
   1520 				return;
   1521 			case 'D':
   1522 				ip->G0 = CSET_JIS1990;
   1523 				ip->escape = 0;
   1524 				return;
   1525 			default:
   1526 				/* not supported */
   1527 				ip->escape = 0;
   1528 				return;
   1529 			}
   1530 
   1531 		case ' ':
   1532 			switch (c) {
   1533 			case 'F':
   1534 				ip->eightbit_C1 = 0;
   1535 				ip->escape = 0;
   1536 				return;
   1537 
   1538 			case 'G':
   1539 				ip->eightbit_C1 = 1;
   1540 				ip->escape = 0;
   1541 				return;
   1542 
   1543 			default:
   1544 				/* not supported */
   1545 				ip->escape = 0;
   1546 				return;
   1547 			}
   1548 			break;
   1549 
   1550 		case '#':
   1551 			switch (c) {
   1552 			case '5':
   1553 				/* single height, single width */
   1554 				ip->escape = 0;
   1555 				return;
   1556 
   1557 			case '6':
   1558 				/* double width, single height */
   1559 				ip->escape = 0;
   1560 				return;
   1561 
   1562 			case '3':
   1563 				/* top half */
   1564 				ip->escape = 0;
   1565 				return;
   1566 
   1567 			case '4':
   1568 				/* bottom half */
   1569 				ip->escape = 0;
   1570 				return;
   1571 
   1572 			case '8':
   1573 				/* screen alignment pattern... */
   1574 				alignment_display(ip);
   1575 				ip->escape = 0;
   1576 				return;
   1577 
   1578 			default:
   1579 				ip->escape = 0;
   1580 				return;
   1581 			}
   1582 			break;
   1583 
   1584 		case CSI:
   1585 			/* the biggie... */
   1586 			switch (c) {
   1587 			case '0':
   1588 			case '1':
   1589 			case '2':
   1590 			case '3':
   1591 			case '4':
   1592 			case '5':
   1593 			case '6':
   1594 			case '7':
   1595 			case '8':
   1596 			case '9':
   1597 			case ';':
   1598 			case '\"':
   1599 			case '$':
   1600 			case '>':
   1601 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   1602 					*ip->ap++ = c;
   1603 				return;
   1604 
   1605 			case 'p':
   1606 				*ip->ap = 0;
   1607 				if (!strncmp(ip->argbuf, "61\"", 3))
   1608 					ip->emul_level = EMUL_VT100;
   1609 				else if (!strncmp(ip->argbuf, "63;1\"", 5) ||
   1610 					 !strncmp(ip->argbuf, "62;1\"", 5))
   1611 					ip->emul_level = EMUL_VT300_7;
   1612 				else
   1613 					ip->emul_level = EMUL_VT300_8;
   1614 				ip->escape = 0;
   1615 				return;
   1616 
   1617 			case '?':
   1618 				*ip->ap = 0;
   1619 				ip->escape = '?';
   1620 				ip->ap = ip->argbuf;
   1621 				return;
   1622 
   1623 			case 'c':
   1624 				/* device attributes */
   1625 				*ip->ap = 0;
   1626 				if (ip->argbuf[0] == '>') {
   1627 					ite_sendstr(ip, "\033[>24;0;0;0c");
   1628 				} else {
   1629 					switch (ite_zargnum(ip)) {
   1630 					case 0:
   1631 						/*
   1632 						 * primary DA request, send
   1633 						 * primary DA response
   1634 						 */
   1635 						if (ip->emul_level ==
   1636 						    EMUL_VT100)
   1637 							ite_sendstr(ip,
   1638 							    "\033[?1;1c");
   1639 						else
   1640 #if defined(ITE_SIXEL)
   1641 							ite_sendstr(ip,
   1642 							    "\033[63;4c");
   1643 #else
   1644 							ite_sendstr(ip,
   1645 							    "\033[63;0c");
   1646 #endif
   1647 						break;
   1648 					}
   1649 				}
   1650 				ip->escape = 0;
   1651 				return;
   1652 
   1653 			case 'n':
   1654 				switch (ite_zargnum(ip)) {
   1655 				case 5:
   1656 					/* no malfunction */
   1657 					ite_sendstr(ip, "\033[0n");
   1658 					break;
   1659 				case 6:
   1660 					/* cursor position report */
   1661 					snprintf(ip->argbuf, sizeof(ip->argbuf),
   1662 					    "\033[%d;%dR",
   1663 					     ip->cury + 1, ip->curx + 1);
   1664 					ite_sendstr(ip, ip->argbuf);
   1665 					break;
   1666 				}
   1667 				ip->escape = 0;
   1668 				return;
   1669 
   1670 			case 'x':
   1671 				switch (ite_zargnum(ip)) {
   1672 				case 0:
   1673 					/* Fake some terminal parameters.  */
   1674 					ite_sendstr(ip,
   1675 					    "\033[2;1;1;112;112;1;0x");
   1676 					break;
   1677 				case 1:
   1678 					ite_sendstr(ip,
   1679 					    "\033[3;1;1;112;112;1;0x");
   1680 					break;
   1681 				}
   1682 				ip->escape = 0;
   1683 				return;
   1684 
   1685 			case 'g':
   1686 				/* clear tabs */
   1687 				switch (ite_zargnum(ip)) {
   1688 				case 0:
   1689 					if (ip->curx < ip->cols)
   1690 						ip->tabs[ip->curx] = 0;
   1691 					break;
   1692 				case 3:
   1693 					for (n = 0; n < ip->cols; n++)
   1694 						ip->tabs[n] = 0;
   1695 					break;
   1696 
   1697 				default:
   1698 					/* ignore */
   1699 					break;
   1700 				}
   1701 				ip->escape = 0;
   1702 				return;
   1703 
   1704 			case 'h': /* set mode */
   1705 			case 'l': /* reset mode */
   1706 				n = ite_zargnum(ip);
   1707 				switch (n) {
   1708 				case 4:
   1709 					/* insert/replace mode */
   1710 					ip->imode = (c == 'h');
   1711 					break;
   1712 				case 20:
   1713 					ip->linefeed_newline = (c == 'h');
   1714 					break;
   1715 				}
   1716 				ip->escape = 0;
   1717 				return;
   1718 
   1719 			case 'M':
   1720 				/* delete line */
   1721 				ite_dnline(ip, ite_argnum(ip));
   1722 				ip->escape = 0;
   1723 				return;
   1724 
   1725 			case 'L':
   1726 				/* insert line */
   1727 				ite_inline(ip, ite_argnum(ip));
   1728 				ip->escape = 0;
   1729 				return;
   1730 
   1731 			case 'P':
   1732 				/* delete char */
   1733 				ite_dnchar(ip, ite_argnum(ip));
   1734 				ip->escape = 0;
   1735 				return;
   1736 
   1737 			case '@':
   1738 				/* insert char(s) */
   1739 				ite_inchar(ip, ite_argnum(ip));
   1740 				ip->escape = 0;
   1741 				return;
   1742 
   1743 			case '!':
   1744 				/* soft terminal reset */
   1745 				ip->escape = 0; /* XXX */
   1746 				return;
   1747 
   1748 			case 'G':
   1749 				/*
   1750 				 * this one was *not* in my vt320 manual but
   1751 				 * in a vt320 termcap entry.. who is right?
   1752 				 * It's supposed to set the horizontal cursor
   1753 				 * position.
   1754 				 */
   1755 				*ip->ap = 0;
   1756 				x = atoi(ip->argbuf);
   1757 				if (x != 0)
   1758 					x--;
   1759 				ip->curx = uimin(x, ip->cols - 1);
   1760 				ip->escape = 0;
   1761 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1762 				clr_attr(ip, ATTR_INV);
   1763 				return;
   1764 
   1765 			case 'd':
   1766 				/*
   1767 				 * same thing here, this one's for setting
   1768 				 * the absolute vertical cursor position.
   1769 				 * Not documented...
   1770 				 */
   1771 				*ip->ap = 0;
   1772 				y = atoi(ip->argbuf);
   1773 				if (y)
   1774 					y--;
   1775 				if (ip->inside_margins)
   1776 					y += ip->top_margin;
   1777 				ip->cury = uimin(y, ip->rows - 1);
   1778 				ip->escape = 0;
   1779 				snap_cury(ip);
   1780 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1781 				clr_attr(ip, ATTR_INV);
   1782 				return;
   1783 
   1784 			case 'H':
   1785 			case 'f':
   1786 				*ip->ap = 0;
   1787 				y = atoi(ip->argbuf);
   1788 				x = 0;
   1789 				cp = strchr(ip->argbuf, ';');
   1790 				if (cp != NULL)
   1791 					x = atoi(cp + 1);
   1792 				if (x != 0)
   1793 					x--;
   1794 				if (y != 0)
   1795 					y--;
   1796 				if (ip->inside_margins != 0)
   1797 					y += ip->top_margin;
   1798 				ip->cury = uimin(y, ip->rows - 1);
   1799 				ip->curx = uimin(x, ip->cols - 1);
   1800 				ip->escape = 0;
   1801 				snap_cury(ip);
   1802 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1803 				/*clr_attr(ip, ATTR_INV);*/
   1804 				return;
   1805 
   1806 			case 'A':
   1807 				/* cursor up */
   1808 				n = ite_argnum(ip);
   1809 				n = ip->cury - (n ? n : 1);
   1810 				if (n < 0)
   1811 					n = 0;
   1812 				if (ip->inside_margins)
   1813 					n = uimax(ip->top_margin, n);
   1814 				else if (n == ip->top_margin - 1)
   1815 					/*
   1816 					 * allow scrolling outside region,
   1817 					 * but don't scroll out of active
   1818 					 * region without explicit CUP
   1819 					 */
   1820 					n = ip->top_margin;
   1821 				ip->cury = n;
   1822 				ip->escape = 0;
   1823 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1824 				clr_attr(ip, ATTR_INV);
   1825 				return;
   1826 
   1827 			case 'B':
   1828 				/* cursor down */
   1829 				n = ite_argnum(ip);
   1830 				n = ip->cury + (n ? n : 1);
   1831 				n = uimin(ip->rows - 1, n);
   1832 #if 0
   1833 				if (ip->inside_margins)
   1834 #endif
   1835 					n = uimin(ip->bottom_margin, n);
   1836 #if 0
   1837 				else if (n == ip->bottom_margin + 1)
   1838 					/*
   1839 					 * allow scrolling outside region,
   1840 					 * but don't scroll out of active
   1841 					 * region without explicit CUP
   1842 					 */
   1843 					n = ip->bottom_margin;
   1844 #endif
   1845 				ip->cury = n;
   1846 				ip->escape = 0;
   1847 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1848 				clr_attr(ip, ATTR_INV);
   1849 				return;
   1850 
   1851 			case 'C':
   1852 				/* cursor forward */
   1853 				n = ite_argnum(ip);
   1854 				n = n ? n : 1;
   1855 				ip->curx = uimin(ip->curx + n, ip->cols - 1);
   1856 				ip->escape = 0;
   1857 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1858 				clr_attr(ip, ATTR_INV);
   1859 				return;
   1860 
   1861 			case 'D':
   1862 				/* cursor backward */
   1863 				n = ite_argnum(ip);
   1864 				n = n ? n : 1;
   1865 				n = ip->curx - n;
   1866 				ip->curx = n >= 0 ? n : 0;
   1867 				ip->escape = 0;
   1868 				SUBR_CURSOR(ip, MOVE_CURSOR);
   1869 				clr_attr(ip, ATTR_INV);
   1870 				return;
   1871 
   1872 			case 'J':
   1873 				/* erase screen */
   1874 				*ip->ap = 0;
   1875 				n = ite_zargnum(ip);
   1876 				if (n == 0)
   1877 					ite_clrtoeos(ip);
   1878 				else if (n == 1)
   1879 					ite_clrtobos(ip);
   1880 				else if (n == 2)
   1881 					ite_clrscreen(ip);
   1882 				ip->escape = 0;
   1883 				return;
   1884 
   1885 			case 'K':
   1886 				/* erase line */
   1887 				n = ite_zargnum(ip);
   1888 				if (n == 0)
   1889 					ite_clrtoeol(ip);
   1890 				else if (n == 1)
   1891 					ite_clrtobol(ip);
   1892 				else if (n == 2)
   1893 					ite_clrline(ip);
   1894 				ip->escape = 0;
   1895 				return;
   1896 
   1897 			case 'S':
   1898 				/* scroll up */
   1899 				n = ite_zargnum(ip);
   1900 				if (n <= 0)
   1901 					n = 1;
   1902 				else if (n > ip->rows-1)
   1903 					n = ip->rows-1;
   1904 				SUBR_SCROLL(ip, ip->rows-1, 0, n, SCROLL_UP);
   1905 				ip->escape = 0;
   1906 				return;
   1907 
   1908 			case 'T':
   1909 				/* scroll down */
   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, 0, 0, n, SCROLL_DOWN);
   1916 				ip->escape = 0;
   1917 				return;
   1918 
   1919 			case 'X':
   1920 				/* erase character */
   1921 				n = ite_argnum(ip) - 1;
   1922 				n = uimin(n, ip->cols - 1 - ip->curx);
   1923 				for (; n >= 0; n--) {
   1924 					attrclr(ip, ip->cury, ip->curx + n,
   1925 					    1, 1);
   1926 					SUBR_PUTC(ip, ' ',
   1927 					    ip->cury, ip->curx + n, ATTR_NOR);
   1928 				}
   1929 				ip->escape = 0;
   1930 				return;
   1931 
   1932 			case '}':
   1933 			case '`':
   1934 				/* status line control */
   1935 				ip->escape = 0;
   1936 				return;
   1937 
   1938 			case 'r':
   1939 				/* set scrolling region */
   1940 				ip->escape = 0;
   1941 				*ip->ap = 0;
   1942 				x = atoi(ip->argbuf);
   1943 				x = x ? x : 1;
   1944 				y = ip->rows;
   1945 				cp = strchr(ip->argbuf, ';');
   1946 				if (cp) {
   1947 					y = atoi(cp + 1);
   1948 					y = y ? y : ip->rows;
   1949 				}
   1950 				if (y <= x)
   1951 					return;
   1952 				x--;
   1953 				y--;
   1954 				ip->top_margin = uimin(x, ip->rows - 2);
   1955 				ip->bottom_margin = uimin(y, ip->rows - 1);
   1956 				if (ip->inside_margins) {
   1957 					ip->cury = ip->top_margin;
   1958 				} else
   1959 					ip->cury = 0;
   1960 				ip->curx = 0;
   1961 				return;
   1962 
   1963 			case 'm':
   1964 				*ip->ap = 0;
   1965 				ite_sgr(ip);
   1966 				ip->escape = 0;
   1967 				return;
   1968 
   1969 			case 'u':
   1970 				/* DECRQTSR */
   1971 				ite_sendstr(ip, "\033P\033\\");
   1972 				ip->escape = 0;
   1973 				return;
   1974 
   1975 			default:
   1976 				ip->escape = 0;
   1977 				return;
   1978 			}
   1979 			break;
   1980 
   1981 		case '?':	/* CSI ? */
   1982 			switch (c) {
   1983 			case '0':
   1984 			case '1':
   1985 			case '2':
   1986 			case '3':
   1987 			case '4':
   1988 			case '5':
   1989 			case '6':
   1990 			case '7':
   1991 			case '8':
   1992 			case '9':
   1993 			case ';':
   1994 			case '\"':
   1995 			case '$':
   1996 				/*
   1997 				 * Don't fill the last character; it's needed.
   1998 				 */
   1999 				/* XXX yeah, where ?? */
   2000 				if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
   2001 					*ip->ap++ = c;
   2002 				return;
   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 					else if (!strncmp(ip->argbuf, "25", 2))
   2012 						/* udk status */
   2013 						ite_sendstr(ip, "\033[20n");
   2014 					else if (!strncmp(ip->argbuf, "26", 2))
   2015 						/* keyboard dialect: US */
   2016 						ite_sendstr(ip, "\033[27;1n");
   2017 				}
   2018 				ip->escape = 0;
   2019 				return;
   2020 
   2021 			case 'h': /* set dec private modes */
   2022 			case 'l': /* reset dec private modes */
   2023 				n = ite_zargnum(ip);
   2024 				switch (n) {
   2025 				case 1:
   2026 					/* CKM - cursor key mode */
   2027 					ip->cursor_appmode = (c == 'h');
   2028 					break;
   2029 
   2030 				case 3:
   2031 					/* 132/80 columns (132 == 'h') */
   2032 					break;
   2033 
   2034 				case 4: /* smooth scroll */
   2035 					break;
   2036 
   2037 				case 5:
   2038 					/*
   2039 					 * light background (=='h') /
   2040 					 * dark background (=='l')
   2041 					 */
   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 ?
   2049 					    ip->top_margin : 0;
   2050 					SUBR_CURSOR(ip, MOVE_CURSOR);
   2051 #endif
   2052 					break;
   2053 
   2054 				case 7: /* auto wraparound */
   2055 					ip->auto_wrap = (c == 'h');
   2056 					break;
   2057 
   2058 				case 8: /* keyboard repeat */
   2059 					ip->key_repeat = (c == 'h');
   2060 					break;
   2061 
   2062 				case 20: /* newline mode */
   2063 					ip->linefeed_newline = (c == 'h');
   2064 					break;
   2065 
   2066 				case 25: /* cursor on/off */
   2067 					SUBR_CURSOR(ip, (c == 'h') ?
   2068 					    DRAW_CURSOR : ERASE_CURSOR);
   2069 					break;
   2070 				}
   2071 				ip->escape = 0;
   2072 				return;
   2073 
   2074 			case 'K':
   2075 				/* selective erase in line */
   2076 			case 'J':
   2077 				/* selective erase in display */
   2078 
   2079 			default:
   2080 				ip->escape = 0;
   2081 				return;
   2082 			}
   2083 			break;
   2084 
   2085 #if defined(ITE_SIXEL)
   2086 		case DCS:
   2087 			if (ite_dcs(c, ip) == 0) {
   2088 				return;
   2089 			}
   2090 			break;
   2091 #endif /* ITE_SIXEL */
   2092 
   2093 		default:
   2094 			ip->escape = 0;
   2095 			return;
   2096 		}
   2097 	}
   2098 
   2099 	switch (c) {
   2100 	case 0x00:	/* NUL */
   2101 	case 0x01:	/* SOH */
   2102 	case 0x02:	/* STX */
   2103 	case 0x03:	/* ETX */
   2104 	case 0x04:	/* EOT */
   2105 	case 0x05:	/* ENQ */
   2106 	case 0x06:	/* ACK */
   2107 		break;
   2108 
   2109 	case BEL:
   2110 #if NBELL > 0
   2111 		if (kbd_ite && ite_tty[device_unit(kbd_ite->device)])
   2112 			opm_bell();
   2113 #endif
   2114 		break;
   2115 
   2116 	case BS:
   2117 		if (--ip->curx < 0)
   2118 			ip->curx = 0;
   2119 		else
   2120 			SUBR_CURSOR(ip, MOVE_CURSOR);
   2121 		break;
   2122 
   2123 	case HT:
   2124 		for (n = ip->curx + 1; n < ip->cols; n++) {
   2125 			if (ip->tabs[n]) {
   2126 				ip->curx = n;
   2127 				SUBR_CURSOR(ip, MOVE_CURSOR);
   2128 				break;
   2129 			}
   2130 		}
   2131 		break;
   2132 
   2133 	case VT:	/* VT is treated like LF */
   2134 	case FF:	/* so is FF */
   2135 	case LF:
   2136 		/*
   2137 		 * cr->crlf distinction is done here, on output,
   2138 		 * not on input!
   2139 		 */
   2140 		if (ip->linefeed_newline)
   2141 			ite_crlf(ip);
   2142 		else
   2143 			ite_lf(ip);
   2144 		break;
   2145 
   2146 	case CR:
   2147 		ite_cr(ip);
   2148 		break;
   2149 
   2150 	case SO:
   2151 		ip->GL = &ip->G1;
   2152 		break;
   2153 
   2154 	case SI:
   2155 		ip->GL = &ip->G0;
   2156 		break;
   2157 
   2158 	case 0x10:	/* DLE */
   2159 	case 0x11:	/* DC1/XON */
   2160 	case 0x12:	/* DC2 */
   2161 	case 0x13:	/* DC3/XOFF */
   2162 	case 0x14:	/* DC4 */
   2163 	case 0x15:	/* NAK */
   2164 	case 0x16:	/* SYN */
   2165 	case 0x17:	/* ETB */
   2166 		break;
   2167 
   2168 	case CAN:
   2169 		ip->escape = 0;	/* cancel any escape sequence in progress */
   2170 		break;
   2171 
   2172 	case 0x19:	/* EM */
   2173 		break;
   2174 
   2175 	case SUB:
   2176 		ip->escape = 0;	/* dito, but see below */
   2177 		/* should also display a reverse question mark!! */
   2178 		break;
   2179 
   2180 	case ESC:
   2181 		ip->escape = ESC;
   2182 		break;
   2183 
   2184 	case 0x1c:	/* FS */
   2185 	case 0x1d:	/* GS */
   2186 	case 0x1e:	/* RS */
   2187 	case 0x1f:	/* US */
   2188 		break;
   2189 
   2190 	/* now it gets weird.. 8bit control sequences.. */
   2191 	case IND:	/* index: move cursor down, scroll */
   2192 		ite_index(ip);
   2193 		break;
   2194 
   2195 	case NEL:	/* next line. next line, first pos. */
   2196 		ite_crlf(ip);
   2197 		break;
   2198 
   2199 	case HTS:	/* set horizontal tab */
   2200 		if (ip->curx < ip->cols)
   2201 			ip->tabs[ip->curx] = 1;
   2202 		break;
   2203 
   2204 	case RI:	/* reverse index */
   2205 		ite_rlf(ip);
   2206 		break;
   2207 
   2208 	case SS2:	/* go into G2 for one character */
   2209 		ip->save_GL = ip->GR;	/* GL XXX EUC */
   2210 		ip->GR = &ip->G2;	/* GL XXX */
   2211 		break;
   2212 
   2213 	case SS3:	/* go into G3 for one character */
   2214 		ip->save_GL = ip->GR;	/* GL XXX EUC */
   2215 		ip->GR = &ip->G3;	/* GL XXX */
   2216 		break;
   2217 
   2218 	case DCS:	/* device control string introducer */
   2219 		ip->escape = DCS;
   2220 #if defined(ITE_SIXEL)
   2221 		ip->dcs_cmd = DCS_START;
   2222 #endif
   2223 		ip->ap = ip->argbuf;
   2224 		break;
   2225 
   2226 	case CSI:	/* control sequence introducer */
   2227 		ip->escape = CSI;
   2228 		ip->ap = ip->argbuf;
   2229 		break;
   2230 
   2231 	case ST:	/* string terminator */
   2232 		/* ignore, if not used as terminator */
   2233 		break;
   2234 
   2235 	case OSC:	/* introduces OS command. */
   2236 		/* Ignore everything upto ST */
   2237 		ip->escape = OSC;
   2238 		break;
   2239 
   2240 	case PM:	/* privacy message */
   2241 		/* ignore everything upto ST */
   2242 		ip->escape = PM;
   2243 		break;
   2244 
   2245 	case APC:	/* application program command */
   2246 		/* ignore everything upto ST */
   2247 		ip->escape = APC;
   2248 		break;
   2249 
   2250 	case DEL:
   2251 		break;
   2252 
   2253 	default:
   2254 		if (ip->save_char == 0 &&
   2255 		    (*((c & 0x80) ? ip->GR : ip->GL) & CSET_MULTI) != 0) {
   2256 			ip->save_char = c;
   2257 			break;
   2258 		}
   2259 		if (ip->imode != 0)
   2260 			ite_inchar(ip, ip->save_char ? 2 : 1);
   2261 		iteprecheckwrap(ip);
   2262 #ifdef DO_WEIRD_ATTRIBUTES
   2263 		if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
   2264 			attrset(ip, ATTR_INV);
   2265 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
   2266 		}
   2267 		else
   2268 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
   2269 #else
   2270 		SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
   2271 #endif
   2272 		/*SUBR_CURSOR(ip, DRAW_CURSOR);*/
   2273 		itecheckwrap(ip);
   2274 		if (ip->save_char) {
   2275 			itecheckwrap(ip);
   2276 			ip->save_char = 0;
   2277 		}
   2278 		if (ip->save_GL) {
   2279 			/*
   2280 			 * reset single shift
   2281 			 */
   2282 			ip->GR = ip->save_GL;
   2283 			ip->save_GL = 0;
   2284 		}
   2285 		break;
   2286 	}
   2287 }
   2288 
   2289 /* SGR (CSI <n> m) */
   2290 static inline void
   2291 ite_sgr(struct ite_softc *ip)
   2292 {
   2293 	int n;
   2294 	char *p;
   2295 
   2296 	/* kludge to make CSIm work (== CSI0m) */
   2297 	for (p = ip->argbuf; ;) {
   2298 		n = 0;
   2299 		/* atoi() but keep the end position */
   2300 		for (; *p && '0' <= *p && *p <= '9'; p++) {
   2301 			if (n < 100)
   2302 				n = n * 10 + (*p - '0');
   2303 		}
   2304 		switch (n) {
   2305 		case 0:
   2306 			clr_attr(ip, ATTR_ALL);
   2307 			ip->fgcolor = 7;
   2308 			ip->bgcolor = 0;
   2309 			break;
   2310 		case 1:
   2311 			set_attr(ip, ATTR_BOLD);
   2312 			break;
   2313 		case 4:
   2314 			set_attr(ip, ATTR_UL);
   2315 			break;
   2316 		case 5:
   2317 			set_attr(ip, ATTR_BLINK);
   2318 			break;
   2319 		case 7:
   2320 			set_attr(ip, ATTR_INV);
   2321 			break;
   2322 		case 22:
   2323 			clr_attr(ip, ATTR_BOLD);
   2324 			break;
   2325 		case 24:
   2326 			clr_attr(ip, ATTR_UL);
   2327 			break;
   2328 		case 25:
   2329 			clr_attr(ip, ATTR_BLINK);
   2330 			break;
   2331 		case 27:
   2332 			clr_attr(ip, ATTR_INV);
   2333 			break;
   2334 		case 30:
   2335 		case 31:
   2336 		case 32:
   2337 		case 33:
   2338 		case 34:
   2339 		case 35:
   2340 		case 36:
   2341 		case 37:
   2342 			/* foreground colors */
   2343 			ip->fgcolor = n - 30;
   2344 			break;
   2345 		case 40:
   2346 		case 41:
   2347 		case 42:
   2348 		case 43:
   2349 		case 44:
   2350 		case 45:
   2351 		case 46:
   2352 		case 47:
   2353 			/* background colors */
   2354 			ip->bgcolor = n - 40;
   2355 			break;
   2356 		default:
   2357 			/* ignore unknown sequence */
   2358 			break;
   2359 		}
   2360 		if (*p++ != ';')
   2361 			break;
   2362 	}
   2363 }
   2364 
   2365 #if defined(ITE_SIXEL)
   2366 /*
   2367  * Handle DCS.
   2368  * 0:  return in the caller.
   2369  * !0: break the switch-case in the caller.
   2370  */
   2371 static int
   2372 ite_dcs(const int c, struct ite_softc *ip)
   2373 {
   2374 	static const uint32_t table[64] = {
   2375 		0x000000, 0x000001, 0x000010, 0x000011,
   2376 		0x000100, 0x000101, 0x000110, 0x000111,
   2377 		0x001000, 0x001001, 0x001010, 0x001011,
   2378 		0x001100, 0x001101, 0x001110, 0x001111,
   2379 		0x010000, 0x010001, 0x010010, 0x010011,
   2380 		0x010100, 0x010101, 0x010110, 0x010111,
   2381 		0x011000, 0x011001, 0x011010, 0x011011,
   2382 		0x011100, 0x011101, 0x011110, 0x011111,
   2383 		0x100000, 0x100001, 0x100010, 0x100011,
   2384 		0x100100, 0x100101, 0x100110, 0x100111,
   2385 		0x101000, 0x101001, 0x101010, 0x101011,
   2386 		0x101100, 0x101101, 0x101110, 0x101111,
   2387 		0x110000, 0x110001, 0x110010, 0x110011,
   2388 		0x110100, 0x110101, 0x110110, 0x110111,
   2389 		0x111000, 0x111001, 0x111010, 0x111011,
   2390 		0x111100, 0x111101, 0x111110, 0x111111,
   2391 	};
   2392 
   2393 	switch (ip->dcs_cmd) {
   2394 	case DCS_DISCARD:
   2395 		/* discard sixel cause kernel message interrupted */
   2396 		switch (c) {
   2397 		case '-':
   2398 			/* restart from next SIXEL line */
   2399 			ite_lf(ip);
   2400 			goto sixel_restart;
   2401 
   2402 		case CAN:
   2403 		case SUB:
   2404 			/* SUB should also display a reverse question mark... */
   2405 			ip->escape = 0;
   2406 			return 0;
   2407 
   2408 		case ESC:
   2409 			ip->escape = ESC;
   2410 			return 0;
   2411 
   2412 		default:
   2413 			return 0;
   2414 		}
   2415 		break;
   2416 
   2417 	case DCS_START:
   2418 		/* the biggie... */
   2419 		switch (c) {
   2420 		case '0':
   2421 		case '1':
   2422 		case '2':
   2423 		case '3':
   2424 		case '4':
   2425 		case '5':
   2426 		case '6':
   2427 		case '7':
   2428 		case '8':
   2429 		case '9':
   2430 		case ';':
   2431 		case '$':
   2432 			if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   2433 				*ip->ap++ = c;
   2434 			return 0;
   2435 
   2436 		case 'q':
   2437 		{
   2438 			char *cp;
   2439 
   2440 			/* init sixel */
   2441 			/*
   2442 			 * DCS <P1> ; <P2> ; <P3> q
   2443 			 * P1 is aspect ratio, XXX not supported.
   2444 			 * P2 is bgcolor mode.
   2445 			 *  0..2: bgcolor mode, XXX not supported here.
   2446 			 *  bit2 means 'OR'ed color mode.
   2447 			 *  This is an original extension.
   2448 			 */
   2449 			ip->ap = ip->argbuf;
   2450 			cp = strchr(ip->ap, ';');
   2451 			if (cp != NULL) {
   2452 				int mode;
   2453 				mode = atoi(cp + 1) - '0';
   2454 				ip->decsixel_ormode = (mode & 4);
   2455 			} else {
   2456 				ip->decsixel_ormode = 0;
   2457 			}
   2458 sixel_restart:
   2459 			ip->dcs_cmd = DCS_SIXEL;
   2460 			ip->decsixel_state = DECSIXEL_INIT;
   2461 			ip->decsixel_ph = MAX_SIXEL_WIDTH;
   2462 			ip->decsixel_x = 0;
   2463 			ip->decsixel_y = 0;
   2464 			ip->decsixel_repcount = 0;
   2465 			ip->decsixel_color = ip->fgcolor;
   2466 			memset(ip->decsixel_buf, 0, sizeof(ip->decsixel_buf));
   2467 			return 0;
   2468 		}
   2469 
   2470 		case CAN:
   2471 		case SUB:
   2472 			/* SUB should also display a reverse question mark... */
   2473 			ip->escape = 0;
   2474 			return 0;
   2475 
   2476 		case ESC:
   2477 			ip->escape = ESC;
   2478 			return 0;
   2479 
   2480 		default:
   2481 			return 0;
   2482 		}
   2483 		break;
   2484 
   2485 	case DCS_SIXEL:
   2486 sixel_loop:
   2487 		switch (ip->decsixel_state) {
   2488 		case DECSIXEL_INIT:
   2489 			switch (c) {
   2490 			case CAN:
   2491 			case SUB:
   2492 				/*
   2493 				 * SUB should also display a reverse question
   2494 				 * mark...
   2495 				 */
   2496 				ip->escape = 0;
   2497 				return 0;
   2498 
   2499 			case ESC:
   2500 				ip->escape = ESC;
   2501 				return 0;
   2502 
   2503 			case DECSIXEL_REPEAT:
   2504 				ip->decsixel_state = c;
   2505 				ip->decsixel_repcount = 0;
   2506 				return 0;
   2507 
   2508 			case DECSIXEL_RASTER:
   2509 			case DECSIXEL_COLOR:
   2510 				ip->decsixel_state = c;
   2511 				ip->ap = ip->argbuf;
   2512 				return 0;
   2513 
   2514 			case '$':	/* CR */
   2515 				ip->decsixel_x = 0;
   2516 				return 0;
   2517 
   2518 			case '-':	/* LF */
   2519 				/*
   2520 				 * XXX
   2521 				 * FONTHEIGHT is defined in ite_tv.c, not here..
   2522 				 */
   2523 				if (ip->decsixel_y + 6 > 15) {
   2524 					ite_lf(ip);
   2525 					ip->decsixel_y -= 16;
   2526 				}
   2527 				SUBR_SIXEL(ip, ip->cury, ip->curx);
   2528 				memset(ip->decsixel_buf, 0,
   2529 				    sizeof(ip->decsixel_buf));
   2530 				ip->decsixel_x = 0;
   2531 				ip->decsixel_y += 6;
   2532 				return 0;
   2533 
   2534 			default:
   2535 				if ('?' <= c && c <= '~'
   2536 				    && ip->decsixel_x < MAX_SIXEL_WIDTH) {
   2537 					uint32_t d;
   2538 					d = table[c - '?'] * ip->decsixel_color;
   2539 					ip->decsixel_buf[ip->decsixel_x] |= d;
   2540 					ip->decsixel_x++;
   2541 				} else {
   2542 					/* ignore */
   2543 				}
   2544 				return 0;
   2545 			}
   2546 			break;
   2547 
   2548 		case DECSIXEL_REPEAT:
   2549 			if ('0' <= c && c <= '9') {
   2550 				ip->decsixel_repcount =
   2551 				    ip->decsixel_repcount * 10 + (c - '0');
   2552 			} else if ('?' <= c && c <= '~') {
   2553 				uint32_t d;
   2554 				int i;
   2555 				int cnt = MIN(ip->decsixel_repcount,
   2556 				    MAX_SIXEL_WIDTH - ip->decsixel_x);
   2557 				d = table[c - '?'] * ip->decsixel_color;
   2558 				for (i = 0; i < cnt; i++) {
   2559 					ip->decsixel_buf[ip->decsixel_x + i] |=
   2560 					    d;
   2561 				}
   2562 				ip->decsixel_x += cnt;
   2563 				ip->decsixel_state = DECSIXEL_INIT;
   2564 			} else {
   2565 				/* invalid ? */
   2566 				ip->decsixel_state = DECSIXEL_INIT;
   2567 			}
   2568 			return 0;
   2569 
   2570 		case DECSIXEL_RASTER:
   2571 		case DECSIXEL_RASTER_PAD:
   2572 		case DECSIXEL_RASTER_PH:
   2573 		case DECSIXEL_RASTER_PV:
   2574 			switch (c) {
   2575 			case '0':
   2576 			case '1':
   2577 			case '2':
   2578 			case '3':
   2579 			case '4':
   2580 			case '5':
   2581 			case '6':
   2582 			case '7':
   2583 			case '8':
   2584 			case '9':
   2585 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   2586 					*ip->ap++ = c;
   2587 				return 0;
   2588 
   2589 			case ';':
   2590 			default:
   2591 				switch (ip->decsixel_state) {
   2592 				case DECSIXEL_RASTER:
   2593 					/* ignore PAN */
   2594 					ip->ap = ip->argbuf;
   2595 					ip->decsixel_state =
   2596 					    DECSIXEL_RASTER_PAD;
   2597 					return 0;
   2598 
   2599 				case DECSIXEL_RASTER_PAD:
   2600 					/* ignore PAD */
   2601 					ip->ap = ip->argbuf;
   2602 					ip->decsixel_state = DECSIXEL_RASTER_PH;
   2603 					return 0;
   2604 
   2605 				case DECSIXEL_RASTER_PH:
   2606 					ip->decsixel_ph = ite_zargnum(ip);
   2607 					ip->ap = ip->argbuf;
   2608 					ip->decsixel_state = DECSIXEL_RASTER_PV;
   2609 					return 0;
   2610 
   2611 				case DECSIXEL_RASTER_PV:
   2612 					/* ignore PV */
   2613 					ip->decsixel_state = DECSIXEL_INIT;
   2614 					/* c is a next sequence char. */
   2615 					goto sixel_loop;
   2616 
   2617 				default:
   2618 					/* NOTREACHED */
   2619 					return 0;
   2620 				}
   2621 			}
   2622 			return 0;
   2623 
   2624 		case DECSIXEL_COLOR:
   2625 			switch (c) {
   2626 			case '0':
   2627 			case '1':
   2628 			case '2':
   2629 			case '3':
   2630 			case '4':
   2631 			case '5':
   2632 			case '6':
   2633 			case '7':
   2634 			case '8':
   2635 			case '9':
   2636 			case ';':
   2637 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
   2638 					*ip->ap++ = c;
   2639 				return 0;
   2640 
   2641 			default:
   2642 				*ip->ap = '\0';
   2643 				if (strchr(ip->argbuf, ';')) {
   2644 					/* ignore the palette definition. */
   2645 				} else {
   2646 					/* otherwise, it specifies color. */
   2647 					ip->decsixel_color =
   2648 					    ite_zargnum(ip) & 7;
   2649 				}
   2650 				ip->decsixel_state = DECSIXEL_INIT;
   2651 				ip->ap = ip->argbuf;
   2652 				/* c is a next sequence char. */
   2653 				goto sixel_loop;
   2654 			}
   2655 			return 0;
   2656 		}
   2657 		break;
   2658 	}
   2659 
   2660 	/* Continue in caller's switch-case. */
   2661 	return 1;
   2662 }
   2663 #endif /* ITE_SIXEL */
   2664 
   2665 static void
   2666 iteprecheckwrap(struct ite_softc *ip)
   2667 {
   2668 
   2669 	if (ip->auto_wrap && ip->curx + (ip->save_char ? 1 : 0) == ip->cols) {
   2670 		ip->curx = 0;
   2671 		clr_attr(ip, ATTR_INV);
   2672 		if (++ip->cury >= ip->bottom_margin + 1) {
   2673 			ip->cury = ip->bottom_margin;
   2674 			/*SUBR_CURSOR(ip, MOVE_CURSOR);*/
   2675 			SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
   2676 			ite_clrtoeol(ip);
   2677 		} /*else
   2678 			SUBR_CURSOR(ip, MOVE_CURSOR);*/
   2679 	}
   2680 }
   2681 
   2682 static void
   2683 itecheckwrap(struct ite_softc *ip)
   2684 {
   2685 
   2686 #if 0
   2687 	if (++ip->curx == ip->cols) {
   2688 		if (ip->auto_wrap) {
   2689 			ip->curx = 0;
   2690 			clr_attr(ip, ATTR_INV);
   2691 			if (++ip->cury >= ip->bottom_margin + 1) {
   2692 				ip->cury = ip->bottom_margin;
   2693 				SUBR_CURSOR(ip, MOVE_CURSOR);
   2694 				SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1,
   2695 				    SCROLL_UP);
   2696 				ite_clrtoeol(ip);
   2697 				return;
   2698 			}
   2699 		} else
   2700 			/* stay there if no autowrap.. */
   2701 			ip->curx--;
   2702 	}
   2703 #else
   2704 	if (ip->curx < ip->cols) {
   2705 		ip->curx++;
   2706 		/*SUBR_CURSOR(ip, MOVE_CURSOR);*/
   2707 	}
   2708 #endif
   2709 }
   2710 
   2711 /*
   2712  * A convertion table from DEC special graphics characters to ASCII characters.
   2713  * Mostly for box drawing on sysinst(8).
   2714  */
   2715 const uint8_t ite_decgraph2ascii[128] = {
   2716 	/* same as ASCII from 0x00 to 0x5e */
   2717 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
   2718 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
   2719 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   2720 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
   2721 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
   2722 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
   2723 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
   2724 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
   2725 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
   2726 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
   2727 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
   2728 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e,
   2729 
   2730 	/* special graphics characters from 0x5f to 0x7e */
   2731 	' ',	/* 0x5f NBSP */
   2732 	'*',	/* 0x60 diamond */
   2733 	' ',	/* 0x61 medium shade */
   2734 	' ',	/* 0x62 HT */
   2735 	' ',	/* 0x63 FF */
   2736 	' ',	/* 0x64 CR */
   2737 	' ',	/* 0x65 LF */
   2738 	' ',	/* 0x66 degree symbol */
   2739 	' ',	/* 0x67 plus-minus sign */
   2740 	' ',	/* 0x68 NL */
   2741 	' ',	/* 0x69 VT */
   2742 	'+',	/* 0x6a box drawings up left */
   2743 	'+',	/* 0x6b box drawings down left */
   2744 	'+',	/* 0x6c box drawings down right */
   2745 	'+',	/* 0x6d box drawings up right */
   2746 	'+',	/* 0x6e box drawings vertical horizontal */
   2747 	'~',	/* 0x6f scan line 1 */
   2748 	'-',	/* 0x70 scan line 3 */
   2749 	'-',	/* 0x71 scan line 5 */
   2750 	'-',	/* 0x72 scan line 7 */
   2751 	'_',	/* 0x73 scan line 9 */
   2752 	'+',	/* 0x74 box drawings vertical right */
   2753 	'+',	/* 0x75 box drawings vertical left */
   2754 	'+',	/* 0x76 box drawings horizontal up */
   2755 	'+',	/* 0x77 box drawings horizontal down */
   2756 	'|',	/* 0x78 box drawings vertical */
   2757 	'<',	/* 0x79 less than or equal to */
   2758 	'>',	/* 0x7a greater than or equal to */
   2759 	' ',	/* 0x7b pi */
   2760 	' ',	/* 0x7c not equal */
   2761 	' ',	/* 0x7d pound sign */
   2762 	'.',	/* 0x7e middle dot */
   2763 	/* end of special graphics characters */
   2764 	0x7f
   2765 };
   2766 
   2767 #endif
   2768 
   2769 #if NITE > 0 && NKBD > 0
   2770 
   2771 /*
   2772  * Console functions
   2773  */
   2774 #include <dev/cons.h>
   2775 extern void kbdenable(int);
   2776 extern int kbdcngetc(void);
   2777 
   2778 /*
   2779  * Return a priority in consdev->cn_pri field highest wins.  This function
   2780  * is called before any devices have been probed.
   2781  */
   2782 void
   2783 itecnprobe(struct consdev *cd)
   2784 {
   2785 	int maj;
   2786 
   2787 	/* locate the major number */
   2788 	maj = cdevsw_lookup_major(&ite_cdevsw);
   2789 
   2790 	/*
   2791 	 * return priority of the best ite (already picked from attach)
   2792 	 * or CN_DEAD.
   2793 	 */
   2794 	if (con_itesoftc.grf == NULL)
   2795 		cd->cn_pri = CN_DEAD;
   2796 	else {
   2797 		con_itesoftc.flags = (ITE_ALIVE|ITE_CONSOLE);
   2798 		/*
   2799 		 * hardcode the minor number.
   2800 		 * currently we support only one ITE, it is enough for now.
   2801 		 */
   2802 		con_itesoftc.isw = &itesw[0];
   2803 		cd->cn_pri = CN_INTERNAL;
   2804 		cd->cn_dev = makedev(maj, 0);
   2805 	}
   2806 }
   2807 
   2808 void
   2809 itecninit(struct consdev *cd)
   2810 {
   2811 	struct ite_softc *ip;
   2812 
   2813 	ip = getitesp(cd->cn_dev);
   2814 	iteinit(cd->cn_dev);	       /* init console unit */
   2815 	ip->flags |= ITE_ACTIVE | ITE_ISCONS;
   2816 	kbdenable(0);
   2817 	mfp_send_usart(0x49);
   2818 }
   2819 
   2820 /*
   2821  * itecnfinish() is called in ite_init() when the device is
   2822  * being probed in the normal fashion, thus we can finish setting
   2823  * up this ite now that the system is more functional.
   2824  */
   2825 void
   2826 itecnfinish(struct ite_softc *ip)
   2827 {
   2828 	static int done;
   2829 
   2830 	if (done)
   2831 		return;
   2832 	done = 1;
   2833 }
   2834 
   2835 /*ARGSUSED*/
   2836 int
   2837 itecngetc(dev_t dev)
   2838 {
   2839 	int c;
   2840 
   2841 	do {
   2842 		c = kbdcngetc();
   2843 		c = ite_cnfilter(c);
   2844 	} while (c == -1);
   2845 	return (c);
   2846 }
   2847 
   2848 void
   2849 itecnputc(dev_t dev, int c)
   2850 {
   2851 	static int paniced = 0;
   2852 	struct ite_softc *ip = getitesp(dev);
   2853 	char ch = c;
   2854 #ifdef ITE_KERNEL_ATTR
   2855 	short save_attribute;
   2856 #endif
   2857 #if defined(ITE_SIXEL)
   2858 	int save_escape;
   2859 #endif
   2860 
   2861 	if (panicstr && !paniced &&
   2862 	    (ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE) {
   2863 		(void) iteon(dev, 3);
   2864 		paniced = 1;
   2865 	}
   2866 
   2867 #if defined(ITE_SIXEL)
   2868 	save_escape = ip->escape;
   2869 	if (ip->escape == DCS) {
   2870 		ip->escape = 0;
   2871 		ip->dcs_cmd = DCS_DISCARD;
   2872 	}
   2873 #endif
   2874 #ifdef ITE_KERNEL_ATTR
   2875 	save_attribute = ip->attribute;
   2876 	ip->attribute = ITE_KERNEL_ATTR;
   2877 #endif
   2878 	ite_putstr(&ch, 1, dev);
   2879 #ifdef ITE_KERNEL_ATTR
   2880 	ip->attribute = save_attribute;
   2881 #endif
   2882 #if defined(ITE_SIXEL)
   2883 	if (ip->escape == 0) {
   2884 		ip->escape = save_escape;
   2885 	}
   2886 #endif
   2887 }
   2888 #endif
   2889