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