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