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