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