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