xref: /netbsd-src/sys/arch/atari/dev/ite.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: ite.c,v 1.59 2008/06/11 14:35:53 tsutsui 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.59 2008/06/11 14:35:53 tsutsui 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(device_lookup_private(&ite_cd, 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
446 		return (EBUSY);
447 
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 	void *		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 (ttypull(tp)) {
681 			tp->t_state |= TS_TIMEOUT;
682 			callout_schedule(&tp->t_rstrt_ch, 1);
683 		}
684 	      out: ;
685 	} splx(s);
686 }
687 
688 void
689 ite_on(dev, flag)
690 	dev_t dev;
691 	int flag;
692 {
693 	struct ite_softc *ip;
694 	int unit;
695 
696 	unit = ITEUNIT(dev);
697 	ip = getitesp(dev);
698 
699 	/* force ite active, overriding graphics mode */
700 	if (flag & 1) {
701 		ip->flags |= ITE_ACTIVE;
702 		ip->flags &= ~(ITE_INGRF | ITE_INITED);
703 	}
704 	/* leave graphics mode */
705 	if (flag & 2) {
706 		ip->flags &= ~ITE_INGRF;
707 		if ((ip->flags & ITE_ACTIVE) == 0)
708 			return;
709 	}
710 	ip->flags |= ITE_ACTIVE;
711 	if (ip->flags & ITE_INGRF)
712 		return;
713 	iteinit(dev);
714 }
715 
716 void
717 ite_off(dev, flag)
718 dev_t	dev;
719 int	flag;
720 {
721 	struct ite_softc *ip;
722 
723 	ip = getitesp(dev);
724 	if (flag & 2)
725 		ip->flags |= ITE_INGRF;
726 	if ((ip->flags & ITE_ACTIVE) == 0)
727 		return;
728 	if ((flag & 1) ||
729 	    (ip->flags & (ITE_INGRF | ITE_ISCONS | ITE_INITED)) == ITE_INITED)
730 		SUBR_DEINIT(ip);
731 	if ((flag & 2) == 0)	/* XXX hmm grfon() I think wants this to  go inactive. */
732 		ip->flags &= ~ITE_ACTIVE;
733 }
734 
735 static void
736 ite_switch(unit)
737 int	unit;
738 {
739 	struct ite_softc	*ip;
740 	extern const struct cdevsw view_cdevsw;
741 
742 	ip = getitesp(unit);
743 	if ((ip->flags & (ITE_ATTACHED | ITE_INITED)) == 0)
744 		return;
745 
746 	/*
747 	 * If switching to an active ite, also switch the keyboard.
748 	 */
749 	if (ip->flags & ITE_ACTIVE)
750 		kbd_ite = ip;
751 
752 	/*
753 	 * Now make it visible
754 	 */
755 	(*view_cdevsw.d_ioctl)(ip->grf->g_viewdev, VIOCDISPLAY, NULL,
756 			       0, NOLWP);
757 
758 	/*
759 	 * Make sure the cursor's there too....
760 	 */
761   	SUBR_CURSOR(ip, DRAW_CURSOR);
762 }
763 
764 /* XXX called after changes made in underlying grf layer. */
765 /* I want to nuke this */
766 void
767 ite_reinit(dev)
768 	dev_t dev;
769 {
770 	struct ite_softc *ip;
771 
772 	ip = getitesp(dev);
773 	ip->flags &= ~ITE_INITED;
774 	iteinit(dev);
775 }
776 
777 int
778 ite_param(tp, t)
779 	struct tty *tp;
780 	struct termios *t;
781 {
782 	tp->t_ispeed = t->c_ispeed;
783 	tp->t_ospeed = t->c_ospeed;
784 	tp->t_cflag = t->c_cflag;
785 	return (0);
786 }
787 
788 void
789 ite_reset(ip)
790 	struct ite_softc *ip;
791 {
792 	int i;
793 
794 	ip->curx = 0;
795 	ip->cury = 0;
796 	ip->attribute = ATTR_NOR;
797 	ip->save_curx = 0;
798 	ip->save_cury = 0;
799 	ip->save_attribute = ATTR_NOR;
800 	ip->ap = ip->argbuf;
801 	ip->emul_level = 0;
802 	ip->eightbit_C1 = 0;
803 	ip->top_margin = 0;
804 	ip->bottom_margin = ip->rows - 1;
805 	ip->inside_margins = 0;
806 	ip->linefeed_newline = 0;
807 	ip->auto_wrap = ite_default_wrap;
808 	ip->cursor_appmode = 0;
809 	ip->keypad_appmode = 0;
810 	ip->imode = 0;
811 	ip->key_repeat = 1;
812 	bzero(ip->tabs, ip->cols);
813 	for (i = 0; i < ip->cols; i++)
814 		ip->tabs[i] = ((i & 7) == 0);
815 }
816 
817 /*
818  * has to be global because of the shared filters.
819  */
820 static u_char last_dead;
821 
822 /*
823  * Used in console at startup only and for DDB.
824  */
825 int
826 ite_cnfilter(c, caller)
827 u_int		c;
828 enum caller	caller;
829 {
830 	struct key	key;
831 	struct kbdmap	*kbdmap;
832 	u_char		code, up, mask;
833 	int		s;
834 
835 	up   = KBD_RELEASED(c);
836 	c    = KBD_SCANCODE(c);
837 	code = 0;
838 	mask = 0;
839 	kbdmap = (kbd_ite == NULL) ? &ascii_kbdmap : kbd_ite->kbdmap;
840 
841 	s = spltty();
842 
843 	/*
844 	 * No special action if key released
845 	 */
846 	if(up) {
847 		splx(s);
848 		return -1;
849 	}
850 
851 	/* translate modifiers */
852 	if(kbd_modifier & KBD_MOD_SHIFT) {
853 		if(kbd_modifier & KBD_MOD_ALT)
854 			key = kbdmap->alt_shift_keys[c];
855 		else key = kbdmap->shift_keys[c];
856 	}
857 	else if(kbd_modifier & KBD_MOD_ALT)
858 			key = kbdmap->alt_keys[c];
859 	else {
860 		key = kbdmap->keys[c];
861 		/*
862 		 * If CAPS and key is CAPable (no pun intended)
863 		 */
864 		if((kbd_modifier & KBD_MOD_CAPS) && (key.mode & KBD_MODE_CAPS))
865 			key = kbdmap->shift_keys[c];
866 	}
867 	code = key.code;
868 
869 #ifdef notyet /* LWP: Didn't have time to look at this yet */
870 	/*
871 	 * If string return simple console filter
872 	 */
873 	if(key->mode & (KBD_MODE_STRING | KBD_MODE_KPAD)) {
874 		splx(s);
875 		return -1;
876 	}
877 	/* handle dead keys */
878 	if(key->mode & KBD_MODE_DEAD) {
879 		/* if entered twice, send accent itself */
880 		if (last_dead == key->mode & KBD_MODE_ACCMASK)
881 			last_dead = 0;
882 		else {
883 			last_dead = key->mode & KBD_MODE_ACCMASK;
884 			splx(s);
885 			return -1;
886 		}
887 	}
888 	if(last_dead) {
889 		/* can't apply dead flag to string-keys */
890 		if (code >= '@' && code < 0x7f)
891 			code =
892 			    acctable[KBD_MODE_ACCENT(last_dead)][code - '@'];
893 		last_dead = 0;
894 	}
895 #endif
896 	if(kbd_modifier & KBD_MOD_CTRL)
897 		code &= 0x1f;
898 
899 	/*
900 	 * Do console mapping.
901 	 */
902 	code = code == '\r' ? '\n' : code;
903 
904 	splx(s);
905 	return (code);
906 }
907 
908 /* And now the old stuff. */
909 
910 /* these are used to implement repeating keys.. */
911 static u_int last_char;
912 static u_char tout_pending;
913 
914 static callout_t repeat_ch;
915 
916 /*ARGSUSED*/
917 static void
918 repeat_handler(arg)
919 void *arg;
920 {
921 	tout_pending = 0;
922 	if(last_char)
923 		add_sicallback((si_farg)ite_filter, (void *)last_char,
924 						    (void *)ITEFILT_REPEATER);
925 }
926 
927 void
928 ite_filter(c, caller)
929 u_int		c;
930 enum caller	caller;
931 {
932 	struct tty	*kbd_tty;
933 	struct kbdmap	*kbdmap;
934 	u_char		code, *str, up, mask;
935 	struct key	key;
936 	int		s, i;
937 	static bool	again;
938 
939 	if (!again) {
940 		/* XXX */
941 		callout_init(&repeat_ch, 0);
942 		again = true;
943 	}
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