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