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