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