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