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