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