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