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