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