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