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