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