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