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