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