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