Home | History | Annotate | Line # | Download | only in dev
ite.c revision 1.73
      1 /*	$NetBSD: ite.c,v 1.73 2014/03/16 05:20:23 dholland 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.73 2014/03/16 05:20:23 dholland 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 	struct ite_softc *sc;
    597 	u_char buf[ITEBURST];
    598 	int s, len;
    599 
    600 	sc = getitesp(tp->t_dev);
    601 
    602 	KDASSERT(tp);
    603 
    604 	s = spltty(); {
    605 		if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
    606 			goto out;
    607 
    608 		tp->t_state |= TS_BUSY;
    609 		rbp = &tp->t_outq;
    610 
    611 		len = q_to_b(rbp, buf, ITEBURST);
    612 	} splx(s);
    613 
    614 	/* Here is a really good place to implement pre/jumpscroll() */
    615 	ite_putstr((char *)buf, len, tp->t_dev);
    616 
    617 	s = spltty(); {
    618 		tp->t_state &= ~TS_BUSY;
    619 		/* we have characters remaining. */
    620 		if (ttypull(tp)) {
    621 			tp->t_state |= TS_TIMEOUT;
    622 			callout_schedule(&tp->t_rstrt_ch, 1);
    623 		}
    624 	      out: ;
    625 	} splx(s);
    626 }
    627 
    628 void
    629 ite_on(dev_t dev, int flag)
    630 {
    631 	struct ite_softc *sc;
    632 	int unit;
    633 
    634 	unit = ITEUNIT(dev);
    635 	sc = getitesp(dev);
    636 
    637 	/* force ite active, overriding graphics mode */
    638 	if (flag & 1) {
    639 		sc->flags |= ITE_ACTIVE;
    640 		sc->flags &= ~(ITE_INGRF | ITE_INITED);
    641 	}
    642 	/* leave graphics mode */
    643 	if (flag & 2) {
    644 		sc->flags &= ~ITE_INGRF;
    645 		if ((sc->flags & ITE_ACTIVE) == 0)
    646 			return;
    647 	}
    648 	sc->flags |= ITE_ACTIVE;
    649 	if (sc->flags & ITE_INGRF)
    650 		return;
    651 	iteinit(dev);
    652 }
    653 
    654 void
    655 ite_off(dev_t dev, int flag)
    656 {
    657 	struct ite_softc *sc;
    658 
    659 	sc = getitesp(dev);
    660 	if (flag & 2)
    661 		sc->flags |= ITE_INGRF;
    662 	if ((sc->flags & ITE_ACTIVE) == 0)
    663 		return;
    664 	if ((flag & 1) ||
    665 	    (sc->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
    666 		SUBR_DEINIT(sc);
    667 	if ((flag & 2) == 0)	/* XXX hmm grfon() I think wants this to  go inactive. */
    668 		sc->flags &= ~ITE_ACTIVE;
    669 }
    670 
    671 static void
    672 ite_switch(int unit)
    673 {
    674 	struct ite_softc	*sc;
    675 	extern const struct cdevsw view_cdevsw;
    676 
    677 	sc = device_lookup_private(&ite_cd, unit);
    678 	if (sc == NULL || (sc->flags & (ITE_ATTACHED | ITE_INITED)) == 0)
    679 		return;
    680 
    681 	/*
    682 	 * If switching to an active ite, also switch the keyboard.
    683 	 */
    684 	if (sc->flags & ITE_ACTIVE)
    685 		kbd_ite = sc;
    686 
    687 	/*
    688 	 * Now make it visible
    689 	 */
    690 	(*view_cdevsw.d_ioctl)(sc->grf->g_viewdev, VIOCDISPLAY, NULL,
    691 			       0, NOLWP);
    692 
    693 	/*
    694 	 * Make sure the cursor's there too....
    695 	 */
    696   	SUBR_CURSOR(sc, DRAW_CURSOR);
    697 }
    698 
    699 /* XXX called after changes made in underlying grf layer. */
    700 /* I want to nuke this */
    701 void
    702 ite_reinit(dev_t dev)
    703 {
    704 	struct ite_softc *sc;
    705 
    706 	sc = getitesp(dev);
    707 	sc->flags &= ~ITE_INITED;
    708 	iteinit(dev);
    709 }
    710 
    711 int
    712 ite_param(struct tty *tp, struct termios *t)
    713 {
    714 	tp->t_ispeed = t->c_ispeed;
    715 	tp->t_ospeed = t->c_ospeed;
    716 	tp->t_cflag = t->c_cflag;
    717 	return (0);
    718 }
    719 
    720 void
    721 ite_reset(struct ite_softc *sc)
    722 {
    723 	int i;
    724 
    725 	sc->curx = 0;
    726 	sc->cury = 0;
    727 	sc->attribute = ATTR_NOR;
    728 	sc->save_curx = 0;
    729 	sc->save_cury = 0;
    730 	sc->save_attribute = ATTR_NOR;
    731 	sc->ap = sc->argbuf;
    732 	sc->emul_level = 0;
    733 	sc->eightbit_C1 = 0;
    734 	sc->top_margin = 0;
    735 	sc->bottom_margin = sc->rows - 1;
    736 	sc->inside_margins = 0;
    737 	sc->linefeed_newline = 0;
    738 	sc->auto_wrap = ite_default_wrap;
    739 	sc->cursor_appmode = 0;
    740 	sc->keypad_appmode = 0;
    741 	sc->imode = 0;
    742 	sc->key_repeat = 1;
    743 	memset(sc->tabs, 0, sc->cols);
    744 	for (i = 0; i < sc->cols; i++)
    745 		sc->tabs[i] = ((i & 7) == 0);
    746 }
    747 
    748 /*
    749  * has to be global because of the shared filters.
    750  */
    751 static u_char last_dead;
    752 
    753 /*
    754  * Used in console at startup only and for DDB.
    755  */
    756 int
    757 ite_cnfilter(u_int c, enum caller caller)
    758 {
    759 	struct key	key;
    760 	struct kbdmap	*kbdmap;
    761 	u_char		code, up, mask;
    762 	int		s;
    763 
    764 	up   = KBD_RELEASED(c);
    765 	c    = KBD_SCANCODE(c);
    766 	code = 0;
    767 	mask = 0;
    768 	kbdmap = (kbd_ite == NULL) ? &ascii_kbdmap : kbd_ite->kbdmap;
    769 
    770 	s = spltty();
    771 
    772 	/*
    773 	 * No special action if key released
    774 	 */
    775 	if (up) {
    776 		splx(s);
    777 		return -1;
    778 	}
    779 
    780 	/* translate modifiers */
    781 	if (kbd_modifier & KBD_MOD_SHIFT) {
    782 		if (kbd_modifier & KBD_MOD_ALT)
    783 			key = kbdmap->alt_shift_keys[c];
    784 		else
    785 			key = kbdmap->shift_keys[c];
    786 	}
    787 	else if (kbd_modifier & KBD_MOD_ALT)
    788 			key = kbdmap->alt_keys[c];
    789 	else {
    790 		key = kbdmap->keys[c];
    791 		/*
    792 		 * If CAPS and key is CAPable (no pun intended)
    793 		 */
    794 		if ((kbd_modifier & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
    795 			key = kbdmap->shift_keys[c];
    796 	}
    797 	code = key.code;
    798 
    799 #ifdef notyet /* LWP: Didn't have time to look at this yet */
    800 	/*
    801 	 * If string return simple console filter
    802 	 */
    803 	if (key->mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
    804 		splx(s);
    805 		return -1;
    806 	}
    807 	/* handle dead keys */
    808 	if (key->mode & KBD_MODE_DEAD) {
    809 		/* if entered twice, send accent itself */
    810 		if (last_dead == key->mode & KBD_MODE_ACCMASK)
    811 			last_dead = 0;
    812 		else {
    813 			last_dead = key->mode & KBD_MODE_ACCMASK;
    814 			splx(s);
    815 			return -1;
    816 		}
    817 	}
    818 	if (last_dead) {
    819 		/* can't apply dead flag to string-keys */
    820 		if (code >= '@' && code < 0x7f)
    821 			code =
    822 			    acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
    823 		last_dead = 0;
    824 	}
    825 #endif
    826 	if (kbd_modifier & KBD_MOD_CTRL)
    827 		code &= 0x1f;
    828 
    829 	/*
    830 	 * Do console mapping.
    831 	 */
    832 	code = code == '\r' ? '\n' : code;
    833 
    834 	splx(s);
    835 	return (code);
    836 }
    837 
    838 /* And now the old stuff. */
    839 
    840 /* these are used to implement repeating keys.. */
    841 static u_int last_char;
    842 static u_char tout_pending;
    843 
    844 static callout_t repeat_ch;
    845 
    846 /*ARGSUSED*/
    847 static void
    848 repeat_handler(void *arg)
    849 {
    850 	tout_pending = 0;
    851 	if (last_char)
    852 		add_sicallback((si_farg)ite_filter, (void *)last_char,
    853 						    (void *)ITEFILT_REPEATER);
    854 }
    855 
    856 void
    857 ite_filter(u_int c, enum caller caller)
    858 {
    859 	struct tty	*kbd_tty;
    860 	struct kbdmap	*kbdmap;
    861 	u_char		code, *str, up, mask;
    862 	struct key	key;
    863 	int		s, i;
    864 	static bool	again;
    865 
    866 	if (!again) {
    867 		/* XXX */
    868 		callout_init(&repeat_ch, 0);
    869 		again = true;
    870 	}
    871 
    872 	if (kbd_ite == NULL)
    873 		return;
    874 
    875 	kbd_tty = kbd_ite->tp;
    876 	kbdmap  = kbd_ite->kbdmap;
    877 
    878 	up   = KBD_RELEASED(c);
    879 	c    = KBD_SCANCODE(c);
    880 	code = 0;
    881 	mask = 0;
    882 
    883 	/* have to make sure we're at spltty in here */
    884 	s = spltty();
    885 
    886 	/*
    887 	 * keyboard interrupts come at priority 2, while softint
    888 	 * generated keyboard-repeat interrupts come at level 1.  So,
    889 	 * to not allow a key-up event to get thru before a repeat for
    890 	 * the key-down, we remove any outstanding callout requests..
    891 	 */
    892 	rem_sicallback((si_farg)ite_filter);
    893 
    894 	/*
    895 	 * Stop repeating on up event
    896 	 */
    897 	if (up) {
    898 		if (tout_pending) {
    899 			callout_stop(&repeat_ch);
    900 			tout_pending = 0;
    901 			last_char    = 0;
    902 		}
    903 		splx(s);
    904 		return;
    905 	}
    906 	else if (tout_pending && last_char != c) {
    907 		/*
    908 		 * Different character, stop also
    909 		 */
    910 		callout_stop(&repeat_ch);
    911 		tout_pending = 0;
    912 		last_char    = 0;
    913 	}
    914 
    915 	/*
    916 	 * Handle ite-switching ALT + Fx
    917 	 */
    918 	if ((kbd_modifier == KBD_MOD_ALT) && (c >= 0x3b) && (c <= 0x44)) {
    919 		ite_switch(c - 0x3b);
    920 		splx(s);
    921 		return;
    922 	}
    923 	/*
    924 	 * Safety button, switch back to ascii keymap.
    925 	 */
    926 	if (kbd_modifier == (KBD_MOD_ALT | KBD_MOD_LSHIFT) && c == 0x3b) {
    927 		/* ALT + LSHIFT + F1 */
    928 		memcpy(kbdmap, &ascii_kbdmap, sizeof(struct kbdmap));
    929 		splx(s);
    930 		return;
    931 #ifdef DDB
    932 	} else if (kbd_modifier == (KBD_MOD_ALT | KBD_MOD_LSHIFT) &&
    933 	    c == 0x43) {
    934 		/*
    935 		 * ALT + LSHIFT + F9 -> Debugger!
    936 		 */
    937 		Debugger();
    938 		splx(s);
    939 		return;
    940 #endif
    941 	}
    942 
    943 	/*
    944 	 * The rest of the code is senseless when the device is not open.
    945 	 */
    946 	if (kbd_tty == NULL) {
    947 		splx(s);
    948 		return;
    949 	}
    950 
    951 	/*
    952 	 * Translate modifiers
    953 	 */
    954 	if (kbd_modifier & KBD_MOD_SHIFT) {
    955 		if (kbd_modifier & KBD_MOD_ALT)
    956 			key = kbdmap->alt_shift_keys[c];
    957 		else
    958 			key = kbdmap->shift_keys[c];
    959 	}
    960 	else if (kbd_modifier & KBD_MOD_ALT)
    961 		key = kbdmap->alt_keys[c];
    962 	else {
    963 		key = kbdmap->keys[c];
    964 		/*
    965 		 * If CAPS and key is CAPable (no pun intended)
    966 		 */
    967 		if ((kbd_modifier & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
    968 			key = kbdmap->shift_keys[c];
    969 	}
    970 	code = key.code;
    971 
    972 	/*
    973 	 * Arrange to repeat the keystroke. By doing this at the level
    974 	 * of scan-codes, we can have function keys, and keys that
    975 	 * send strings, repeat too. This also entitles an additional
    976 	 * overhead, since we have to do the conversion each time, but
    977 	 * I guess that's ok.
    978 	 */
    979 	if (!tout_pending && caller == ITEFILT_TTY && kbd_ite->key_repeat) {
    980 		tout_pending = 1;
    981 		last_char    = c;
    982 		callout_reset(&repeat_ch, start_repeat_timeo * hz / 100,
    983 		    repeat_handler, NULL);
    984 	} else if (!tout_pending && caller==ITEFILT_REPEATER &&
    985 	    kbd_ite->key_repeat) {
    986 		tout_pending = 1;
    987 		last_char    = c;
    988 		callout_reset(&repeat_ch, next_repeat_timeo * hz / 100,
    989 		    repeat_handler, NULL);
    990 	}
    991 	/* handle dead keys */
    992 	if (key.mode & KBD_MODE_DEAD) {
    993 		/* if entered twice, send accent itself */
    994 		if (last_dead == (key.mode & KBD_MODE_ACCMASK))
    995 			last_dead = 0;
    996 		else {
    997 			last_dead = key.mode & KBD_MODE_ACCMASK;
    998 			splx(s);
    999 			return;
   1000 		}
   1001 	}
   1002 	if (last_dead) {
   1003 		/* can't apply dead flag to string-keys */
   1004 		if (!(key.mode & KBD_MODE_STRING) && code >= '@' &&
   1005 		    code < 0x7f)
   1006 			code = acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
   1007 		last_dead = 0;
   1008 	}
   1009 
   1010 	/*
   1011 	 * If not string, apply CTRL modifiers
   1012 	 */
   1013 	if (!(key.mode & KBD_MODE_STRING) &&
   1014 	    (!(key.mode & KBD_MODE_KPAD) ||
   1015 	     (kbd_ite && !kbd_ite->keypad_appmode))) {
   1016 		if (kbd_modifier & KBD_MOD_CTRL)
   1017 			code &= 0x1f;
   1018 	} else if ((key.mode & KBD_MODE_KPAD) &&
   1019 	    (kbd_ite && kbd_ite->keypad_appmode)) {
   1020 		static const char * const in  = "0123456789-+.\r()/*";
   1021 		static const char * const out = "pqrstuvwxymlnMPQRS";
   1022 			   char *cp  = strchr(in, code);
   1023 
   1024 		/*
   1025 		 * keypad-appmode sends SS3 followed by the above
   1026 		 * translated character
   1027 		 */
   1028 		kbd_tty->t_linesw->l_rint(27, kbd_tty);
   1029 		kbd_tty->t_linesw->l_rint('O', kbd_tty);
   1030 		kbd_tty->t_linesw->l_rint(out[cp - in], kbd_tty);
   1031 		splx(s);
   1032 		return;
   1033 	} else {
   1034 		/* *NO* I don't like this.... */
   1035 		static u_char app_cursor[] =
   1036 		{
   1037 		    3, 27, 'O', 'A',
   1038 		    3, 27, 'O', 'B',
   1039 		    3, 27, 'O', 'C',
   1040 		    3, 27, 'O', 'D'};
   1041 
   1042 		str = kbdmap->strings + code;
   1043 		/*
   1044 		 * if this is a cursor key, AND it has the default
   1045 		 * keymap setting, AND we're in app-cursor mode, switch
   1046 		 * to the above table. This is *nasty* !
   1047 		 */
   1048 		if (((c == 0x48) || (c == 0x4b) ||(c == 0x4d) || (c == 0x50)) &&
   1049 		     kbd_ite->cursor_appmode &&
   1050 		    !memcmp(str, "\x03\x1b[", 3) &&
   1051 		    strchr("ABCD", str[3]))
   1052 			str = app_cursor + 4 * (str[3] - 'A');
   1053 
   1054 		/*
   1055 		 * using a length-byte instead of 0-termination allows
   1056 		 * to embed \0 into strings, although this is not used
   1057 		 * in the default keymap
   1058 		 */
   1059 		for (i = *str++; i; i--)
   1060 			kbd_tty->t_linesw->l_rint(*str++, kbd_tty);
   1061 		splx(s);
   1062 		return;
   1063 	}
   1064 	kbd_tty->t_linesw->l_rint(code, kbd_tty);
   1065 
   1066 	splx(s);
   1067 	return;
   1068 }
   1069 
   1070 /* helper functions, makes the code below more readable */
   1071 static inline void
   1072 ite_sendstr(const char *str)
   1073 {
   1074 	struct tty *kbd_tty;
   1075 
   1076 	kbd_tty = kbd_ite->tp;
   1077 	KDASSERT(kbd_tty);
   1078 	while (*str)
   1079 		kbd_tty->t_linesw->l_rint(*str++, kbd_tty);
   1080 }
   1081 
   1082 static void
   1083 alignment_display(struct ite_softc *sc)
   1084 {
   1085   int i, j;
   1086 
   1087   for (j = 0; j < sc->rows; j++)
   1088     for (i = 0; i < sc->cols; i++)
   1089       SUBR_PUTC(sc, 'E', j, i, ATTR_NOR);
   1090   attrclr(sc, 0, 0, sc->rows, sc->cols);
   1091   SUBR_CURSOR(sc, DRAW_CURSOR);
   1092 }
   1093 
   1094 static inline void
   1095 snap_cury(struct ite_softc *sc)
   1096 {
   1097   if (sc->inside_margins)
   1098     {
   1099       if (sc->cury < sc->top_margin)
   1100 	sc->cury = sc->top_margin;
   1101       if (sc->cury > sc->bottom_margin)
   1102 	sc->cury = sc->bottom_margin;
   1103     }
   1104 }
   1105 
   1106 static inline void
   1107 ite_dnchar(struct ite_softc *sc, int n)
   1108 {
   1109   n = min(n, sc->cols - sc->curx);
   1110   if (n < sc->cols - sc->curx)
   1111     {
   1112       SUBR_SCROLL(sc, sc->cury, sc->curx + n, n, SCROLL_LEFT);
   1113       attrmov(sc, sc->cury, sc->curx + n, sc->cury, sc->curx,
   1114 	      1, sc->cols - sc->curx - n);
   1115       attrclr(sc, sc->cury, sc->cols - n, 1, n);
   1116     }
   1117   while (n-- > 0)
   1118     SUBR_PUTC(sc, ' ', sc->cury, sc->cols - n - 1, ATTR_NOR);
   1119   SUBR_CURSOR(sc, DRAW_CURSOR);
   1120 }
   1121 
   1122 static inline void
   1123 ite_inchar(struct ite_softc *sc, int n)
   1124 {
   1125   n = min(n, sc->cols - sc->curx);
   1126   if (n < sc->cols - sc->curx)
   1127     {
   1128       SUBR_SCROLL(sc, sc->cury, sc->curx, n, SCROLL_RIGHT);
   1129       attrmov(sc, sc->cury, sc->curx, sc->cury, sc->curx + n,
   1130 	      1, sc->cols - sc->curx - n);
   1131       attrclr(sc, sc->cury, sc->curx, 1, n);
   1132     }
   1133   while (n--)
   1134     SUBR_PUTC(sc, ' ', sc->cury, sc->curx + n, ATTR_NOR);
   1135   SUBR_CURSOR(sc, DRAW_CURSOR);
   1136 }
   1137 
   1138 static inline void
   1139 ite_clrtoeol(struct ite_softc *sc)
   1140 {
   1141   int y = sc->cury, x = sc->curx;
   1142   if (sc->cols - x > 0)
   1143     {
   1144       SUBR_CLEAR(sc, y, x, 1, sc->cols - x);
   1145       attrclr(sc, y, x, 1, sc->cols - x);
   1146       SUBR_CURSOR(sc, DRAW_CURSOR);
   1147     }
   1148 }
   1149 
   1150 static inline void
   1151 ite_clrtobol(struct ite_softc *sc)
   1152 {
   1153   int y = sc->cury, x = min(sc->curx + 1, sc->cols);
   1154   SUBR_CLEAR(sc, y, 0, 1, x);
   1155   attrclr(sc, y, 0, 1, x);
   1156   SUBR_CURSOR(sc, DRAW_CURSOR);
   1157 }
   1158 
   1159 static inline void
   1160 ite_clrline(struct ite_softc *sc)
   1161 {
   1162   int y = sc->cury;
   1163   SUBR_CLEAR(sc, y, 0, 1, sc->cols);
   1164   attrclr(sc, y, 0, 1, sc->cols);
   1165   SUBR_CURSOR(sc, DRAW_CURSOR);
   1166 }
   1167 
   1168 
   1169 
   1170 static inline void
   1171 ite_clrtoeos(struct ite_softc *sc)
   1172 {
   1173   ite_clrtoeol(sc);
   1174   if (sc->cury < sc->rows - 1)
   1175     {
   1176       SUBR_CLEAR(sc, sc->cury + 1, 0, sc->rows - 1 - sc->cury, sc->cols);
   1177       attrclr(sc, sc->cury, 0, sc->rows - sc->cury, sc->cols);
   1178       SUBR_CURSOR(sc, DRAW_CURSOR);
   1179     }
   1180 }
   1181 
   1182 static inline void
   1183 ite_clrtobos(struct ite_softc *sc)
   1184 {
   1185   ite_clrtobol(sc);
   1186   if (sc->cury > 0)
   1187     {
   1188       SUBR_CLEAR(sc, 0, 0, sc->cury, sc->cols);
   1189       attrclr(sc, 0, 0, sc->cury, sc->cols);
   1190       SUBR_CURSOR(sc, DRAW_CURSOR);
   1191     }
   1192 }
   1193 
   1194 static inline void
   1195 ite_clrscreen(struct ite_softc *sc)
   1196 {
   1197   SUBR_CLEAR(sc, 0, 0, sc->rows, sc->cols);
   1198   attrclr(sc, 0, 0, sc->rows, sc->cols);
   1199   SUBR_CURSOR(sc, DRAW_CURSOR);
   1200 }
   1201 
   1202 
   1203 
   1204 static inline void
   1205 ite_dnline(struct ite_softc *sc, int n)
   1206 {
   1207   /* interesting.. if the cursor is outside the scrolling
   1208      region, this command is simply ignored.. */
   1209   if (sc->cury < sc->top_margin || sc->cury > sc->bottom_margin)
   1210     return;
   1211 
   1212   n = min(n, sc->bottom_margin + 1 - sc->cury);
   1213   if (n <= sc->bottom_margin - sc->cury)
   1214     {
   1215       SUBR_SCROLL(sc, sc->cury + n, 0, n, SCROLL_UP);
   1216       attrmov(sc, sc->cury + n, 0, sc->cury, 0,
   1217 	      sc->bottom_margin + 1 - sc->cury - n, sc->cols);
   1218     }
   1219   SUBR_CLEAR(sc, sc->bottom_margin - n + 1, 0, n, sc->cols);
   1220   attrclr(sc, sc->bottom_margin - n + 1, 0, n, sc->cols);
   1221   SUBR_CURSOR(sc, DRAW_CURSOR);
   1222 }
   1223 
   1224 static inline void
   1225 ite_inline(struct ite_softc *sc, int n)
   1226 {
   1227   /* interesting.. if the cursor is outside the scrolling
   1228      region, this command is simply ignored.. */
   1229   if (sc->cury < sc->top_margin || sc->cury > sc->bottom_margin)
   1230     return;
   1231 
   1232   n = min(n, sc->bottom_margin + 1 - sc->cury);
   1233   if (n <= sc->bottom_margin - sc->cury)
   1234     {
   1235       SUBR_SCROLL(sc, sc->cury, 0, n, SCROLL_DOWN);
   1236       attrmov(sc, sc->cury, 0, sc->cury + n, 0,
   1237 	      sc->bottom_margin + 1 - sc->cury - n, sc->cols);
   1238     }
   1239   SUBR_CLEAR(sc, sc->cury, 0, n, sc->cols);
   1240   attrclr(sc, sc->cury, 0, n, sc->cols);
   1241   SUBR_CURSOR(sc, DRAW_CURSOR);
   1242 }
   1243 
   1244 static inline void
   1245 ite_lf (struct ite_softc *sc)
   1246 {
   1247   ++sc->cury;
   1248   if ((sc->cury == sc->bottom_margin+1) || (sc->cury == sc->rows))
   1249     {
   1250       sc->cury--;
   1251       SUBR_SCROLL(sc, sc->top_margin + 1, 0, 1, SCROLL_UP);
   1252       ite_clrline(sc);
   1253     }
   1254   SUBR_CURSOR(sc, MOVE_CURSOR);
   1255   clr_attr(sc, ATTR_INV);
   1256 }
   1257 
   1258 static inline void
   1259 ite_crlf (struct ite_softc *sc)
   1260 {
   1261   sc->curx = 0;
   1262   ite_lf (sc);
   1263 }
   1264 
   1265 static inline void
   1266 ite_cr (struct ite_softc *sc)
   1267 {
   1268   if (sc->curx)
   1269     {
   1270       sc->curx = 0;
   1271       SUBR_CURSOR(sc, MOVE_CURSOR);
   1272     }
   1273 }
   1274 
   1275 static inline void
   1276 ite_rlf (struct ite_softc *sc)
   1277 {
   1278   sc->cury--;
   1279   if ((sc->cury < 0) || (sc->cury == sc->top_margin - 1))
   1280     {
   1281       sc->cury++;
   1282       SUBR_SCROLL(sc, sc->top_margin, 0, 1, SCROLL_DOWN);
   1283       ite_clrline(sc);
   1284     }
   1285   SUBR_CURSOR(sc, MOVE_CURSOR);
   1286   clr_attr(sc, ATTR_INV);
   1287 }
   1288 
   1289 static inline int
   1290 atoi (const char *cp)
   1291 {
   1292   int n;
   1293 
   1294   for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
   1295     n = n * 10 + *cp - '0';
   1296 
   1297   return n;
   1298 }
   1299 
   1300 static inline int
   1301 ite_argnum (struct ite_softc *sc)
   1302 {
   1303   char ch;
   1304   int n;
   1305 
   1306   /* convert argument string into number */
   1307   if (sc->ap == sc->argbuf)
   1308     return 1;
   1309   ch = *sc->ap;
   1310   *sc->ap = 0;
   1311   n = atoi (sc->argbuf);
   1312   *sc->ap = ch;
   1313 
   1314   return n;
   1315 }
   1316 
   1317 static inline int
   1318 ite_zargnum (struct ite_softc *sc)
   1319 {
   1320   char ch;
   1321   int n;
   1322 
   1323   /* convert argument string into number */
   1324   if (sc->ap == sc->argbuf)
   1325     return 0;
   1326   ch = *sc->ap;
   1327   *sc->ap = 0;
   1328   n = atoi (sc->argbuf);
   1329   *sc->ap = ch;
   1330 
   1331   return n;	/* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
   1332 }
   1333 
   1334 void
   1335 ite_putstr(const u_char *s, int len, dev_t dev)
   1336 {
   1337 	struct ite_softc *sc;
   1338 	int i;
   1339 
   1340 	sc = getitesp(dev);
   1341 
   1342 	/* XXX avoid problems */
   1343 	if ((sc->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
   1344 	  	return;
   1345 
   1346 	SUBR_CURSOR(sc, START_CURSOROPT);
   1347 	for (i = 0; i < len; i++)
   1348 		if (s[i])
   1349 			iteputchar(s[i], sc);
   1350 	SUBR_CURSOR(sc, END_CURSOROPT);
   1351 }
   1352 
   1353 
   1354 void
   1355 iteputchar(register int c, struct ite_softc *sc)
   1356 {
   1357 	struct tty *kbd_tty;
   1358 	int n, x, y;
   1359 	char *cp;
   1360 
   1361 	if (kbd_ite == NULL)
   1362 		kbd_tty = NULL;
   1363 	else
   1364 		kbd_tty = kbd_ite->tp;
   1365 
   1366 	if (sc->escape)
   1367 	  {
   1368 	    switch (sc->escape)
   1369 	      {
   1370 	      case ESC:
   1371 	        switch (c)
   1372 	          {
   1373 		  /* first 7bit equivalents for the 8bit control characters */
   1374 
   1375 	          case 'D':
   1376 		    c = IND;
   1377 		    sc->escape = 0;
   1378 		    break; /* and fall into the next switch below (same for all `break') */
   1379 
   1380 		  case 'E':
   1381 		    c = NEL;
   1382 		    sc->escape = 0;
   1383 		    break;
   1384 
   1385 		  case 'H':
   1386 		    c = HTS;
   1387 		    sc->escape = 0;
   1388 		    break;
   1389 
   1390 		  case 'M':
   1391 		    c = RI;
   1392 		    sc->escape = 0;
   1393 		    break;
   1394 
   1395 		  case 'N':
   1396 		    c = SS2;
   1397 		    sc->escape = 0;
   1398 		    break;
   1399 
   1400 		  case 'O':
   1401 		    c = SS3;
   1402 		    sc->escape = 0;
   1403 		    break;
   1404 
   1405 		  case 'P':
   1406 		    c = DCS;
   1407 		    sc->escape = 0;
   1408 		    break;
   1409 
   1410 		  case '[':
   1411 		    c = CSI;
   1412 		    sc->escape = 0;
   1413 		    break;
   1414 
   1415 		  case '\\':
   1416 		    c = ST;
   1417 		    sc->escape = 0;
   1418 		    break;
   1419 
   1420 		  case ']':
   1421 		    c = OSC;
   1422 		    sc->escape = 0;
   1423 		    break;
   1424 
   1425 		  case '^':
   1426 		    c = PM;
   1427 		    sc->escape = 0;
   1428 		    break;
   1429 
   1430 		  case '_':
   1431 		    c = APC;
   1432 		    sc->escape = 0;
   1433 		    break;
   1434 
   1435 
   1436 		  /* introduces 7/8bit control */
   1437 		  case ' ':
   1438 		     /* can be followed by either F or G */
   1439 		     sc->escape = ' ';
   1440 		     break;
   1441 
   1442 
   1443 		  /* a lot of character set selections, not yet used...
   1444 		     94-character sets: */
   1445 		  case '(':	/* G0 */
   1446 		  case ')':	/* G1 */
   1447 		    sc->escape = c;
   1448 		    return;
   1449 
   1450 		  case '*':	/* G2 */
   1451 		  case '+':	/* G3 */
   1452 		  case 'B':	/* ASCII */
   1453 		  case 'A':	/* ISO latin 1 */
   1454 		  case '<':	/* user preferred suplemental */
   1455 		  case '0':	/* dec special graphics */
   1456 
   1457 		  /* 96-character sets: */
   1458 		  case '-':	/* G1 */
   1459 		  case '.':	/* G2 */
   1460 		  case '/':	/* G3 */
   1461 
   1462 		  /* national character sets: */
   1463 		  case '4':	/* dutch */
   1464 		  case '5':
   1465 		  case 'C':	/* finnish */
   1466 		  case 'R':	/* french */
   1467 		  case 'Q':	/* french canadian */
   1468 		  case 'K':	/* german */
   1469 		  case 'Y':	/* italian */
   1470 		  case '6':	/* norwegian/danish */
   1471 		  /* note: %5 and %6 are not supported (two chars..) */
   1472 
   1473 		    sc->escape = 0;
   1474 		    /* just ignore for now */
   1475 		    return;
   1476 
   1477 
   1478 		  /* locking shift modes (as you might guess, not yet supported..) */
   1479 		  case '`':
   1480 		    sc->GR = sc->G1;
   1481 		    sc->escape = 0;
   1482 		    return;
   1483 
   1484 		  case 'n':
   1485 		    sc->GL = sc->G2;
   1486 		    sc->escape = 0;
   1487 		    return;
   1488 
   1489 		  case '}':
   1490 		    sc->GR = sc->G2;
   1491 		    sc->escape = 0;
   1492 		    return;
   1493 
   1494 		  case 'o':
   1495 		    sc->GL = sc->G3;
   1496 		    sc->escape = 0;
   1497 		    return;
   1498 
   1499 		  case '|':
   1500 		    sc->GR = sc->G3;
   1501 		    sc->escape = 0;
   1502 		    return;
   1503 
   1504 
   1505 		  /* font width/height control */
   1506 		  case '#':
   1507 		    sc->escape = '#';
   1508 		    return;
   1509 
   1510 
   1511 		  /* hard terminal reset .. */
   1512 		  case 'c':
   1513 		    ite_reset (sc);
   1514 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1515 		    sc->escape = 0;
   1516 		    return;
   1517 
   1518 
   1519 		  case '7':
   1520 		    sc->save_curx = sc->curx;
   1521 		    sc->save_cury = sc->cury;
   1522 		    sc->save_attribute = sc->attribute;
   1523 		    sc->escape = 0;
   1524 		    return;
   1525 
   1526 		  case '8':
   1527 		    sc->curx = sc->save_curx;
   1528 		    sc->cury = sc->save_cury;
   1529 		    sc->attribute = sc->save_attribute;
   1530 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1531 		    sc->escape = 0;
   1532 		    return;
   1533 
   1534 		  case '=':
   1535 		    sc->keypad_appmode = 1;
   1536 		    sc->escape = 0;
   1537 		    return;
   1538 
   1539 		  case '>':
   1540 		    sc->keypad_appmode = 0;
   1541 		    sc->escape = 0;
   1542 		    return;
   1543 
   1544 		  case 'Z':	/* request ID */
   1545 		    if (sc->emul_level == EMUL_VT100)
   1546 		      ite_sendstr ("\033[?61;0c"); /* XXX not clean */
   1547 		    else
   1548 		      ite_sendstr ("\033[?63;0c"); /* XXX not clean */
   1549 		    sc->escape = 0;
   1550 		    return;
   1551 
   1552 		  /* default catch all for not recognized ESC sequences */
   1553 		  default:
   1554 		    sc->escape = 0;
   1555 		    return;
   1556 		  }
   1557 		break;
   1558 
   1559 
   1560 	      case '(':
   1561 	      case ')':
   1562 		sc->escape = 0;
   1563 		return;
   1564 
   1565 
   1566 	      case ' ':
   1567 	        switch (c)
   1568 	          {
   1569 	          case 'F':
   1570 		    sc->eightbit_C1 = 0;
   1571 		    sc->escape = 0;
   1572 		    return;
   1573 
   1574 		  case 'G':
   1575 		    sc->eightbit_C1 = 1;
   1576 		    sc->escape = 0;
   1577 		    return;
   1578 
   1579 		  default:
   1580 		    /* not supported */
   1581 		    sc->escape = 0;
   1582 		    return;
   1583 		  }
   1584 		break;
   1585 
   1586 
   1587 	      case '#':
   1588 		switch (c)
   1589 		  {
   1590 		  case '5':
   1591 		    /* single height, single width */
   1592 		    sc->escape = 0;
   1593 		    return;
   1594 
   1595 		  case '6':
   1596 		    /* double width, single height */
   1597 		    sc->escape = 0;
   1598 		    return;
   1599 
   1600 		  case '3':
   1601 		    /* top half */
   1602 		    sc->escape = 0;
   1603 		    return;
   1604 
   1605 		  case '4':
   1606 		    /* bottom half */
   1607 		    sc->escape = 0;
   1608 		    return;
   1609 
   1610 		  case '8':
   1611 		    /* screen alignment pattern... */
   1612 		    alignment_display (sc);
   1613 		    sc->escape = 0;
   1614 		    return;
   1615 
   1616 		  default:
   1617 		    sc->escape = 0;
   1618 		    return;
   1619 		  }
   1620 		break;
   1621 
   1622 
   1623 
   1624 	      case CSI:
   1625 	        /* the biggie... */
   1626 	        switch (c)
   1627 	          {
   1628 	          case '0': case '1': case '2': case '3': case '4':
   1629 	          case '5': case '6': case '7': case '8': case '9':
   1630 	          case ';': case '\"': case '$': case '>':
   1631 	            if (sc->ap < sc->argbuf + MAX_ARGSIZE)
   1632 	              *sc->ap++ = c;
   1633 	            return;
   1634 
   1635 		  case BS:
   1636 		    /* you wouldn't believe such perversion is possible?
   1637 		       it is.. BS is allowed in between cursor sequences
   1638 		       (at least), according to vttest.. */
   1639 		    if (--sc->curx < 0)
   1640 		      sc->curx = 0;
   1641 		    else
   1642 		      SUBR_CURSOR(sc, MOVE_CURSOR);
   1643 		    break;
   1644 
   1645 	          case 'p':
   1646 		    *sc->ap = 0;
   1647 	            if (! strncmp (sc->argbuf, "61\"", 3))
   1648 	              sc->emul_level = EMUL_VT100;
   1649 	            else if (! strncmp (sc->argbuf, "63;1\"", 5)
   1650 	            	     || ! strncmp (sc->argbuf, "62;1\"", 5))
   1651 	              sc->emul_level = EMUL_VT300_7;
   1652 	            else
   1653 	              sc->emul_level = EMUL_VT300_8;
   1654 	            sc->escape = 0;
   1655 	            return;
   1656 
   1657 
   1658 	          case '?':
   1659 		    *sc->ap = 0;
   1660 	            sc->escape = '?';
   1661 	            sc->ap = sc->argbuf;
   1662 	            return;
   1663 
   1664 
   1665 		  case 'c':
   1666   		    *sc->ap = 0;
   1667 		    if (sc->argbuf[0] == '>')
   1668 		      {
   1669 		        ite_sendstr ("\033[>24;0;0;0c");
   1670 		      }
   1671 		    else switch (ite_zargnum(sc))
   1672 		      {
   1673 		      case 0:
   1674 			/* primary DA request, send primary DA response */
   1675 			if (sc->emul_level == EMUL_VT100)
   1676 		          ite_sendstr ("\033[?1;1c");
   1677 		        else
   1678 		          ite_sendstr ("\033[?63;1c");
   1679 			break;
   1680 		      }
   1681 		    sc->escape = 0;
   1682 		    return;
   1683 
   1684 		  case 'n':
   1685 		    switch (ite_zargnum(sc))
   1686 		      {
   1687 		      case 5:
   1688 		        ite_sendstr ("\033[0n");	/* no malfunction */
   1689 			break;
   1690 		      case 6:
   1691 			/* cursor position report */
   1692 		        sprintf (sc->argbuf, "\033[%d;%dR",
   1693 				 sc->cury + 1, sc->curx + 1);
   1694 			ite_sendstr (sc->argbuf);
   1695 			break;
   1696 		      }
   1697 		    sc->escape = 0;
   1698 		    return;
   1699 
   1700 
   1701 		  case 'x':
   1702 		    switch (ite_zargnum(sc))
   1703 		      {
   1704 		      case 0:
   1705 			/* Fake some terminal parameters.  */
   1706 		        ite_sendstr ("\033[2;1;1;112;112;1;0x");
   1707 			break;
   1708 		      case 1:
   1709 		        ite_sendstr ("\033[3;1;1;112;112;1;0x");
   1710 			break;
   1711 		      }
   1712 		    sc->escape = 0;
   1713 		    return;
   1714 
   1715 
   1716 		  case 'g':
   1717 		    switch (ite_zargnum(sc))
   1718 		      {
   1719 		      case 0:
   1720 			if (sc->curx < sc->cols)
   1721 			  sc->tabs[sc->curx] = 0;
   1722 			break;
   1723 		      case 3:
   1724 		        for (n = 0; n < sc->cols; n++)
   1725 		          sc->tabs[n] = 0;
   1726 			break;
   1727 		      }
   1728 		    sc->escape = 0;
   1729 		    return;
   1730 
   1731 
   1732   	          case 'h': case 'l':
   1733 		    n = ite_zargnum (sc);
   1734 		    switch (n)
   1735 		      {
   1736 		      case 4:
   1737 		        sc->imode = (c == 'h');	/* insert/replace mode */
   1738 			break;
   1739 		      case 20:
   1740 			sc->linefeed_newline = (c == 'h');
   1741 			break;
   1742 		      }
   1743 		    sc->escape = 0;
   1744 		    return;
   1745 
   1746 
   1747 		  case 'M':
   1748 		    ite_dnline (sc, ite_argnum (sc));
   1749 	            sc->escape = 0;
   1750 	            return;
   1751 
   1752 
   1753 		  case 'L':
   1754 		    ite_inline (sc, ite_argnum (sc));
   1755 	            sc->escape = 0;
   1756 	            return;
   1757 
   1758 
   1759 		  case 'P':
   1760 		    ite_dnchar (sc, ite_argnum (sc));
   1761 	            sc->escape = 0;
   1762 	            return;
   1763 
   1764 
   1765 		  case '@':
   1766 		    ite_inchar (sc, ite_argnum (sc));
   1767 	            sc->escape = 0;
   1768 	            return;
   1769 
   1770 
   1771 		  case 'G':
   1772 		    /* this one was *not* in my vt320 manual but in
   1773 		       a vt320 termcap entry.. who is right?
   1774 		       It's supposed to set the horizontal cursor position. */
   1775 		    *sc->ap = 0;
   1776 		    x = atoi (sc->argbuf);
   1777 		    if (x) x--;
   1778 		    sc->curx = min(x, sc->cols - 1);
   1779 		    sc->escape = 0;
   1780 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1781 		    clr_attr (sc, ATTR_INV);
   1782 		    return;
   1783 
   1784 
   1785 		  case 'd':
   1786 		    /* same thing here, this one's for setting the absolute
   1787 		       vertical cursor position. Not documented... */
   1788 		    *sc->ap = 0;
   1789 		    y = atoi (sc->argbuf);
   1790 		    if (y) y--;
   1791 		    if (sc->inside_margins)
   1792 		      y += sc->top_margin;
   1793 		    sc->cury = min(y, sc->rows - 1);
   1794 		    sc->escape = 0;
   1795 		    snap_cury(sc);
   1796 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1797 		    clr_attr (sc, ATTR_INV);
   1798 		    return;
   1799 
   1800 
   1801 		  case 'H':
   1802 		  case 'f':
   1803 		    *sc->ap = 0;
   1804 		    y = atoi (sc->argbuf);
   1805 		    x = 0;
   1806 		    cp = strchr(sc->argbuf, ';');
   1807 		    if (cp)
   1808 		      x = atoi (cp + 1);
   1809 		    if (x) x--;
   1810 		    if (y) y--;
   1811 		    if (sc->inside_margins)
   1812 		      y += sc->top_margin;
   1813 		    sc->cury = min(y, sc->rows - 1);
   1814 		    sc->curx = min(x, sc->cols - 1);
   1815 		    sc->escape = 0;
   1816 		    snap_cury(sc);
   1817 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1818 		    clr_attr (sc, ATTR_INV);
   1819 		    return;
   1820 
   1821 		  case 'A':
   1822 		    n = ite_argnum (sc);
   1823 		    n = sc->cury - (n ? n : 1);
   1824 		    if (n < 0) n = 0;
   1825 		    if (sc->inside_margins)
   1826 		      n = max(sc->top_margin, n);
   1827 		    else if (n == sc->top_margin - 1)
   1828 		      /* allow scrolling outside region, but don't scroll out
   1829 			 of active region without explicit CUP */
   1830 		      n = sc->top_margin;
   1831 		    sc->cury = n;
   1832 		    sc->escape = 0;
   1833 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1834 		    clr_attr (sc, ATTR_INV);
   1835 		    return;
   1836 
   1837 		  case 'B':
   1838 		    n = ite_argnum (sc);
   1839 		    n = sc->cury + (n ? n : 1);
   1840 		    n = min(sc->rows - 1, n);
   1841 		    if (sc->inside_margins)
   1842 		      n = min(sc->bottom_margin, n);
   1843 		    else if (n == sc->bottom_margin + 1)
   1844 		      /* allow scrolling outside region, but don't scroll out
   1845 			 of active region without explicit CUP */
   1846 		      n = sc->bottom_margin;
   1847 		    sc->cury = n;
   1848 		    sc->escape = 0;
   1849 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1850 		    clr_attr (sc, ATTR_INV);
   1851 		    return;
   1852 
   1853 		  case 'C':
   1854 		    n = ite_argnum (sc);
   1855 		    n = n ? n : 1;
   1856 		    sc->curx = min(sc->curx + n, sc->cols - 1);
   1857 		    sc->escape = 0;
   1858 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1859 		    clr_attr (sc, ATTR_INV);
   1860 		    return;
   1861 
   1862 		  case 'D':
   1863 		    n = ite_argnum (sc);
   1864 		    n = n ? n : 1;
   1865 		    n = sc->curx - n;
   1866 		    sc->curx = n >= 0 ? n : 0;
   1867 		    sc->escape = 0;
   1868 		    SUBR_CURSOR(sc, MOVE_CURSOR);
   1869 		    clr_attr (sc, ATTR_INV);
   1870 		    return;
   1871 
   1872 
   1873 
   1874 
   1875 		  case 'J':
   1876 		    *sc->ap = 0;
   1877 		    n = ite_zargnum (sc);
   1878 		    if (n == 0)
   1879 	              ite_clrtoeos(sc);
   1880 		    else if (n == 1)
   1881 		      ite_clrtobos(sc);
   1882 		    else if (n == 2)
   1883 		      ite_clrscreen(sc);
   1884 	            sc->escape = 0;
   1885 	            return;
   1886 
   1887 
   1888 		  case 'K':
   1889 		    n = ite_zargnum (sc);
   1890 		    if (n == 0)
   1891 		      ite_clrtoeol(sc);
   1892 		    else if (n == 1)
   1893 		      ite_clrtobol(sc);
   1894 		    else if (n == 2)
   1895 		      ite_clrline(sc);
   1896 		    sc->escape = 0;
   1897 		    return;
   1898 
   1899 
   1900 		  case 'X':
   1901 		    n = ite_argnum(sc) - 1;
   1902 		    n = min(n, sc->cols - 1 - sc->curx);
   1903 		    for (; n >= 0; n--)
   1904 		      {
   1905 			attrclr(sc, sc->cury, sc->curx + n, 1, 1);
   1906 			SUBR_PUTC(sc, ' ', sc->cury, sc->curx + n, ATTR_NOR);
   1907 		      }
   1908 		    sc->escape = 0;
   1909 		    return;
   1910 
   1911 
   1912 	          case '}': case '`':
   1913 	            /* status line control */
   1914 	            sc->escape = 0;
   1915 	            return;
   1916 
   1917 
   1918 		  case 'r':
   1919 		    *sc->ap = 0;
   1920 		    x = atoi (sc->argbuf);
   1921 		    x = x ? x : 1;
   1922 		    y = sc->rows;
   1923 		    cp = strchr(sc->argbuf, ';');
   1924 		    if (cp)
   1925 		      {
   1926 			y = atoi (cp + 1);
   1927 			y = y ? y : sc->rows;
   1928 		      }
   1929 		    if (y - x < 2)
   1930 		      {
   1931 			/* if illegal scrolling region, reset to defaults */
   1932 			x = 1;
   1933 			y = sc->rows;
   1934 		      }
   1935 		    x--;
   1936 		    y--;
   1937 		    sc->top_margin = min(x, sc->rows - 1);
   1938 		    sc->bottom_margin = min(y, sc->rows - 1);
   1939 		    if (sc->inside_margins)
   1940 		      {
   1941 			sc->cury = sc->top_margin;
   1942 			sc->curx = 0;
   1943 			SUBR_CURSOR(sc, MOVE_CURSOR);
   1944 		      }
   1945 		    sc->escape = 0;
   1946 		    return;
   1947 
   1948 
   1949 		  case 'm':
   1950 		    /* big attribute setter/resetter */
   1951 		    {
   1952 		      char *chp;
   1953 		      *sc->ap = 0;
   1954 		      /* kludge to make CSIm work (== CSI0m) */
   1955 		      if (sc->ap == sc->argbuf)
   1956 		        sc->ap++;
   1957 		      for (chp = sc->argbuf; chp < sc->ap; )
   1958 		        {
   1959 			  switch (*chp)
   1960 			    {
   1961 			    case 0:
   1962 			    case '0':
   1963 			      clr_attr (sc, ATTR_ALL);
   1964 			      chp++;
   1965 			      break;
   1966 
   1967 			    case '1':
   1968 			      set_attr (sc, ATTR_BOLD);
   1969 			      chp++;
   1970 			      break;
   1971 
   1972 			    case '2':
   1973 			      switch (chp[1])
   1974 			        {
   1975 			        case '2':
   1976 			          clr_attr (sc, ATTR_BOLD);
   1977 			          chp += 2;
   1978 			          break;
   1979 
   1980 			        case '4':
   1981 			          clr_attr (sc, ATTR_UL);
   1982 			          chp += 2;
   1983 			          break;
   1984 
   1985 			        case '5':
   1986 			          clr_attr (sc, ATTR_BLINK);
   1987 			          chp += 2;
   1988 			          break;
   1989 
   1990 			        case '7':
   1991 			          clr_attr (sc, ATTR_INV);
   1992 			          chp += 2;
   1993 			          break;
   1994 
   1995 		        	default:
   1996 		        	  chp++;
   1997 		        	  break;
   1998 		        	}
   1999 			      break;
   2000 
   2001 			    case '4':
   2002 			      set_attr (sc, ATTR_UL);
   2003 			      chp++;
   2004 			      break;
   2005 
   2006 			    case '5':
   2007 			      set_attr (sc, ATTR_BLINK);
   2008 			      chp++;
   2009 			      break;
   2010 
   2011 			    case '7':
   2012 			      set_attr (sc, ATTR_INV);
   2013 			      chp++;
   2014 			      break;
   2015 
   2016 			    default:
   2017 			      chp++;
   2018 			      break;
   2019 			    }
   2020 		        }
   2021 
   2022 		    }
   2023 		    sc->escape = 0;
   2024 		    return;
   2025 
   2026 
   2027 		  case 'u':
   2028 		    /* DECRQTSR */
   2029 		    ite_sendstr ("\033P\033\\");
   2030 		    sc->escape = 0;
   2031 		    return;
   2032 
   2033 
   2034 
   2035 		  default:
   2036 		    sc->escape = 0;
   2037 		    return;
   2038 		  }
   2039 		break;
   2040 
   2041 
   2042 
   2043 	      case '?':	/* CSI ? */
   2044 	      	switch (c)
   2045 	      	  {
   2046 	          case '0': case '1': case '2': case '3': case '4':
   2047 	          case '5': case '6': case '7': case '8': case '9':
   2048 	          case ';': case '\"': case '$':
   2049 		    /* Don't fill the last character; it's needed.  */
   2050 		    /* XXX yeah, where ?? */
   2051 	            if (sc->ap < sc->argbuf + MAX_ARGSIZE - 1)
   2052 	              *sc->ap++ = c;
   2053 	            return;
   2054 
   2055 
   2056 		  case 'n':
   2057 		    *sc->ap = 0;
   2058 		    if (sc->ap == &sc->argbuf[2])
   2059 		      {
   2060 		        if (! strncmp (sc->argbuf, "15", 2))
   2061 		          /* printer status: no printer */
   2062 		          ite_sendstr ("\033[13n");
   2063 
   2064 		        else if (! strncmp (sc->argbuf, "25", 2))
   2065 		          /* udk status */
   2066 		          ite_sendstr ("\033[20n");
   2067 
   2068 		        else if (! strncmp (sc->argbuf, "26", 2))
   2069 		          /* keyboard dialect: US */
   2070 		          ite_sendstr ("\033[27;1n");
   2071 		      }
   2072 		    sc->escape = 0;
   2073 		    return;
   2074 
   2075 
   2076   		  case 'h': case 'l':
   2077 		    n = ite_zargnum (sc);
   2078 		    switch (n)
   2079 		      {
   2080 		      case 1:
   2081 		        sc->cursor_appmode = (c == 'h');
   2082 		        break;
   2083 
   2084 		      case 3:
   2085 		        /* 132/80 columns (132 == 'h') */
   2086 		        break;
   2087 
   2088 		      case 4: /* smooth scroll */
   2089 			break;
   2090 
   2091 		      case 5:
   2092 		        /* light background (=='h') /dark background(=='l') */
   2093 		        break;
   2094 
   2095 		      case 6: /* origin mode */
   2096 			sc->inside_margins = (c == 'h');
   2097 			sc->curx = 0;
   2098 			sc->cury = sc->inside_margins ? sc->top_margin : 0;
   2099 			SUBR_CURSOR(sc, MOVE_CURSOR);
   2100 			break;
   2101 
   2102 		      case 7: /* auto wraparound */
   2103 			sc->auto_wrap = (c == 'h');
   2104 			break;
   2105 
   2106 		      case 8: /* keyboard repeat */
   2107 			sc->key_repeat = (c == 'h');
   2108 			break;
   2109 
   2110 		      case 20: /* newline mode */
   2111 			sc->linefeed_newline = (c == 'h');
   2112 			break;
   2113 
   2114 		      case 25: /* cursor on/off */
   2115 			SUBR_CURSOR(sc, (c == 'h') ? DRAW_CURSOR : ERASE_CURSOR);
   2116 			break;
   2117 		      }
   2118 		    sc->escape = 0;
   2119 		    return;
   2120 
   2121 		  default:
   2122 		    sc->escape = 0;
   2123 		    return;
   2124 		  }
   2125 		break;
   2126 
   2127 
   2128 	      default:
   2129 	        sc->escape = 0;
   2130 	        return;
   2131 	      }
   2132           }
   2133 
   2134 	switch (c) {
   2135 
   2136 	case VT:	/* VT is treated like LF */
   2137 	case FF:	/* so is FF */
   2138 	case LF:
   2139 		/* cr->crlf distinction is done here, on output,
   2140 		   not on input! */
   2141 		if (sc->linefeed_newline)
   2142 		  ite_crlf (sc);
   2143 		else
   2144 		  ite_lf (sc);
   2145 		break;
   2146 
   2147 	case CR:
   2148 		ite_cr (sc);
   2149 		break;
   2150 
   2151 	case BS:
   2152 		if (--sc->curx < 0)
   2153 			sc->curx = 0;
   2154 		else
   2155 			SUBR_CURSOR(sc, MOVE_CURSOR);
   2156 		break;
   2157 
   2158 	case HT:
   2159 		for (n = sc->curx + 1; n < sc->cols; n++) {
   2160 			if (sc->tabs[n]) {
   2161 				sc->curx = n;
   2162 				SUBR_CURSOR(sc, MOVE_CURSOR);
   2163 				break;
   2164 			}
   2165 		}
   2166 		break;
   2167 
   2168 	case BEL:
   2169 		if (kbd_tty && kbd_ite && kbd_ite->tp == kbd_tty)
   2170 			kbdbell();
   2171 		break;
   2172 
   2173 	case SO:
   2174 		sc->GL = sc->G1;
   2175 		break;
   2176 
   2177 	case SI:
   2178 		sc->GL = sc->G0;
   2179 		break;
   2180 
   2181 	case ENQ:
   2182 		/* send answer-back message !! */
   2183 		break;
   2184 
   2185 	case CAN:
   2186 		sc->escape = 0;	/* cancel any escape sequence in progress */
   2187 		break;
   2188 
   2189 	case SUB:
   2190 		sc->escape = 0;	/* dito, but see below */
   2191 		/* should also display a reverse question mark!! */
   2192 		break;
   2193 
   2194 	case ESC:
   2195 		sc->escape = ESC;
   2196 		break;
   2197 
   2198 
   2199 	/* now it gets weird.. 8bit control sequences.. */
   2200 	case IND:	/* index: move cursor down, scroll */
   2201 		ite_lf (sc);
   2202 		break;
   2203 
   2204 	case NEL:	/* next line. next line, first pos. */
   2205 		ite_crlf (sc);
   2206 		break;
   2207 
   2208 	case HTS:	/* set horizontal tab */
   2209 		if (sc->curx < sc->cols)
   2210 		  sc->tabs[sc->curx] = 1;
   2211 		break;
   2212 
   2213 	case RI:	/* reverse index */
   2214 		ite_rlf (sc);
   2215 		break;
   2216 
   2217 	case SS2:	/* go into G2 for one character */
   2218 		/* not yet supported */
   2219 		break;
   2220 
   2221 	case SS3:	/* go into G3 for one character */
   2222 		break;
   2223 
   2224 	case DCS:	/* device control string introducer */
   2225 		sc->escape = DCS;
   2226 		sc->ap = sc->argbuf;
   2227 		break;
   2228 
   2229 	case CSI:	/* control sequence introducer */
   2230 		sc->escape = CSI;
   2231 		sc->ap = sc->argbuf;
   2232 		break;
   2233 
   2234 	case ST:	/* string terminator */
   2235 		/* ignore, if not used as terminator */
   2236 		break;
   2237 
   2238 	case OSC:	/* introduces OS command. Ignore everything upto ST */
   2239 		sc->escape = OSC;
   2240 		break;
   2241 
   2242 	case PM:	/* privacy message, ignore everything upto ST */
   2243 		sc->escape = PM;
   2244 		break;
   2245 
   2246 	case APC:	/* application program command, ignore everything upto ST */
   2247 		sc->escape = APC;
   2248 		break;
   2249 
   2250 	default:
   2251 		if (c < ' ' || c == DEL)
   2252 			break;
   2253 		if (sc->imode)
   2254 			ite_inchar(sc, 1);
   2255 		iteprecheckwrap(sc);
   2256 #ifdef DO_WEIRD_ATTRIBUTES
   2257 		if ((sc->attribute & ATTR_INV) || attrtest(sc, ATTR_INV)) {
   2258 			attrset(sc, ATTR_INV);
   2259 			SUBR_PUTC(sc, c, sc->cury, sc->curx, ATTR_INV);
   2260 		}
   2261 		else
   2262 			SUBR_PUTC(sc, c, sc->cury, sc->curx, ATTR_NOR);
   2263 #else
   2264 		SUBR_PUTC(sc, c, sc->cury, sc->curx, sc->attribute);
   2265 #endif
   2266 		SUBR_CURSOR(sc, DRAW_CURSOR);
   2267 		itecheckwrap(sc);
   2268 		break;
   2269 	}
   2270 }
   2271 
   2272 static void
   2273 iteprecheckwrap(struct ite_softc *sc)
   2274 {
   2275 	if (sc->auto_wrap && sc->curx == sc->cols) {
   2276 		sc->curx = 0;
   2277 		clr_attr(sc, ATTR_INV);
   2278 		if (++sc->cury >= sc->bottom_margin + 1) {
   2279 			sc->cury = sc->bottom_margin;
   2280 			SUBR_CURSOR(sc, MOVE_CURSOR);
   2281 			SUBR_SCROLL(sc, sc->top_margin + 1, 0, 1, SCROLL_UP);
   2282 			ite_clrtoeol(sc);
   2283 		} else
   2284 			SUBR_CURSOR(sc, MOVE_CURSOR);
   2285 	}
   2286 }
   2287 
   2288 static void
   2289 itecheckwrap(struct ite_softc *sc)
   2290 {
   2291 	if (sc->curx < sc->cols) {
   2292 		sc->curx++;
   2293 		SUBR_CURSOR(sc, MOVE_CURSOR);
   2294 	}
   2295 }
   2296