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