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