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