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