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