Home | History | Annotate | Line # | Download | only in wscons
wsemul_vt100.c revision 1.40
      1 /* $NetBSD: wsemul_vt100.c,v 1.40 2017/11/03 18:42:35 maya 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.c,v 1.40 2017/11/03 18:42:35 maya Exp $");
     31 
     32 #ifdef _KERNEL_OPT
     33 #include "opt_wsmsgattrs.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/time.h>
     39 #include <sys/malloc.h>
     40 #include <sys/fcntl.h>
     41 
     42 #include <dev/wscons/wsconsio.h>
     43 #include <dev/wscons/wsdisplayvar.h>
     44 #include <dev/wscons/wsemulvar.h>
     45 #include <dev/wscons/wsemul_vt100var.h>
     46 #include <dev/wscons/ascii.h>
     47 
     48 void	*wsemul_vt100_cnattach(const struct wsscreen_descr *, void *,
     49 			       int, int, long);
     50 void	*wsemul_vt100_attach(int console, const struct wsscreen_descr *,
     51 			     void *, int, int, void *, long);
     52 void	wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int);
     53 void	wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp);
     54 void	wsemul_vt100_resetop(void *, enum wsemul_resetops);
     55 #ifdef WSDISPLAY_CUSTOM_OUTPUT
     56 static void wsemul_vt100_getmsgattrs(void *, struct wsdisplay_msgattrs *);
     57 static void wsemul_vt100_setmsgattrs(void *, const struct wsscreen_descr *,
     58                                      const struct wsdisplay_msgattrs *);
     59 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
     60 static void wsemul_vt100_resize(void *, const struct wsscreen_descr *);
     61 
     62 const struct wsemul_ops wsemul_vt100_ops = {
     63 	.name = "vt100",
     64 	.cnattach = wsemul_vt100_cnattach,
     65 	.attach = wsemul_vt100_attach,
     66 	.output = wsemul_vt100_output,
     67 	.translate = wsemul_vt100_translate,
     68 	.detach = wsemul_vt100_detach,
     69 	.reset = wsemul_vt100_resetop,
     70 #ifdef WSDISPLAY_CUSTOM_OUTPUT
     71 	.getmsgattrs = wsemul_vt100_getmsgattrs,
     72 	.setmsgattrs = wsemul_vt100_setmsgattrs,
     73 #else
     74 	.getmsgattrs = NULL,
     75 	.setmsgattrs = NULL,
     76 #endif
     77 	.resize = wsemul_vt100_resize
     78 };
     79 
     80 struct wsemul_vt100_emuldata wsemul_vt100_console_emuldata;
     81 
     82 static void wsemul_vt100_init(struct wsemul_vt100_emuldata *,
     83 			      const struct wsscreen_descr *,
     84 			      void *, int, int, long);
     85 
     86 static void wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *,
     87 				       u_char, int);
     88 static void wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *,
     89 				     u_char, int);
     90 static void wsemul_vt100_nextline(struct wsemul_vt100_emuldata *);
     91 typedef u_int vt100_handler(struct wsemul_vt100_emuldata *, u_char);
     92 
     93 static vt100_handler
     94 wsemul_vt100_output_esc,
     95 wsemul_vt100_output_csi,
     96 wsemul_vt100_output_scs94,
     97 wsemul_vt100_output_scs94_percent,
     98 wsemul_vt100_output_scs96,
     99 wsemul_vt100_output_scs96_percent,
    100 wsemul_vt100_output_esc_hash,
    101 wsemul_vt100_output_esc_spc,
    102 wsemul_vt100_output_string,
    103 wsemul_vt100_output_string_esc,
    104 wsemul_vt100_output_dcs,
    105 wsemul_vt100_output_dcs_dollar;
    106 
    107 #define	VT100_EMUL_STATE_NORMAL		0	/* normal processing */
    108 #define	VT100_EMUL_STATE_ESC		1	/* got ESC */
    109 #define	VT100_EMUL_STATE_CSI		2	/* got CSI (ESC[) */
    110 #define	VT100_EMUL_STATE_SCS94		3	/* got ESC{()*+} */
    111 #define	VT100_EMUL_STATE_SCS94_PERCENT	4	/* got ESC{()*+}% */
    112 #define	VT100_EMUL_STATE_SCS96		5	/* got ESC{-./} */
    113 #define	VT100_EMUL_STATE_SCS96_PERCENT	6	/* got ESC{-./}% */
    114 #define	VT100_EMUL_STATE_ESC_HASH	7	/* got ESC# */
    115 #define	VT100_EMUL_STATE_ESC_SPC	8	/* got ESC<SPC> */
    116 #define	VT100_EMUL_STATE_STRING		9	/* waiting for ST (ESC\) */
    117 #define	VT100_EMUL_STATE_STRING_ESC	10	/* waiting for ST, got ESC */
    118 #define	VT100_EMUL_STATE_DCS		11	/* got DCS (ESC P) */
    119 #define	VT100_EMUL_STATE_DCS_DOLLAR	12	/* got DCS<p>$ */
    120 
    121 vt100_handler *vt100_output[] = {
    122 	wsemul_vt100_output_esc,
    123 	wsemul_vt100_output_csi,
    124 	wsemul_vt100_output_scs94,
    125 	wsemul_vt100_output_scs94_percent,
    126 	wsemul_vt100_output_scs96,
    127 	wsemul_vt100_output_scs96_percent,
    128 	wsemul_vt100_output_esc_hash,
    129 	wsemul_vt100_output_esc_spc,
    130 	wsemul_vt100_output_string,
    131 	wsemul_vt100_output_string_esc,
    132 	wsemul_vt100_output_dcs,
    133 	wsemul_vt100_output_dcs_dollar,
    134 };
    135 
    136 static void
    137 wsemul_vt100_init(struct wsemul_vt100_emuldata *edp,
    138 	const struct wsscreen_descr *type, void *cookie, int ccol, int crow,
    139 	long defattr)
    140 {
    141 	struct vt100base_data *vd = &edp->bd;
    142 	int error;
    143 
    144 	vd->emulops = type->textops;
    145 	vd->emulcookie = cookie;
    146 	vd->scrcapabilities = type->capabilities;
    147 	vd->nrows = type->nrows;
    148 	vd->ncols = type->ncols;
    149 	vd->crow = crow;
    150 	vd->ccol = ccol;
    151 
    152 	/* The underlying driver has already allocated a default and simple
    153 	 * attribute for us, which is stored in defattr.  We try to set the
    154 	 * values specified by the kernel options below, but in case of
    155 	 * failure we fallback to the value given by the driver. */
    156 
    157 	if (type->capabilities & WSSCREEN_WSCOLORS) {
    158 		vd->msgattrs.default_attrs = WS_DEFAULT_COLATTR |
    159 		    WSATTR_WSCOLORS;
    160 		vd->msgattrs.default_bg = WS_DEFAULT_BG;
    161 		vd->msgattrs.default_fg = WS_DEFAULT_FG;
    162 
    163 		vd->msgattrs.kernel_attrs = WS_KERNEL_COLATTR |
    164 		    WSATTR_WSCOLORS;
    165 		vd->msgattrs.kernel_bg = WS_KERNEL_BG;
    166 		vd->msgattrs.kernel_fg = WS_KERNEL_FG;
    167 	} else {
    168 		vd->msgattrs.default_attrs = WS_DEFAULT_MONOATTR;
    169 		vd->msgattrs.default_bg = vd->msgattrs.default_fg = 0;
    170 
    171 		vd->msgattrs.kernel_attrs = WS_KERNEL_MONOATTR;
    172 		vd->msgattrs.kernel_bg = vd->msgattrs.kernel_fg = 0;
    173 	}
    174 
    175 	error = (*vd->emulops->allocattr)(cookie,
    176 					   vd->msgattrs.default_fg,
    177 					   vd->msgattrs.default_bg,
    178 					   vd->msgattrs.default_attrs,
    179 					   &vd->defattr);
    180 	if (error) {
    181 		vd->defattr = defattr;
    182 		/* XXX This assumes the driver has allocated white on black
    183 		 * XXX as the default attribute, which is not always true.
    184 		 * XXX Maybe we need an emulop that, given an attribute,
    185 		 * XXX (defattr) returns its flags and colors? */
    186 		vd->msgattrs.default_attrs = 0;
    187 		vd->msgattrs.default_bg = WSCOL_BLACK;
    188 		vd->msgattrs.default_fg = WSCOL_WHITE;
    189 	} else {
    190 		if (vd->emulops->replaceattr != NULL)
    191 			(*vd->emulops->replaceattr)(cookie, defattr,
    192 			                             vd->defattr);
    193 	}
    194 
    195 #if defined(WS_KERNEL_CUSTOMIZED)
    196 	/* Set up kernel colors, in case they were customized by the user;
    197 	 * otherwise default to the colors specified for the console.
    198 	 * In case of failure, we use console colors too; we can assume
    199 	 * they are good as they have been previously allocated and
    200 	 * verified. */
    201 	error = (*vd->emulops->allocattr)(cookie,
    202 					   vd->msgattrs.kernel_fg,
    203 					   vd->msgattrs.kernel_bg,
    204 					   vd->msgattrs.kernel_attrs,
    205 					   &edp->kernattr);
    206 	if (error)
    207 #endif
    208 	edp->kernattr = vd->defattr;
    209 }
    210 
    211 void *
    212 wsemul_vt100_cnattach(const struct wsscreen_descr *type, void *cookie,
    213 	int ccol, int crow, long defattr)
    214 {
    215 	struct wsemul_vt100_emuldata *edp;
    216 	struct vt100base_data *vd;
    217 
    218 	edp = &wsemul_vt100_console_emuldata;
    219 	vd = &edp->bd;
    220 	wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    221 #ifdef DIAGNOSTIC
    222 	edp->console = 1;
    223 #endif
    224 	vd->cbcookie = NULL;
    225 
    226 	vd->tabs = 0;
    227 	vd->dblwid = 0;
    228 	vd->dw = 0;
    229 	vd->dcsarg = 0;
    230 	edp->isolatin1tab = edp->decgraphtab = edp->dectechtab = 0;
    231 	edp->nrctab = 0;
    232 	wsemul_vt100_reset(edp);
    233 	return (edp);
    234 }
    235 
    236 void *
    237 wsemul_vt100_attach(int console, const struct wsscreen_descr *type,
    238 	void *cookie, int ccol, int crow, void *cbcookie, long defattr)
    239 {
    240 	struct wsemul_vt100_emuldata *edp;
    241 	struct vt100base_data *vd;
    242 
    243 	if (console) {
    244 		edp = &wsemul_vt100_console_emuldata;
    245 		KASSERT(edp->console == 1);
    246 	} else {
    247 		edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
    248 		wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    249 #ifdef DIAGNOSTIC
    250 		edp->console = 0;
    251 #endif
    252 	}
    253 	vd = &edp->bd;
    254 	vd->cbcookie = cbcookie;
    255 
    256 	vd->tabs = malloc(1024, M_DEVBUF, M_NOWAIT);
    257 	vd->dblwid = malloc(1024, M_DEVBUF, M_NOWAIT|M_ZERO);
    258 	vd->dw = 0;
    259 	vd->dcsarg = malloc(DCS_MAXLEN, M_DEVBUF, M_NOWAIT);
    260 	edp->isolatin1tab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    261 	edp->decgraphtab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    262 	edp->dectechtab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    263 	edp->nrctab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    264 	vt100_initchartables(edp);
    265 	wsemul_vt100_reset(edp);
    266 	return (edp);
    267 }
    268 
    269 void
    270 wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp)
    271 {
    272 	struct wsemul_vt100_emuldata *edp = cookie;
    273 	struct vt100base_data *vd = &edp->bd;
    274 
    275 	*crowp = vd->crow;
    276 	*ccolp = vd->ccol;
    277 #define f(ptr) if (ptr) {free(ptr, M_DEVBUF); ptr = 0;}
    278 	f(vd->tabs)
    279 	f(vd->dblwid)
    280 	f(vd->dcsarg)
    281 	f(edp->isolatin1tab)
    282 	f(edp->decgraphtab)
    283 	f(edp->dectechtab)
    284 	f(edp->nrctab)
    285 #undef f
    286 	if (edp != &wsemul_vt100_console_emuldata)
    287 		free(edp, M_DEVBUF);
    288 }
    289 
    290 static void
    291 wsemul_vt100_resize(void * cookie, const struct wsscreen_descr *type)
    292 {
    293 	struct wsemul_vt100_emuldata *edp = cookie;
    294 
    295 	edp->bd.nrows = type->nrows;
    296 	edp->bd.ncols = type->ncols;
    297 	wsemul_vt100_reset(edp);
    298 	wsemul_vt100_resetop(cookie, WSEMUL_CLEARSCREEN);
    299 }
    300 
    301 void
    302 wsemul_vt100_resetop(void *cookie, enum wsemul_resetops op)
    303 {
    304 	struct wsemul_vt100_emuldata *edp = cookie;
    305 	struct vt100base_data *vd = &edp->bd;
    306 
    307 	switch (op) {
    308 	case WSEMUL_RESET:
    309 		wsemul_vt100_reset(edp);
    310 		break;
    311 	case WSEMUL_SYNCFONT:
    312 		vt100_initchartables(edp);
    313 		break;
    314 	case WSEMUL_CLEARSCREEN:
    315 		wsemul_vt100_ed(vd, 2);
    316 		vd->ccol = vd->crow = 0;
    317 		(*vd->emulops->cursor)(vd->emulcookie,
    318 					vd->flags & VTFL_CURSORON, 0, 0);
    319 		break;
    320 	default:
    321 		break;
    322 	}
    323 }
    324 
    325 void
    326 wsemul_vt100_reset(struct wsemul_vt100_emuldata *edp)
    327 {
    328 	struct vt100base_data *vd = &edp->bd;
    329 	int i;
    330 
    331 	edp->state = VT100_EMUL_STATE_NORMAL;
    332 	vd->flags = VTFL_DECAWM | VTFL_CURSORON;
    333 	vd->bkgdattr = vd->curattr = vd->defattr;
    334 	vd->attrflags = vd->msgattrs.default_attrs;
    335 	vd->fgcol = vd->msgattrs.default_fg;
    336 	vd->bgcol = vd->msgattrs.default_bg;
    337 	vd->scrreg_startrow = 0;
    338 	vd->scrreg_nrows = vd->nrows;
    339 	if (vd->tabs) {
    340 		memset(vd->tabs, 0, vd->ncols);
    341 		for (i = 8; i < vd->ncols; i += 8)
    342 			vd->tabs[i] = 1;
    343 	}
    344 	vd->dcspos = 0;
    345 	vd->dcstype = 0;
    346 	edp->chartab_G[0] = 0;
    347 	edp->chartab_G[1] = edp->nrctab; /* ??? */
    348 	edp->chartab_G[2] = edp->isolatin1tab;
    349 	edp->chartab_G[3] = edp->isolatin1tab;
    350 	edp->chartab0 = 0;
    351 	edp->chartab1 = 2;
    352 	edp->sschartab = 0;
    353 }
    354 
    355 /*
    356  * now all the state machine bits
    357  */
    358 
    359 /*
    360  * Move the cursor to the next line if possible. If the cursor is at
    361  * the bottom of the scroll area, then scroll it up. If the cursor is
    362  * at the bottom of the screen then don't move it down.
    363  */
    364 static void
    365 wsemul_vt100_nextline(struct wsemul_vt100_emuldata *edp)
    366 {
    367 	struct vt100base_data *vd = &edp->bd;
    368 
    369 	if (ROWS_BELOW(vd) == 0) {
    370 		/* Bottom of the scroll region. */
    371 	  	wsemul_vt100_scrollup(vd, 1);
    372 	} else {
    373 		if ((vd->crow+1) < vd->nrows)
    374 			/* Cursor not at the bottom of the screen. */
    375 			vd->crow++;
    376 		CHECK_DW(vd);
    377 	}
    378 }
    379 
    380 static void
    381 wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *edp, u_char c,
    382 	int kernel)
    383 {
    384 	struct vt100base_data *vd = &edp->bd;
    385 	u_int *ct, dc;
    386 
    387 	if ((vd->flags & (VTFL_LASTCHAR | VTFL_DECAWM)) ==
    388 	    (VTFL_LASTCHAR | VTFL_DECAWM)) {
    389 		wsemul_vt100_nextline(edp);
    390 		vd->ccol = 0;
    391 		vd->flags &= ~VTFL_LASTCHAR;
    392 	}
    393 
    394 	if (c & 0x80) {
    395 		c &= 0x7f;
    396 		ct = edp->chartab_G[edp->chartab1];
    397 	} else {
    398 		if (edp->sschartab) {
    399 			ct = edp->chartab_G[edp->sschartab];
    400 			edp->sschartab = 0;
    401 		} else
    402 			ct = edp->chartab_G[edp->chartab0];
    403 	}
    404 	dc = (ct ? ct[c] : c);
    405 
    406 	if ((vd->flags & VTFL_INSERTMODE) && COLS_LEFT(vd))
    407 		COPYCOLS(vd, vd->ccol, vd->ccol + 1, COLS_LEFT(vd));
    408 
    409 	(*vd->emulops->putchar)(vd->emulcookie, vd->crow,
    410 				 vd->ccol << vd->dw, dc,
    411 				 kernel ? edp->kernattr : vd->curattr);
    412 
    413 	if (COLS_LEFT(vd))
    414 		vd->ccol++;
    415 	else
    416 		vd->flags |= VTFL_LASTCHAR;
    417 }
    418 
    419 static void
    420 wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *edp, u_char c,
    421 	int kernel)
    422 {
    423 	struct vt100base_data *vd = &edp->bd;
    424 	u_int n;
    425 
    426 	switch (c) {
    427 	case ASCII_NUL:
    428 	default:
    429 		/* ignore */
    430 		break;
    431 	case ASCII_BEL:
    432 		wsdisplay_emulbell(vd->cbcookie);
    433 		break;
    434 	case ASCII_BS:
    435 		if (vd->ccol > 0) {
    436 			vd->ccol--;
    437 			vd->flags &= ~VTFL_LASTCHAR;
    438 		}
    439 		break;
    440 	case ASCII_CR:
    441 		vd->ccol = 0;
    442 		vd->flags &= ~VTFL_LASTCHAR;
    443 		break;
    444 	case ASCII_HT:
    445 		if (vd->tabs) {
    446 			if (!COLS_LEFT(vd))
    447 				break;
    448 			for (n = vd->ccol + 1; n < NCOLS(vd) - 1; n++)
    449 				if (vd->tabs[n])
    450 					break;
    451 		} else {
    452 			n = vd->ccol + min(8 - (vd->ccol & 7), COLS_LEFT(vd));
    453 		}
    454 		vd->ccol = n;
    455 		break;
    456 	case ASCII_SO: /* LS1 */
    457 		edp->chartab0 = 1;
    458 		break;
    459 	case ASCII_SI: /* LS0 */
    460 		edp->chartab0 = 0;
    461 		break;
    462 	case ASCII_ESC:
    463 		if (kernel) {
    464 			printf("wsemul_vt100_output_c0c1: ESC in kernel output ignored\n");
    465 			break;	/* ignore the ESC */
    466 		}
    467 
    468 		if (edp->state == VT100_EMUL_STATE_STRING) {
    469 			/* might be a string end */
    470 			edp->state = VT100_EMUL_STATE_STRING_ESC;
    471 		} else {
    472 			/* XXX cancel current escape sequence */
    473 			edp->state = VT100_EMUL_STATE_ESC;
    474 		}
    475 		break;
    476 #if 0
    477 	case CSI: /* 8-bit */
    478 		/* XXX cancel current escape sequence */
    479 		edp->nargs = 0;
    480 		memset(edp->args, 0, sizeof (edp->args));
    481 		edp->modif1 = edp->modif2 = '\0';
    482 		edp->state = VT100_EMUL_STATE_CSI;
    483 		break;
    484 	case DCS: /* 8-bit */
    485 		/* XXX cancel current escape sequence */
    486 		edp->nargs = 0;
    487 		memset(edp->args, 0, sizeof (edp->args));
    488 		edp->state = VT100_EMUL_STATE_DCS;
    489 		break;
    490 	case ST: /* string end 8-bit */
    491 		/* XXX only in VT100_EMUL_STATE_STRING */
    492 		wsemul_vt100_handle_dcs(edp);
    493 		return (VT100_EMUL_STATE_NORMAL);
    494 #endif
    495 	case ASCII_LF:
    496 	case ASCII_VT:
    497 	case ASCII_FF:
    498 		wsemul_vt100_nextline(edp);
    499 		break;
    500 	}
    501 }
    502 
    503 static u_int
    504 wsemul_vt100_output_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    505 {
    506 	struct vt100base_data *vd = &edp->bd;
    507 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    508 	int i;
    509 
    510 	switch (c) {
    511 	case '[': /* CSI */
    512 		vd->nargs = 0;
    513 		memset(vd->args, 0, sizeof (vd->args));
    514 		vd->modif1 = vd->modif2 = '\0';
    515 		newstate = VT100_EMUL_STATE_CSI;
    516 		break;
    517 	case '7': /* DECSC */
    518 		vd->flags |= VTFL_SAVEDCURS;
    519 		edp->savedcursor_row = vd->crow;
    520 		edp->savedcursor_col = vd->ccol;
    521 		edp->savedattr = vd->curattr;
    522 		edp->savedbkgdattr = vd->bkgdattr;
    523 		edp->savedattrflags = vd->attrflags;
    524 		edp->savedfgcol = vd->fgcol;
    525 		edp->savedbgcol = vd->bgcol;
    526 		for (i = 0; i < 4; i++)
    527 			edp->savedchartab_G[i] = edp->chartab_G[i];
    528 		edp->savedchartab0 = edp->chartab0;
    529 		edp->savedchartab1 = edp->chartab1;
    530 		break;
    531 	case '8': /* DECRC */
    532 		if ((vd->flags & VTFL_SAVEDCURS) == 0)
    533 			break;
    534 		vd->crow = edp->savedcursor_row;
    535 		vd->ccol = edp->savedcursor_col;
    536 		vd->curattr = edp->savedattr;
    537 		vd->bkgdattr = edp->savedbkgdattr;
    538 		vd->attrflags = edp->savedattrflags;
    539 		vd->fgcol = edp->savedfgcol;
    540 		vd->bgcol = edp->savedbgcol;
    541 		for (i = 0; i < 4; i++)
    542 			edp->chartab_G[i] = edp->savedchartab_G[i];
    543 		edp->chartab0 = edp->savedchartab0;
    544 		edp->chartab1 = edp->savedchartab1;
    545 		break;
    546 	case '=': /* DECKPAM application mode */
    547 		vd->flags |= VTFL_APPLKEYPAD;
    548 		break;
    549 	case '>': /* DECKPNM numeric mode */
    550 		vd->flags &= ~VTFL_APPLKEYPAD;
    551 		break;
    552 	case 'E': /* NEL */
    553 		vd->ccol = 0;
    554 		/* FALLTHRU */
    555 	case 'D': /* IND */
    556 		wsemul_vt100_nextline(edp);
    557 		break;
    558 	case 'H': /* HTS */
    559 		KASSERT(vd->tabs != 0);
    560 		vd->tabs[vd->ccol] = 1;
    561 		break;
    562 	case '~': /* LS1R */
    563 		edp->chartab1 = 1;
    564 		break;
    565 	case 'n': /* LS2 */
    566 		edp->chartab0 = 2;
    567 		break;
    568 	case '}': /* LS2R */
    569 		edp->chartab1 = 2;
    570 		break;
    571 	case 'o': /* LS3 */
    572 		edp->chartab0 = 3;
    573 		break;
    574 	case '|': /* LS3R */
    575 		edp->chartab1 = 3;
    576 		break;
    577 	case 'N': /* SS2 */
    578 		edp->sschartab = 2;
    579 		break;
    580 	case 'O': /* SS3 */
    581 		edp->sschartab = 3;
    582 		break;
    583 	case 'M': /* RI */
    584 		if (ROWS_ABOVE(vd) > 0) {
    585 			vd->crow--;
    586 			CHECK_DW(vd);
    587 			break;
    588 		}
    589 		wsemul_vt100_scrolldown(vd, 1);
    590 		break;
    591 	case 'P': /* DCS */
    592 		vd->nargs = 0;
    593 		memset(vd->args, 0, sizeof (vd->args));
    594 		newstate = VT100_EMUL_STATE_DCS;
    595 		break;
    596 	case 'c': /* RIS */
    597 		wsemul_vt100_reset(edp);
    598 		wsemul_vt100_ed(vd, 2);
    599 		vd->ccol = vd->crow = 0;
    600 		break;
    601 	case '(': case ')': case '*': case '+': /* SCS */
    602 		edp->designating = c - '(';
    603 		newstate = VT100_EMUL_STATE_SCS94;
    604 		break;
    605 	case '-': case '.': case '/': /* SCS */
    606 		edp->designating = c - '-' + 1;
    607 		newstate = VT100_EMUL_STATE_SCS96;
    608 		break;
    609 	case '#':
    610 		newstate = VT100_EMUL_STATE_ESC_HASH;
    611 		break;
    612 	case ' ': /* 7/8 bit */
    613 		newstate = VT100_EMUL_STATE_ESC_SPC;
    614 		break;
    615 	case ']': /* OSC operating system command */
    616 	case '^': /* PM privacy message */
    617 	case '_': /* APC application program command */
    618 		/* ignored */
    619 		newstate = VT100_EMUL_STATE_STRING;
    620 		break;
    621 	case '<': /* exit VT52 mode - ignored */
    622 		break;
    623 	default:
    624 #ifdef VT100_PRINTUNKNOWN
    625 		printf("ESC%c unknown\n", c);
    626 #endif
    627 		break;
    628 	}
    629 
    630 	return (newstate);
    631 }
    632 
    633 static u_int
    634 wsemul_vt100_output_scs94(struct wsemul_vt100_emuldata *edp, u_char c)
    635 {
    636 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    637 
    638 	switch (c) {
    639 	case '%': /* probably DEC supplemental graphic */
    640 		newstate = VT100_EMUL_STATE_SCS94_PERCENT;
    641 		break;
    642 	case 'A': /* british / national */
    643 		edp->chartab_G[edp->designating] = edp->nrctab;
    644 		break;
    645 	case 'B': /* ASCII */
    646 		edp->chartab_G[edp->designating] = 0;
    647 		break;
    648 	case '<': /* user preferred supplemental */
    649 		/* XXX not really "user" preferred */
    650 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    651 		break;
    652 	case '0': /* DEC special graphic */
    653 		edp->chartab_G[edp->designating] = edp->decgraphtab;
    654 		break;
    655 	case '>': /* DEC tech */
    656 		edp->chartab_G[edp->designating] = edp->dectechtab;
    657 		break;
    658 	default:
    659 #ifdef VT100_PRINTUNKNOWN
    660 		printf("ESC%c%c unknown\n", edp->designating + '(', c);
    661 #endif
    662 		break;
    663 	}
    664 	return (newstate);
    665 }
    666 
    667 static u_int
    668 wsemul_vt100_output_scs94_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    669 {
    670 	switch (c) {
    671 	case '5': /* DEC supplemental graphic */
    672 		/* XXX there are differences */
    673 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    674 		break;
    675 	default:
    676 #ifdef VT100_PRINTUNKNOWN
    677 		printf("ESC%c%%%c unknown\n", edp->designating + '(', c);
    678 #endif
    679 		break;
    680 	}
    681 	return (VT100_EMUL_STATE_NORMAL);
    682 }
    683 
    684 static u_int
    685 wsemul_vt100_output_scs96(struct wsemul_vt100_emuldata *edp, u_char c)
    686 {
    687 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    688 	int nrc;
    689 
    690 	switch (c) {
    691 	case '%': /* probably portuguese */
    692 		newstate = VT100_EMUL_STATE_SCS96_PERCENT;
    693 		break;
    694 	case 'A': /* ISO-latin-1 supplemental */
    695 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    696 		break;
    697 	case '4': /* dutch */
    698 		nrc = 1;
    699 		goto setnrc;
    700 	case '5': case 'C': /* finnish */
    701 		nrc = 2;
    702 		goto setnrc;
    703 	case 'R': /* french */
    704 		nrc = 3;
    705 		goto setnrc;
    706 	case 'Q': /* french canadian */
    707 		nrc = 4;
    708 		goto setnrc;
    709 	case 'K': /* german */
    710 		nrc = 5;
    711 		goto setnrc;
    712 	case 'Y': /* italian */
    713 		nrc = 6;
    714 		goto setnrc;
    715 	case 'E': case '6': /* norwegian / danish */
    716 		nrc = 7;
    717 		goto setnrc;
    718 	case 'Z': /* spanish */
    719 		nrc = 9;
    720 		goto setnrc;
    721 	case '7': case 'H': /* swedish */
    722 		nrc = 10;
    723 		goto setnrc;
    724 	case '=': /* swiss */
    725 		nrc = 11;
    726 setnrc:
    727 		vt100_setnrc(edp, nrc); /* what table ??? */
    728 		break;
    729 	default:
    730 #ifdef VT100_PRINTUNKNOWN
    731 		printf("ESC%c%c unknown\n", edp->designating + '-' - 1, c);
    732 #endif
    733 		break;
    734 	}
    735 	return (newstate);
    736 }
    737 
    738 static u_int
    739 wsemul_vt100_output_scs96_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    740 {
    741 	switch (c) {
    742 	case '6': /* portuguese */
    743 		vt100_setnrc(edp, 8);
    744 		break;
    745 	default:
    746 #ifdef VT100_PRINTUNKNOWN
    747 		printf("ESC%c%%%c unknown\n", edp->designating + '-', c);
    748 #endif
    749 		break;
    750 	}
    751 	return (VT100_EMUL_STATE_NORMAL);
    752 }
    753 
    754 static u_int
    755 wsemul_vt100_output_esc_spc(struct wsemul_vt100_emuldata *edp,
    756     u_char c)
    757 {
    758 	switch (c) {
    759 	case 'F': /* 7-bit controls */
    760 	case 'G': /* 8-bit controls */
    761 #ifdef VT100_PRINTNOTIMPL
    762 		printf("ESC<SPC>%c ignored\n", c);
    763 #endif
    764 		break;
    765 	default:
    766 #ifdef VT100_PRINTUNKNOWN
    767 		printf("ESC<SPC>%c unknown\n", c);
    768 #endif
    769 		break;
    770 	}
    771 	return (VT100_EMUL_STATE_NORMAL);
    772 }
    773 
    774 static u_int
    775 wsemul_vt100_output_string(struct wsemul_vt100_emuldata *edp, u_char c)
    776 {
    777 	struct vt100base_data *vd = &edp->bd;
    778 
    779 	if (vd->dcstype && vd->dcspos < DCS_MAXLEN)
    780 		vd->dcsarg[vd->dcspos++] = c;
    781 	return (VT100_EMUL_STATE_STRING);
    782 }
    783 
    784 static u_int
    785 wsemul_vt100_output_string_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    786 {
    787 	struct vt100base_data *vd = &edp->bd;
    788 
    789 	if (c == '\\') { /* ST complete */
    790 		wsemul_vt100_handle_dcs(vd);
    791 		return (VT100_EMUL_STATE_NORMAL);
    792 	} else
    793 		return (VT100_EMUL_STATE_STRING);
    794 }
    795 
    796 static u_int
    797 wsemul_vt100_output_dcs(struct wsemul_vt100_emuldata *edp, u_char c)
    798 {
    799 	struct vt100base_data *vd = &edp->bd;
    800 	u_int newstate = VT100_EMUL_STATE_DCS;
    801 
    802 	switch (c) {
    803 	case '0': case '1': case '2': case '3': case '4':
    804 	case '5': case '6': case '7': case '8': case '9':
    805 		/* argument digit */
    806 		if (vd->nargs > VT100_EMUL_NARGS - 1)
    807 			break;
    808 		vd->args[vd->nargs] = (vd->args[vd->nargs] * 10) +
    809 		    (c - '0');
    810 		break;
    811 	case ';': /* argument terminator */
    812 		vd->nargs++;
    813 		break;
    814 	default:
    815 		vd->nargs++;
    816 		if (vd->nargs > VT100_EMUL_NARGS) {
    817 #ifdef VT100_DEBUG
    818 			printf("vt100: too many arguments\n");
    819 #endif
    820 			vd->nargs = VT100_EMUL_NARGS;
    821 		}
    822 		newstate = VT100_EMUL_STATE_STRING;
    823 		switch (c) {
    824 		case '$':
    825 			newstate = VT100_EMUL_STATE_DCS_DOLLAR;
    826 			break;
    827 		case '{': /* DECDLD soft charset */
    828 		case '!': /* DECRQUPSS user preferred supplemental set */
    829 			/* 'u' must follow - need another state */
    830 		case '|': /* DECUDK program F6..F20 */
    831 #ifdef VT100_PRINTNOTIMPL
    832 			printf("DCS%c ignored\n", c);
    833 #endif
    834 			break;
    835 		default:
    836 #ifdef VT100_PRINTUNKNOWN
    837 			printf("DCS%c (%d, %d) unknown\n", c, ARG(vd, 0), ARG(vd, 1));
    838 #endif
    839 			break;
    840 		}
    841 	}
    842 
    843 	return (newstate);
    844 }
    845 
    846 static u_int
    847 wsemul_vt100_output_dcs_dollar(struct wsemul_vt100_emuldata *edp, u_char c)
    848 {
    849 	struct vt100base_data *vd = &edp->bd;
    850 
    851 	switch (c) {
    852 	case 'p': /* DECRSTS terminal state restore */
    853 	case 'q': /* DECRQSS control function request */
    854 #ifdef VT100_PRINTNOTIMPL
    855 		printf("DCS$%c ignored\n", c);
    856 #endif
    857 		break;
    858 	case 't': /* DECRSPS restore presentation state */
    859 		switch (ARG(vd, 0)) {
    860 		case 0: /* error */
    861 			break;
    862 		case 1: /* cursor information restore */
    863 #ifdef VT100_PRINTNOTIMPL
    864 			printf("DCS1$t ignored\n");
    865 #endif
    866 			break;
    867 		case 2: /* tab stop restore */
    868 			vd->dcspos = 0;
    869 			vd->dcstype = DCSTYPE_TABRESTORE;
    870 			break;
    871 		default:
    872 #ifdef VT100_PRINTUNKNOWN
    873 			printf("DCS%d$t unknown\n", ARG(vd, 0));
    874 #endif
    875 			break;
    876 		}
    877 		break;
    878 	default:
    879 #ifdef VT100_PRINTUNKNOWN
    880 		printf("DCS$%c (%d, %d) unknown\n", c, ARG(vd, 0), ARG(vd, 1));
    881 #endif
    882 		break;
    883 	}
    884 	return (VT100_EMUL_STATE_STRING);
    885 }
    886 
    887 static u_int
    888 wsemul_vt100_output_esc_hash(struct wsemul_vt100_emuldata *edp, u_char c)
    889 {
    890 	struct vt100base_data *vd = &edp->bd;
    891 	int i, j;
    892 
    893 	switch (c) {
    894 	case '5': /*  DECSWL single width, single height */
    895 		if (vd->dw) {
    896 			for (i = 0; i < vd->ncols / 2; i++)
    897 				(*vd->emulops->copycols)(vd->emulcookie,
    898 							  vd->crow,
    899 							  2 * i, i, 1);
    900 			(*vd->emulops->erasecols)(vd->emulcookie, vd->crow,
    901 						   i, vd->ncols - i,
    902 						   vd->bkgdattr);
    903 			vd->dblwid[vd->crow] = 0;
    904 			vd->dw = 0;
    905 		}
    906 		break;
    907 	case '6': /*  DECDWL double width, single height */
    908 	case '3': /*  DECDHL double width, double height, top half */
    909 	case '4': /*  DECDHL double width, double height, bottom half */
    910 		if (!vd->dw) {
    911 			for (i = vd->ncols / 2 - 1; i >= 0; i--)
    912 				(*vd->emulops->copycols)(vd->emulcookie,
    913 							  vd->crow,
    914 							  i, 2 * i, 1);
    915 			for (i = 0; i < vd->ncols / 2; i++)
    916 				(*vd->emulops->erasecols)(vd->emulcookie,
    917 							   vd->crow,
    918 							   2 * i + 1, 1,
    919 							   vd->bkgdattr);
    920 			vd->dblwid[vd->crow] = 1;
    921 			vd->dw = 1;
    922 			if (vd->ccol > (vd->ncols >> 1) - 1)
    923 				vd->ccol = (vd->ncols >> 1) - 1;
    924 		}
    925 		break;
    926 	case '8': /* DECALN */
    927 		for (i = 0; i < vd->nrows; i++)
    928 			for (j = 0; j < vd->ncols; j++)
    929 				(*vd->emulops->putchar)(vd->emulcookie, i, j,
    930 							 'E', vd->curattr);
    931 		vd->ccol = 0;
    932 		vd->crow = 0;
    933 		break;
    934 	default:
    935 #ifdef VT100_PRINTUNKNOWN
    936 		printf("ESC#%c unknown\n", c);
    937 #endif
    938 		break;
    939 	}
    940 	return (VT100_EMUL_STATE_NORMAL);
    941 }
    942 
    943 static u_int
    944 wsemul_vt100_output_csi(struct wsemul_vt100_emuldata *edp, u_char c)
    945 {
    946 	struct vt100base_data *vd = &edp->bd;
    947 	u_int newstate = VT100_EMUL_STATE_CSI;
    948 
    949 	switch (c) {
    950 	case '0': case '1': case '2': case '3': case '4':
    951 	case '5': case '6': case '7': case '8': case '9':
    952 		/* argument digit */
    953 		if (vd->nargs > VT100_EMUL_NARGS - 1)
    954 			break;
    955 		vd->args[vd->nargs] = (vd->args[vd->nargs] * 10) +
    956 		    (c - '0');
    957 		break;
    958 	case ';': /* argument terminator */
    959 		vd->nargs++;
    960 		break;
    961 	case '?': /* DEC specific */
    962 	case '>': /* DA query */
    963 		vd->modif1 = c;
    964 		break;
    965 	case '!':
    966 	case '"':
    967 	case '$':
    968 	case '&':
    969 		vd->modif2 = c;
    970 		break;
    971 	default: /* end of escape sequence */
    972 		vd->nargs++;
    973 		if (vd->nargs > VT100_EMUL_NARGS) {
    974 #ifdef VT100_DEBUG
    975 			printf("vt100: too many arguments\n");
    976 #endif
    977 			vd->nargs = VT100_EMUL_NARGS;
    978 		}
    979 		wsemul_vt100_handle_csi(vd, c);
    980 		newstate = VT100_EMUL_STATE_NORMAL;
    981 		break;
    982 	}
    983 	return (newstate);
    984 }
    985 
    986 void
    987 wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int kernel)
    988 {
    989 	struct wsemul_vt100_emuldata *edp = cookie;
    990 	struct vt100base_data *vd = &edp->bd;
    991 
    992 #ifdef DIAGNOSTIC
    993 	if (kernel && !edp->console)
    994 		panic("wsemul_vt100_output: kernel output, not console");
    995 #endif
    996 
    997 	if (vd->flags & VTFL_CURSORON)
    998 		(*vd->emulops->cursor)(vd->emulcookie, 0,
    999 					vd->crow, vd->ccol << vd->dw);
   1000 	for (; count > 0; data++, count--) {
   1001 		if ((*data & 0x7f) < 0x20) {
   1002 			wsemul_vt100_output_c0c1(edp, *data, kernel);
   1003 			continue;
   1004 		}
   1005 		if (edp->state == VT100_EMUL_STATE_NORMAL || kernel) {
   1006 			wsemul_vt100_output_normal(edp, *data, kernel);
   1007 			continue;
   1008 		}
   1009 #ifdef DIAGNOSTIC
   1010 		if (edp->state > sizeof(vt100_output) / sizeof(vt100_output[0]))
   1011 			panic("wsemul_vt100: invalid state %d", edp->state);
   1012 #endif
   1013 		edp->state = vt100_output[edp->state - 1](edp, *data);
   1014 	}
   1015 	if (vd->flags & VTFL_CURSORON)
   1016 		(*vd->emulops->cursor)(vd->emulcookie, 1,
   1017 					vd->crow, vd->ccol << vd->dw);
   1018 }
   1019 
   1020 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1021 static void
   1022 wsemul_vt100_getmsgattrs(void *cookie, struct wsdisplay_msgattrs *ma)
   1023 {
   1024 	struct wsemul_vt100_emuldata *edp = cookie;
   1025 	struct vt100base_data *vd = &edp->bd;
   1026 
   1027 	*ma = vd->msgattrs;
   1028 }
   1029 
   1030 static void
   1031 wsemul_vt100_setmsgattrs(void *cookie, const struct wsscreen_descr *type,
   1032                          const struct wsdisplay_msgattrs *ma)
   1033 {
   1034 	int error;
   1035 	long tmp;
   1036 	struct wsemul_vt100_emuldata *edp = cookie;
   1037 	struct vt100base_data *vd = &edp->bd;
   1038 
   1039 	vd->msgattrs = *ma;
   1040 	if (type->capabilities & WSSCREEN_WSCOLORS) {
   1041 		vd->msgattrs.default_attrs |= WSATTR_WSCOLORS;
   1042 		vd->msgattrs.kernel_attrs |= WSATTR_WSCOLORS;
   1043 	} else {
   1044 		vd->msgattrs.default_bg = vd->msgattrs.kernel_bg = 0;
   1045 		vd->msgattrs.default_fg = vd->msgattrs.kernel_fg = 0;
   1046 	}
   1047 
   1048 	error = (*vd->emulops->allocattr)(vd->emulcookie,
   1049 	                                   vd->msgattrs.default_fg,
   1050 					   vd->msgattrs.default_bg,
   1051 	                                   vd->msgattrs.default_attrs,
   1052 	                                   &tmp);
   1053 #ifndef VT100_DEBUG
   1054 	__USE(error);
   1055 #else
   1056 	if (error)
   1057 		printf("vt100: failed to allocate attribute for default "
   1058 		       "messages\n");
   1059 	else
   1060 #endif
   1061 	{
   1062 		if (vd->curattr == vd->defattr) {
   1063 			vd->bkgdattr = vd->curattr = tmp;
   1064 			vd->attrflags = vd->msgattrs.default_attrs;
   1065 			vd->bgcol = vd->msgattrs.default_bg;
   1066 			vd->fgcol = vd->msgattrs.default_fg;
   1067 		} else {
   1068 			edp->savedbkgdattr = edp->savedattr = tmp;
   1069 			edp->savedattrflags = vd->msgattrs.default_attrs;
   1070 			edp->savedbgcol = vd->msgattrs.default_bg;
   1071 			edp->savedfgcol = vd->msgattrs.default_fg;
   1072 		}
   1073 		if (vd->emulops->replaceattr != NULL)
   1074 			(*vd->emulops->replaceattr)(vd->emulcookie,
   1075 			                             vd->defattr, tmp);
   1076 		vd->defattr = tmp;
   1077 	}
   1078 
   1079 	error = (*vd->emulops->allocattr)(vd->emulcookie,
   1080 	                                   vd->msgattrs.kernel_fg,
   1081 					   vd->msgattrs.kernel_bg,
   1082 	                                   vd->msgattrs.kernel_attrs,
   1083 	                                   &tmp);
   1084 #ifdef VT100_DEBUG
   1085 	if (error)
   1086 		printf("vt100: failed to allocate attribute for kernel "
   1087 		       "messages\n");
   1088 	else
   1089 #endif
   1090 	{
   1091 		if (vd->emulops->replaceattr != NULL)
   1092 			(*vd->emulops->replaceattr)(vd->emulcookie,
   1093 			                             edp->kernattr, tmp);
   1094 		edp->kernattr = tmp;
   1095 	}
   1096 }
   1097 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
   1098