wsemul_vt100_subr.c revision 1.7 1 /* $NetBSD: wsemul_vt100_subr.c,v 1.7 2000/04/28 21:56:16 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1998
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Matthias Drochner.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37
38 #include <dev/wscons/wsksymvar.h>
39 #include <dev/wscons/wsdisplayvar.h>
40 #include <dev/wscons/wsemulvar.h>
41 #include <dev/wscons/wsemul_vt100var.h>
42
43 static int vt100_selectattribute __P((struct wsemul_vt100_emuldata *,
44 int, int, int, long *, long *));
45 static int vt100_ansimode __P((struct wsemul_vt100_emuldata *, int, int));
46 static int vt100_decmode __P((struct wsemul_vt100_emuldata *, int, int));
47 #define VTMODE_SET 33
48 #define VTMODE_RESET 44
49 #define VTMODE_REPORT 55
50
51 /*
52 * scroll up within scrolling region
53 */
54 void
55 wsemul_vt100_scrollup(edp, n)
56 struct wsemul_vt100_emuldata *edp;
57 int n;
58 {
59 int help;
60
61 if (n > edp->scrreg_nrows)
62 n = edp->scrreg_nrows;
63
64 help = edp->scrreg_nrows - n;
65 if (help > 0) {
66 (*edp->emulops->copyrows)(edp->emulcookie,
67 edp->scrreg_startrow + n,
68 edp->scrreg_startrow,
69 help);
70 if (edp->dblwid)
71 memmove(&edp->dblwid[edp->scrreg_startrow],
72 &edp->dblwid[edp->scrreg_startrow + n],
73 help);
74 }
75 (*edp->emulops->eraserows)(edp->emulcookie,
76 edp->scrreg_startrow + help, n,
77 edp->bkgdattr);
78 if (edp->dblwid)
79 memset(&edp->dblwid[edp->scrreg_startrow + help], 0, n);
80 CHECK_DW;
81 }
82
83 /*
84 * scroll down within scrolling region
85 */
86 void
87 wsemul_vt100_scrolldown(edp, n)
88 struct wsemul_vt100_emuldata *edp;
89 int n;
90 {
91 int help;
92
93 if (n > edp->scrreg_nrows)
94 n = edp->scrreg_nrows;
95
96 help = edp->scrreg_nrows - n;
97 if (help > 0) {
98 (*edp->emulops->copyrows)(edp->emulcookie,
99 edp->scrreg_startrow,
100 edp->scrreg_startrow + n,
101 help);
102 if (edp->dblwid)
103 memmove(&edp->dblwid[edp->scrreg_startrow + n],
104 &edp->dblwid[edp->scrreg_startrow],
105 help);
106 }
107 (*edp->emulops->eraserows)(edp->emulcookie,
108 edp->scrreg_startrow, n,
109 edp->bkgdattr);
110 if (edp->dblwid)
111 memset(&edp->dblwid[edp->scrreg_startrow], 0, n);
112 CHECK_DW;
113 }
114
115 /*
116 * erase in display
117 */
118 void
119 wsemul_vt100_ed(edp, arg)
120 struct wsemul_vt100_emuldata *edp;
121 int arg;
122 {
123 int n;
124
125 switch (arg) {
126 case 0: /* cursor to end */
127 ERASECOLS(edp->ccol, COLS_LEFT + 1, edp->bkgdattr);
128 n = edp->nrows - edp->crow - 1;
129 if (n > 0) {
130 (*edp->emulops->eraserows)(edp->emulcookie,
131 edp->crow + 1, n,
132 edp->bkgdattr);
133 if (edp->dblwid)
134 memset(&edp->dblwid[edp->crow + 1], 0, n);
135 }
136 break;
137 case 1: /* beginning to cursor */
138 if (edp->crow > 0) {
139 (*edp->emulops->eraserows)(edp->emulcookie,
140 0, edp->crow,
141 edp->bkgdattr);
142 if (edp->dblwid)
143 memset(&edp->dblwid[0], 0, edp->crow);
144 }
145 ERASECOLS(0, edp->ccol + 1, edp->bkgdattr);
146 break;
147 case 2: /* complete display */
148 (*edp->emulops->eraserows)(edp->emulcookie,
149 0, edp->nrows,
150 edp->bkgdattr);
151 if (edp->dblwid)
152 memset(&edp->dblwid[0], 0, edp->nrows);
153 break;
154 default:
155 #ifdef VT100_PRINTUNKNOWN
156 printf("ed(%d) unknown\n", arg);
157 #endif
158 break;
159 }
160 CHECK_DW;
161 }
162
163 /*
164 * erase in line
165 */
166 void
167 wsemul_vt100_el(edp, arg)
168 struct wsemul_vt100_emuldata *edp;
169 int arg;
170 {
171 switch (arg) {
172 case 0: /* cursor to end */
173 ERASECOLS(edp->ccol, COLS_LEFT + 1, edp->bkgdattr);
174 break;
175 case 1: /* beginning to cursor */
176 ERASECOLS(0, edp->ccol + 1, edp->bkgdattr);
177 break;
178 case 2: /* complete line */
179 (*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
180 0, edp->ncols,
181 edp->bkgdattr);
182 break;
183 default:
184 #ifdef VT100_PRINTUNKNOWN
185 printf("el(%d) unknown\n", arg);
186 #endif
187 break;
188 }
189 }
190
191 /*
192 * handle commands after CSI (ESC[)
193 */
194 void
195 wsemul_vt100_handle_csi(edp, c)
196 struct wsemul_vt100_emuldata *edp;
197 u_char c;
198 {
199 int n, help, flags, fgcol, bgcol;
200 long attr, bkgdattr;
201
202 #define A3(a, b, c) (((a) << 16) | ((b) << 8) | (c))
203 switch (A3(edp->modif1, edp->modif2, c)) {
204 case A3('>', '\0', 'c'): /* DA secondary */
205 wsdisplay_emulinput(edp->cbcookie, WSEMUL_VT_ID2,
206 sizeof(WSEMUL_VT_ID2));
207 break;
208
209 case A3('\0', '\0', 'J'): /* ED selective erase in display */
210 case A3('?', '\0', 'J'): /* DECSED selective erase in display */
211 wsemul_vt100_ed(edp, ARG(0));
212 break;
213 case A3('\0', '\0', 'K'): /* EL selective erase in line */
214 case A3('?', '\0', 'K'): /* DECSEL selective erase in line */
215 wsemul_vt100_el(edp, ARG(0));
216 break;
217 case A3('\0', '\0', 'h'): /* SM */
218 for (n = 0; n < edp->nargs; n++)
219 vt100_ansimode(edp, ARG(n), VTMODE_SET);
220 break;
221 case A3('?', '\0', 'h'): /* DECSM */
222 for (n = 0; n < edp->nargs; n++)
223 vt100_decmode(edp, ARG(n), VTMODE_SET);
224 break;
225 case A3('\0', '\0', 'l'): /* RM */
226 for (n = 0; n < edp->nargs; n++)
227 vt100_ansimode(edp, ARG(n), VTMODE_RESET);
228 break;;
229 case A3('?', '\0', 'l'): /* DECRM */
230 for (n = 0; n < edp->nargs; n++)
231 vt100_decmode(edp, ARG(n), VTMODE_RESET);
232 break;;
233 case A3('\0', '$', 'p'): /* DECRQM request mode ANSI */
234 vt100_ansimode(edp, ARG(0), VTMODE_REPORT);
235 break;
236 case A3('?', '$', 'p'): /* DECRQM request mode DEC */
237 vt100_decmode(edp, ARG(0), VTMODE_REPORT);
238 break;
239 case A3('\0', '\0', 'i'): /* MC printer controller mode */
240 case A3('?', '\0', 'i'): /* MC printer controller mode */
241 switch (ARG(0)) {
242 case 0: /* print screen */
243 case 1: /* print cursor line */
244 case 4: /* off */
245 case 5: /* on */
246 #ifdef VT100_PRINTNOTIMPL
247 printf("CSI%di ignored\n", ARG(0));
248 #endif
249 break;
250 default:
251 #ifdef VT100_PRINTUNKNOWN
252 printf("CSI%di unknown\n", ARG(0));
253 #endif
254 break;
255 }
256 break;
257
258 #define A2(a, b) (((a) << 8) | (b))
259 case A2('!', 'p'): /* DECSTR soft reset VT300 only */
260 wsemul_vt100_reset(edp);
261 break;
262
263 case A2('"', 'p'): /* DECSCL */
264 switch (ARG(0)) {
265 case 61: /* VT100 mode (no further arguments!) */
266 break;
267 case 62:
268 case 63: /* VT300 mode */
269 break;
270 default:
271 #ifdef VT100_PRINTUNKNOWN
272 printf("CSI%d\"p unknown\n", ARG(0));
273 #endif
274 break;
275 }
276 switch (ARG(1)) {
277 case 0:
278 case 2: /* 8-bit controls */
279 #ifdef VT100_PRINTNOTIMPL
280 printf("CSI%d;%d\"p ignored\n", ARG(0), ARG(1));
281 #endif
282 break;
283 case 1: /* 7-bit controls */
284 break;
285 default:
286 #ifdef VT100_PRINTUNKNOWN
287 printf("CSI%d;%d\"p unknown\n", ARG(0), ARG(1));
288 #endif
289 break;
290 }
291 break;
292 case A2('"', 'q'): /* DECSCA select character attribute VT300 */
293 switch (ARG(0)) {
294 case 0:
295 case 1: /* erasable */
296 break;
297 case 2: /* not erasable */
298 #ifdef VT100_PRINTNOTIMPL
299 printf("CSI2\"q ignored\n");
300 #endif
301 break;
302 default:
303 #ifdef VT100_PRINTUNKNOWN
304 printf("CSI%d\"q unknown\n", ARG(0));
305 #endif
306 break;
307 }
308 break;
309
310 case A2('$', 'u'): /* DECRQTSR request terminal status report */
311 switch (ARG(0)) {
312 case 0: /* ignored */
313 break;
314 case 1: /* terminal state report */
315 #ifdef VT100_PRINTNOTIMPL
316 printf("CSI1$u ignored\n");
317 #endif
318 break;
319 default:
320 #ifdef VT100_PRINTUNKNOWN
321 printf("CSI%d$u unknown\n", ARG(0));
322 #endif
323 break;
324 }
325 break;
326 case A2('$', 'w'): /* DECRQPSR request presentation status report
327 (VT300 only) */
328 switch (ARG(0)) {
329 case 0: /* error */
330 break;
331 case 1: /* cursor information report */
332 #ifdef VT100_PRINTNOTIMPL
333 printf("CSI1$w ignored\n");
334 #endif
335 break;
336 case 2: /* tab stop report */
337 {
338 int i, n, ps = 0;
339 char buf[20];
340 KASSERT(edp->tabs != 0);
341 wsdisplay_emulinput(edp->cbcookie, "\033P2$u", 5);
342 for (i = 0; i < edp->ncols; i++)
343 if (edp->tabs[i]) {
344 n = sprintf(buf, "%s%d",
345 (ps ? "/" : ""), i + 1);
346 wsdisplay_emulinput(edp->cbcookie,
347 buf, n);
348 ps = 1;
349 }
350 }
351 wsdisplay_emulinput(edp->cbcookie, "\033\\", 2);
352 break;
353 default:
354 #ifdef VT100_PRINTUNKNOWN
355 printf("CSI%d$w unknown\n", ARG(0));
356 #endif
357 break;
358 }
359 break;
360 case A2('$', '}'): /* DECSASD select active status display */
361 switch (ARG(0)) {
362 case 0: /* main display */
363 case 1: /* status line */
364 #ifdef VT100_PRINTNOTIMPL
365 printf("CSI%d$} ignored\n", ARG(0));
366 #endif
367 break;
368 default:
369 #ifdef VT100_PRINTUNKNOWN
370 printf("CSI%d$} unknown\n", ARG(0));
371 #endif
372 break;
373 }
374 break;
375 case A2('$', '~'): /* DECSSDD select status line type */
376 switch (ARG(0)) {
377 case 0: /* none */
378 case 1: /* indicator */
379 case 2: /* host-writable */
380 #ifdef VT100_PRINTNOTIMPL
381 printf("CSI%d$~ ignored\n", ARG(0));
382 #endif
383 break;
384 default:
385 #ifdef VT100_PRINTUNKNOWN
386 printf("CSI%d$~ unknown\n", ARG(0));
387 #endif
388 break;
389 }
390 break;
391
392 case A2('&', 'u'): /* DECRQUPSS request user preferred
393 supplemental set */
394 wsdisplay_emulinput(edp->emulcookie, "\033P0!u%5\033\\", 9);
395 break;
396
397 case '@': /* ICH insert character VT300 only */
398 n = min(DEF1_ARG(0), COLS_LEFT + 1);
399 help = NCOLS - (edp->ccol + n);
400 if (help > 0)
401 COPYCOLS(edp->ccol, edp->ccol + n, help);
402 ERASECOLS(edp->ccol, n, edp->bkgdattr);
403 break;
404 case 'A': /* CUU */
405 edp->crow -= min(DEF1_ARG(0), ROWS_ABOVE);
406 CHECK_DW;
407 break;
408 case 'B': /* CUD */
409 edp->crow += min(DEF1_ARG(0), ROWS_BELOW);
410 CHECK_DW;
411 break;
412 case 'C': /* CUF */
413 edp->ccol += min(DEF1_ARG(0), COLS_LEFT);
414 break;
415 case 'D': /* CUB */
416 edp->ccol -= min(DEF1_ARG(0), edp->ccol);
417 edp->flags &= ~VTFL_LASTCHAR;
418 break;
419 case 'H': /* CUP */
420 case 'f': /* HVP */
421 if (edp->flags & VTFL_DECOM)
422 edp->crow = edp->scrreg_startrow +
423 min(DEF1_ARG(0), edp->scrreg_nrows) - 1;
424 else
425 edp->crow = min(DEF1_ARG(0), edp->nrows) - 1;
426 CHECK_DW;
427 edp->ccol = min(DEF1_ARG(1), NCOLS) - 1;
428 edp->flags &= ~VTFL_LASTCHAR;
429 break;
430 case 'L': /* IL insert line */
431 case 'M': /* DL delete line */
432 n = min(DEF1_ARG(0), ROWS_BELOW + 1);
433 {
434 int savscrstartrow, savscrnrows;
435 savscrstartrow = edp->scrreg_startrow;
436 savscrnrows = edp->scrreg_nrows;
437 edp->scrreg_nrows -= ROWS_ABOVE;
438 edp->scrreg_startrow = edp->crow;
439 if (c == 'L')
440 wsemul_vt100_scrolldown(edp, n);
441 else
442 wsemul_vt100_scrollup(edp, n);
443 edp->scrreg_startrow = savscrstartrow;
444 edp->scrreg_nrows = savscrnrows;
445 }
446 break;
447 case 'P': /* DCH delete character */
448 n = min(DEF1_ARG(0), COLS_LEFT + 1);
449 help = NCOLS - (edp->ccol + n);
450 if (help > 0)
451 COPYCOLS(edp->ccol + n, edp->ccol, help);
452 ERASECOLS(NCOLS - n, n, edp->bkgdattr);
453 break;
454 case 'X': /* ECH erase character */
455 n = min(DEF1_ARG(0), COLS_LEFT + 1);
456 ERASECOLS(edp->ccol, n, edp->bkgdattr);
457 break;
458 case 'c': /* DA primary */
459 if (ARG(0) == 0)
460 wsdisplay_emulinput(edp->cbcookie, WSEMUL_VT_ID1,
461 sizeof(WSEMUL_VT_ID1));
462 break;
463 case 'g': /* TBC */
464 KASSERT(edp->tabs != 0);
465 switch (ARG(0)) {
466 case 0:
467 edp->tabs[edp->ccol] = 0;
468 break;
469 case 3:
470 memset(edp->tabs, 0, edp->ncols);
471 break;
472 default:
473 #ifdef VT100_PRINTUNKNOWN
474 printf("CSI%dg unknown\n", ARG(0));
475 #endif
476 break;
477 }
478 break;
479 case 'm': /* SGR select graphic rendition */
480 flags = edp->attrflags;
481 fgcol = edp->fgcol;
482 bgcol = edp->bgcol;
483 for (n = 0; n < edp->nargs; n++) {
484 switch (ARG(n)) {
485 case 0: /* reset */
486 if (n == edp->nargs - 1) {
487 edp->bkgdattr = edp->curattr = edp->defattr;
488 edp->attrflags = 0;
489 edp->fgcol = WSCOL_WHITE;
490 edp->bgcol = WSCOL_BLACK;
491 return;
492 }
493 flags = 0;
494 fgcol = WSCOL_WHITE;
495 bgcol = WSCOL_BLACK;
496 break;
497 case 1: /* bold */
498 flags |= WSATTR_HILIT;
499 break;
500 case 4: /* underline */
501 flags |= WSATTR_UNDERLINE;
502 break;
503 case 5: /* blink */
504 flags |= WSATTR_BLINK;
505 break;
506 case 7: /* reverse */
507 flags |= WSATTR_REVERSE;
508 break;
509 case 22: /* ~bold VT300 only */
510 flags &= ~WSATTR_HILIT;
511 break;
512 case 24: /* ~underline VT300 only */
513 flags &= ~WSATTR_UNDERLINE;
514 break;
515 case 25: /* ~blink VT300 only */
516 flags &= ~WSATTR_BLINK;
517 break;
518 case 27: /* ~reverse VT300 only */
519 flags &= ~WSATTR_REVERSE;
520 break;
521 case 30: case 31: case 32: case 33:
522 case 34: case 35: case 36: case 37:
523 /* fg color */
524 flags |= WSATTR_WSCOLORS;
525 fgcol = ARG(n) - 30;
526 break;
527 case 40: case 41: case 42: case 43:
528 case 44: case 45: case 46: case 47:
529 /* bg color */
530 flags |= WSATTR_WSCOLORS;
531 bgcol = ARG(n) - 40;
532 break;
533 default:
534 #ifdef VT100_PRINTUNKNOWN
535 printf("CSI%dm unknown\n", ARG(n));
536 #endif
537 }
538 }
539 if (vt100_selectattribute(edp, flags, fgcol, bgcol, &attr,
540 &bkgdattr)) {
541 #ifdef VT100_DEBUG
542 printf("error allocating attr %d/%d/%x\n",
543 fgcol, bgcol, flags);
544 #endif
545 } else {
546 edp->curattr = attr;
547 edp->bkgdattr = bkgdattr;
548 edp->attrflags = flags;
549 edp->fgcol = fgcol;
550 edp->bgcol = bgcol;
551 }
552 break;
553 case 'n': /* reports */
554 switch (ARG(0)) {
555 case 5: /* DSR operating status */
556 /* 0 = OK, 3 = malfunction */
557 wsdisplay_emulinput(edp->cbcookie, "\033[0n", 4);
558 break;
559 case 6: { /* DSR cursor position report */
560 char buf[20];
561 int row;
562 if (edp->flags & VTFL_DECOM)
563 row = ROWS_ABOVE;
564 else
565 row = edp->crow;
566 n = sprintf(buf, "\033[%d;%dR",
567 row + 1, edp->ccol + 1);
568 wsdisplay_emulinput(edp->cbcookie, buf, n);
569 }
570 break;
571 case 15: /* DSR printer status */
572 /* 13 = no printer, 10 = ready, 11 = not ready */
573 wsdisplay_emulinput(edp->cbcookie, "\033[?13n", 6);
574 break;
575 case 25: /* UDK status - VT300 only */
576 /* 20 = locked, 21 = unlocked */
577 wsdisplay_emulinput(edp->cbcookie, "\033[?21n", 6);
578 break;
579 case 26: /* keyboard dialect */
580 /* 1 = north american , 7 = german */
581 wsdisplay_emulinput(edp->cbcookie, "\033[?27;1n", 8);
582 break;
583 default:
584 #ifdef VT100_PRINTUNKNOWN
585 printf("CSI%dn unknown\n", ARG(0));
586 #endif
587 break;
588 }
589 break;
590 case 'r': /* DECSTBM set top/bottom margins */
591 help = min(DEF1_ARG(0), edp->nrows) - 1;
592 n = min(DEFx_ARG(1, edp->nrows), edp->nrows) - help;
593 if (n < 2) {
594 /* minimal scrolling region has 2 lines */
595 return;
596 } else {
597 edp->scrreg_startrow = help;
598 edp->scrreg_nrows = n;
599 }
600 edp->crow = ((edp->flags & VTFL_DECOM) ?
601 edp->scrreg_startrow : 0);
602 edp->ccol = 0;
603 break;
604 case 'y':
605 switch (ARG(0)) {
606 case 4: /* DECTST invoke confidence test */
607 /* ignore */
608 break;
609 default:
610 #ifdef VT100_PRINTUNKNOWN
611 printf("CSI%dy unknown\n", ARG(0));
612 #endif
613 break;
614 }
615 break;
616 default:
617 #ifdef VT100_PRINTUNKNOWN
618 printf("CSI%c (%d, %d) unknown\n", c, ARG(0), ARG(1));
619 #endif
620 }
621 }
622
623 /*
624 * get an attribute from the graphics driver,
625 * try to find replacements if the desired appearance
626 * is not supported
627 */
628 static int
629 vt100_selectattribute(edp, flags, fgcol, bgcol, attr, bkgdattr)
630 struct wsemul_vt100_emuldata *edp;
631 int flags, fgcol, bgcol;
632 long *attr, *bkgdattr;
633 {
634 int error;
635
636 if ((flags & WSATTR_WSCOLORS) &&
637 !(edp->scrcapabilities & WSSCREEN_WSCOLORS)) {
638 flags &= ~WSATTR_WSCOLORS;
639 #ifdef VT100_DEBUG
640 printf("colors ignored (impossible)\n");
641 #endif
642 }
643 error = (*edp->emulops->alloc_attr)(edp->emulcookie, fgcol, bgcol,
644 flags & WSATTR_WSCOLORS, bkgdattr);
645 if (error)
646 return (error);
647
648 if ((flags & WSATTR_HILIT) &&
649 !(edp->scrcapabilities & WSSCREEN_HILIT)) {
650 flags &= ~WSATTR_HILIT;
651 if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
652 fgcol = WSCOL_RED;
653 flags |= WSATTR_WSCOLORS;
654 } else {
655 #ifdef VT100_DEBUG
656 printf("bold ignored (impossible)\n");
657 #endif
658 }
659 }
660 if ((flags & WSATTR_UNDERLINE) &&
661 !(edp->scrcapabilities & WSSCREEN_UNDERLINE)) {
662 flags &= ~WSATTR_UNDERLINE;
663 if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
664 bgcol = WSCOL_BROWN;
665 flags &= ~WSATTR_UNDERLINE;
666 flags |= WSATTR_WSCOLORS;
667 } else {
668 #ifdef VT100_DEBUG
669 printf("underline ignored (impossible)\n");
670 #endif
671 }
672 }
673 if ((flags & WSATTR_BLINK) &&
674 !(edp->scrcapabilities & WSSCREEN_BLINK)) {
675 flags &= ~WSATTR_BLINK;
676 #ifdef VT100_DEBUG
677 printf("blink ignored (impossible)\n");
678 #endif
679 }
680 if ((flags & WSATTR_REVERSE) &&
681 !(edp->scrcapabilities & WSSCREEN_REVERSE)) {
682 flags &= ~WSATTR_REVERSE;
683 if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
684 int help;
685 help = bgcol;
686 bgcol = fgcol;
687 fgcol = help;
688 flags |= WSATTR_WSCOLORS;
689 } else {
690 #ifdef VT100_DEBUG
691 printf("reverse ignored (impossible)\n");
692 #endif
693 }
694 }
695 error = (*edp->emulops->alloc_attr)(edp->emulcookie, fgcol, bgcol,
696 flags, attr);
697 if (error)
698 return (error);
699
700 return (0);
701 }
702
703 /*
704 * handle device control sequences if the main state machine
705 * told so by setting edp->dcstype to a nonzero value
706 */
707 void
708 wsemul_vt100_handle_dcs(edp)
709 struct wsemul_vt100_emuldata *edp;
710 {
711 int i, pos;
712
713 switch (edp->dcstype) {
714 case 0: /* not handled */
715 return;
716 case DCSTYPE_TABRESTORE:
717 KASSERT(edp->tabs != 0);
718 memset(edp->tabs, 0, edp->ncols);
719 pos = 0;
720 for (i = 0; i < edp->dcspos; i++) {
721 char c = edp->dcsarg[i];
722 switch (c) {
723 case '0': case '1': case '2': case '3': case '4':
724 case '5': case '6': case '7': case '8': case '9':
725 pos = pos * 10 + (edp->dcsarg[i] - '0');
726 break;
727 case '/':
728 if (pos > 0)
729 edp->tabs[pos - 1] = 1;
730 pos = 0;
731 break;
732 default:
733 #ifdef VT100_PRINTUNKNOWN
734 printf("unknown char %c in DCS\n", c);
735 #endif
736 }
737 }
738 if (pos > 0)
739 edp->tabs[pos - 1] = 1;
740 break;
741 default:
742 panic("wsemul_vt100_handle_dcs: bad type %d", edp->dcstype);
743 }
744 edp->dcstype = 0;
745 }
746
747 static int
748 vt100_ansimode(edp, nr, op)
749 struct wsemul_vt100_emuldata *edp;
750 int nr, op;
751 {
752 int res = 0; /* default: unknown */
753
754 switch (nr) {
755 case 2: /* KAM keyboard locked/unlocked */
756 break;
757 case 3: /* CRM control representation */
758 break;
759 case 4: /* IRM insert/replace characters */
760 if (op == VTMODE_SET)
761 edp->flags |= VTFL_INSERTMODE;
762 else if (op == VTMODE_RESET)
763 edp->flags &= ~VTFL_INSERTMODE;
764 res = ((edp->flags & VTFL_INSERTMODE) ? 1 : 2);
765 break;
766 case 10: /* HEM horizontal editing (permanently reset) */
767 res = 4;
768 break;
769 case 12: /* SRM local echo off/on */
770 res = 4; /* permanently reset ??? */
771 break;
772 case 20: /* LNM newline = newline/linefeed */
773 break;
774 default:
775 #ifdef VT100_PRINTUNKNOWN
776 printf("ANSI mode %d unknown\n", nr);
777 #endif
778 }
779 return (res);
780 }
781
782 static int
783 vt100_decmode(edp, nr, op)
784 struct wsemul_vt100_emuldata *edp;
785 int nr, op;
786 {
787 int res = 0; /* default: unknown */
788 int flags;
789
790 flags = edp->flags;
791 switch (nr) {
792 case 1: /* DECCKM application/nomal cursor keys */
793 if (op == VTMODE_SET)
794 flags |= VTFL_APPLCURSOR;
795 else if (op == VTMODE_RESET)
796 flags &= ~VTFL_APPLCURSOR;
797 res = ((flags & VTFL_APPLCURSOR) ? 1 : 2);
798 break;
799 case 2: /* DECANM ANSI vt100/vt52 */
800 res = 3; /* permanently set ??? */
801 break;
802 case 3: /* DECCOLM 132/80 cols */
803 case 4: /* DECSCLM smooth/jump scroll */
804 case 5: /* DECSCNM light/dark background */
805 res = 4; /* all permanently reset ??? */
806 break;
807 case 6: /* DECOM move within/outside margins */
808 if (op == VTMODE_SET)
809 flags |= VTFL_DECOM;
810 else if (op == VTMODE_RESET)
811 flags &= ~VTFL_DECOM;
812 res = ((flags & VTFL_DECOM) ? 1 : 2);
813 break;
814 case 7: /* DECAWM autowrap */
815 if (op == VTMODE_SET)
816 flags |= VTFL_DECAWM;
817 else if (op == VTMODE_RESET)
818 flags &= ~VTFL_DECAWM;
819 res = ((flags & VTFL_DECAWM) ? 1 : 2);
820 break;
821 case 8: /* DECARM keyboard autorepeat */
822 break;
823 case 18: /* DECPFF print form feed */
824 break;
825 case 19: /* DECPEX printer extent: screen/scrolling region */
826 break;
827 case 25: /* DECTCEM text cursor on/off */
828 if (op == VTMODE_SET)
829 flags |= VTFL_CURSORON;
830 else if (op == VTMODE_RESET)
831 flags &= ~VTFL_CURSORON;
832 if (flags != edp->flags)
833 (*edp->emulops->cursor)(edp->emulcookie,
834 flags & VTFL_CURSORON,
835 edp->crow, edp->ccol);
836 res = ((flags & VTFL_CURSORON) ? 1 : 2);
837 break;
838 case 42: /* DECNRCM use 7-bit NRC /
839 7/8 bit from DEC multilingual or ISO-latin-1*/
840 if (op == VTMODE_SET)
841 flags |= VTFL_NATCHARSET;
842 else if (op == VTMODE_RESET)
843 flags &= ~VTFL_NATCHARSET;
844 res = ((flags & VTFL_NATCHARSET) ? 1 : 2);
845 break;
846 case 66: /* DECNKM numeric keypad */
847 break;
848 case 68: /* DECKBUM keyboard usage data processing/typewriter */
849 break;
850 default:
851 #ifdef VT100_PRINTUNKNOWN
852 printf("DEC mode %d unknown\n", nr);
853 #endif
854 break;
855 }
856 edp->flags = flags;
857
858 return (res);
859 }
860