xref: /netbsd-src/sys/arch/x68k/dev/ite.c (revision 89c5a767f8fc7a4633b2d409966e2becbb98ff92)
1 /*	$NetBSD: ite.c,v 1.20 1999/06/27 14:14:30 minoura Exp $	*/
2 
3 /*
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * from: Utah $Hdr: ite.c 1.1 90/07/09$
41  *
42  *	@(#)ite.c	7.6 (Berkeley) 5/16/91
43  */
44 
45 /*
46  * ite - bitmaped terminal.
47  * Supports VT200, a few terminal features will be unavailable until
48  * the system actually probes the device (i.e. not after consinit())
49  */
50 
51 #include "ite.h"
52 #if NITE > 0
53 
54 #include "bell.h"
55 #include "kbd.h"
56 
57 #include "opt_ite.h"
58 
59 #include <sys/param.h>
60 #include <sys/conf.h>
61 #include <sys/proc.h>
62 #include <sys/ioctl.h>
63 #include <sys/tty.h>
64 #include <sys/systm.h>
65 #include <sys/device.h>
66 #include <sys/malloc.h>
67 
68 #include <machine/cpu.h>
69 #include <machine/kbio.h>
70 #include <machine/bus.h>
71 #include <machine/grfioctl.h>
72 #include <machine/iteioctl.h>
73 
74 #include <arch/x68k/dev/grfvar.h>
75 #include <arch/x68k/dev/itevar.h>
76 #include <arch/x68k/dev/kbdmap.h>
77 #include <arch/x68k/dev/mfp.h>
78 #if NBELL > 0
79 void opm_bell __P((void));
80 #endif
81 
82 #define SUBR_CNPROBE(min)	itesw[min].ite_cnprobe(min)
83 #define SUBR_INIT(ip)		ip->isw->ite_init(ip)
84 #define SUBR_DEINIT(ip)		ip->isw->ite_deinit(ip)
85 #define SUBR_PUTC(ip,c,dy,dx,m)	ip->isw->ite_putc(ip,c,dy,dx,m)
86 #define SUBR_CURSOR(ip,flg)	ip->isw->ite_cursor(ip,flg)
87 #define SUBR_CLEAR(ip,sy,sx,h,w)	ip->isw->ite_clear(ip,sy,sx,h,w)
88 #define SUBR_SCROLL(ip,sy,sx,count,dir)	\
89     ip->isw->ite_scroll(ip,sy,sx,count,dir)
90 
91 struct consdev;
92 
93 __inline static void itesendch __P((int));
94 __inline static void alignment_display __P((struct ite_softc *));
95 __inline static void snap_cury __P((struct ite_softc *));
96 __inline static void ite_dnchar __P((struct ite_softc *, int));
97 static void ite_inchar __P((struct ite_softc *,	int));
98 __inline static void ite_clrtoeol __P((struct ite_softc *));
99 __inline static void ite_clrtobol __P((struct ite_softc *));
100 __inline static void ite_clrline __P((struct ite_softc *));
101 __inline static void ite_clrtoeos __P((struct ite_softc *));
102 __inline static void ite_clrtobos __P((struct ite_softc *));
103 __inline static void ite_clrscreen __P((struct ite_softc *));
104 __inline static void ite_dnline __P((struct ite_softc *, int));
105 __inline static void ite_inline __P((struct ite_softc *, int));
106 __inline static void ite_index __P((struct ite_softc *));
107 __inline static void ite_lf __P((struct ite_softc *));
108 __inline static void ite_crlf __P((struct ite_softc *));
109 __inline static void ite_cr __P((struct ite_softc *));
110 __inline static void ite_rlf __P((struct ite_softc *));
111 static void iteprecheckwrap __P((struct ite_softc *ip));
112 static void itecheckwrap __P((struct ite_softc *ip));
113 static int ite_argnum __P((struct ite_softc *ip));
114 static int ite_zargnum __P((struct ite_softc *ip));
115 static void ite_sendstr __P((struct ite_softc *ip, char *str));
116 __inline static int atoi __P((const char *cp));
117 __inline static char *index __P((const char *cp, char ch));
118 void ite_reset __P((struct ite_softc *ip));
119 struct ite_softc *getitesp __P((dev_t));
120 int iteon __P((dev_t, int));
121 void iteoff __P((dev_t, int));
122 
123 struct itesw itesw[] = {
124 	{0,	tv_init,	tv_deinit,	0,
125 	 0,	0,		0}
126 };
127 int	nitesw = sizeof(itesw) / sizeof(itesw[0]);
128 
129 /*
130  * # of chars are output in a single itestart() call.
131  * If this is too big, user processes will be blocked out for
132  * long periods of time while we are emptying the queue in itestart().
133  * If it is too small, console output will be very ragged.
134  */
135 #define ITEBURST 64
136 
137 int	nite = NITE;
138 struct	tty *ite_tty[NITE];
139 struct	ite_softc *kbd_ite = NULL;
140 struct  ite_softc con_itesoftc;
141 
142 struct  tty *kbd_tty = NULL;
143 
144 int	start_repeat_timeo = 20; /* /100: initial timeout till pressed key repeats */
145 int	next_repeat_timeo  = 3;  /* /100: timeout when repeating for next char */
146 
147 u_char	cons_tabs[MAX_TABS];
148 
149 cdev_decl(ite);
150 
151 void	itestart __P((struct tty *tp));
152 
153 void iteputchar __P((int c, struct ite_softc *ip));
154 void ite_putstr __P((const u_char * s, int len, dev_t dev));
155 
156 void iteattach __P((struct device *, struct device *, void *));
157 int itematch __P((struct device *, struct cfdata *, void *));
158 
159 struct cfattach ite_ca = {
160 	sizeof(struct ite_softc), itematch, iteattach
161 };
162 
163 extern struct cfdriver ite_cd;
164 
165 int
166 itematch(pdp, cdp, auxp)
167 	struct device *pdp;
168 	struct cfdata *cdp;
169 	void *auxp;
170 {
171 	struct grf_softc *gp;
172 #if 0
173 	int maj;
174 #endif
175 
176 	gp = auxp;
177 
178 	/* ite0 should be at grf0 XXX */
179 	if(cdp->cf_unit != gp->g_device.dv_unit)
180 		return(0);
181 
182 #if 0
183 	/*
184 	 * all that our mask allows (more than enough no one
185 	 * has > 32 monitors for text consoles on one machine)
186 	 */
187 	if (cdp->cf_unit >= sizeof(ite_confunits) * NBBY)
188 		return(0);
189 	/*
190 	 * XXX
191 	 * normally this would be done in attach, however
192 	 * during early init we do not have a device pointer
193 	 * and thus no unit number.
194 	 */
195 	for(maj = 0; maj < nchrdev; maj++)
196 		if (cdevsw[maj].d_open == iteopen)
197 			break;
198 	gp->g_itedev = makedev(maj, cdp->cf_unit);
199 #endif
200 	return(1);
201 }
202 
203 /*
204  * iteinit() is the standard entry point for initialization of
205  * an ite device, it is also called from ite_cninit().
206  */
207 void
208 iteattach(pdp, dp, auxp)
209 	struct device *pdp, *dp;
210 	void *auxp;
211 {
212 	struct ite_softc *ip;
213 	struct grf_softc *gp;
214 
215 	gp = (struct grf_softc *)auxp;
216 	if (dp) {
217 		ip = (struct ite_softc *)dp;
218 		if(con_itesoftc.grf != NULL
219 			/*&& con_itesoftc.grf->g_unit == gp->g_unit*/) {
220 			/*
221 			 * console reinit copy params over.
222 			 * and console always gets keyboard
223 			 */
224 			bcopy(&con_itesoftc.grf, &ip->grf,
225 			    (char *)&ip[1] - (char *)&ip->grf);
226 			con_itesoftc.grf = NULL;
227 			kbd_ite = ip;
228 		}
229 		ip->grf = gp;
230 		iteinit(ip->device.dv_unit); /* XXX */
231 		printf(": rows %d cols %d", ip->rows, ip->cols);
232 		if (kbd_ite == NULL)
233 			kbd_ite = ip;
234 		printf("\n");
235 	} else {
236 		if (con_itesoftc.grf != NULL)
237 			return;
238 		con_itesoftc.grf = gp;
239 		con_itesoftc.tabs = cons_tabs;
240 	}
241 }
242 
243 struct ite_softc *
244 getitesp(dev)
245 	dev_t dev;
246 {
247 	extern int x68k_realconfig;
248 
249 	if (x68k_realconfig && con_itesoftc.grf == NULL)
250 		return(ite_cd.cd_devs[UNIT(dev)]);
251 
252 	if (con_itesoftc.grf == NULL)
253 		panic("no ite_softc for console");
254 	return(&con_itesoftc);
255 }
256 
257 void
258 iteinit(dev)
259 	dev_t dev;
260 {
261 	struct ite_softc *ip;
262 
263 	ip = getitesp(dev);
264 
265 	if (ip->flags & ITE_INITED)
266 		return;
267 	bcopy(&ascii_kbdmap, &kbdmap, sizeof(struct kbdmap));
268 
269 	ip->curx = 0;
270 	ip->cury = 0;
271 	ip->cursorx = 0;
272 	ip->cursory = 0;
273 
274 	ip->isw = &itesw[ip->device.dv_unit]; /* XXX */
275 	SUBR_INIT(ip);
276 	SUBR_CURSOR(ip, DRAW_CURSOR);
277 	if (!ip->tabs)
278 		ip->tabs = malloc(MAX_TABS*sizeof(u_char), M_DEVBUF, M_WAITOK);
279 	ite_reset(ip);
280 	ip->flags |= ITE_INITED;
281 }
282 
283 /*
284  * Perform functions necessary to setup device as a terminal emulator.
285  */
286 int
287 iteon(dev, flag)
288 	dev_t dev;
289 	int flag;
290 {
291 	int unit = UNIT(dev);
292 	struct ite_softc *ip;
293 
294 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
295 	    (ip = getitesp(unit)) == NULL || (ip->flags&ITE_ALIVE) == 0)
296 		return(ENXIO);
297 	/* force ite active, overriding graphics mode */
298 	if (flag & 1) {
299 		ip->flags |= ITE_ACTIVE;
300 		ip->flags &= ~(ITE_INGRF|ITE_INITED);
301 	}
302 	/* leave graphics mode */
303 	if (flag & 2) {
304 		ip->flags &= ~ITE_INGRF;
305 		if ((ip->flags & ITE_ACTIVE) == 0)
306 			return(0);
307 	}
308 	ip->flags |= ITE_ACTIVE;
309 	if (ip->flags & ITE_INGRF)
310 		return(0);
311 	iteinit(dev);
312 	if (flag & 2)
313 		ite_reset(ip);
314 #if NKBD > 0
315 	mfp_send_usart (0x49);	/* XXX */
316 #endif
317 	return(0);
318 }
319 
320 /*
321  * "Shut down" device as terminal emulator.
322  * Note that we do not deinit the console device unless forced.
323  * Deinit'ing the console every time leads to a very active
324  * screen when processing /etc/rc.
325  */
326 void
327 iteoff(dev, flag)
328 	dev_t dev;
329 	int flag;
330 {
331 	int unit = UNIT(dev);
332 	register struct ite_softc *ip;
333 
334 	/* XXX check whether when call from grf.c */
335 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
336 	    (ip = getitesp(unit)) == NULL || (ip->flags&ITE_ALIVE) == 0)
337 		return;
338 	if (flag & 2)
339 		ip->flags |= ITE_INGRF;
340 
341 	if ((ip->flags & ITE_ACTIVE) == 0)
342 		return;
343 	if ((flag & 1) ||
344 	    (ip->flags & (ITE_INGRF|ITE_ISCONS|ITE_INITED)) == ITE_INITED)
345 		SUBR_DEINIT(ip);
346 
347 	/*
348 	 * XXX When the system is rebooted with "reboot", init(8)
349 	 * kills the last process to have the console open.
350 	 * If we don't revent the the ITE_ACTIVE bit from being
351 	 * cleared, we will never see messages printed during
352 	 * the process of rebooting.
353 	 */
354 	if ((flag & 2) == 0 && (ip->flags & ITE_ISCONS) == 0) {
355 		ip->flags &= ~ITE_ACTIVE;
356 #if NKBD > 0
357 		mfp_send_usart (0x48);	/* XXX */
358 #endif
359 	}
360 }
361 
362 /*
363  * standard entry points to the device.
364  */
365 
366 /* ARGSUSED */
367 int
368 iteopen(dev, mode, devtype, p)
369 	dev_t dev;
370 	int mode, devtype;
371 	struct proc *p;
372 {
373 	int unit = UNIT(dev);
374 	register struct tty *tp;
375 	register struct ite_softc *ip;
376 	register int error;
377 	int first = 0;
378 
379 	if (unit >= ite_cd.cd_ndevs || (ip = getitesp(dev)) == NULL)
380 		return (ENXIO);
381 	if (!ite_tty[unit]) {
382 		tp = ite_tty[unit] = ttymalloc();
383 		tty_attach(tp);
384 	} else
385 		tp = ite_tty[unit];
386 	if ((tp->t_state&(TS_ISOPEN|TS_XCLUDE)) == (TS_ISOPEN|TS_XCLUDE)
387 	    && p->p_ucred->cr_uid != 0)
388 		return (EBUSY);
389 	if ((ip->flags & ITE_ACTIVE) == 0) {
390 		error = iteon(dev, 0);
391 		if (error)
392 			return (error);
393 		first = 1;
394 	}
395 	tp->t_oproc = itestart;
396 	tp->t_param = NULL;
397 	tp->t_dev = dev;
398 	if ((tp->t_state&TS_ISOPEN) == 0) {
399 		ttychars(tp);
400 		tp->t_iflag = TTYDEF_IFLAG;
401 		tp->t_oflag = TTYDEF_OFLAG;
402 		tp->t_cflag = TTYDEF_CFLAG;
403 		tp->t_lflag = TTYDEF_LFLAG;
404 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
405 		tp->t_state = TS_ISOPEN|TS_CARR_ON;
406 		ttsetwater(tp);
407 	}
408 	error = (*linesw[tp->t_line].l_open)(dev, tp);
409 	if (error == 0) {
410 		tp->t_winsize.ws_row = ip->rows;
411 		tp->t_winsize.ws_col = ip->cols;
412 	} else if (first)
413 		iteoff(dev, 0);
414 	return (error);
415 }
416 
417 /*ARGSUSED*/
418 int
419 iteclose(dev, flag, mode, p)
420 	dev_t dev;
421 	int flag, mode;
422 	struct proc *p;
423 {
424 	register struct tty *tp = ite_tty[UNIT(dev)];
425 
426 	(*linesw[tp->t_line].l_close)(tp, flag);
427 	ttyclose(tp);
428 	iteoff(dev, 0);
429 #if 0
430 	ttyfree(tp);
431 	ite_tty[UNIT(dev)] = (struct tty *)0;
432 #endif
433 	return(0);
434 }
435 
436 int
437 iteread(dev, uio, flag)
438 	dev_t dev;
439 	struct uio *uio;
440 	int flag;
441 {
442 	register struct tty *tp = ite_tty[UNIT(dev)];
443 
444 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
445 }
446 
447 int
448 itewrite(dev, uio, flag)
449 	dev_t dev;
450 	struct uio *uio;
451 	int flag;
452 {
453 	register struct tty *tp = ite_tty[UNIT(dev)];
454 
455 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
456 }
457 
458 struct tty *
459 itetty(dev)
460 	dev_t dev;
461 {
462 
463 	return (ite_tty[UNIT(dev)]);
464 }
465 
466 int
467 iteioctl(dev, cmd, addr, flag, p)
468 	dev_t dev;
469 	u_long cmd;
470 	caddr_t addr;
471 	int flag;
472 	struct proc *p;
473 {
474 	struct iterepeat *irp;
475 	register struct tty *tp = ite_tty[UNIT(dev)];
476 	int error;
477 
478 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr, flag, p);
479 	if (error >= 0)
480 		return (error);
481 	error = ttioctl(tp, cmd, addr, flag, p);
482 	if (error >= 0)
483 		return (error);
484 
485 	switch (cmd) {
486 	case ITEIOCSKMAP:
487 		if (addr == 0)
488 			return(EFAULT);
489 		bcopy(addr, &kbdmap, sizeof(struct kbdmap));
490 		return(0);
491 
492 	case ITEIOCGKMAP:
493 		if (addr == NULL)
494 			return(EFAULT);
495 		bcopy(&kbdmap, addr, sizeof(struct kbdmap));
496 		return(0);
497 
498 	case ITEIOCGREPT:
499 		irp = (struct iterepeat *)addr;
500 		irp->start = start_repeat_timeo;
501 		irp->next = next_repeat_timeo;
502 
503 	case ITEIOCSREPT:
504 		irp = (struct iterepeat *)addr;
505 		if (irp->start < ITEMINREPEAT && irp->next < ITEMINREPEAT)
506 			return(EINVAL);
507 		start_repeat_timeo = irp->start;
508 		next_repeat_timeo = irp->next;
509 #if x68k
510 	case ITELOADFONT:
511 		if (addr) {
512 			bcopy(addr, kern_font, 4096 /*sizeof (kernel_font)*/);
513 			return 0;
514 		} else
515 			return EFAULT;
516 
517 	case ITETVCTRL:
518 		if (addr && *(u_int8_t *)addr < 0x40) {
519 			return mfp_send_usart (* (u_int8_t *)addr);
520 		} else {
521 			return EFAULT;
522 		}
523 #endif
524 	}
525 	return (ENOTTY);
526 }
527 
528 void
529 itestart(tp)
530 	register struct tty *tp;
531 {
532 	struct clist *rbp;
533 	struct ite_softc *ip;
534 	u_char buf[ITEBURST];
535 	int s, len;
536 
537 	ip = getitesp(tp->t_dev);
538 	/*
539 	 * (Potentially) lower priority.  We only need to protect ourselves
540 	 * from keyboard interrupts since that is all that can affect the
541 	 * state of our tty (kernel printf doesn't go through this routine).
542 	 */
543 	s = spltty();
544 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
545 		goto out;
546 	tp->t_state |= TS_BUSY;
547 	rbp = &tp->t_outq;
548 	len = q_to_b(rbp, buf, ITEBURST);
549 	/*splx(s);*/
550 
551 	/* Here is a really good place to implement pre/jumpscroll() */
552 	ite_putstr(buf, len, tp->t_dev);
553 
554 	/*s = spltty();*/
555 	tp->t_state &= ~TS_BUSY;
556 	/* we have characters remaining. */
557 	if (rbp->c_cc) {
558 		tp->t_state |= TS_TIMEOUT;
559 		timeout(ttrstrt, (caddr_t)tp, 1);
560 	}
561 	/* wakeup we are below */
562 	if (rbp->c_cc <= tp->t_lowat) {
563 		if (tp->t_state & TS_ASLEEP) {
564 			tp->t_state &= ~TS_ASLEEP;
565 			wakeup((caddr_t)rbp);
566 		}
567 		selwakeup(&tp->t_wsel);
568 	}
569 out:
570 	splx(s);
571 }
572 
573 /* XXX called after changes made in underlying grf layer. */
574 /* I want to nuke this */
575 void
576 ite_reinit(dev)
577 	dev_t dev;
578 {
579 	struct ite_softc *ip;
580 	int unit = UNIT(dev);
581 
582 	/* XXX check whether when call from grf.c */
583 	if (unit < 0 || unit >= ite_cd.cd_ndevs ||
584 	    (ip = getitesp(unit)) == NULL)
585 		return;
586 
587 	ip->flags &= ~ITE_INITED;
588 	iteinit(dev);
589 }
590 
591 void
592 ite_reset(ip)
593     struct ite_softc *ip;
594 {
595 	int i;
596 
597 	ip->curx = 0;
598 	ip->cury = 0;
599 	ip->attribute = 0;
600 	ip->save_curx = 0;
601 	ip->save_cury = 0;
602 	ip->save_attribute = 0;
603 	ip->ap = ip->argbuf;
604 	ip->emul_level = EMUL_VT300_8;
605 	ip->eightbit_C1 = 0;
606 	ip->top_margin = 0;
607 	ip->bottom_margin = ip->rows - 1;
608 	ip->inside_margins = 0; /* origin mode == absolute */
609 	ip->linefeed_newline = 0;
610 	ip->auto_wrap = 1;
611 	ip->cursor_appmode = 0;
612 	ip->keypad_appmode = 0;
613 	ip->imode = 0;
614 	ip->key_repeat = 1;
615 	ip->G0 = CSET_ASCII;
616 	ip->G1 = CSET_JIS1983;
617 	ip->G2 = CSET_JISKANA;
618 	ip->G3 = CSET_JIS1990;
619 	ip->GL = &ip->G0;
620 	ip->GR = &ip->G1;
621 	ip->save_GL = 0;
622 	ip->save_char = 0;
623 	ip->fgcolor = 7;
624 	ip->bgcolor = 0;
625 	for (i = 0; i < ip->cols; i++)
626 		ip->tabs[i] = ((i & 7) == 0);
627 	/* XXX clear screen */
628 	SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
629 	attrclr(ip, 0, 0, ip->rows, ip->cols);
630 }
631 
632 /* Used in console at startup only */
633 int
634 ite_cnfilter(c)
635 	u_char c;
636 {
637 	static u_char mod = 0;
638 	struct key key;
639 	u_char code, up, mask;
640 	int s;
641 
642 	up = c & 0x80 ? 1 : 0;
643 	c &= 0x7f;
644 	code = 0;
645 
646 	s = spltty();
647 
648 	mask = 0;
649 	if (c >= KBD_LEFT_ALT && !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
650 		switch (c) {
651 		case KBD_LEFT_SHIFT:
652 			mask = KBD_MOD_SHIFT;
653 			break;
654 
655 		case KBD_LEFT_ALT:
656 			mask = KBD_MOD_LALT;
657 			break;
658 
659 		case KBD_RIGHT_ALT:
660 			mask = KBD_MOD_RALT;
661 			break;
662 
663 		case KBD_LEFT_META:
664 			mask = KBD_MOD_LMETA;
665 			break;
666 
667 		case KBD_RIGHT_META:
668 			mask = KBD_MOD_RMETA;
669 			break;
670 
671 		case KBD_CAPS_LOCK:
672 			/*
673 			 * capslock already behaves `right', don't need to
674 			 * keep track of the state in here.
675 			 */
676 			mask = KBD_MOD_CAPS;
677 			break;
678 
679 		case KBD_CTRL:
680 			mask = KBD_MOD_CTRL;
681 			break;
682 
683 		case KBD_RECONNECT:
684 			/* ite got 0xff */
685 			if (up)
686 				kbd_setLED();
687 			break;
688 		}
689 		if (mask & KBD_MOD_CAPS) {
690 			if (!up) {
691 				mod ^= KBD_MOD_CAPS;
692 				kbdled ^= LED_CAPS_LOCK;
693 				kbd_setLED();
694 			}
695 		} else if (up)
696 			mod &= ~mask;
697 		else mod |= mask;
698 		splx (s);
699 		return -1;
700 	}
701 
702 	if (up) {
703 		splx(s);
704 		return -1;
705 	}
706 
707 	/* translate modifiers */
708 	if (mod & KBD_MOD_SHIFT) {
709 		if (mod & KBD_MOD_ALT)
710 			key = kbdmap.alt_shift_keys[c];
711 		else
712 			key = kbdmap.shift_keys[c];
713 	} else if (mod & KBD_MOD_ALT)
714 		key = kbdmap.alt_keys[c];
715 	else {
716 		key = kbdmap.keys[c];
717 		/* if CAPS and key is CAPable (no pun intended) */
718 		if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
719 			key = kbdmap.shift_keys[c];
720 	}
721 	code = key.code;
722 
723 	/* if string return */
724 	if (key.mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
725 		splx(s);
726 		return -1;
727 	}
728 	/* handle dead keys */
729 	if (key.mode & KBD_MODE_DEAD) {
730 		splx(s);
731 		return -1;
732 	}
733 	if (mod & KBD_MOD_CTRL)
734 		code &= 0x1f;
735 	if (mod & KBD_MOD_META)
736 		code |= 0x80;
737 
738 	/* do console mapping. */
739 	code = code == '\r' ? '\n' : code;
740 
741 	splx(s);
742 	return (code);
743 }
744 
745 /* And now the old stuff. */
746 __inline static void
747 itesendch (ch)
748 	int ch;
749 {
750 	(*linesw[kbd_tty->t_line].l_rint)(ch, kbd_tty);
751 }
752 
753 
754 void
755 ite_filter(c)
756 	u_char c;
757 {
758 	static u_short mod = 0;
759 	register unsigned char code, *str;
760 	u_short up, mask;
761 	struct key key;
762 	int s, i;
763 
764 	if (!kbd_ite)
765 		return;
766 	kbd_tty = ite_tty[kbd_ite->device.dv_unit];
767 
768 	/* have to make sure we're at spltty in here */
769 	s = spltty ();
770 
771 	up = c & 0x80 ? 1 : 0;
772 	c &= 0x7f;
773 	code = 0;
774 
775 	mask = 0;
776 	if (c >= KBD_LEFT_ALT &&
777 	    !(c >= 0x63 && c <= 0x6c)) {	/* 0x63: F1, 0x6c:F10 */
778 		switch (c) {
779 		case KBD_LEFT_SHIFT:
780 			mask = KBD_MOD_SHIFT;
781 			break;
782 
783 		case KBD_LEFT_ALT:
784 			mask = KBD_MOD_LALT;
785 			break;
786 
787 		case KBD_RIGHT_ALT:
788 			mask = KBD_MOD_RALT;
789 			break;
790 
791 		case KBD_LEFT_META:
792 			mask = KBD_MOD_LMETA;
793 			break;
794 
795 		case KBD_RIGHT_META:
796 			mask = KBD_MOD_RMETA;
797 			break;
798 
799 		case KBD_CAPS_LOCK:
800 			/*
801 			 * capslock already behaves `right', don't need to keep
802 			 * track of the state in here.
803 			 */
804 			mask = KBD_MOD_CAPS;
805 			break;
806 
807 		case KBD_CTRL:
808 			mask = KBD_MOD_CTRL;
809 			break;
810 
811 		case KBD_OPT1:
812 			mask = KBD_MOD_OPT1;
813 			break;
814 
815 		case KBD_OPT2:
816 			mask = KBD_MOD_OPT2;
817 			break;
818 
819 		case KBD_RECONNECT:
820 			if (up) { /* ite got 0xff */
821 				kbd_setLED();
822 			}
823 			break;
824 		}
825 
826 		if (mask & KBD_MOD_CAPS) {
827 			if (!up) {
828 				mod ^= KBD_MOD_CAPS;
829 				kbdled ^= LED_CAPS_LOCK;
830 				kbd_setLED();
831 			}
832 		} else if (up) {
833 			mod &= ~mask;
834 		} else mod |= mask;
835 
836 		/*
837 		 * return even if it wasn't a modifier key, the other
838 		 * codes up here are either special (like reset warning),
839 		 * or not yet defined
840 		 */
841 		splx (s);
842 		return;
843 	}
844 
845 	if (up) {
846 		splx (s);
847 		return;
848 	}
849 
850 	/*
851 	 * intercept LAlt-LMeta-F1 here to switch back to original ascii-keymap.
852 	 * this should probably be configurable..
853 	 */
854 	if (mod == (KBD_MOD_LALT|KBD_MOD_LMETA) && c == 0x63) {
855 		bcopy (&ascii_kbdmap, &kbdmap, sizeof (struct kbdmap));
856 		splx (s);
857 		return;
858 	}
859 
860 	/* translate modifiers */
861 	if (mod & KBD_MOD_SHIFT) {
862 		if (mod & KBD_MOD_ALT)
863 			key = kbdmap.alt_shift_keys[c];
864 		else
865 			key = kbdmap.shift_keys[c];
866 	} else if (mod & KBD_MOD_ALT)
867 		key = kbdmap.alt_keys[c];
868 	else {
869 		key = kbdmap.keys[c];
870 		/* if CAPS and key is CAPable (no pun intended) */
871 		if ((mod & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
872 			key = kbdmap.shift_keys[c];
873 		else if ((mod & KBD_MOD_OPT2) && (key.mode & KBD_MODE_KPAD))
874 			key = kbdmap.shift_keys[c];
875 	}
876 	code = key.code;
877 
878 	/* handle dead keys */
879 	if (key.mode & KBD_MODE_DEAD) {
880 		splx (s);
881 		return;
882 	}
883   	/* if not string, apply META and CTRL modifiers */
884 	if (! (key.mode & KBD_MODE_STRING)
885 	    && (!(key.mode & KBD_MODE_KPAD) ||
886 		(kbd_ite && !kbd_ite->keypad_appmode))) {
887 		if ((mod & KBD_MOD_CTRL) &&
888 		    (code == ' ' || (code >= '@' && code <= 'z')))
889 			code &= 0x1f;
890 		if (mod & KBD_MOD_META)
891 			code |= 0x80;
892 	} else if ((key.mode & KBD_MODE_KPAD) &&
893 	       (kbd_ite && kbd_ite->keypad_appmode)) {
894 		static char *in = "0123456789-+.\r()/*";
895 		static char *out = "pqrstuvwxymlnMPQRS";
896 		char *cp = index (in, code);
897 
898 		/*
899 		 * keypad-appmode sends SS3 followed by the above
900 		 * translated character
901 		 */
902 		(*linesw[kbd_tty->t_line].l_rint) (27, kbd_tty);
903 		(*linesw[kbd_tty->t_line].l_rint) ('O', kbd_tty);
904 		(*linesw[kbd_tty->t_line].l_rint) (out[cp - in], kbd_tty);
905 		splx(s);
906 		return;
907 	} else {
908 		/* *NO* I don't like this.... */
909 		static u_char app_cursor[] =
910 		{
911 		  3, 27, 'O', 'A',
912 		  3, 27, 'O', 'B',
913 		  3, 27, 'O', 'C',
914 		  3, 27, 'O', 'D'};
915 
916 		str = kbdmap.strings + code;
917 		/*
918 		 * if this is a cursor key, AND it has the default
919 		 * keymap setting, AND we're in app-cursor mode, switch
920 		 * to the above table. This is *nasty* !
921 		 */
922 		if (c >= 0x3b && c <= 0x3e && kbd_ite->cursor_appmode
923 		    && !bcmp(str, "\x03\x1b[", 3) &&
924 		    index("ABCD", str[3]))
925 			str = app_cursor + 4 * (str[3] - 'A');
926 
927 		/*
928 		 * using a length-byte instead of 0-termination allows
929 		 * to embed \0 into strings, although this is not used
930 		 * in the default keymap
931 		 */
932 		for (i = *str++; i; i--)
933 			(*linesw[kbd_tty->t_line].l_rint) (*str++, kbd_tty);
934 		splx(s);
935 		return;
936 	}
937 	(*linesw[kbd_tty->t_line].l_rint)(code, kbd_tty);
938 
939 	splx(s);
940 	return;
941 }
942 
943 /* helper functions, makes the code below more readable */
944 __inline static void
945 ite_sendstr (ip, str)
946 	struct ite_softc *ip;
947 	char *str;
948 {
949 	while (*str)
950 		itesendch (*str++);
951 }
952 
953 __inline static void
954 alignment_display(ip)
955 	struct ite_softc *ip;
956 {
957 	int i, j;
958 
959 	for (j = 0; j < ip->rows; j++)
960 		for (i = 0; i < ip->cols; i++)
961 			SUBR_PUTC(ip, 'E', j, i, ATTR_NOR);
962 	attrclr(ip, 0, 0, ip->rows, ip->cols);
963 }
964 
965 __inline static void
966 snap_cury(ip)
967 	struct ite_softc *ip;
968 {
969 	if (ip->inside_margins) {
970 		if (ip->cury < ip->top_margin)
971 			ip->cury = ip->top_margin;
972 		if (ip->cury > ip->bottom_margin)
973 			ip->cury = ip->bottom_margin;
974 	}
975 }
976 
977 __inline static void
978 ite_dnchar(ip, n)
979 	struct ite_softc *ip;
980 	int n;
981 {
982 	n = min(n, ip->cols - ip->curx);
983 	if (n < ip->cols - ip->curx) {
984 		SUBR_SCROLL(ip, ip->cury, ip->curx + n, n, SCROLL_LEFT);
985 		attrmov(ip, ip->cury, ip->curx + n, ip->cury, ip->curx,
986 			1, ip->cols - ip->curx - n);
987 		attrclr(ip, ip->cury, ip->cols - n, 1, n);
988 	}
989 	while (n-- > 0)
990 		SUBR_PUTC(ip, ' ', ip->cury, ip->cols - n - 1, ATTR_NOR);
991 }
992 
993 static void
994 ite_inchar(ip, n)
995 	struct ite_softc *ip;
996 	int n;
997 {
998 	int c = ip->save_char;
999 	ip->save_char = 0;
1000 	n = min(n, ip->cols - ip->curx);
1001 	if (n < ip->cols - ip->curx) {
1002 		SUBR_SCROLL(ip, ip->cury, ip->curx, n, SCROLL_RIGHT);
1003 		attrmov(ip, ip->cury, ip->curx, ip->cury, ip->curx + n,
1004 			1, ip->cols - ip->curx - n);
1005 		attrclr(ip, ip->cury, ip->curx, 1, n);
1006 	}
1007 	while (n--)
1008 		SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1009 	ip->save_char = c;
1010 }
1011 
1012 __inline static void
1013 ite_clrtoeol(ip)
1014 	struct ite_softc *ip;
1015 {
1016 	int y = ip->cury, x = ip->curx;
1017 	if (ip->cols - x > 0) {
1018 		SUBR_CLEAR(ip, y, x, 1, ip->cols - x);
1019 		attrclr(ip, y, x, 1, ip->cols - x);
1020 	}
1021 }
1022 
1023 __inline static void
1024 ite_clrtobol(ip)
1025 	struct ite_softc *ip;
1026 {
1027 	int y = ip->cury, x = min(ip->curx + 1, ip->cols);
1028 	SUBR_CLEAR(ip, y, 0, 1, x);
1029 	attrclr(ip, y, 0, 1, x);
1030 }
1031 
1032 __inline static void
1033 ite_clrline(ip)
1034 	struct ite_softc *ip;
1035 {
1036 	int y = ip->cury;
1037 	SUBR_CLEAR(ip, y, 0, 1, ip->cols);
1038 	attrclr(ip, y, 0, 1, ip->cols);
1039 }
1040 
1041 __inline static void
1042 ite_clrtoeos(ip)
1043 	struct ite_softc *ip;
1044 {
1045 	ite_clrtoeol(ip);
1046 	if (ip->cury < ip->rows - 1) {
1047 		SUBR_CLEAR(ip, ip->cury + 1, 0, ip->rows - 1 - ip->cury, ip->cols);
1048 		attrclr(ip, ip->cury, 0, ip->rows - ip->cury, ip->cols);
1049 	}
1050 }
1051 
1052 __inline static void
1053 ite_clrtobos(ip)
1054 	struct ite_softc *ip;
1055 {
1056 	ite_clrtobol(ip);
1057 	if (ip->cury > 0) {
1058 		SUBR_CLEAR(ip, 0, 0, ip->cury, ip->cols);
1059 		attrclr(ip, 0, 0, ip->cury, ip->cols);
1060 	}
1061 }
1062 
1063 __inline static void
1064 ite_clrscreen(ip)
1065 	struct ite_softc *ip;
1066 {
1067 	SUBR_CLEAR(ip, 0, 0, ip->rows, ip->cols);
1068 	attrclr(ip, 0, 0, ip->rows, ip->cols);
1069 }
1070 
1071 
1072 
1073 __inline static void
1074 ite_dnline(ip, n)
1075 	struct ite_softc *ip;
1076 	int n;
1077 {
1078 	/*
1079 	 * interesting.. if the cursor is outside the scrolling
1080 	 * region, this command is simply ignored..
1081 	 */
1082 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
1083 		return;
1084 
1085 	n = min(n, ip->bottom_margin + 1 - ip->cury);
1086 	if (n <= ip->bottom_margin - ip->cury) {
1087 		SUBR_SCROLL(ip, ip->cury + n, 0, n, SCROLL_UP);
1088 		attrmov(ip, ip->cury + n, 0, ip->cury, 0,
1089 			ip->bottom_margin + 1 - ip->cury - n, ip->cols);
1090 	}
1091 	SUBR_CLEAR(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
1092 	attrclr(ip, ip->bottom_margin - n + 1, 0, n, ip->cols);
1093 }
1094 
1095 __inline static void
1096 ite_inline(ip, n)
1097 	struct ite_softc *ip;
1098 	int n;
1099 {
1100 	/*
1101 	 * interesting.. if the cursor is outside the scrolling
1102 	 * region, this command is simply ignored..
1103 	 */
1104 	if (ip->cury < ip->top_margin || ip->cury > ip->bottom_margin)
1105 		return;
1106 
1107 	if (n <= 0)
1108 		n = 1;
1109 	else n = min(n, ip->bottom_margin + 1 - ip->cury);
1110 	if (n <= ip->bottom_margin  - ip->cury) {
1111 		SUBR_SCROLL(ip, ip->cury, 0, n, SCROLL_DOWN);
1112 		attrmov(ip, ip->cury, 0, ip->cury + n, 0,
1113 			ip->bottom_margin + 1 - ip->cury - n, ip->cols);
1114 	}
1115 	SUBR_CLEAR(ip, ip->cury, 0, n, ip->cols);
1116 	attrclr(ip, ip->cury, 0, n, ip->cols);
1117 	ip->curx = 0;
1118 }
1119 
1120 __inline static void
1121 ite_index (ip)
1122 	struct ite_softc *ip;
1123 {
1124 	++ip->cury;
1125 	if ((ip->cury == ip->bottom_margin+1) || (ip->cury == ip->rows)) {
1126 		ip->cury--;
1127 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1128 		ite_clrline(ip);
1129 	}
1130 	/*clr_attr(ip, ATTR_INV);*/
1131 }
1132 
1133 __inline static void
1134 ite_lf (ip)
1135 	struct ite_softc *ip;
1136 {
1137 	++ip->cury;
1138 	if (ip->cury > ip->bottom_margin) {
1139 		ip->cury--;
1140 		SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
1141 		ite_clrline(ip);
1142 	}
1143 /*	SUBR_CURSOR(ip, MOVE_CURSOR);*/
1144 	/*clr_attr(ip, ATTR_INV);*/
1145 	/* reset character set ... thanks for mohta. */
1146 	ip->G0 = CSET_ASCII;
1147 	ip->G1 = CSET_JIS1983;
1148 	ip->G2 = CSET_JISKANA;
1149 	ip->G3 = CSET_JIS1990;
1150 	ip->GL = &ip->G0;
1151 	ip->GR = &ip->G1;
1152 	ip->save_GL = 0;
1153 	ip->save_char = 0;
1154 }
1155 
1156 __inline static void
1157 ite_crlf (ip)
1158 	struct ite_softc *ip;
1159 {
1160 	ip->curx = 0;
1161 	ite_lf (ip);
1162 }
1163 
1164 __inline static void
1165 ite_cr (ip)
1166 	struct ite_softc *ip;
1167 {
1168 	if (ip->curx) {
1169 		ip->curx = 0;
1170 	}
1171 }
1172 
1173 __inline static void
1174 ite_rlf (ip)
1175 	struct ite_softc *ip;
1176 {
1177 	ip->cury--;
1178 	if ((ip->cury < 0) || (ip->cury == ip->top_margin - 1)) {
1179 		ip->cury++;
1180 		SUBR_SCROLL(ip, ip->top_margin, 0, 1, SCROLL_DOWN);
1181 		ite_clrline(ip);
1182 	}
1183 	clr_attr(ip, ATTR_INV);
1184 }
1185 
1186 __inline static int
1187 atoi (cp)
1188     const char *cp;
1189 {
1190 	int n;
1191 
1192 	for (n = 0; *cp && *cp >= '0' && *cp <= '9'; cp++)
1193 		n = n * 10 + *cp - '0';
1194 	return n;
1195 }
1196 
1197 __inline static char *
1198 index(cp, ch)
1199 	const char *cp;
1200 	char ch;
1201 {
1202 	while (*cp && *cp != ch)
1203 		cp++;
1204 	return *cp ? (char *) cp : 0;
1205 }
1206 
1207 __inline static int
1208 ite_argnum (ip)
1209 	struct ite_softc *ip;
1210 {
1211 	char ch;
1212 	int n;
1213 
1214 	/* convert argument string into number */
1215 	if (ip->ap == ip->argbuf)
1216 		return 1;
1217 	ch = *ip->ap;
1218 	*ip->ap = 0;
1219 	n = atoi (ip->argbuf);
1220 	*ip->ap = ch;
1221 
1222 	return n;
1223 }
1224 
1225 __inline static int
1226 ite_zargnum (ip)
1227 	struct ite_softc *ip;
1228 {
1229 	char ch;
1230 	int n;
1231 
1232 	/* convert argument string into number */
1233 	if (ip->ap == ip->argbuf)
1234 		return 0;
1235 	ch = *ip->ap;
1236 	*ip->ap = 0;	/* terminate string */
1237 	n = atoi (ip->argbuf);
1238 	*ip->ap = ch;
1239 
1240 	return n;	/* don't "n ? n : 1" here, <CSI>0m != <CSI>1m ! */
1241 }
1242 
1243 void
1244 ite_putstr(s, len, dev)
1245 	const u_char *s;
1246 	int len;
1247 	dev_t dev;
1248 {
1249 	struct ite_softc *ip;
1250 	int i;
1251 
1252 	ip = getitesp(dev);
1253 
1254 	/* XXX avoid problems */
1255 	if ((ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE)
1256 	  	return;
1257 
1258 	SUBR_CURSOR(ip, START_CURSOROPT);
1259 	for (i = 0; i < len; i++)
1260 		if (s[i])
1261 			iteputchar(s[i], ip);
1262 	SUBR_CURSOR(ip, END_CURSOROPT);
1263 }
1264 
1265 void
1266 iteputchar(c, ip)
1267 	register int c;
1268 	struct ite_softc *ip;
1269 {
1270 	int n, x, y;
1271 	char *cp;
1272 
1273 	if (c >= 0x20 && ip->escape) {
1274 		switch (ip->escape) {
1275 
1276 		case ESC:
1277 			switch (c) {
1278 				/* first 7bit equivalents for the 8bit control characters */
1279 
1280 			case 'D':
1281 				c = IND;
1282 				ip->escape = 0;
1283 				break; /* and fall into the next switch below (same for all `break') */
1284 
1285 			case 'E':
1286 				/* next line */
1287 				c = NEL;
1288 				ip->escape = 0;
1289 				break;
1290 
1291 			case 'H':
1292 				/* set TAB at current col */
1293 				c = HTS;
1294 				ip->escape = 0;
1295 				break;
1296 
1297 			case 'M':
1298 				/* reverse index */
1299 				c = RI;
1300 				ip->escape = 0;
1301 				break;
1302 
1303 			case 'N':
1304 				/* single shift G2 */
1305 				c = SS2;
1306 				ip->escape = 0;
1307 				break;
1308 
1309 			case 'O':
1310 				/* single shift G3 */
1311 				c = SS3;
1312 				ip->escape = 0;
1313 				break;
1314 
1315 			case 'P':
1316 				/* DCS detected */
1317 				c = DCS;
1318 				ip->escape = 0;
1319 				break;
1320 
1321 			case '[':
1322 				c = CSI;
1323 				ip->escape = 0;
1324 				break;
1325 
1326 			case '\\':
1327 				/* String Terminator */
1328 				c = ST;
1329 				ip->escape = 0;
1330 				break;
1331 
1332 			case ']':
1333 				c = OSC;
1334 				ip->escape = 0;
1335 				break;
1336 
1337 			case '^':
1338 				c = PM;
1339 				ip->escape = 0;
1340 				break;
1341 
1342 			case '_':
1343 				c = APC;
1344 				ip->escape = 0;
1345 				break;
1346 
1347 
1348 			/* introduces 7/8bit control */
1349 			case ' ':
1350 				/* can be followed by either F or G */
1351 				ip->escape = ' ';
1352 				break;
1353 
1354 
1355 			/* a lot of character set selections, not yet used...
1356 			   94-character sets: */
1357 			case '(':	/* G0 */
1358 			case ')':	/* G1 */
1359 				ip->escape = c;
1360 				return;
1361 
1362 			case '*':	/* G2 */
1363 			case '+':	/* G3 */
1364 			case 'B':	/* ASCII */
1365 			case 'A':	/* ISO latin 1 */
1366 			case '<':	/* user preferred suplemental */
1367 			case '0':	/* dec special graphics */
1368 
1369 			/* 96-character sets: */
1370 			case '-':	/* G1 */
1371 			case '.':	/* G2 */
1372 			case '/':	/* G3 */
1373 
1374 			/* national character sets: */
1375 			case '4':	/* dutch */
1376 			case '5':
1377 			case 'C':	/* finnish */
1378 			case 'R':	/* french */
1379 			case 'Q':	/* french canadian */
1380 			case 'K':	/* german */
1381 			case 'Y':	/* italian */
1382 			case '6':	/* norwegian/danish */
1383 			/* note: %5 and %6 are not supported (two chars..) */
1384 
1385 				ip->escape = 0;
1386 				/* just ignore for now */
1387 				return;
1388 
1389 			/* 94-multibyte character sets designate */
1390 			case '$':
1391 				ip->escape = '$';
1392 				return;
1393 
1394 			/* locking shift modes */
1395 			case '`':
1396 				ip->GR = &ip->G1;
1397 				ip->escape = 0;
1398 				return;
1399 
1400 			case 'n':
1401 				ip->GL = &ip->G2;
1402 				ip->escape = 0;
1403 				return;
1404 
1405 			case '}':
1406 				ip->GR = &ip->G2;
1407 				ip->escape = 0;
1408 				return;
1409 
1410 			case 'o':
1411 				ip->GL = &ip->G3;
1412 				ip->escape = 0;
1413 				return;
1414 
1415 			case '|':
1416 				ip->GR = &ip->G3;
1417 				ip->escape = 0;
1418 				return;
1419 
1420 			case '~':
1421 				ip->GR = &ip->G1;
1422 				ip->escape = 0;
1423 				return;
1424 
1425 			/* font width/height control */
1426 			case '#':
1427 				ip->escape = '#';
1428 				return;
1429 
1430 			case 'c':
1431 				/* hard terminal reset .. */
1432 				ite_reset (ip);
1433 				SUBR_CURSOR(ip, MOVE_CURSOR);
1434 				ip->escape = 0;
1435 				return;
1436 
1437 
1438 			case '7':
1439 				/* save cursor */
1440 				ip->save_curx = ip->curx;
1441 				ip->save_cury = ip->cury;
1442 				ip->save_attribute = ip->attribute;
1443 				ip->sc_om = ip->inside_margins;
1444 				ip->sc_G0 = ip->G0;
1445 				ip->sc_G1 = ip->G1;
1446 				ip->sc_G2 = ip->G2;
1447 				ip->sc_G3 = ip->G3;
1448 				ip->sc_GL = ip->GL;
1449 				ip->sc_GR = ip->GR;
1450 				ip->escape = 0;
1451 				return;
1452 
1453 			case '8':
1454 				/* restore cursor */
1455 				ip->curx = ip->save_curx;
1456 				ip->cury = ip->save_cury;
1457 				ip->attribute = ip->save_attribute;
1458 				ip->inside_margins = ip->sc_om;
1459 				ip->G0 = ip->sc_G0;
1460 				ip->G1 = ip->sc_G1;
1461 				ip->G2 = ip->sc_G2;
1462 				ip->G3 = ip->sc_G3;
1463 				ip->GL = ip->sc_GL;
1464 				ip->GR = ip->sc_GR;
1465 				SUBR_CURSOR(ip, MOVE_CURSOR);
1466 				ip->escape = 0;
1467 				return;
1468 
1469 			case '=':
1470 				/* keypad application mode */
1471 				ip->keypad_appmode = 1;
1472 				ip->escape = 0;
1473 				return;
1474 
1475 			case '>':
1476 				/* keypad numeric mode */
1477 				ip->keypad_appmode = 0;
1478 				ip->escape = 0;
1479 				return;
1480 
1481 			case 'Z':	/* request ID */
1482 				if (ip->emul_level == EMUL_VT100)
1483 					ite_sendstr (ip, "\033[61;0c"); /* XXX not clean */
1484 				else
1485 					ite_sendstr (ip, "\033[63;0c"); /* XXX not clean */
1486 				ip->escape = 0;
1487 				return;
1488 
1489 			/* default catch all for not recognized ESC sequences */
1490 			default:
1491 				ip->escape = 0;
1492 				return;
1493 			}
1494 			break;
1495 
1496 
1497 		case '(': /* designate G0 */
1498 			switch (c) {
1499 			case 'B': /* USASCII */
1500 				ip->G0 = CSET_ASCII;
1501 				ip->escape = 0;
1502 				return;
1503 			case 'I':
1504 				ip->G0 = CSET_JISKANA;
1505 				ip->escape = 0;
1506 				return;
1507 			case 'J':
1508 				ip->G0 = CSET_JISROMA;
1509 				ip->escape = 0;
1510 				return;
1511 			case 'A': /* British or ISO-Latin-1 */
1512 			case 'H': /* Swedish */
1513 			case 'K': /* German */
1514 			case 'R': /* French */
1515 			case 'Y': /* Italian */
1516 			case 'Z': /* Spanish */
1517 			default:
1518 				/* not supported */
1519 				ip->escape = 0;
1520 				return;
1521 			}
1522 
1523 		case ')': /* designate G1 */
1524 			ip->escape = 0;
1525 			return;
1526 
1527 		case '$': /* 94-multibyte character set */
1528 			switch (c) {
1529 			case '@':
1530 				ip->G0 = CSET_JIS1978;
1531 				ip->escape = 0;
1532 				return;
1533 			case 'B':
1534 				ip->G0 = CSET_JIS1983;
1535 				ip->escape = 0;
1536 				return;
1537 			case 'D':
1538 				ip->G0 = CSET_JIS1990;
1539 				ip->escape = 0;
1540 				return;
1541 			default:
1542 				/* not supported */
1543 				ip->escape = 0;
1544 				return;
1545 			}
1546 
1547 		case ' ':
1548 			switch (c) {
1549 			case 'F':
1550 				ip->eightbit_C1 = 0;
1551 				ip->escape = 0;
1552 				return;
1553 
1554 			case 'G':
1555 				ip->eightbit_C1 = 1;
1556 				ip->escape = 0;
1557 				return;
1558 
1559 			default:
1560 				/* not supported */
1561 				ip->escape = 0;
1562 				return;
1563 			}
1564 			break;
1565 
1566 		case '#':
1567 			switch (c) {
1568 			case '5':
1569 				/* single height, single width */
1570 				ip->escape = 0;
1571 				return;
1572 
1573 			case '6':
1574 				/* double width, single height */
1575 				ip->escape = 0;
1576 				return;
1577 
1578 			case '3':
1579 				/* top half */
1580 				ip->escape = 0;
1581 				return;
1582 
1583 			case '4':
1584 				/* bottom half */
1585 				ip->escape = 0;
1586 				return;
1587 
1588 			case '8':
1589 				/* screen alignment pattern... */
1590 				alignment_display (ip);
1591 				ip->escape = 0;
1592 				return;
1593 
1594 			default:
1595 				ip->escape = 0;
1596 				return;
1597 			}
1598 			break;
1599 
1600 
1601 
1602 		case CSI:
1603 			/* the biggie... */
1604 			switch (c) {
1605 			case '0': case '1': case '2': case '3': case '4':
1606 			case '5': case '6': case '7': case '8': case '9':
1607 			case ';': case '\"': case '$': case '>':
1608 				if (ip->ap < ip->argbuf + MAX_ARGSIZE)
1609 					*ip->ap++ = c;
1610 				return;
1611 
1612 			case 'p':
1613 				*ip->ap = 0;
1614 				if (!strncmp(ip->argbuf, "61\"", 3))
1615 					ip->emul_level = EMUL_VT100;
1616 				else if (!strncmp(ip->argbuf, "63;1\"", 5)
1617 					 || !strncmp(ip->argbuf, "62;1\"", 5))
1618 					ip->emul_level = EMUL_VT300_7;
1619 				else
1620 					ip->emul_level = EMUL_VT300_8;
1621 				ip->escape = 0;
1622 				return;
1623 
1624 
1625 			case '?':
1626 				*ip->ap = 0;
1627 				ip->escape = '?';
1628 				ip->ap = ip->argbuf;
1629 				return;
1630 
1631 
1632 			case 'c':
1633 				/* device attributes */
1634 				*ip->ap = 0;
1635 				if (ip->argbuf[0] == '>') {
1636 					ite_sendstr (ip, "\033[>24;0;0;0c");
1637 				} else
1638 					switch (ite_zargnum(ip)) {
1639 					case 0:
1640 						/* primary DA request, send primary DA response */
1641 						if (ip->emul_level == EMUL_VT100)
1642 							ite_sendstr (ip, "\033[?1;1c");
1643 						else
1644 							ite_sendstr (ip, "\033[63;0c");
1645 						break;
1646 					}
1647 				ip->escape = 0;
1648 				return;
1649 
1650 			case 'n':
1651 				switch (ite_zargnum(ip)) {
1652 				case 5:
1653 					ite_sendstr (ip, "\033[0n");	/* no malfunction */
1654 					break;
1655 				case 6:
1656 					/* cursor position report */
1657 					sprintf (ip->argbuf, "\033[%d;%dR",
1658 						 ip->cury + 1, ip->curx + 1);
1659 					ite_sendstr (ip, ip->argbuf);
1660 					break;
1661 				}
1662 				ip->escape = 0;
1663 				return;
1664 
1665 
1666 			case 'x':
1667 				switch (ite_zargnum(ip)) {
1668 				case 0:
1669 					/* Fake some terminal parameters.  */
1670 					ite_sendstr (ip, "\033[2;1;1;112;112;1;0x");
1671 					break;
1672 				case 1:
1673 					ite_sendstr (ip, "\033[3;1;1;112;112;1;0x");
1674 					break;
1675 				}
1676 				ip->escape = 0;
1677 				return;
1678 
1679 
1680 			case 'g':
1681 				/* clear tabs */
1682 				switch (ite_zargnum(ip)) {
1683 				case 0:
1684 					if (ip->curx < ip->cols)
1685 						ip->tabs[ip->curx] = 0;
1686 					break;
1687 				case 3:
1688 					for (n = 0; n < ip->cols; n++)
1689 						ip->tabs[n] = 0;
1690 					break;
1691 
1692 				default:
1693 					/* ignore */
1694 					break;
1695 				}
1696 				ip->escape = 0;
1697 				return;
1698 
1699 
1700 			case 'h': /* set mode */
1701 			case 'l': /* reset mode */
1702 				n = ite_zargnum (ip);
1703 				switch (n) {
1704 				case 4:
1705 					ip->imode = (c == 'h');	/* insert/replace mode */
1706 					break;
1707 				case 20:
1708 					ip->linefeed_newline = (c == 'h');
1709 					break;
1710 				}
1711 				ip->escape = 0;
1712 				return;
1713 
1714 
1715 			case 'M':
1716 				/* delete line */
1717 				ite_dnline (ip, ite_argnum (ip));
1718 				ip->escape = 0;
1719 				return;
1720 
1721 
1722 			case 'L':
1723 				/* insert line */
1724 				ite_inline (ip, ite_argnum (ip));
1725 				ip->escape = 0;
1726 				return;
1727 
1728 
1729 			case 'P':
1730 				/* delete char */
1731 				ite_dnchar (ip, ite_argnum (ip));
1732 				ip->escape = 0;
1733 				return;
1734 
1735 
1736 			case '@':
1737 				/* insert char(s) */
1738 				ite_inchar (ip, ite_argnum (ip));
1739 				ip->escape = 0;
1740 				return;
1741 
1742 			case '!':
1743 				/* soft terminal reset */
1744 				ip->escape = 0; /* XXX */
1745 				return;
1746 
1747 			case 'G':
1748 				/* this one was *not* in my vt320 manual but in
1749 				   a vt320 termcap entry.. who is right?
1750 				   It's supposed to set the horizontal cursor position. */
1751 				*ip->ap = 0;
1752 				x = atoi (ip->argbuf);
1753 				if (x) x--;
1754 				ip->curx = min(x, ip->cols - 1);
1755 				ip->escape = 0;
1756 				SUBR_CURSOR(ip, MOVE_CURSOR);
1757 				clr_attr (ip, ATTR_INV);
1758 				return;
1759 
1760 
1761 			case 'd':
1762 				/* same thing here, this one's for setting the absolute
1763 				   vertical cursor position. Not documented... */
1764 				*ip->ap = 0;
1765 				y = atoi (ip->argbuf);
1766 				if (y) y--;
1767 				if (ip->inside_margins)
1768 					y += ip->top_margin;
1769 				ip->cury = min(y, ip->rows - 1);
1770 				ip->escape = 0;
1771 				snap_cury(ip);
1772 				SUBR_CURSOR(ip, MOVE_CURSOR);
1773 				clr_attr (ip, ATTR_INV);
1774 				return;
1775 
1776 
1777 			case 'H':
1778 			case 'f':
1779 				*ip->ap = 0;
1780 				y = atoi (ip->argbuf);
1781 				x = 0;
1782 				cp = index (ip->argbuf, ';');
1783 				if (cp)
1784 					x = atoi (cp + 1);
1785 				if (x) x--;
1786 				if (y) y--;
1787 				if (ip->inside_margins)
1788 					y += ip->top_margin;
1789 				ip->cury = min(y, ip->rows - 1);
1790 				ip->curx = min(x, ip->cols - 1);
1791 				ip->escape = 0;
1792 				snap_cury(ip);
1793 				SUBR_CURSOR(ip, MOVE_CURSOR);
1794 				/*clr_attr (ip, ATTR_INV);*/
1795 				return;
1796 
1797 			case 'A':
1798 				/* cursor up */
1799 				n = ite_argnum (ip);
1800 				n = ip->cury - (n ? n : 1);
1801 				if (n < 0) n = 0;
1802 				if (ip->inside_margins)
1803 					n = max(ip->top_margin, n);
1804 				else if (n == ip->top_margin - 1)
1805 					/* allow scrolling outside region, but don't scroll out
1806 					   of active region without explicit CUP */
1807 					n = ip->top_margin;
1808 				ip->cury = n;
1809 				ip->escape = 0;
1810 				SUBR_CURSOR(ip, MOVE_CURSOR);
1811 				clr_attr (ip, ATTR_INV);
1812 				return;
1813 
1814 			case 'B':
1815 				/* cursor down */
1816 				n = ite_argnum (ip);
1817 				n = ip->cury + (n ? n : 1);
1818 				n = min(ip->rows - 1, n);
1819 #if 0
1820 				if (ip->inside_margins)
1821 #endif
1822 					n = min(ip->bottom_margin, n);
1823 #if 0
1824 				else if (n == ip->bottom_margin + 1)
1825 					/* allow scrolling outside region, but don't scroll out
1826 					   of active region without explicit CUP */
1827 					n = ip->bottom_margin;
1828 #endif
1829 				ip->cury = n;
1830 				ip->escape = 0;
1831 				SUBR_CURSOR(ip, MOVE_CURSOR);
1832 				clr_attr (ip, ATTR_INV);
1833 				return;
1834 
1835 			case 'C':
1836 				/* cursor forward */
1837 				n = ite_argnum (ip);
1838 				n = n ? n : 1;
1839 				ip->curx = min(ip->curx + n, ip->cols - 1);
1840 				ip->escape = 0;
1841 				SUBR_CURSOR(ip, MOVE_CURSOR);
1842 				clr_attr (ip, ATTR_INV);
1843 				return;
1844 
1845 			case 'D':
1846 				/* cursor backward */
1847 				n = ite_argnum (ip);
1848 				n = n ? n : 1;
1849 				n = ip->curx - n;
1850 				ip->curx = n >= 0 ? n : 0;
1851 				ip->escape = 0;
1852 				SUBR_CURSOR(ip, MOVE_CURSOR);
1853 				clr_attr (ip, ATTR_INV);
1854 				return;
1855 
1856 
1857 			case 'J':
1858 				/* erase screen */
1859 				*ip->ap = 0;
1860 				n = ite_zargnum (ip);
1861 				if (n == 0)
1862 					ite_clrtoeos(ip);
1863 				else if (n == 1)
1864 					ite_clrtobos(ip);
1865 				else if (n == 2)
1866 					ite_clrscreen(ip);
1867 				ip->escape = 0;
1868 				return;
1869 
1870 
1871 			case 'K':
1872 				/* erase line */
1873 				n = ite_zargnum (ip);
1874 				if (n == 0)
1875 					ite_clrtoeol(ip);
1876 				else if (n == 1)
1877 					ite_clrtobol(ip);
1878 				else if (n == 2)
1879 					ite_clrline(ip);
1880 				ip->escape = 0;
1881 				return;
1882 
1883 			case 'S':
1884 				/* scroll up */
1885 				n = ite_zargnum (ip);
1886 				if (n <= 0)
1887 					n = 1;
1888 				else if (n > ip->rows-1)
1889 					n = ip->rows-1;
1890 				SUBR_SCROLL(ip, ip->rows-1, 0, n, SCROLL_UP);
1891 				ip->escape = 0;
1892 				return;
1893 
1894 			case 'T':
1895 				/* scroll down */
1896 				n = ite_zargnum (ip);
1897 				if (n <= 0)
1898 					n = 1;
1899 				else if (n > ip->rows-1)
1900 					n = ip->rows-1;
1901 				SUBR_SCROLL(ip, 0, 0, n, SCROLL_DOWN);
1902 				ip->escape = 0;
1903 				return;
1904 
1905 			case 'X':
1906 				/* erase character */
1907 				n = ite_argnum(ip) - 1;
1908 				n = min(n, ip->cols - 1 - ip->curx);
1909 				for (; n >= 0; n--) {
1910 					attrclr(ip, ip->cury, ip->curx + n, 1, 1);
1911 					SUBR_PUTC(ip, ' ', ip->cury, ip->curx + n, ATTR_NOR);
1912 				}
1913 				ip->escape = 0;
1914 				return;
1915 
1916 
1917 			case '}': case '`':
1918 				/* status line control */
1919 				ip->escape = 0;
1920 				return;
1921 
1922 			case 'r':
1923 				/* set scrolling region */
1924 				ip->escape = 0;
1925 				*ip->ap = 0;
1926 				x = atoi (ip->argbuf);
1927 				x = x ? x : 1;
1928 				y = ip->rows;
1929 				cp = index (ip->argbuf, ';');
1930 				if (cp) {
1931 					y = atoi (cp + 1);
1932 					y = y ? y : ip->rows;
1933 				}
1934 				if (y <= x)
1935 					return;
1936 				x--;
1937 				y--;
1938 				ip->top_margin = min(x, ip->rows - 2);
1939 				ip->bottom_margin = min(y, ip->rows - 1);
1940 				if (ip->inside_margins) {
1941 					ip->cury = ip->top_margin;
1942 				} else
1943 					ip->cury = 0;
1944 				ip->curx = 0;
1945 				return;
1946 
1947 
1948 			case 'm':
1949 				/* big attribute setter/resetter */
1950 			{
1951 				char *cp;
1952 				*ip->ap = 0;
1953 				/* kludge to make CSIm work (== CSI0m) */
1954 				if (ip->ap == ip->argbuf)
1955 					ip->ap++;
1956 				for (cp = ip->argbuf; cp < ip->ap; ) {
1957 					switch (*cp) {
1958 					case 0:
1959 					case '0':
1960 						clr_attr (ip, ATTR_ALL);
1961 						ip->fgcolor = 7;
1962 						ip->bgcolor = 0;
1963 						cp++;
1964 						break;
1965 
1966 					case '1':
1967 						set_attr (ip, ATTR_BOLD);
1968 						cp++;
1969 						break;
1970 
1971 					case '2':
1972 						switch (cp[1]) {
1973 						case '2':
1974 							clr_attr (ip, ATTR_BOLD);
1975 							cp += 2;
1976 							break;
1977 
1978 						case '4':
1979 							clr_attr (ip, ATTR_UL);
1980 							cp += 2;
1981 							break;
1982 
1983 						case '5':
1984 							clr_attr (ip, ATTR_BLINK);
1985 							cp += 2;
1986 							break;
1987 
1988 						case '7':
1989 							clr_attr (ip, ATTR_INV);
1990 							cp += 2;
1991 							break;
1992 
1993 						default:
1994 							cp++;
1995 							break;
1996 						}
1997 						break;
1998 
1999 					case '3':
2000 						switch (cp[1]) {
2001 						case '0': case '1': case '2': case '3':
2002 						case '4': case '5': case '6': case '7':
2003 							/* foreground colors */
2004 							ip->fgcolor = cp[1] - '0';
2005 							cp += 2;
2006 							break;
2007 						default:
2008 							cp++;
2009 							break;
2010 						}
2011 						break;
2012 
2013 					case '4':
2014 						switch (cp[1]) {
2015 						case '0': case '1': case '2': case '3':
2016 						case '4': case '5': case '6': case '7':
2017 							/* background colors */
2018 							ip->bgcolor = cp[1] - '0';
2019 							cp += 2;
2020 							break;
2021 						default:
2022 							set_attr (ip, ATTR_UL);
2023 							cp++;
2024 							break;
2025 						}
2026 						break;
2027 
2028 					case '5':
2029 						set_attr (ip, ATTR_BLINK);
2030 						cp++;
2031 						break;
2032 
2033 					case '7':
2034 						set_attr (ip, ATTR_INV);
2035 						cp++;
2036 						break;
2037 
2038 					default:
2039 						cp++;
2040 						break;
2041 					}
2042 				}
2043 
2044 			}
2045 				ip->escape = 0;
2046 				return;
2047 
2048 
2049 			case 'u':
2050 				/* DECRQTSR */
2051 				ite_sendstr (ip, "\033P\033\\");
2052 				ip->escape = 0;
2053 				return;
2054 
2055 			default:
2056 				ip->escape = 0;
2057 				return;
2058 			}
2059 			break;
2060 
2061 
2062 
2063 		case '?':	/* CSI ? */
2064 			switch (c) {
2065 			case '0': case '1': case '2': case '3': case '4':
2066 			case '5': case '6': case '7': case '8': case '9':
2067 			case ';': case '\"': case '$':
2068 				/* Don't fill the last character; it's needed.  */
2069 				/* XXX yeah, where ?? */
2070 				if (ip->ap < ip->argbuf + MAX_ARGSIZE - 1)
2071 					*ip->ap++ = c;
2072 				return;
2073 
2074 
2075 			case 'n':
2076 				/* Terminal Reports */
2077 				*ip->ap = 0;
2078 				if (ip->ap == &ip->argbuf[2]) {
2079 					if (!strncmp(ip->argbuf, "15", 2))
2080 						/* printer status: no printer */
2081 						ite_sendstr (ip, "\033[13n");
2082 
2083 					else if (!strncmp(ip->argbuf, "25", 2))
2084 						/* udk status */
2085 						ite_sendstr (ip, "\033[20n");
2086 
2087 					else if (!strncmp(ip->argbuf, "26", 2))
2088 						/* keyboard dialect: US */
2089 						ite_sendstr (ip, "\033[27;1n");
2090 				}
2091 				ip->escape = 0;
2092 				return;
2093 
2094 
2095 			case 'h': /* set dec private modes */
2096 			case 'l': /* reset dec private modes */
2097 				n = ite_zargnum (ip);
2098 				switch (n) {
2099 				case 1:
2100 					/* CKM - cursor key mode */
2101 					ip->cursor_appmode = (c == 'h');
2102 					break;
2103 
2104 				case 3:
2105 					/* 132/80 columns (132 == 'h') */
2106 					break;
2107 
2108 				case 4: /* smooth scroll */
2109 					break;
2110 
2111 				case 5:
2112 					/* light background (=='h') /dark background(=='l') */
2113 					break;
2114 
2115 				case 6: /* origin mode */
2116 					ip->inside_margins = (c == 'h');
2117 #if 0
2118 					ip->curx = 0;
2119 					ip->cury = ip->inside_margins ? ip->top_margin : 0;
2120 					SUBR_CURSOR(ip, MOVE_CURSOR);
2121 #endif
2122 					break;
2123 
2124 				case 7: /* auto wraparound */
2125 					ip->auto_wrap = (c == 'h');
2126 					break;
2127 
2128 				case 8: /* keyboard repeat */
2129 					ip->key_repeat = (c == 'h');
2130 					break;
2131 
2132 				case 20: /* newline mode */
2133 					ip->linefeed_newline = (c == 'h');
2134 					break;
2135 
2136 				case 25: /* cursor on/off */
2137 					SUBR_CURSOR(ip, (c == 'h') ? DRAW_CURSOR : ERASE_CURSOR);
2138 					break;
2139 				}
2140 				ip->escape = 0;
2141 				return;
2142 
2143 			case 'K':
2144 				/* selective erase in line */
2145 			case 'J':
2146 				/* selective erase in display */
2147 
2148 			default:
2149 				ip->escape = 0;
2150 				return;
2151 			}
2152 			break;
2153 
2154 
2155 		default:
2156 			ip->escape = 0;
2157 			return;
2158 		}
2159 	}
2160 
2161 	switch (c) {
2162 	case 0x00:	/* NUL */
2163 	case 0x01:	/* SOH */
2164 	case 0x02:	/* STX */
2165 	case 0x03:	/* ETX */
2166 	case 0x04:	/* EOT */
2167 	case 0x05:	/* ENQ */
2168 	case 0x06:	/* ACK */
2169 		break;
2170 
2171 	case BEL:
2172 #if NBELL > 0
2173 		if (kbd_ite && ite_tty[kbd_ite->device.dv_unit])
2174 			opm_bell();
2175 #endif
2176 		break;
2177 
2178 	case BS:
2179 		if (--ip->curx < 0)
2180 			ip->curx = 0;
2181 		else
2182 			SUBR_CURSOR(ip, MOVE_CURSOR);
2183 		break;
2184 
2185 	case HT:
2186 		for (n = ip->curx + 1; n < ip->cols; n++) {
2187 			if (ip->tabs[n]) {
2188 				ip->curx = n;
2189 				SUBR_CURSOR(ip, MOVE_CURSOR);
2190 				break;
2191 			}
2192 		}
2193 		break;
2194 
2195 	case VT:	/* VT is treated like LF */
2196 	case FF:	/* so is FF */
2197 	case LF:
2198 		/* cr->crlf distinction is done here, on output,
2199 		   not on input! */
2200 		if (ip->linefeed_newline)
2201 			ite_crlf (ip);
2202 		else
2203 			ite_lf (ip);
2204 		break;
2205 
2206 	case CR:
2207 		ite_cr (ip);
2208 		break;
2209 
2210 
2211 	case SO:
2212 		ip->GL = &ip->G1;
2213 		break;
2214 
2215 	case SI:
2216 		ip->GL = &ip->G0;
2217 		break;
2218 
2219 	case 0x10:	/* DLE */
2220 	case 0x11:	/* DC1/XON */
2221 	case 0x12:	/* DC2 */
2222 	case 0x13:	/* DC3/XOFF */
2223 	case 0x14:	/* DC4 */
2224 	case 0x15:	/* NAK */
2225 	case 0x16:	/* SYN */
2226 	case 0x17:	/* ETB */
2227 		break;
2228 
2229 	case CAN:
2230 		ip->escape = 0;	/* cancel any escape sequence in progress */
2231 		break;
2232 
2233 	case 0x19:	/* EM */
2234 		break;
2235 
2236 	case SUB:
2237 		ip->escape = 0;	/* dito, but see below */
2238 		/* should also display a reverse question mark!! */
2239 		break;
2240 
2241 	case ESC:
2242 		ip->escape = ESC;
2243 		break;
2244 
2245 	case 0x1c:	/* FS */
2246 	case 0x1d:	/* GS */
2247 	case 0x1e:	/* RS */
2248 	case 0x1f:	/* US */
2249 		break;
2250 
2251 	/* now it gets weird.. 8bit control sequences.. */
2252 	case IND:	/* index: move cursor down, scroll */
2253 		ite_index (ip);
2254 		break;
2255 
2256 	case NEL:	/* next line. next line, first pos. */
2257 		ite_crlf (ip);
2258 		break;
2259 
2260 	case HTS:	/* set horizontal tab */
2261 		if (ip->curx < ip->cols)
2262 			ip->tabs[ip->curx] = 1;
2263 		break;
2264 
2265 	case RI:	/* reverse index */
2266 		ite_rlf (ip);
2267 		break;
2268 
2269 	case SS2:	/* go into G2 for one character */
2270 		ip->save_GL = ip->GR;	/* GL XXX EUC */
2271 		ip->GR = &ip->G2;	/* GL XXX */
2272 		break;
2273 
2274 	case SS3:	/* go into G3 for one character */
2275 		ip->save_GL = ip->GR;	/* GL XXX EUC */
2276 		ip->GR = &ip->G3;	/* GL XXX */
2277 		break;
2278 
2279 	case DCS:	/* device control string introducer */
2280 		ip->escape = DCS;
2281 		ip->ap = ip->argbuf;
2282 		break;
2283 
2284 	case CSI:	/* control sequence introducer */
2285 		ip->escape = CSI;
2286 		ip->ap = ip->argbuf;
2287 		break;
2288 
2289 	case ST:	/* string terminator */
2290 		/* ignore, if not used as terminator */
2291 		break;
2292 
2293 	case OSC:	/* introduces OS command. Ignore everything upto ST */
2294 		ip->escape = OSC;
2295 		break;
2296 
2297 	case PM:	/* privacy message, ignore everything upto ST */
2298 		ip->escape = PM;
2299 		break;
2300 
2301 	case APC:	/* application program command, ignore everything upto ST */
2302 		ip->escape = APC;
2303 		break;
2304 
2305 	case DEL:
2306 		break;
2307 
2308 	default:
2309 		if (!ip->save_char && (*((c & 0x80) ? ip->GR : ip->GL) & CSET_MULTI)) {
2310 			ip->save_char = c;
2311 			break;
2312 		}
2313 		if (ip->imode)
2314 			ite_inchar(ip, ip->save_char ? 2 : 1);
2315 		iteprecheckwrap(ip);
2316 #ifdef DO_WEIRD_ATTRIBUTES
2317 		if ((ip->attribute & ATTR_INV) || attrtest(ip, ATTR_INV)) {
2318 			attrset(ip, ATTR_INV);
2319 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_INV);
2320 		}
2321 		else
2322 			SUBR_PUTC(ip, c, ip->cury, ip->curx, ATTR_NOR);
2323 #else
2324 		SUBR_PUTC(ip, c, ip->cury, ip->curx, ip->attribute);
2325 #endif
2326 /*		SUBR_CURSOR(ip, DRAW_CURSOR);*/
2327 		itecheckwrap(ip);
2328 		if (ip->save_char) {
2329 			itecheckwrap(ip);
2330 			ip->save_char = 0;
2331 		}
2332 		if (ip->save_GL) {
2333 			/*
2334 			 * reset single shift
2335 			 */
2336 			ip->GR = ip->save_GL;
2337 			ip->save_GL = 0;
2338 		}
2339 		break;
2340 	}
2341 }
2342 
2343 static void
2344 iteprecheckwrap(ip)
2345 	struct ite_softc *ip;
2346 {
2347 	if (ip->auto_wrap && ip->curx + (ip->save_char ? 1 : 0) == ip->cols) {
2348 		ip->curx = 0;
2349 		clr_attr(ip, ATTR_INV);
2350 		if (++ip->cury >= ip->bottom_margin + 1) {
2351 			ip->cury = ip->bottom_margin;
2352 			/*SUBR_CURSOR(ip, MOVE_CURSOR);*/
2353 			SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
2354 			ite_clrtoeol(ip);
2355 		} /*else
2356 			SUBR_CURSOR(ip, MOVE_CURSOR);*/
2357 	}
2358 }
2359 
2360 static void
2361 itecheckwrap(ip)
2362      struct ite_softc *ip;
2363 {
2364 #if 0
2365 	if (++ip->curx == ip->cols) {
2366 		if (ip->auto_wrap) {
2367 			ip->curx = 0;
2368 			clr_attr(ip, ATTR_INV);
2369 			if (++ip->cury >= ip->bottom_margin + 1) {
2370 				ip->cury = ip->bottom_margin;
2371 				SUBR_CURSOR(ip, MOVE_CURSOR);
2372 				SUBR_SCROLL(ip, ip->top_margin + 1, 0, 1, SCROLL_UP);
2373 				ite_clrtoeol(ip);
2374 				return;
2375 			}
2376 		} else
2377 			/* stay there if no autowrap.. */
2378 			ip->curx--;
2379 	}
2380 #else
2381 	if (ip->curx < ip->cols) {
2382 		ip->curx++;
2383 		/*SUBR_CURSOR(ip, MOVE_CURSOR);*/
2384 	}
2385 #endif
2386 }
2387 
2388 #endif
2389 
2390 #if NITE > 0 && NKBD > 0
2391 
2392 /*
2393  * Console functions
2394  */
2395 #include <dev/cons.h>
2396 extern void kbdenable __P((int));
2397 extern int kbdcngetc __P((void));
2398 
2399 /*
2400  * Return a priority in consdev->cn_pri field highest wins.  This function
2401  * is called before any devices have been probed.
2402  */
2403 void
2404 itecnprobe(cd)
2405 	struct consdev *cd;
2406 {
2407 	int maj;
2408 
2409 	/* locate the major number */
2410 	for (maj = 0; maj < nchrdev; maj++)
2411 		if (cdevsw[maj].d_open == iteopen)
2412 			break;
2413 
2414 	/*
2415 	 * return priority of the best ite (already picked from attach)
2416 	 * or CN_DEAD.
2417 	 */
2418 	if (con_itesoftc.grf == NULL)
2419 		cd->cn_pri = CN_DEAD;
2420 	else {
2421 		con_itesoftc.flags = (ITE_ALIVE|ITE_CONSOLE);
2422 		/*
2423 		 * hardcode the minor number.
2424 		 * currently we support only one ITE, it is enough for now.
2425 		 */
2426 		con_itesoftc.isw = &itesw[0];
2427 		cd->cn_pri = CN_INTERNAL;
2428 		cd->cn_dev = makedev(maj, 0);
2429 	}
2430 
2431 }
2432 
2433 void
2434 itecninit(cd)
2435 	struct consdev *cd;
2436 {
2437 	struct ite_softc *ip;
2438 
2439 	ip = getitesp(cd->cn_dev);
2440 	iteinit(cd->cn_dev);	       /* init console unit */
2441 	ip->flags |= ITE_ACTIVE | ITE_ISCONS;
2442 	kbdenable(0);
2443 	mfp_send_usart(0x49);
2444 }
2445 
2446 /*
2447  * itecnfinish() is called in ite_init() when the device is
2448  * being probed in the normal fasion, thus we can finish setting
2449  * up this ite now that the system is more functional.
2450  */
2451 void
2452 itecnfinish(ip)
2453 	struct ite_softc *ip;
2454 {
2455 	static int done;
2456 
2457 	if (done)
2458 		return;
2459 	done = 1;
2460 }
2461 
2462 /*ARGSUSED*/
2463 int
2464 itecngetc(dev)
2465 	dev_t dev;
2466 {
2467 	register int c;
2468 
2469 	do {
2470 		c = kbdcngetc();
2471 		c = ite_cnfilter(c);
2472 	} while (c == -1);
2473 	return (c);
2474 }
2475 
2476 void
2477 itecnputc(dev, c)
2478 	dev_t dev;
2479 	int c;
2480 {
2481 	static int paniced = 0;
2482 	struct ite_softc *ip = getitesp(dev);
2483 	char ch = c;
2484 #ifdef ITE_KERNEL_ATTR
2485 	short save_attribute;
2486 #endif
2487 
2488 	if (panicstr && !paniced &&
2489 	    (ip->flags & (ITE_ACTIVE|ITE_INGRF)) != ITE_ACTIVE) {
2490 		(void) iteon(dev, 3);
2491 		paniced = 1;
2492 	}
2493 #ifdef ITE_KERNEL_ATTR
2494 	save_attribute = ip->attribute;
2495 	ip->attribute = ITE_KERNEL_ATTR;
2496 #endif
2497 	ite_putstr(&ch, 1, dev);
2498 #ifdef ITE_KERNEL_ATTR
2499 	ip->attribute = save_attribute;
2500 #endif
2501 }
2502 #endif
2503