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