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