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