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