xref: /freebsd/sys/dev/syscons/scterm-sc.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * Copyright (c) 1992-1998 S�ren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer as
11  *    the first lines of this file unmodified.
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_syscons.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/consio.h>
38 
39 #if defined(__sparc64__) || defined(__powerpc__)
40 #include <machine/sc_machdep.h>
41 #else
42 #include <machine/pc/display.h>
43 #endif
44 
45 #include <dev/syscons/syscons.h>
46 #include <dev/syscons/sctermvar.h>
47 
48 #ifndef SC_DUMB_TERMINAL
49 
50 #define MAX_ESC_PAR	5
51 
52 /* attribute flags */
53 typedef struct {
54 	u_short		fg;			/* foreground color */
55 	u_short		bg;			/* background color */
56 } color_t;
57 
58 typedef struct {
59 	int		flags;
60 #define SCTERM_BUSY	(1 << 0)
61 	int		esc;
62 	int		num_param;
63 	int		last_param;
64 	int		param[MAX_ESC_PAR];
65 	int		saved_xpos;
66 	int		saved_ypos;
67 	int		attr_mask;		/* current logical attr mask */
68 #define NORMAL_ATTR	0x00
69 #define BLINK_ATTR	0x01
70 #define BOLD_ATTR	0x02
71 #define UNDERLINE_ATTR	0x04
72 #define REVERSE_ATTR	0x08
73 #define FG_CHANGED	0x10
74 #define BG_CHANGED	0x20
75 	int		cur_attr;		/* current hardware attr word */
76 	color_t		cur_color;		/* current hardware color */
77 	color_t		std_color;		/* normal hardware color */
78 	color_t		rev_color;		/* reverse hardware color */
79 	color_t		dflt_std_color;		/* default normal color */
80 	color_t		dflt_rev_color;		/* default reverse color */
81 } term_stat;
82 
83 static sc_term_init_t	scterm_init;
84 static sc_term_term_t	scterm_term;
85 static sc_term_puts_t	scterm_puts;
86 static sc_term_ioctl_t	scterm_ioctl;
87 static sc_term_reset_t	scterm_reset;
88 static sc_term_default_attr_t	scterm_default_attr;
89 static sc_term_clear_t	scterm_clear;
90 static sc_term_notify_t	scterm_notify;
91 static sc_term_input_t	scterm_input;
92 
93 static sc_term_sw_t sc_term_sc = {
94 	{ NULL, NULL },
95 	"sc",					/* emulator name */
96 	"syscons terminal",			/* description */
97 	"*",					/* matching renderer, any :-) */
98 	sizeof(term_stat),			/* softc size */
99 	0,
100 	scterm_init,
101 	scterm_term,
102 	scterm_puts,
103 	scterm_ioctl,
104 	scterm_reset,
105 	scterm_default_attr,
106 	scterm_clear,
107 	scterm_notify,
108 	scterm_input,
109 };
110 
111 SCTERM_MODULE(sc, sc_term_sc);
112 
113 static term_stat	reserved_term_stat;
114 static void		scterm_scan_esc(scr_stat *scp, term_stat *tcp,
115 					u_char c);
116 static int		mask2attr(term_stat *tcp);
117 
118 static int
119 scterm_init(scr_stat *scp, void **softc, int code)
120 {
121 	term_stat *tcp;
122 
123 	if (*softc == NULL) {
124 		if (reserved_term_stat.flags & SCTERM_BUSY)
125 			return EINVAL;
126 		*softc = &reserved_term_stat;
127 	}
128 	tcp = *softc;
129 
130 	switch (code) {
131 	case SC_TE_COLD_INIT:
132 		bzero(tcp, sizeof(*tcp));
133 		tcp->flags = SCTERM_BUSY;
134 		tcp->esc = 0;
135 		tcp->saved_xpos = -1;
136 		tcp->saved_ypos = -1;
137 		tcp->attr_mask = NORMAL_ATTR;
138 		/* XXX */
139 		tcp->dflt_std_color.fg = SC_NORM_ATTR & 0x0f;
140 		tcp->dflt_std_color.bg = (SC_NORM_ATTR >> 4) & 0x0f;
141 		tcp->dflt_rev_color.fg = SC_NORM_REV_ATTR & 0x0f;
142 		tcp->dflt_rev_color.bg = (SC_NORM_REV_ATTR >> 4) & 0x0f;
143 		tcp->std_color = tcp->dflt_std_color;
144 		tcp->rev_color = tcp->dflt_rev_color;
145 		tcp->cur_color = tcp->std_color;
146 		tcp->cur_attr = mask2attr(tcp);
147 		++sc_term_sc.te_refcount;
148 		break;
149 
150 	case SC_TE_WARM_INIT:
151 		tcp->esc = 0;
152 		tcp->saved_xpos = -1;
153 		tcp->saved_ypos = -1;
154 #if 0
155 		tcp->std_color = tcp->dflt_std_color;
156 		tcp->rev_color = tcp->dflt_rev_color;
157 #endif
158 		tcp->cur_color = tcp->std_color;
159 		tcp->cur_attr = mask2attr(tcp);
160 		break;
161 	}
162 
163 	return 0;
164 }
165 
166 static int
167 scterm_term(scr_stat *scp, void **softc)
168 {
169 	if (*softc == &reserved_term_stat) {
170 		*softc = NULL;
171 		bzero(&reserved_term_stat, sizeof(reserved_term_stat));
172 	}
173 	--sc_term_sc.te_refcount;
174 	return 0;
175 }
176 
177 static void
178 scterm_scan_esc(scr_stat *scp, term_stat *tcp, u_char c)
179 {
180 	static u_char ansi_col[16] = {
181 		FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
182 		FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
183 		FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
184 		FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE
185 	};
186 	static int cattrs[] = {
187 		0,					/* block */
188 		CONS_BLINK_CURSOR,			/* blinking block */
189 		CONS_CHAR_CURSOR,			/* underline */
190 		CONS_CHAR_CURSOR | CONS_BLINK_CURSOR,	/* blinking underline */
191 		CONS_RESET_CURSOR,			/* reset to default */
192 		CONS_HIDDEN_CURSOR,			/* hide cursor */
193 	};
194 	static int tcattrs[] = {
195 		CONS_RESET_CURSOR | CONS_LOCAL_CURSOR,	/* normal */
196 		CONS_HIDDEN_CURSOR | CONS_LOCAL_CURSOR,	/* invisible */
197 		CONS_BLINK_CURSOR | CONS_LOCAL_CURSOR,	/* very visible */
198 	};
199 	sc_softc_t *sc;
200 	int v0, v1, v2;
201 	int i, n;
202 
203 	i = n = 0;
204 	sc = scp->sc;
205 	if (tcp->esc == 1) {	/* seen ESC */
206 		switch (c) {
207 
208 		case '7':	/* Save cursor position */
209 			tcp->saved_xpos = scp->xpos;
210 			tcp->saved_ypos = scp->ypos;
211 			break;
212 
213 		case '8':	/* Restore saved cursor position */
214 			if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
215 				sc_move_cursor(scp, tcp->saved_xpos,
216 					       tcp->saved_ypos);
217 			break;
218 
219 		case '[':	/* Start ESC [ sequence */
220 			tcp->esc = 2;
221 			tcp->last_param = -1;
222 			for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
223 				tcp->param[i] = 1;
224 			tcp->num_param = 0;
225 			return;
226 
227 		case 'M':	/* Move cursor up 1 line, scroll if at top */
228 			sc_term_up_scroll(scp, 1, sc->scr_map[0x20],
229 					  tcp->cur_attr, 0, 0);
230 			break;
231 #ifdef notyet
232 		case 'Q':
233 			tcp->esc = 4;
234 			return;
235 #endif
236 		case 'c':       /* reset */
237 			tcp->attr_mask = NORMAL_ATTR;
238 			tcp->cur_color = tcp->std_color
239 				       = tcp->dflt_std_color;
240 			tcp->rev_color = tcp->dflt_rev_color;
241 			tcp->cur_attr = mask2attr(tcp);
242 			sc_change_cursor_shape(scp,
243 			    CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, -1, -1);
244 			sc_clear_screen(scp);
245 			break;
246 
247 		case '(':	/* iso-2022: designate 94 character set to G0 */
248 			tcp->esc = 5;
249 			return;
250 		}
251 	} else if (tcp->esc == 2) {	/* seen ESC [ */
252 		if (c >= '0' && c <= '9') {
253 			if (tcp->num_param < MAX_ESC_PAR) {
254 				if (tcp->last_param != tcp->num_param) {
255 					tcp->last_param = tcp->num_param;
256 					tcp->param[tcp->num_param] = 0;
257 				} else {
258 					tcp->param[tcp->num_param] *= 10;
259 				}
260 				tcp->param[tcp->num_param] += c - '0';
261 				return;
262 			}
263 		}
264 		tcp->num_param = tcp->last_param + 1;
265 		switch (c) {
266 
267 		case ';':
268 			if (tcp->num_param < MAX_ESC_PAR)
269 				return;
270 			break;
271 
272 		case '=':
273 			tcp->esc = 3;
274 			tcp->last_param = -1;
275 			for (i = tcp->num_param; i < MAX_ESC_PAR; i++)
276 				tcp->param[i] = 1;
277 			tcp->num_param = 0;
278 			return;
279 
280 		case 'A':	/* up n rows */
281 			sc_term_up(scp, tcp->param[0], 0);
282 			break;
283 
284 		case 'B':	/* down n rows */
285 			sc_term_down(scp, tcp->param[0], 0);
286 			break;
287 
288 		case 'C':	/* right n columns */
289 			sc_term_right(scp, tcp->param[0]);
290 			break;
291 
292 		case 'D':	/* left n columns */
293 			sc_term_left(scp, tcp->param[0]);
294 			break;
295 
296 		case 'E':	/* cursor to start of line n lines down */
297 			n = tcp->param[0];
298 			if (n < 1)
299 				n = 1;
300 			sc_move_cursor(scp, 0, scp->ypos + n);
301 			break;
302 
303 		case 'F':	/* cursor to start of line n lines up */
304 			n = tcp->param[0];
305 			if (n < 1)
306 				n = 1;
307 			sc_move_cursor(scp, 0, scp->ypos - n);
308 			break;
309 
310 		case 'f':	/* Cursor move */
311 		case 'H':
312 			if (tcp->num_param == 0)
313 				sc_move_cursor(scp, 0, 0);
314 			else if (tcp->num_param == 2)
315 				sc_move_cursor(scp, tcp->param[1] - 1,
316 					       tcp->param[0] - 1);
317 			break;
318 
319 		case 'J':	/* Clear all or part of display */
320 			if (tcp->num_param == 0)
321 				n = 0;
322 			else
323 				n = tcp->param[0];
324 			sc_term_clr_eos(scp, n, sc->scr_map[0x20],
325 					tcp->cur_attr);
326 			break;
327 
328 		case 'K':	/* Clear all or part of line */
329 			if (tcp->num_param == 0)
330 				n = 0;
331 			else
332 				n = tcp->param[0];
333 			sc_term_clr_eol(scp, n, sc->scr_map[0x20],
334 					tcp->cur_attr);
335 			break;
336 
337 		case 'L':	/* Insert n lines */
338 			sc_term_ins_line(scp, scp->ypos, tcp->param[0],
339 					 sc->scr_map[0x20], tcp->cur_attr, 0);
340 			break;
341 
342 		case 'M':	/* Delete n lines */
343 			sc_term_del_line(scp, scp->ypos, tcp->param[0],
344 					 sc->scr_map[0x20], tcp->cur_attr, 0);
345 			break;
346 
347 		case 'P':	/* Delete n chars */
348 			sc_term_del_char(scp, tcp->param[0],
349 					 sc->scr_map[0x20], tcp->cur_attr);
350 			break;
351 
352 		case '@':	/* Insert n chars */
353 			sc_term_ins_char(scp, tcp->param[0],
354 					 sc->scr_map[0x20], tcp->cur_attr);
355 			break;
356 
357 		case 'S':	/* scroll up n lines */
358 			sc_term_del_line(scp, 0, tcp->param[0],
359 					 sc->scr_map[0x20], tcp->cur_attr, 0);
360 			break;
361 
362 		case 'T':	/* scroll down n lines */
363 			sc_term_ins_line(scp, 0, tcp->param[0],
364 					 sc->scr_map[0x20], tcp->cur_attr, 0);
365 			break;
366 
367 		case 'X':	/* erase n characters in line */
368 			n = tcp->param[0];
369 			if (n < 1)
370 				n = 1;
371 			if (n > scp->xsize - scp->xpos)
372 				n = scp->xsize - scp->xpos;
373 			sc_vtb_erase(&scp->vtb, scp->cursor_pos, n,
374 				     sc->scr_map[0x20], tcp->cur_attr);
375 			mark_for_update(scp, scp->cursor_pos);
376 			mark_for_update(scp, scp->cursor_pos + n - 1);
377 			break;
378 
379 		case 'Z':	/* move n tabs backwards */
380 			sc_term_backtab(scp, tcp->param[0]);
381 			break;
382 
383 		case '`':	/* move cursor to column n */
384 			sc_term_col(scp, tcp->param[0]);
385 			break;
386 
387 		case 'a':	/* move cursor n columns to the right */
388 			sc_term_right(scp, tcp->param[0]);
389 			break;
390 
391 		case 'd':	/* move cursor to row n */
392 			sc_term_row(scp, tcp->param[0]);
393 			break;
394 
395 		case 'e':	/* move cursor n rows down */
396 			sc_term_down(scp, tcp->param[0], 0);
397 			break;
398 
399 		case 'm':	/* change attribute */
400 			if (tcp->num_param == 0) {
401 				tcp->attr_mask = NORMAL_ATTR;
402 				tcp->cur_color = tcp->std_color;
403 				tcp->cur_attr = mask2attr(tcp);
404 				break;
405 			}
406 			for (i = 0; i < tcp->num_param; i++) {
407 				switch (n = tcp->param[i]) {
408 				case 0:	/* back to normal */
409 					tcp->attr_mask = NORMAL_ATTR;
410 					tcp->cur_color = tcp->std_color;
411 					tcp->cur_attr = mask2attr(tcp);
412 					break;
413 				case 1:	/* bold */
414 					tcp->attr_mask |= BOLD_ATTR;
415 					tcp->cur_attr = mask2attr(tcp);
416 					break;
417 				case 4:	/* underline */
418 					tcp->attr_mask |= UNDERLINE_ATTR;
419 					tcp->cur_attr = mask2attr(tcp);
420 					break;
421 				case 5:	/* blink */
422 					tcp->attr_mask |= BLINK_ATTR;
423 					tcp->cur_attr = mask2attr(tcp);
424 					break;
425 				case 7: /* reverse */
426 					tcp->attr_mask |= REVERSE_ATTR;
427 					tcp->cur_attr = mask2attr(tcp);
428 					break;
429 				case 22: /* remove bold (or dim) */
430 					tcp->attr_mask &= ~BOLD_ATTR;
431 					tcp->cur_attr = mask2attr(tcp);
432 					break;
433 				case 24: /* remove underline */
434 					tcp->attr_mask &= ~UNDERLINE_ATTR;
435 					tcp->cur_attr = mask2attr(tcp);
436 					break;
437 				case 25: /* remove blink */
438 					tcp->attr_mask &= ~BLINK_ATTR;
439 					tcp->cur_attr = mask2attr(tcp);
440 					break;
441 				case 27: /* remove reverse */
442 					tcp->attr_mask &= ~REVERSE_ATTR;
443 					tcp->cur_attr = mask2attr(tcp);
444 					break;
445 				case 30: case 31: /* set ansi fg color */
446 				case 32: case 33: case 34:
447 				case 35: case 36: case 37:
448 					tcp->attr_mask |= FG_CHANGED;
449 					tcp->cur_color.fg = ansi_col[n - 30];
450 					tcp->cur_attr = mask2attr(tcp);
451 					break;
452 				case 39: /* restore fg color back to normal */
453 					tcp->attr_mask &= ~(FG_CHANGED|BOLD_ATTR);
454 					tcp->cur_color.fg = tcp->std_color.fg;
455 					tcp->cur_attr = mask2attr(tcp);
456 					break;
457 				case 40: case 41: /* set ansi bg color */
458 				case 42: case 43: case 44:
459 				case 45: case 46: case 47:
460 					tcp->attr_mask |= BG_CHANGED;
461 					tcp->cur_color.bg = ansi_col[n - 40];
462 					tcp->cur_attr = mask2attr(tcp);
463 		    			break;
464 				case 49: /* restore bg color back to normal */
465 					tcp->attr_mask &= ~BG_CHANGED;
466 					tcp->cur_color.bg = tcp->std_color.bg;
467 					tcp->cur_attr = mask2attr(tcp);
468 					break;
469 				}
470 			}
471 			break;
472 
473 		case 's':	/* Save cursor position */
474 			tcp->saved_xpos = scp->xpos;
475 			tcp->saved_ypos = scp->ypos;
476 			break;
477 
478 		case 'u':	/* Restore saved cursor position */
479 			if (tcp->saved_xpos >= 0 && tcp->saved_ypos >= 0)
480 				sc_move_cursor(scp, tcp->saved_xpos,
481 					       tcp->saved_ypos);
482 			break;
483 
484 		case 'x':
485 			if (tcp->num_param == 0)
486 				n = 0;
487 			else
488 				n = tcp->param[0];
489 			switch (n) {
490 			case 0: /* reset colors and attributes back to normal */
491 				tcp->attr_mask = NORMAL_ATTR;
492 				tcp->cur_color = tcp->std_color
493 					       = tcp->dflt_std_color;
494 				tcp->rev_color = tcp->dflt_rev_color;
495 				tcp->cur_attr = mask2attr(tcp);
496 				break;
497 			case 1:	/* set ansi background */
498 				tcp->attr_mask &= ~BG_CHANGED;
499 				tcp->cur_color.bg = tcp->std_color.bg
500 						  = ansi_col[tcp->param[1] & 0x0f];
501 				tcp->cur_attr = mask2attr(tcp);
502 				break;
503 			case 2:	/* set ansi foreground */
504 				tcp->attr_mask &= ~FG_CHANGED;
505 				tcp->cur_color.fg = tcp->std_color.fg
506 						  = ansi_col[tcp->param[1] & 0x0f];
507 				tcp->cur_attr = mask2attr(tcp);
508 				break;
509 			case 3: /* set adapter attribute directly */
510 				tcp->attr_mask &= ~(FG_CHANGED | BG_CHANGED);
511 				tcp->cur_color.fg = tcp->std_color.fg
512 						  = tcp->param[1] & 0x0f;
513 				tcp->cur_color.bg = tcp->std_color.bg
514 						  = (tcp->param[1] >> 4) & 0x0f;
515 				tcp->cur_attr = mask2attr(tcp);
516 				break;
517 			case 5: /* set ansi reverse background */
518 				tcp->rev_color.bg = ansi_col[tcp->param[1] & 0x0f];
519 				tcp->cur_attr = mask2attr(tcp);
520 				break;
521 			case 6: /* set ansi reverse foreground */
522 				tcp->rev_color.fg = ansi_col[tcp->param[1] & 0x0f];
523 				tcp->cur_attr = mask2attr(tcp);
524 				break;
525 			case 7: /* set adapter reverse attribute directly */
526 				tcp->rev_color.fg = tcp->param[1] & 0x0f;
527 				tcp->rev_color.bg = (tcp->param[1] >> 4) & 0x0f;
528 				tcp->cur_attr = mask2attr(tcp);
529 				break;
530 			}
531 			break;
532 
533 		case 'z':	/* switch to (virtual) console n */
534 			if (tcp->num_param == 1)
535 				sc_switch_scr(sc, tcp->param[0]);
536 			break;
537 		}
538 	} else if (tcp->esc == 3) {	/* seen ESC [0-9]+ = */
539 		if (c >= '0' && c <= '9') {
540 			if (tcp->num_param < MAX_ESC_PAR) {
541 				if (tcp->last_param != tcp->num_param) {
542 					tcp->last_param = tcp->num_param;
543 					tcp->param[tcp->num_param] = 0;
544 				} else {
545 					tcp->param[tcp->num_param] *= 10;
546 				}
547 				tcp->param[tcp->num_param] += c - '0';
548 				return;
549 			}
550 		}
551 		tcp->num_param = tcp->last_param + 1;
552 		switch (c) {
553 
554 		case ';':
555 			if (tcp->num_param < MAX_ESC_PAR)
556 				return;
557 			break;
558 
559 		case 'A':   /* set display border color */
560 			if (tcp->num_param == 1) {
561 				scp->border=tcp->param[0] & 0xff;
562 				if (scp == sc->cur_scp)
563 					sc_set_border(scp, scp->border);
564 			}
565 			break;
566 
567 		case 'B':   /* set bell pitch and duration */
568 			if (tcp->num_param == 2) {
569 				scp->bell_pitch = tcp->param[0];
570 				scp->bell_duration =
571 				    (tcp->param[1] * hz + 99) / 100;
572 			}
573 			break;
574 
575 		case 'C':   /* set global/parmanent cursor type & shape */
576 			i = spltty();
577 			n = tcp->num_param;
578 			v0 = tcp->param[0];
579 			v1 = tcp->param[1];
580 			v2 = tcp->param[2];
581 			switch (n) {
582 			case 1:	/* flags only */
583 				if (v0 < sizeof(cattrs)/sizeof(cattrs[0]))
584 					v0 = cattrs[v0];
585 				else	/* backward compatibility */
586 					v0 = cattrs[v0 & 0x3];
587 				sc_change_cursor_shape(scp, v0, -1, -1);
588 				break;
589 			case 2:
590 				v2 = 0;
591 				v0 &= 0x1f;	/* backward compatibility */
592 				v1 &= 0x1f;
593 				/* FALL THROUGH */
594 			case 3:	/* base and height */
595 				if (v2 == 0)	/* count from top */
596 					sc_change_cursor_shape(scp, -1,
597 					    scp->font_size - v1 - 1,
598 					    v1 - v0 + 1);
599 				else if (v2 == 1) /* count from bottom */
600 					sc_change_cursor_shape(scp, -1,
601 					    v0, v1 - v0 + 1);
602 				break;
603 			}
604 			splx(i);
605 			break;
606 
607 		case 'F':   /* set adapter foreground */
608 			if (tcp->num_param == 1) {
609 				tcp->attr_mask &= ~FG_CHANGED;
610 				tcp->cur_color.fg = tcp->std_color.fg
611 						  = tcp->param[0] & 0x0f;
612 				tcp->cur_attr = mask2attr(tcp);
613 			}
614 			break;
615 
616 		case 'G':   /* set adapter background */
617 			if (tcp->num_param == 1) {
618 				tcp->attr_mask &= ~BG_CHANGED;
619 				tcp->cur_color.bg = tcp->std_color.bg
620 						  = tcp->param[0] & 0x0f;
621 				tcp->cur_attr = mask2attr(tcp);
622 			}
623 			break;
624 
625 		case 'H':   /* set adapter reverse foreground */
626 			if (tcp->num_param == 1) {
627 				tcp->rev_color.fg = tcp->param[0] & 0x0f;
628 				tcp->cur_attr = mask2attr(tcp);
629 			}
630 			break;
631 
632 		case 'I':   /* set adapter reverse background */
633 			if (tcp->num_param == 1) {
634 				tcp->rev_color.bg = tcp->param[0] & 0x0f;
635 				tcp->cur_attr = mask2attr(tcp);
636 			}
637 			break;
638 
639 		case 'S':   /* set local/temporary cursor type & shape */
640 			i = spltty();
641 			n = tcp->num_param;
642 			v0 = tcp->param[0];
643 			switch (n) {
644 			case 0:
645 				v0 = 0;
646 				/* FALL THROUGH */
647 			case 1:
648 				if (v0 < sizeof(tcattrs)/sizeof(tcattrs[0]))
649 					sc_change_cursor_shape(scp,
650 					    tcattrs[v0], -1, -1);
651 				break;
652 			}
653 			splx(i);
654 			break;
655 		}
656 #ifdef notyet
657 	} else if (tcp->esc == 4) {	/* seen ESC Q */
658 		/* to be filled */
659 #endif
660 	} else if (tcp->esc == 5) {	/* seen ESC ( */
661 		switch (c) {
662 		case 'B':   /* iso-2022: desginate ASCII into G0 */
663 			break;
664 		/* other items to be filled */
665 		default:
666 			break;
667 		}
668 	}
669 	tcp->esc = 0;
670 }
671 
672 static void
673 scterm_puts(scr_stat *scp, u_char *buf, int len)
674 {
675 	term_stat *tcp;
676 
677 	tcp = scp->ts;
678 outloop:
679 	scp->sc->write_in_progress++;
680 
681 	if (tcp->esc) {
682 		scterm_scan_esc(scp, tcp, *buf);
683 		buf++;
684 		len--;
685 	} else {
686 		switch (*buf) {
687 		case 0x1b:
688 			tcp->esc = 1;
689 			tcp->num_param = 0;
690 			buf++;
691 			len--;
692 			break;
693 		default:
694 			sc_term_gen_print(scp, &buf, &len, tcp->cur_attr);
695 			break;
696 		}
697 	}
698 
699 	sc_term_gen_scroll(scp, scp->sc->scr_map[0x20], tcp->cur_attr);
700 
701 	scp->sc->write_in_progress--;
702 	if (len)
703 		goto outloop;
704 }
705 
706 static int
707 scterm_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
708 	     int flag, struct thread *td)
709 {
710 	term_stat *tcp = scp->ts;
711 	vid_info_t *vi;
712 
713 	switch (cmd) {
714 	case GIO_ATTR:      	/* get current attributes */
715 		/* FIXME: */
716 		*(int*)data = (tcp->cur_attr >> 8) & 0xff;
717 		return 0;
718 	case CONS_GETINFO:  	/* get current (virtual) console info */
719 		vi = (vid_info_t *)data;
720 		if (vi->size != sizeof(struct vid_info))
721 			return EINVAL;
722 		vi->mv_norm.fore = tcp->std_color.fg;
723 		vi->mv_norm.back = tcp->std_color.bg;
724 		vi->mv_rev.fore = tcp->rev_color.fg;
725 		vi->mv_rev.back = tcp->rev_color.bg;
726 		/*
727 		 * The other fields are filled by the upper routine. XXX
728 		 */
729 		return ENOIOCTL;
730 	}
731 	return ENOIOCTL;
732 }
733 
734 static int
735 scterm_reset(scr_stat *scp, int code)
736 {
737 	/* FIXME */
738 	return 0;
739 }
740 
741 static void
742 scterm_default_attr(scr_stat *scp, int color, int rev_color)
743 {
744 	term_stat *tcp = scp->ts;
745 
746 	tcp->dflt_std_color.fg = color & 0x0f;
747 	tcp->dflt_std_color.bg = (color >> 4) & 0x0f;
748 	tcp->dflt_rev_color.fg = rev_color & 0x0f;
749 	tcp->dflt_rev_color.bg = (rev_color >> 4) & 0x0f;
750 	tcp->std_color = tcp->dflt_std_color;
751 	tcp->rev_color = tcp->dflt_rev_color;
752 	tcp->cur_color = tcp->std_color;
753 	tcp->cur_attr = mask2attr(tcp);
754 }
755 
756 static void
757 scterm_clear(scr_stat *scp)
758 {
759 	term_stat *tcp = scp->ts;
760 
761 	sc_move_cursor(scp, 0, 0);
762 	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], tcp->cur_attr);
763 	mark_all(scp);
764 }
765 
766 static void
767 scterm_notify(scr_stat *scp, int event)
768 {
769 	switch (event) {
770 	case SC_TE_NOTIFY_VTSWITCH_IN:
771 		break;
772 	case SC_TE_NOTIFY_VTSWITCH_OUT:
773 		break;
774 	}
775 }
776 
777 static int
778 scterm_input(scr_stat *scp, int c, struct tty *tp)
779 {
780 	return FALSE;
781 }
782 
783 /*
784  * Calculate hardware attributes word using logical attributes mask and
785  * hardware colors
786  */
787 
788 /* FIXME */
789 static int
790 mask2attr(term_stat *tcp)
791 {
792 	int attr, mask = tcp->attr_mask;
793 
794 	if (mask & REVERSE_ATTR) {
795 		attr = ((mask & FG_CHANGED) ?
796 			tcp->cur_color.bg : tcp->rev_color.fg) |
797 			(((mask & BG_CHANGED) ?
798 			tcp->cur_color.fg : tcp->rev_color.bg) << 4);
799 	} else
800 		attr = tcp->cur_color.fg | (tcp->cur_color.bg << 4);
801 
802 	/* XXX: underline mapping for Hercules adapter can be better */
803 	if (mask & (BOLD_ATTR | UNDERLINE_ATTR))
804 		attr ^= 0x08;
805 	if (mask & BLINK_ATTR)
806 		attr ^= 0x80;
807 
808 	return (attr << 8);
809 }
810 
811 #endif /* SC_DUMB_TERMINAL */
812