xref: /netbsd-src/sys/arch/atari/dev/ite.c (revision e4d7c2e329d54c97e0c0bd3016bbe74f550c3d5e)
1 /*	$NetBSD: ite.c,v 1.28 2000/02/11 21:42:52 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1988 University of Utah.
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * the Systems Programming Group of the University of Utah Computer
10  * Science Department.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the University of
23  *      California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *      from: Utah Hdr: ite.c 1.1 90/07/09
41  *      from: @(#)ite.c 7.6 (Berkeley) 5/16/91
42  */
43 
44 /*
45  * ite - bitmapped terminal.
46  * Supports VT200, a few terminal features will be unavailable until
47  * the system actually probes the device (i.e. not after consinit())
48  */
49 
50 #include "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 /*
94  * Patchable
95  */
96 int ite_default_x      = 0;	/* def leftedge offset			*/
97 int ite_default_y      = 0;	/* def topedge offset			*/
98 int ite_default_width  = 640;	/* def width				*/
99 int ite_default_depth  = 1;	/* def depth				*/
100 int ite_default_height = 400;	/* def height				*/
101 int ite_default_wrap   = 1;	/* if you want vtxxx-nam -> binpatch	*/
102 
103 struct	ite_softc con_itesoftc;
104 u_char	cons_tabs[MAX_TABS];
105 
106 struct ite_softc *kbd_ite;
107 int kbd_init;
108 
109 static __inline__ int  atoi __P((const char *));
110 static __inline__ int  ite_argnum __P((struct ite_softc *));
111 static __inline__ int  ite_zargnum __P((struct ite_softc *));
112 static __inline__ void ite_cr __P((struct ite_softc *));
113 static __inline__ void ite_crlf __P((struct ite_softc *));
114 static __inline__ void ite_clrline __P((struct ite_softc *));
115 static __inline__ void ite_clrscreen __P((struct ite_softc *));
116 static __inline__ void ite_clrtobos __P((struct ite_softc *));
117 static __inline__ void ite_clrtobol __P((struct ite_softc *));
118 static __inline__ void ite_clrtoeol __P((struct ite_softc *));
119 static __inline__ void ite_clrtoeos __P((struct ite_softc *));
120 static __inline__ void ite_dnchar __P((struct ite_softc *, int));
121 static __inline__ void ite_inchar __P((struct ite_softc *, int));
122 static __inline__ void ite_inline __P((struct ite_softc *, int));
123 static __inline__ void ite_lf __P((struct ite_softc *));
124 static __inline__ void ite_dnline __P((struct ite_softc *, int));
125 static __inline__ void ite_rlf __P((struct ite_softc *));
126 static __inline__ void ite_sendstr __P((char *));
127 static __inline__ void snap_cury __P((struct ite_softc *));
128 
129 static void	alignment_display __P((struct ite_softc *));
130 static char	*index __P((const char *, int));
131 static struct ite_softc *getitesp __P((dev_t));
132 static void	itecheckwrap __P((struct ite_softc *));
133 static void	iteprecheckwrap __P((struct ite_softc *));
134 static void	itestart __P((struct tty *));
135 static void	ite_switch __P((int));
136 static void	repeat_handler __P((void *));
137 
138 void iteputchar __P((int c, struct ite_softc *ip));
139 void ite_putstr __P((const u_char * s, int len, dev_t dev));
140 void iteattach __P((struct device *, struct device *, void *));
141 int  itematch __P((struct device *, struct cfdata *, void *));
142 
143 /*
144  * Console specific types.
145  */
146 dev_type_cnprobe(itecnprobe);
147 dev_type_cninit(itecninit);
148 dev_type_cngetc(itecngetc);
149 dev_type_cnputc(itecnputc);
150 
151 struct cfattach ite_ca = {
152 	sizeof(struct ite_softc), itematch, iteattach
153 };
154 
155 extern struct cfdriver ite_cd;
156 
157 int
158 itematch(pdp, cfp, auxp)
159 	struct device	*pdp;
160 	struct cfdata	*cfp;
161 	void		*auxp;
162 {
163 	struct grf_softc *gp  = auxp;
164 	int		 maj;
165 
166 	/*
167 	 * all that our mask allows (more than enough no one
168 	 * has > 32 monitors for text consoles on one machine)
169 	 */
170 	if (cfp->cf_unit >= sizeof(ite_confunits) * NBBY)
171 		return(0);
172 	/*
173 	 * XXX
174 	 * normally this would be done in attach, however
175 	 * during early init we do not have a device pointer
176 	 * and thus no unit number.
177 	 */
178 	for(maj = 0; maj < nchrdev; maj++)
179 		if (cdevsw[maj].d_open == iteopen)
180 			break;
181 
182 	gp->g_itedev = makedev(maj, cfp->cf_unit);
183 	return(1);
184 }
185 
186 void
187 iteattach(pdp, dp, auxp)
188 struct device	*pdp, *dp;
189 void		*auxp;
190 {
191 	struct grf_softc	*gp;
192 	struct ite_softc	*ip;
193 	int			s;
194 
195 	gp = (struct grf_softc *)auxp;
196 
197 	/*
198 	 * mark unit as attached (XXX see itematch)
199 	 */
200 	ite_confunits |= 1 << ITEUNIT(gp->g_itedev);
201 
202 	if(dp) {
203 		ip = (struct ite_softc *)dp;
204 
205 		s = spltty();
206 		if(con_itesoftc.grf != NULL
207 			&& con_itesoftc.grf->g_unit == gp->g_unit) {
208 			/*
209 			 * console reinit copy params over.
210 			 * and console always gets keyboard
211 			 */
212 			bcopy(&con_itesoftc.grf, &ip->grf,
213 			    (char *)&ip[1] - (char *)&ip->grf);
214 			con_itesoftc.grf = NULL;
215 			kbd_ite = ip;
216 		}
217 		ip->grf = gp;
218 		splx(s);
219 
220 		iteinit(gp->g_itedev);
221 		printf(": %dx%d", ip->rows, ip->cols);
222 		printf(" repeat at (%d/100)s next at (%d/100)s",
223 		    start_repeat_timeo, next_repeat_timeo);
224 
225 		if (kbd_ite == NULL)
226 			kbd_ite = ip;
227 		if (kbd_ite == ip)
228 			printf(" has keyboard");
229 		printf("\n");
230 	} else {
231 		if (con_itesoftc.grf != NULL &&
232 		    con_itesoftc.grf->g_conpri > gp->g_conpri)
233 			return;
234 		con_itesoftc.grf = gp;
235 		con_itesoftc.tabs = cons_tabs;
236 	}
237 }
238 
239 static struct ite_softc *
240 getitesp(dev)
241 	dev_t dev;
242 {
243 	if(atari_realconfig && (con_itesoftc.grf == NULL))
244 		return(ite_cd.cd_devs[ITEUNIT(dev)]);
245 
246 	if(con_itesoftc.grf == NULL)
247 		panic("no ite_softc for console");
248 	return(&con_itesoftc);
249 }
250 
251 /*
252  * cons.c entry points into ite device.
253  */
254 
255 /*
256  * Return a priority in consdev->cn_pri field highest wins.  This function
257  * is called before any devices have been probed.
258  */
259 void
260 itecnprobe(cd)
261 	struct consdev *cd;
262 {
263 	/*
264 	 * bring graphics layer up.
265 	 */
266 	config_console();
267 
268 	/*
269 	 * return priority of the best ite (already picked from attach)
270 	 * or CN_DEAD.
271 	 */
272 	if (con_itesoftc.grf == NULL)
273 		cd->cn_pri = CN_DEAD;
274 	else {
275 		cd->cn_pri = con_itesoftc.grf->g_conpri;
276 		cd->cn_dev = con_itesoftc.grf->g_itedev;
277 	}
278 }
279 
280 void
281 itecninit(cd)
282 	struct consdev *cd;
283 {
284 	struct ite_softc *ip;
285 
286 	ip = getitesp(cd->cn_dev);
287 	ip->flags |= ITE_ISCONS;
288 	iteinit(cd->cn_dev);
289 	ip->flags |= ITE_ACTIVE | ITE_ISCONS;
290 }
291 
292 /*
293  * ite_cnfinish() is called in ite_init() when the device is
294  * being probed in the normal fasion, thus we can finish setting
295  * up this ite now that the system is more functional.
296  */
297 void
298 ite_cnfinish(ip)
299 	struct ite_softc *ip;
300 {
301 	static int done;
302 
303 	if (done)
304 		return;
305 	done = 1;
306 }
307 
308 int
309 itecngetc(dev)
310 	dev_t dev;
311 {
312 	int c;
313 
314 	do {
315 		c = kbdgetcn();
316 		c = ite_cnfilter(c, ITEFILT_CONSOLE);
317 	} while (c == -1);
318 	return (c);
319 }
320 
321 void
322 itecnputc(dev, c)
323 	dev_t dev;
324 	int c;
325 {
326 	static int paniced;
327 	struct ite_softc *ip;
328 	char ch;
329 
330 	ip = getitesp(dev);
331 	ch = c;
332 
333 	if (panicstr && !paniced &&
334 	    (ip->flags & (ITE_ACTIVE | ITE_INGRF)) != ITE_ACTIVE) {
335 		(void)ite_on(dev, 3);
336 		paniced = 1;
337 	}
338 	SUBR_CURSOR(ip, START_CURSOROPT);
339 	iteputchar(ch, ip);
340 	SUBR_CURSOR(ip, END_CURSOROPT);
341 }
342 
343 /*
344  * standard entry points to the device.
345  */
346 
347 /*
348  * iteinit() is the standard entry point for initialization of
349  * an ite device, it is also called from itecninit().
350  *
351  */
352 void
353 iteinit(dev)
354 	dev_t dev;
355 {
356 	struct ite_softc	*ip;
357 
358 	ip = getitesp(dev);
359 	if (ip->flags & ITE_INITED)
360 		return;
361 	if (atari_realconfig) {
362 		if (ip->kbdmap && ip->kbdmap != &ascii_kbdmap)
363 			free(ip->kbdmap, M_DEVBUF);
364 		ip->kbdmap = malloc(sizeof(struct kbdmap), M_DEVBUF, M_WAITOK);
365 		bcopy(&ascii_kbdmap, ip->kbdmap, sizeof(struct kbdmap));
366 	}
367 	else ip->kbdmap = &ascii_kbdmap;
368 
369 	ip->cursorx = 0;
370 	ip->cursory = 0;
371 	SUBR_INIT(ip);
372 	SUBR_CURSOR(ip, DRAW_CURSOR);
373 	if (ip->tabs == NULL)
374 		ip->tabs = malloc(MAX_TABS * sizeof(u_char),M_DEVBUF,M_WAITOK);
375 	ite_reset(ip);
376 	ip->flags |= ITE_INITED;
377 }
378 
379 int
380 iteopen(dev, mode, devtype, p)
381 	dev_t dev;
382 	int mode, devtype;
383 	struct proc *p;
384 {
385 	struct ite_softc *ip;
386 	struct tty *tp;
387 	int error, first, unit;
388 
389 	unit = ITEUNIT(dev);
390 	first = 0;
391 
392 	if (((1 << unit) & ite_confunits) == 0)
393 		return (ENXIO);
394 
395 	ip = getitesp(dev);
396 
397 	if (ip->tp == NULL) {
398 		tp = ip->tp = ttymalloc();
399 		tty_attach(tp);
400 	}
401 	else tp = ip->tp;
402 
403 	if ((tp->t_state & (TS_ISOPEN | TS_XCLUDE)) == (TS_ISOPEN | TS_XCLUDE)
404 	    && p->p_ucred->cr_uid != 0)
405 		return (EBUSY);
406 	if ((ip->flags & ITE_ACTIVE) == 0) {
407 		error = ite_on(dev, 0);
408 		if (error)
409 			return (error);
410 		first = 1;
411 	}
412 	if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
413 		tp->t_oproc = itestart;
414 		tp->t_param = ite_param;
415 		tp->t_dev = dev;
416 		tp->t_iflag = TTYDEF_IFLAG;
417 		tp->t_oflag = TTYDEF_OFLAG;
418 		tp->t_cflag = TTYDEF_CFLAG;
419 		tp->t_lflag = TTYDEF_LFLAG;
420 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
421 		tp->t_state = TS_CARR_ON;
422 		ttychars(tp);
423 		ttsetwater(tp);
424 	}
425 
426 
427 	error = ttyopen(tp, 0, (mode & O_NONBLOCK) ? 1 : 0);
428 	if (error)
429 		goto bad;
430 
431 	error = (*linesw[tp->t_line].l_open) (dev, tp);
432 	if (error)
433 		goto bad;
434 
435 	tp->t_winsize.ws_row = ip->rows;
436 	tp->t_winsize.ws_col = ip->cols;
437 	if (!kbd_init) {
438 		kbd_init = 1;
439 		kbdenable();
440 	}
441 	return (0);
442 
443 
444 bad:
445 	if (first)
446 		ite_off(dev, 0);
447 
448 	return (error);
449 }
450 
451 int
452 iteclose(dev, flag, mode, p)
453 	dev_t dev;
454 	int flag, mode;
455 	struct proc *p;
456 {
457 	struct tty *tp;
458 
459 	tp = getitesp(dev)->tp;
460 
461 	KDASSERT(tp);
462 	(*linesw[tp->t_line].l_close) (tp, flag);
463 	ttyclose(tp);
464 	ite_off(dev, 0);
465 	return (0);
466 }
467 
468 int
469 iteread(dev, uio, flag)
470 	dev_t dev;
471 	struct uio *uio;
472 	int flag;
473 {
474 	struct tty *tp;
475 
476 	tp = getitesp(dev)->tp;
477 
478 	KDASSERT(tp);
479 	return ((*linesw[tp->t_line].l_read) (tp, uio, flag));
480 }
481 
482 int
483 itewrite(dev, uio, flag)
484 	dev_t dev;
485 	struct uio *uio;
486 	int flag;
487 {
488 	struct tty *tp;
489 
490 	tp = getitesp(dev)->tp;
491 
492 	KDASSERT(tp);
493 	return ((*linesw[tp->t_line].l_write) (tp, uio, flag));
494 }
495 
496 void
497 itestop(tp, flag)
498 	struct tty *tp;
499 	int flag;
500 {
501 }
502 
503 struct tty *
504 itetty(dev)
505 	dev_t	dev;
506 {
507 	return(getitesp(dev)->tp);
508 }
509 
510 int
511 iteioctl(dev, cmd, addr, flag, p)
512 	dev_t		dev;
513 	u_long		cmd;
514 	int		flag;
515 	caddr_t		addr;
516 	struct proc	*p;
517 {
518 	struct iterepeat	*irp;
519 	struct ite_softc	*ip;
520 	struct tty		*tp;
521 	view_t			*view;
522 	struct itewinsize	*is;
523 	struct itebell		*ib;
524 	int error;
525 
526 	ip   = getitesp(dev);
527 	tp   = ip->tp;
528 	view = viewview(ip->grf->g_viewdev);
529 
530 	KDASSERT(tp);
531 
532 	error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, addr, flag, p);
533 	if(error >= 0)
534 		return (error);
535 	error = ttioctl(tp, cmd, addr, flag, p);
536 	if (error >= 0)
537 		return (error);
538 
539 	switch (cmd) {
540 	case ITEIOCSKMAP:
541 		if (addr == NULL)
542 			return(EFAULT);
543 		bcopy(addr, ip->kbdmap, sizeof(struct kbdmap));
544 		return 0;
545 	case ITEIOCSSKMAP:
546 		if (addr == NULL)
547 			return(EFAULT);
548 		bcopy(addr, &ascii_kbdmap, sizeof(struct kbdmap));
549 		return 0;
550 	case ITEIOCGKMAP:
551 		if (addr == NULL)
552 			return(EFAULT);
553 		bcopy(ip->kbdmap, addr, sizeof(struct kbdmap));
554 		return 0;
555 	case ITEIOCGREPT:
556 		if (addr == NULL)
557 			return(EFAULT);
558 		irp = (struct iterepeat *)addr;
559 		irp->start = start_repeat_timeo;
560 		irp->next = next_repeat_timeo;
561 		return 0;
562 	case ITEIOCSREPT:
563 		if (addr == NULL)
564 			return(EFAULT);
565 		irp = (struct iterepeat *)addr;
566 		if (irp->start < ITEMINREPEAT || irp->next < ITEMINREPEAT)
567 			return(EINVAL);
568 		start_repeat_timeo = irp->start;
569 		next_repeat_timeo = irp->next;
570 		return 0;
571 	case ITEIOCGWINSZ:
572 		if (addr == NULL)
573 			return(EFAULT);
574 		is         = (struct itewinsize *)addr;
575 		is->x      = view->display.x;
576 		is->y      = view->display.y;
577 		is->width  = view->display.width;
578 		is->height = view->display.height;
579 		is->depth  = view->bitmap->depth;
580 		return 0;
581 	case ITEIOCDSPWIN:
582 		ip->grf->g_mode(ip->grf, GM_GRFON, NULL, 0, 0);
583 		return 0;
584 	case ITEIOCREMWIN:
585 		ip->grf->g_mode(ip->grf, GM_GRFOFF, NULL, 0, 0);
586 		return 0;
587 	case ITEIOCSBELL:
588 		if (addr == NULL)
589 			return(EFAULT);
590 		ib = (struct itebell *)addr;
591 		kbd_bell_sparms(ib->volume, ib->pitch, ib->msec);
592 		return 0;
593 	case ITEIOCGBELL:
594 		if (addr == NULL)
595 			return(EFAULT);
596 		ib = (struct itebell *)addr;
597 		kbd_bell_gparms(&ib->volume, &ib->pitch, &ib->msec);
598 		return 0;
599 	}
600 	error = (ip->itexx_ioctl)(ip, cmd, addr, flag, p);
601 	if(error >= 0)
602 		return(error);
603 	return (ENOTTY);
604 }
605 
606 void
607 itestart(tp)
608 	struct tty *tp;
609 {
610 	struct clist *rbp;
611 	struct ite_softc *ip;
612 	u_char buf[ITEBURST];
613 	int s, len;
614 
615 	ip = getitesp(tp->t_dev);
616 
617 	KDASSERT(tp);
618 
619 	s = spltty(); {
620 		if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
621 			goto out;
622 
623 		tp->t_state |= TS_BUSY;
624 		rbp = &tp->t_outq;
625 
626 		len = q_to_b(rbp, buf, ITEBURST);
627 	} splx(s);
628 
629 	/* Here is a really good place to implement pre/jumpscroll() */
630 	ite_putstr((char *)buf, len, tp->t_dev);
631 
632 	s = spltty(); {
633 		tp->t_state &= ~TS_BUSY;
634 		/* we have characters remaining. */
635 		if (rbp->c_cc) {
636 			tp->t_state |= TS_TIMEOUT;
637 			timeout(ttrstrt, tp, 1);
638 		}
639 		/* wakeup we are below */
640 		if (rbp->c_cc <= tp->t_lowat) {
641 			if (tp->t_state & TS_ASLEEP) {
642 				tp->t_state &= ~TS_ASLEEP;
643 				wakeup((caddr_t) rbp);
644 			}
645 			selwakeup(&tp->t_wsel);
646 		}
647 	      out: ;
648 	} splx(s);
649 }
650 
651 int
652 ite_on(dev, flag)
653 	dev_t dev;
654 	int flag;
655 {
656 	struct ite_softc *ip;
657 	int unit;
658 
659 	unit = ITEUNIT(dev);
660 	if (((1 << unit) & ite_confunits) == 0)
661 		return (ENXIO);
662 
663 	ip = getitesp(dev);
664 
665 	/* force ite active, overriding graphics mode */
666 	if (flag & 1) {
667 		ip->flags |= ITE_ACTIVE;
668 		ip->flags &= ~(ITE_INGRF | ITE_INITED);
669 	}
670 	/* leave graphics mode */
671 	if (flag & 2) {
672 		ip->flags &= ~ITE_INGRF;
673 		if ((ip->flags & ITE_ACTIVE) == 0)
674 			return (0);
675 	}
676 	ip->flags |= ITE_ACTIVE;
677 	if (ip->flags & ITE_INGRF)
678 		return (0);
679 	iteinit(dev);
680 	return (0);
681 }
682 
683 void
684 ite_off(dev, flag)
685 dev_t	dev;
686 int	flag;
687 {
688 	struct ite_softc *ip;
689 
690 	ip = getitesp(dev);
691 	if (flag & 2)
692 		ip->flags |= ITE_INGRF;
693 	if ((ip->flags & ITE_ACTIVE) == 0)
694 		return;
695 	if ((flag & 1) ||
696 	    (ip->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
697 		SUBR_DEINIT(ip);
698 	if ((flag & 2) == 0)	/* XXX hmm grfon() I think wants this to  go inactive. */
699 		ip->flags &= ~ITE_ACTIVE;
700 }
701 
702 static void
703 ite_switch(unit)
704 int	unit;
705 {
706 	struct ite_softc	*ip;
707 
708 	if(!(ite_confunits & (1 << unit)))
709 		return;	/* Don't try unconfigured units	*/
710 	ip = getitesp(unit);
711 	if(!(ip->flags & ITE_INITED))
712 		return;
713 
714 	/*
715 	 * If switching to an active ite, also switch the keyboard.
716 	 */
717 	if(ip->flags & ITE_ACTIVE)
718 		kbd_ite = ip;
719 
720 	/*
721 	 * Now make it visible
722 	 */
723 	viewioctl(ip->grf->g_viewdev, VIOCDISPLAY, NULL, 0, NOPROC);
724 
725 	/*
726 	 * Make sure the cursor's there too....
727 	 */
728   	SUBR_CURSOR(ip, DRAW_CURSOR);
729 }
730 
731 /* XXX called after changes made in underlying grf layer. */
732 /* I want to nuke this */
733 void
734 ite_reinit(dev)
735 	dev_t dev;
736 {
737 	struct ite_softc *ip;
738 
739 	ip = getitesp(dev);
740 	ip->flags &= ~ITE_INITED;
741 	iteinit(dev);
742 }
743 
744 int
745 ite_param(tp, t)
746 	struct tty *tp;
747 	struct termios *t;
748 {
749 	tp->t_ispeed = t->c_ispeed;
750 	tp->t_ospeed = t->c_ospeed;
751 	tp->t_cflag = t->c_cflag;
752 	return (0);
753 }
754 
755 void
756 ite_reset(ip)
757 	struct ite_softc *ip;
758 {
759 	int i;
760 
761 	ip->curx = 0;
762 	ip->cury = 0;
763 	ip->attribute = ATTR_NOR;
764 	ip->save_curx = 0;
765 	ip->save_cury = 0;
766 	ip->save_attribute = ATTR_NOR;
767 	ip->ap = ip->argbuf;
768 	ip->emul_level = 0;
769 	ip->eightbit_C1 = 0;
770 	ip->top_margin = 0;
771 	ip->bottom_margin = ip->rows - 1;
772 	ip->inside_margins = 0;
773 	ip->linefeed_newline = 0;
774 	ip->auto_wrap = ite_default_wrap;
775 	ip->cursor_appmode = 0;
776 	ip->keypad_appmode = 0;
777 	ip->imode = 0;
778 	ip->key_repeat = 1;
779 	bzero(ip->tabs, ip->cols);
780 	for (i = 0; i < ip->cols; i++)
781 		ip->tabs[i] = ((i & 7) == 0);
782 }
783 
784 /*
785  * has to be global because of the shared filters.
786  */
787 static u_char last_dead;
788 
789 /*
790  * Used in console at startup only and for DDB.
791  */
792 int
793 ite_cnfilter(c, caller)
794 u_int		c;
795 enum caller	caller;
796 {
797 	struct key	key;
798 	struct kbdmap	*kbdmap;
799 	u_char		code, up, mask;
800 	int		s;
801 
802 	up   = KBD_RELEASED(c);
803 	c    = KBD_SCANCODE(c);
804 	code = 0;
805 	mask = 0;
806 	kbdmap = (kbd_ite == NULL) ? &ascii_kbdmap : kbd_ite->kbdmap;
807 
808 	s = spltty();
809 
810 	/*
811 	 * No special action if key released
812 	 */
813 	if(up) {
814 		splx(s);
815 		return -1;
816 	}
817 
818 	/* translate modifiers */
819 	if(kbd_modifier & KBD_MOD_SHIFT) {
820 		if(kbd_modifier & KBD_MOD_ALT)
821 			key = kbdmap->alt_shift_keys[c];
822 		else key = kbdmap->shift_keys[c];
823 	}
824 	else if(kbd_modifier & KBD_MOD_ALT)
825 			key = kbdmap->alt_keys[c];
826 	else {
827 		key = kbdmap->keys[c];
828 		/*
829 		 * If CAPS and key is CAPable (no pun intended)
830 		 */
831 		if((kbd_modifier & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
832 			key = kbdmap->shift_keys[c];
833 	}
834 	code = key.code;
835 
836 #ifdef notyet /* LWP: Didn't have time to look at this yet */
837 	/*
838 	 * If string return simple console filter
839 	 */
840 	if(key->mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
841 		splx(s);
842 		return -1;
843 	}
844 	/* handle dead keys */
845 	if(key->mode & KBD_MODE_DEAD) {
846 		/* if entered twice, send accent itself */
847 		if (last_dead == key->mode & KBD_MODE_ACCMASK)
848 			last_dead = 0;
849 		else {
850 			last_dead = key->mode & KBD_MODE_ACCMASK;
851 			splx(s);
852 			return -1;
853 		}
854 	}
855 	if(last_dead) {
856 		/* can't apply dead flag to string-keys */
857 		if (code >= '@' && code < 0x7f)
858 			code =
859 			    acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
860 		last_dead = 0;
861 	}
862 #endif
863 	if(kbd_modifier & KBD_MOD_CTRL)
864 		code &= 0x1f;
865 
866 	/*
867 	 * Do console mapping.
868 	 */
869 	code = code == '\r' ? '\n' : code;
870 
871 	splx(s);
872 	return (code);
873 }
874 
875 /* And now the old stuff. */
876 
877 /* these are used to implement repeating keys.. */
878 static u_int last_char;
879 static u_char tout_pending;
880 
881 /*ARGSUSED*/
882 static void
883 repeat_handler(arg)
884 void *arg;
885 {
886 	tout_pending = 0;
887 	if(last_char)
888 		add_sicallback((si_farg)ite_filter, (void *)last_char,
889 						    (void *)ITEFILT_REPEATER);
890 }
891 
892 void
893 ite_filter(c, caller)
894 u_int		c;
895 enum caller	caller;
896 {
897 	struct tty	*kbd_tty;
898 	struct kbdmap	*kbdmap;
899 	u_char		code, *str, up, mask;
900 	struct key	key;
901 	int		s, i;
902 
903 	if(kbd_ite == NULL)
904 		return;
905 
906 	kbd_tty = kbd_ite->tp;
907 	kbdmap  = kbd_ite->kbdmap;
908 
909 	up   = KBD_RELEASED(c);
910 	c    = KBD_SCANCODE(c);
911 	code = 0;
912 	mask = 0;
913 
914 	/* have to make sure we're at spltty in here */
915 	s = spltty();
916 
917 	/*
918 	 * keyboard interrupts come at priority 2, while softint
919 	 * generated keyboard-repeat interrupts come at level 1.  So,
920 	 * to not allow a key-up event to get thru before a repeat for
921 	 * the key-down, we remove any outstanding callout requests..
922 	 */
923 	rem_sicallback((si_farg)ite_filter);
924 
925 	/*
926 	 * Stop repeating on up event
927 	 */
928 	if (up) {
929 		if(tout_pending) {
930 			untimeout(repeat_handler, 0);
931 			tout_pending = 0;
932 			last_char    = 0;
933 		}
934 		splx(s);
935 		return;
936 	}
937 	else if(tout_pending && last_char != c) {
938 		/*
939 		 * Different character, stop also
940 		 */
941 		untimeout(repeat_handler, 0);
942 		tout_pending = 0;
943 		last_char    = 0;
944 	}
945 
946 	/*
947 	 * Handle ite-switching ALT + Fx
948 	 */
949 	if((kbd_modifier == KBD_MOD_ALT) && (c >= 0x3b) && (c <= 0x44)) {
950 		ite_switch(c - 0x3b);
951 		splx(s);
952 		return;
953 	}
954 	/*
955 	 * Safety button, switch back to ascii keymap.
956 	 */
957 	if(kbd_modifier == (KBD_MOD_ALT | KBD_MOD_LSHIFT) && c == 0x3b) {
958 		/* ALT + LSHIFT + F1 */
959 		bcopy(&ascii_kbdmap, kbdmap, sizeof(struct kbdmap));
960 		splx(s);
961 		return;
962 #ifdef DDB
963 	}
964 	else if(kbd_modifier == (KBD_MOD_ALT | KBD_MOD_LSHIFT) && c == 0x43) {
965 		/*
966 		 * ALT + LSHIFT + F9 -> Debugger!
967 		 */
968 		Debugger();
969 		splx(s);
970 		return;
971 #endif
972 	}
973 
974 	/*
975 	 * The rest of the code is senseless when the device is not open.
976 	 */
977 	if(kbd_tty == NULL) {
978 		splx(s);
979 		return;
980 	}
981 
982 	/*
983 	 * Translate modifiers
984 	 */
985 	if(kbd_modifier & KBD_MOD_SHIFT) {
986 		if(kbd_modifier & KBD_MOD_ALT)
987 			key = kbdmap->alt_shift_keys[c];
988 		else key = kbdmap->shift_keys[c];
989 	}
990 	else if(kbd_modifier & KBD_MOD_ALT)
991 			key = kbdmap->alt_keys[c];
992 	else {
993 		key = kbdmap->keys[c];
994 		/*
995 		 * If CAPS and key is CAPable (no pun intended)
996 		 */
997 		if((kbd_modifier & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
998 			key = kbdmap->shift_keys[c];
999 	}
1000 	code = key.code;
1001 
1002 	/*
1003 	 * Arrange to repeat the keystroke. By doing this at the level
1004 	 * of scan-codes, we can have function keys, and keys that
1005 	 * send strings, repeat too. This also entitles an additional
1006 	 * overhead, since we have to do the conversion each time, but
1007 	 * I guess that's ok.
1008 	 */
1009 	if(!tout_pending && caller == ITEFILT_TTY && kbd_ite->key_repeat) {
1010 		tout_pending = 1;
1011 		last_char    = c;
1012 		timeout(repeat_handler, 0, start_repeat_timeo * hz / 100);
1013 	}
1014 	else if(!tout_pending && caller==ITEFILT_REPEATER
1015 				&& kbd_ite->key_repeat) {
1016 		tout_pending = 1;
1017 		last_char    = c;
1018 		timeout(repeat_handler, 0, next_repeat_timeo * hz / 100);
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