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