Home | History | Annotate | Line # | Download | only in wscons
wsemul_vt100.c revision 1.17.10.1
      1 /* $NetBSD: wsemul_vt100.c,v 1.17.10.1 2003/06/16 14:09:14 grant Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: wsemul_vt100.c,v 1.17.10.1 2003/06/16 14:09:14 grant Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/time.h>
     41 #include <sys/malloc.h>
     42 #include <sys/fcntl.h>
     43 
     44 #include <dev/wscons/wsconsio.h>
     45 #include <dev/wscons/wsdisplayvar.h>
     46 #include <dev/wscons/wsemulvar.h>
     47 #include <dev/wscons/wsemul_vt100var.h>
     48 #include <dev/wscons/ascii.h>
     49 
     50 #include "opt_wskernattr.h"
     51 
     52 void	*wsemul_vt100_cnattach(const struct wsscreen_descr *, void *,
     53 			       int, int, long);
     54 void	*wsemul_vt100_attach(int console, const struct wsscreen_descr *,
     55 			     void *, int, int, void *, long);
     56 void	wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int);
     57 void	wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp);
     58 void	wsemul_vt100_resetop(void *, enum wsemul_resetops);
     59 
     60 const struct wsemul_ops wsemul_vt100_ops = {
     61 	"vt100",
     62 	wsemul_vt100_cnattach,
     63 	wsemul_vt100_attach,
     64 	wsemul_vt100_output,
     65 	wsemul_vt100_translate,
     66 	wsemul_vt100_detach,
     67 	wsemul_vt100_resetop
     68 };
     69 
     70 struct wsemul_vt100_emuldata wsemul_vt100_console_emuldata;
     71 
     72 static void wsemul_vt100_init(struct wsemul_vt100_emuldata *,
     73 			      const struct wsscreen_descr *,
     74 			      void *, int, int, long);
     75 
     76 static void wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *,
     77 				       u_char, int);
     78 static void wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *,
     79 				     u_char, int);
     80 typedef u_int vt100_handler(struct wsemul_vt100_emuldata *, u_char);
     81 static vt100_handler
     82 wsemul_vt100_output_esc,
     83 wsemul_vt100_output_csi,
     84 wsemul_vt100_output_scs94,
     85 wsemul_vt100_output_scs94_percent,
     86 wsemul_vt100_output_scs96,
     87 wsemul_vt100_output_scs96_percent,
     88 wsemul_vt100_output_esc_hash,
     89 wsemul_vt100_output_esc_spc,
     90 wsemul_vt100_output_string,
     91 wsemul_vt100_output_string_esc,
     92 wsemul_vt100_output_dcs,
     93 wsemul_vt100_output_dcs_dollar;
     94 
     95 #define	VT100_EMUL_STATE_NORMAL		0	/* normal processing */
     96 #define	VT100_EMUL_STATE_ESC		1	/* got ESC */
     97 #define	VT100_EMUL_STATE_CSI		2	/* got CSI (ESC[) */
     98 #define	VT100_EMUL_STATE_SCS94		3	/* got ESC{()*+} */
     99 #define	VT100_EMUL_STATE_SCS94_PERCENT	4	/* got ESC{()*+}% */
    100 #define	VT100_EMUL_STATE_SCS96		5	/* got ESC{-./} */
    101 #define	VT100_EMUL_STATE_SCS96_PERCENT	6	/* got ESC{-./}% */
    102 #define	VT100_EMUL_STATE_ESC_HASH	7	/* got ESC# */
    103 #define	VT100_EMUL_STATE_ESC_SPC	8	/* got ESC<SPC> */
    104 #define	VT100_EMUL_STATE_STRING		9	/* waiting for ST (ESC\) */
    105 #define	VT100_EMUL_STATE_STRING_ESC	10	/* waiting for ST, got ESC */
    106 #define	VT100_EMUL_STATE_DCS		11	/* got DCS (ESC P) */
    107 #define	VT100_EMUL_STATE_DCS_DOLLAR	12	/* got DCS<p>$ */
    108 
    109 vt100_handler *vt100_output[] = {
    110 	wsemul_vt100_output_esc,
    111 	wsemul_vt100_output_csi,
    112 	wsemul_vt100_output_scs94,
    113 	wsemul_vt100_output_scs94_percent,
    114 	wsemul_vt100_output_scs96,
    115 	wsemul_vt100_output_scs96_percent,
    116 	wsemul_vt100_output_esc_hash,
    117 	wsemul_vt100_output_esc_spc,
    118 	wsemul_vt100_output_string,
    119 	wsemul_vt100_output_string_esc,
    120 	wsemul_vt100_output_dcs,
    121 	wsemul_vt100_output_dcs_dollar,
    122 };
    123 
    124 static void
    125 wsemul_vt100_init(struct wsemul_vt100_emuldata *edp,
    126 	const struct wsscreen_descr *type, void *cookie, int ccol, int crow,
    127 	long defattr)
    128 {
    129 	edp->emulops = type->textops;
    130 	edp->emulcookie = cookie;
    131 	edp->scrcapabilities = type->capabilities;
    132 	edp->nrows = type->nrows;
    133 	edp->ncols = type->ncols;
    134 	edp->crow = crow;
    135 	edp->ccol = ccol;
    136 	edp->defattr = defattr;
    137 }
    138 
    139 void *
    140 wsemul_vt100_cnattach(const struct wsscreen_descr *type, void *cookie,
    141 	int ccol, int crow, long defattr)
    142 {
    143 	struct wsemul_vt100_emuldata *edp;
    144 #if defined(WS_KERNEL_FG) || defined(WS_KERNEL_BG) || \
    145   defined(WS_KERNEL_COLATTR) || defined(WS_KERNEL_MONOATTR)
    146 	int res;
    147 #endif
    148 
    149 	edp = &wsemul_vt100_console_emuldata;
    150 	wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    151 #ifdef DIAGNOSTIC
    152 	edp->console = 1;
    153 #endif
    154 	edp->cbcookie = NULL;
    155 
    156 #if defined(WS_KERNEL_FG) || defined(WS_KERNEL_BG) || \
    157   defined(WS_KERNEL_COLATTR) || defined(WS_KERNEL_MONOATTR)
    158 #ifndef WS_KERNEL_FG
    159 #define WS_KERNEL_FG WSCOL_WHITE
    160 #endif
    161 #ifndef WS_KERNEL_BG
    162 #define WS_KERNEL_BG WSCOL_BLACK
    163 #endif
    164 #ifndef WS_KERNEL_COLATTR
    165 #define WS_KERNEL_COLATTR 0
    166 #endif
    167 #ifndef WS_KERNEL_MONOATTR
    168 #define WS_KERNEL_MONOATTR 0
    169 #endif
    170 	if (type->capabilities & WSSCREEN_WSCOLORS)
    171 		res = (*edp->emulops->alloc_attr)(cookie,
    172 					    WS_KERNEL_FG, WS_KERNEL_BG,
    173 					    WS_KERNEL_COLATTR | WSATTR_WSCOLORS,
    174 					    &edp->kernattr);
    175 	else
    176 		res = (*edp->emulops->alloc_attr)(cookie, 0, 0,
    177 					    WS_KERNEL_MONOATTR,
    178 					    &edp->kernattr);
    179 	if (res)
    180 #endif
    181 	edp->kernattr = defattr;
    182 
    183 	edp->tabs = 0;
    184 	edp->dblwid = 0;
    185 	edp->dw = 0;
    186 	edp->dcsarg = 0;
    187 	edp->isolatin1tab = edp->decgraphtab = edp->dectechtab = 0;
    188 	edp->nrctab = 0;
    189 	wsemul_vt100_reset(edp);
    190 	return (edp);
    191 }
    192 
    193 void *
    194 wsemul_vt100_attach(int console, const struct wsscreen_descr *type,
    195 	void *cookie, int ccol, int crow, void *cbcookie, long defattr)
    196 {
    197 	struct wsemul_vt100_emuldata *edp;
    198 
    199 	if (console) {
    200 		edp = &wsemul_vt100_console_emuldata;
    201 #ifdef DIAGNOSTIC
    202 		KASSERT(edp->console == 1);
    203 #endif
    204 	} else {
    205 		edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
    206 		wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    207 #ifdef DIAGNOSTIC
    208 		edp->console = 0;
    209 #endif
    210 	}
    211 	edp->cbcookie = cbcookie;
    212 
    213 	edp->tabs = malloc(edp->ncols, M_DEVBUF, M_NOWAIT);
    214 	edp->dblwid = malloc(edp->nrows, M_DEVBUF, M_NOWAIT|M_ZERO);
    215 	edp->dw = 0;
    216 	edp->dcsarg = malloc(DCS_MAXLEN, M_DEVBUF, M_NOWAIT);
    217 	edp->isolatin1tab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    218 	edp->decgraphtab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    219 	edp->dectechtab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    220 	edp->nrctab = malloc(128 * sizeof(int), M_DEVBUF, M_NOWAIT);
    221 	vt100_initchartables(edp);
    222 	wsemul_vt100_reset(edp);
    223 	return (edp);
    224 }
    225 
    226 void
    227 wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp)
    228 {
    229 	struct wsemul_vt100_emuldata *edp = cookie;
    230 
    231 	*crowp = edp->crow;
    232 	*ccolp = edp->ccol;
    233 #define f(ptr) if (ptr) {free(ptr, M_DEVBUF); ptr = 0;}
    234 	f(edp->tabs)
    235 	f(edp->dblwid)
    236 	f(edp->dcsarg)
    237 	f(edp->isolatin1tab)
    238 	f(edp->decgraphtab)
    239 	f(edp->dectechtab)
    240 	f(edp->nrctab)
    241 #undef f
    242 	if (edp != &wsemul_vt100_console_emuldata)
    243 		free(edp, M_DEVBUF);
    244 }
    245 
    246 void
    247 wsemul_vt100_resetop(void *cookie, enum wsemul_resetops op)
    248 {
    249 	struct wsemul_vt100_emuldata *edp = cookie;
    250 
    251 	switch (op) {
    252 	case WSEMUL_RESET:
    253 		wsemul_vt100_reset(edp);
    254 		break;
    255 	case WSEMUL_SYNCFONT:
    256 		vt100_initchartables(edp);
    257 		break;
    258 	case WSEMUL_CLEARSCREEN:
    259 		wsemul_vt100_ed(edp, 2);
    260 		edp->ccol = edp->crow = 0;
    261 		(*edp->emulops->cursor)(edp->emulcookie,
    262 					edp->flags & VTFL_CURSORON, 0, 0);
    263 		break;
    264 	default:
    265 		break;
    266 	}
    267 }
    268 
    269 void
    270 wsemul_vt100_reset(struct wsemul_vt100_emuldata *edp)
    271 {
    272 	int i;
    273 
    274 	edp->state = VT100_EMUL_STATE_NORMAL;
    275 	edp->flags = VTFL_DECAWM | VTFL_CURSORON;
    276 	edp->bkgdattr = edp->curattr = edp->defattr;
    277 	edp->attrflags = 0;
    278 	edp->fgcol = WSCOL_WHITE;
    279 	edp->bgcol = WSCOL_BLACK;
    280 	edp->scrreg_startrow = 0;
    281 	edp->scrreg_nrows = edp->nrows;
    282 	if (edp->tabs) {
    283 		memset(edp->tabs, 0, edp->ncols);
    284 		for (i = 8; i < edp->ncols; i += 8)
    285 			edp->tabs[i] = 1;
    286 	}
    287 	edp->dcspos = 0;
    288 	edp->dcstype = 0;
    289 	edp->chartab_G[0] = 0;
    290 	edp->chartab_G[1] = edp->nrctab; /* ??? */
    291 	edp->chartab_G[2] = edp->isolatin1tab;
    292 	edp->chartab_G[3] = edp->isolatin1tab;
    293 	edp->chartab0 = 0;
    294 	edp->chartab1 = 2;
    295 	edp->sschartab = 0;
    296 }
    297 
    298 /*
    299  * now all the state machine bits
    300  */
    301 
    302 static void
    303 wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *edp, u_char c,
    304 	int kernel)
    305 {
    306 	u_int *ct, dc;
    307 
    308 	if ((edp->flags & (VTFL_LASTCHAR | VTFL_DECAWM)) ==
    309 	    (VTFL_LASTCHAR | VTFL_DECAWM)) {
    310 		if (ROWS_BELOW > 0) {
    311 			edp->crow++;
    312 			CHECK_DW;
    313 		} else
    314 			wsemul_vt100_scrollup(edp, 1);
    315 		edp->ccol = 0;
    316 		edp->flags &= ~VTFL_LASTCHAR;
    317 	}
    318 
    319 	if (c & 0x80) {
    320 		c &= 0x7f;
    321 		ct = edp->chartab_G[edp->chartab1];
    322 	} else {
    323 		if (edp->sschartab) {
    324 			ct = edp->chartab_G[edp->sschartab];
    325 			edp->sschartab = 0;
    326 		} else
    327 			ct = edp->chartab_G[edp->chartab0];
    328 	}
    329 	dc = (ct ? ct[c] : c);
    330 
    331 	if ((edp->flags & VTFL_INSERTMODE) && COLS_LEFT)
    332 		COPYCOLS(edp->ccol, edp->ccol + 1, COLS_LEFT);
    333 
    334 	(*edp->emulops->putchar)(edp->emulcookie, edp->crow,
    335 				 edp->ccol << edp->dw, dc,
    336 				 kernel ? edp->kernattr : edp->curattr);
    337 
    338 	if (COLS_LEFT)
    339 		edp->ccol++;
    340 	else
    341 		edp->flags |= VTFL_LASTCHAR;
    342 }
    343 
    344 static void
    345 wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *edp, u_char c,
    346 	int kernel)
    347 {
    348 	u_int n;
    349 
    350 	switch (c) {
    351 	    case ASCII_NUL:
    352 	    default:
    353 		/* ignore */
    354 		break;
    355 	    case ASCII_BEL:
    356 		wsdisplay_emulbell(edp->cbcookie);
    357 		break;
    358 	    case ASCII_BS:
    359 		if (edp->ccol > 0) {
    360 			edp->ccol--;
    361 			edp->flags &= ~VTFL_LASTCHAR;
    362 		}
    363 		break;
    364 	    case ASCII_CR:
    365 		edp->ccol = 0;
    366 		edp->flags &= ~VTFL_LASTCHAR;
    367 		break;
    368 	    case ASCII_HT:
    369 		if (edp->tabs) {
    370 			if (!COLS_LEFT)
    371 				break;
    372 			for (n = edp->ccol + 1; n < NCOLS - 1; n++)
    373 				if (edp->tabs[n])
    374 					break;
    375 		} else {
    376 			n = edp->ccol + min(8 - (edp->ccol & 7), COLS_LEFT);
    377 		}
    378 		edp->ccol = n;
    379 		break;
    380 	    case ASCII_SO: /* LS1 */
    381 		edp->chartab0 = 1;
    382 		break;
    383 	    case ASCII_SI: /* LS0 */
    384 		edp->chartab0 = 0;
    385 		break;
    386 	    case ASCII_ESC:
    387 		if (kernel) {
    388 			printf("wsemul_vt100_output_c0c1: ESC in kernel output ignored\n");
    389 			break;	/* ignore the ESC */
    390 		}
    391 
    392 		if (edp->state == VT100_EMUL_STATE_STRING) {
    393 			/* might be a string end */
    394 			edp->state = VT100_EMUL_STATE_STRING_ESC;
    395 		} else {
    396 			/* XXX cancel current escape sequence */
    397 			edp->state = VT100_EMUL_STATE_ESC;
    398 		}
    399 		break;
    400 #if 0
    401 	    case CSI: /* 8-bit */
    402 		/* XXX cancel current escape sequence */
    403 		edp->nargs = 0;
    404 		memset(edp->args, 0, sizeof (edp->args));
    405 		edp->modif1 = edp->modif2 = '\0';
    406 		edp->state = VT100_EMUL_STATE_CSI;
    407 		break;
    408 	    case DCS: /* 8-bit */
    409 		/* XXX cancel current escape sequence */
    410 		edp->nargs = 0;
    411 		memset(edp->args, 0, sizeof (edp->args));
    412 		edp->state = VT100_EMUL_STATE_DCS;
    413 		break;
    414 	    case ST: /* string end 8-bit */
    415 		/* XXX only in VT100_EMUL_STATE_STRING */
    416 		wsemul_vt100_handle_dcs(edp);
    417 		return (VT100_EMUL_STATE_NORMAL);
    418 #endif
    419 	    case ASCII_LF:
    420 	    case ASCII_VT:
    421 	    case ASCII_FF:
    422 		if (ROWS_BELOW > 0) {
    423 			edp->crow++;
    424 			CHECK_DW;
    425 		} else
    426 			wsemul_vt100_scrollup(edp, 1);
    427 		break;
    428 	}
    429 }
    430 
    431 static u_int
    432 wsemul_vt100_output_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    433 {
    434 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    435 	int i;
    436 
    437 	switch (c) {
    438 	    case '[': /* CSI */
    439 		edp->nargs = 0;
    440 		memset(edp->args, 0, sizeof (edp->args));
    441 		edp->modif1 = edp->modif2 = '\0';
    442 		newstate = VT100_EMUL_STATE_CSI;
    443 		break;
    444 	    case '7': /* DECSC */
    445 		edp->flags |= VTFL_SAVEDCURS;
    446 		edp->savedcursor_row = edp->crow;
    447 		edp->savedcursor_col = edp->ccol;
    448 		edp->savedattr = edp->curattr;
    449 		edp->savedbkgdattr = edp->bkgdattr;
    450 		edp->savedattrflags = edp->attrflags;
    451 		edp->savedfgcol = edp->fgcol;
    452 		edp->savedbgcol = edp->bgcol;
    453 		for (i = 0; i < 4; i++)
    454 			edp->savedchartab_G[i] = edp->chartab_G[i];
    455 		edp->savedchartab0 = edp->chartab0;
    456 		edp->savedchartab1 = edp->chartab1;
    457 		break;
    458 	    case '8': /* DECRC */
    459 		if ((edp->flags & VTFL_SAVEDCURS) == 0)
    460 			break;
    461 		edp->crow = edp->savedcursor_row;
    462 		edp->ccol = edp->savedcursor_col;
    463 		edp->curattr = edp->savedattr;
    464 		edp->bkgdattr = edp->savedbkgdattr;
    465 		edp->attrflags = edp->savedattrflags;
    466 		edp->fgcol = edp->savedfgcol;
    467 		edp->bgcol = edp->savedbgcol;
    468 		for (i = 0; i < 4; i++)
    469 			edp->chartab_G[i] = edp->savedchartab_G[i];
    470 		edp->chartab0 = edp->savedchartab0;
    471 		edp->chartab1 = edp->savedchartab1;
    472 		break;
    473 	    case '=': /* DECKPAM application mode */
    474 		edp->flags |= VTFL_APPLKEYPAD;
    475 		break;
    476 	    case '>': /* DECKPNM numeric mode */
    477 		edp->flags &= ~VTFL_APPLKEYPAD;
    478 		break;
    479 	    case 'E': /* NEL */
    480 		edp->ccol = 0;
    481 		/* FALLTHRU */
    482 	    case 'D': /* IND */
    483 		if (ROWS_BELOW > 0) {
    484 			edp->crow++;
    485 			CHECK_DW;
    486 			break;
    487 		}
    488 		wsemul_vt100_scrollup(edp, 1);
    489 		break;
    490 	    case 'H': /* HTS */
    491 		KASSERT(edp->tabs != 0);
    492 		edp->tabs[edp->ccol] = 1;
    493 		break;
    494 	    case '~': /* LS1R */
    495 		edp->chartab1 = 1;
    496 		break;
    497 	    case 'n': /* LS2 */
    498 		edp->chartab0 = 2;
    499 		break;
    500 	    case '}': /* LS2R */
    501 		edp->chartab1 = 2;
    502 		break;
    503 	    case 'o': /* LS3 */
    504 		edp->chartab0 = 3;
    505 		break;
    506 	    case '|': /* LS3R */
    507 		edp->chartab1 = 3;
    508 		break;
    509 	    case 'N': /* SS2 */
    510 		edp->sschartab = 2;
    511 		break;
    512 	    case 'O': /* SS3 */
    513 		edp->sschartab = 3;
    514 		break;
    515 	    case 'M': /* RI */
    516 		if (ROWS_ABOVE > 0) {
    517 			edp->crow--;
    518 			CHECK_DW;
    519 			break;
    520 		}
    521 		wsemul_vt100_scrolldown(edp, 1);
    522 		break;
    523 	    case 'P': /* DCS */
    524 		edp->nargs = 0;
    525 		memset(edp->args, 0, sizeof (edp->args));
    526 		newstate = VT100_EMUL_STATE_DCS;
    527 		break;
    528 	    case 'c': /* RIS */
    529 		wsemul_vt100_reset(edp);
    530 		wsemul_vt100_ed(edp, 2);
    531 		edp->ccol = edp->crow = 0;
    532 		break;
    533 	    case '(': case ')': case '*': case '+': /* SCS */
    534 		edp->designating = c - '(';
    535 		newstate = VT100_EMUL_STATE_SCS94;
    536 		break;
    537 	    case '-': case '.': case '/': /* SCS */
    538 		edp->designating = c - '-' + 1;
    539 		newstate = VT100_EMUL_STATE_SCS96;
    540 		break;
    541 	    case '#':
    542 		newstate = VT100_EMUL_STATE_ESC_HASH;
    543 		break;
    544 	    case ' ': /* 7/8 bit */
    545 		newstate = VT100_EMUL_STATE_ESC_SPC;
    546 		break;
    547 	    case ']': /* OSC operating system command */
    548 	    case '^': /* PM privacy message */
    549 	    case '_': /* APC application program command */
    550 		/* ignored */
    551 		newstate = VT100_EMUL_STATE_STRING;
    552 		break;
    553 	    case '<': /* exit VT52 mode - ignored */
    554 		break;
    555 	    default:
    556 #ifdef VT100_PRINTUNKNOWN
    557 		printf("ESC%c unknown\n", c);
    558 #endif
    559 		break;
    560 	}
    561 
    562 	return (newstate);
    563 }
    564 
    565 static u_int
    566 wsemul_vt100_output_scs94(struct wsemul_vt100_emuldata *edp, u_char c)
    567 {
    568 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    569 
    570 	switch (c) {
    571 	    case '%': /* probably DEC supplemental graphic */
    572 		newstate = VT100_EMUL_STATE_SCS94_PERCENT;
    573 		break;
    574 	    case 'A': /* british / national */
    575 		edp->chartab_G[edp->designating] = edp->nrctab;
    576 		break;
    577 	    case 'B': /* ASCII */
    578 		edp->chartab_G[edp->designating] = 0;
    579 		break;
    580 	    case '<': /* user preferred supplemental */
    581 		/* XXX not really "user" preferred */
    582 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    583 		break;
    584 	    case '0': /* DEC special graphic */
    585 		edp->chartab_G[edp->designating] = edp->decgraphtab;
    586 		break;
    587 	    case '>': /* DEC tech */
    588 		edp->chartab_G[edp->designating] = edp->dectechtab;
    589 		break;
    590 	    default:
    591 #ifdef VT100_PRINTUNKNOWN
    592 		printf("ESC%c%c unknown\n", edp->designating + '(', c);
    593 #endif
    594 		break;
    595 	}
    596 	return (newstate);
    597 }
    598 
    599 static u_int
    600 wsemul_vt100_output_scs94_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    601 {
    602 	switch (c) {
    603 	    case '5': /* DEC supplemental graphic */
    604 		/* XXX there are differences */
    605 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    606 		break;
    607 	    default:
    608 #ifdef VT100_PRINTUNKNOWN
    609 		printf("ESC%c%%%c unknown\n", edp->designating + '(', c);
    610 #endif
    611 		break;
    612 	}
    613 	return (VT100_EMUL_STATE_NORMAL);
    614 }
    615 
    616 static u_int
    617 wsemul_vt100_output_scs96(struct wsemul_vt100_emuldata *edp, u_char c)
    618 {
    619 	u_int newstate = VT100_EMUL_STATE_NORMAL;
    620 	int nrc;
    621 
    622 	switch (c) {
    623 	    case '%': /* probably portugese */
    624 		newstate = VT100_EMUL_STATE_SCS96_PERCENT;
    625 		break;
    626 	    case 'A': /* ISO-latin-1 supplemental */
    627 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    628 		break;
    629 	    case '4': /* dutch */
    630 		nrc = 1;
    631 		goto setnrc;
    632 	    case '5': case 'C': /* finnish */
    633 		nrc = 2;
    634 		goto setnrc;
    635 	    case 'R': /* french */
    636 		nrc = 3;
    637 		goto setnrc;
    638 	    case 'Q': /* french canadian */
    639 		nrc = 4;
    640 		goto setnrc;
    641 	    case 'K': /* german */
    642 		nrc = 5;
    643 		goto setnrc;
    644 	    case 'Y': /* italian */
    645 		nrc = 6;
    646 		goto setnrc;
    647 	    case 'E': case '6': /* norwegian / danish */
    648 		nrc = 7;
    649 		goto setnrc;
    650 	    case 'Z': /* spanish */
    651 		nrc = 9;
    652 		goto setnrc;
    653 	    case '7': case 'H': /* swedish */
    654 		nrc = 10;
    655 		goto setnrc;
    656 	    case '=': /* swiss */
    657 		nrc = 11;
    658 setnrc:
    659 		vt100_setnrc(edp, nrc); /* what table ??? */
    660 		break;
    661 	    default:
    662 #ifdef VT100_PRINTUNKNOWN
    663 		printf("ESC%c%c unknown\n", edp->designating + '-' - 1, c);
    664 #endif
    665 		break;
    666 	}
    667 	return (newstate);
    668 }
    669 
    670 static u_int
    671 wsemul_vt100_output_scs96_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    672 {
    673 	switch (c) {
    674 	    case '6': /* portugese */
    675 		vt100_setnrc(edp, 8);
    676 		break;
    677 	    default:
    678 #ifdef VT100_PRINTUNKNOWN
    679 		printf("ESC%c%%%c unknown\n", edp->designating + '-', c);
    680 #endif
    681 		break;
    682 	}
    683 	return (VT100_EMUL_STATE_NORMAL);
    684 }
    685 
    686 static u_int
    687 wsemul_vt100_output_esc_spc(struct wsemul_vt100_emuldata *edp, u_char c)
    688 {
    689 	switch (c) {
    690 	    case 'F': /* 7-bit controls */
    691 	    case 'G': /* 8-bit controls */
    692 #ifdef VT100_PRINTNOTIMPL
    693 		printf("ESC<SPC>%c ignored\n", c);
    694 #endif
    695 		break;
    696 	    default:
    697 #ifdef VT100_PRINTUNKNOWN
    698 		printf("ESC<SPC>%c unknown\n", c);
    699 #endif
    700 		break;
    701 	}
    702 	return (VT100_EMUL_STATE_NORMAL);
    703 }
    704 
    705 static u_int
    706 wsemul_vt100_output_string(struct wsemul_vt100_emuldata *edp, u_char c)
    707 {
    708 	if (edp->dcstype && edp->dcspos < DCS_MAXLEN)
    709 		edp->dcsarg[edp->dcspos++] = c;
    710 	return (VT100_EMUL_STATE_STRING);
    711 }
    712 
    713 static u_int
    714 wsemul_vt100_output_string_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    715 {
    716 	if (c == '\\') { /* ST complete */
    717 		wsemul_vt100_handle_dcs(edp);
    718 		return (VT100_EMUL_STATE_NORMAL);
    719 	} else
    720 		return (VT100_EMUL_STATE_STRING);
    721 }
    722 
    723 static u_int
    724 wsemul_vt100_output_dcs(struct wsemul_vt100_emuldata *edp, u_char c)
    725 {
    726 	u_int newstate = VT100_EMUL_STATE_DCS;
    727 
    728 	switch (c) {
    729 	    case '0': case '1': case '2': case '3': case '4':
    730 	    case '5': case '6': case '7': case '8': case '9':
    731 		/* argument digit */
    732 		if (edp->nargs > VT100_EMUL_NARGS - 1)
    733 			break;
    734 		edp->args[edp->nargs] = (edp->args[edp->nargs] * 10) +
    735 		    (c - '0');
    736 		break;
    737 	    case ';': /* argument terminator */
    738 		edp->nargs++;
    739 		break;
    740 	    default:
    741 		edp->nargs++;
    742 		if (edp->nargs > VT100_EMUL_NARGS) {
    743 #ifdef VT100_DEBUG
    744 			printf("vt100: too many arguments\n");
    745 #endif
    746 			edp->nargs = VT100_EMUL_NARGS;
    747 		}
    748 		newstate = VT100_EMUL_STATE_STRING;
    749 		switch (c) {
    750 		    case '$':
    751 			newstate = VT100_EMUL_STATE_DCS_DOLLAR;
    752 			break;
    753 		    case '{': /* DECDLD soft charset */
    754 		    case '!': /* DECRQUPSS user preferred supplemental set */
    755 			/* 'u' must follow - need another state */
    756 		    case '|': /* DECUDK program F6..F20 */
    757 #ifdef VT100_PRINTNOTIMPL
    758 			printf("DCS%c ignored\n", c);
    759 #endif
    760 			break;
    761 		    default:
    762 #ifdef VT100_PRINTUNKNOWN
    763 			printf("DCS%c (%d, %d) unknown\n", c, ARG(0), ARG(1));
    764 #endif
    765 			break;
    766 		}
    767 	}
    768 
    769 	return (newstate);
    770 }
    771 
    772 static u_int
    773 wsemul_vt100_output_dcs_dollar(struct wsemul_vt100_emuldata *edp, u_char c)
    774 {
    775 	switch (c) {
    776 	    case 'p': /* DECRSTS terminal state restore */
    777 	    case 'q': /* DECRQSS control function request */
    778 #ifdef VT100_PRINTNOTIMPL
    779 		printf("DCS$%c ignored\n", c);
    780 #endif
    781 		break;
    782 	    case 't': /* DECRSPS restore presentation state */
    783 		switch (ARG(0)) {
    784 		    case 0: /* error */
    785 			break;
    786 		    case 1: /* cursor information restore */
    787 #ifdef VT100_PRINTNOTIMPL
    788 			printf("DCS1$t ignored\n");
    789 #endif
    790 			break;
    791 		    case 2: /* tab stop restore */
    792 			edp->dcspos = 0;
    793 			edp->dcstype = DCSTYPE_TABRESTORE;
    794 			break;
    795 		    default:
    796 #ifdef VT100_PRINTUNKNOWN
    797 			printf("DCS%d$t unknown\n", ARG(0));
    798 #endif
    799 			break;
    800 		}
    801 		break;
    802 	    default:
    803 #ifdef VT100_PRINTUNKNOWN
    804 		printf("DCS$%c (%d, %d) unknown\n", c, ARG(0), ARG(1));
    805 #endif
    806 		break;
    807 	}
    808 	return (VT100_EMUL_STATE_STRING);
    809 }
    810 
    811 static u_int
    812 wsemul_vt100_output_esc_hash(struct wsemul_vt100_emuldata *edp, u_char c)
    813 {
    814 	int i;
    815 
    816 	switch (c) {
    817 	    case '5': /*  DECSWL single width, single height */
    818 		if (edp->dw) {
    819 			for (i = 0; i < edp->ncols / 2; i++)
    820 				(*edp->emulops->copycols)(edp->emulcookie,
    821 							  edp->crow,
    822 							  2 * i, i, 1);
    823 			(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    824 						   i, edp->ncols - i,
    825 						   edp->bkgdattr);
    826 			edp->dblwid[edp->crow] = 0;
    827 			edp->dw = 0;
    828 		}
    829 		break;
    830 	    case '6': /*  DECDWL double width, single height */
    831 	    case '3': /*  DECDHL double width, double height, top half */
    832 	    case '4': /*  DECDHL double width, double height, bottom half */
    833 		if (!edp->dw) {
    834 			for (i = edp->ncols / 2 - 1; i >= 0; i--)
    835 				(*edp->emulops->copycols)(edp->emulcookie,
    836 							  edp->crow,
    837 							  i, 2 * i, 1);
    838 			for (i = 0; i < edp->ncols / 2; i++)
    839 				(*edp->emulops->erasecols)(edp->emulcookie,
    840 							   edp->crow,
    841 							   2 * i + 1, 1,
    842 							   edp->bkgdattr);
    843 			edp->dblwid[edp->crow] = 1;
    844 			edp->dw = 1;
    845 			if (edp->ccol > (edp->ncols >> 1) - 1)
    846 				edp->ccol = (edp->ncols >> 1) - 1;
    847 		}
    848 		break;
    849 	    case '8': { /* DECALN */
    850 		int i, j;
    851 		for (i = 0; i < edp->nrows; i++)
    852 			for (j = 0; j < edp->ncols; j++)
    853 				(*edp->emulops->putchar)(edp->emulcookie, i, j,
    854 							 'E', edp->curattr);
    855 		}
    856 		edp->ccol = 0;
    857 		edp->crow = 0;
    858 		break;
    859 	    default:
    860 #ifdef VT100_PRINTUNKNOWN
    861 		printf("ESC#%c unknown\n", c);
    862 #endif
    863 		break;
    864 	}
    865 	return (VT100_EMUL_STATE_NORMAL);
    866 }
    867 
    868 static u_int
    869 wsemul_vt100_output_csi(struct wsemul_vt100_emuldata *edp, u_char c)
    870 {
    871 	u_int newstate = VT100_EMUL_STATE_CSI;
    872 
    873 	switch (c) {
    874 	    case '0': case '1': case '2': case '3': case '4':
    875 	    case '5': case '6': case '7': case '8': case '9':
    876 		/* argument digit */
    877 		if (edp->nargs > VT100_EMUL_NARGS - 1)
    878 			break;
    879 		edp->args[edp->nargs] = (edp->args[edp->nargs] * 10) +
    880 		    (c - '0');
    881 		break;
    882 	    case ';': /* argument terminator */
    883 		edp->nargs++;
    884 		break;
    885 	    case '?': /* DEC specific */
    886 	    case '>': /* DA query */
    887 		edp->modif1 = c;
    888 		break;
    889 	    case '!':
    890 	    case '"':
    891 	    case '$':
    892 	    case '&':
    893 		edp->modif2 = c;
    894 		break;
    895 	    default: /* end of escape sequence */
    896 		edp->nargs++;
    897 		if (edp->nargs > VT100_EMUL_NARGS) {
    898 #ifdef VT100_DEBUG
    899 			printf("vt100: too many arguments\n");
    900 #endif
    901 			edp->nargs = VT100_EMUL_NARGS;
    902 		}
    903 		wsemul_vt100_handle_csi(edp, c);
    904 		newstate = VT100_EMUL_STATE_NORMAL;
    905 		break;
    906 	}
    907 	return (newstate);
    908 }
    909 
    910 void
    911 wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int kernel)
    912 {
    913 	struct wsemul_vt100_emuldata *edp = cookie;
    914 
    915 #ifdef DIAGNOSTIC
    916 	if (kernel && !edp->console)
    917 		panic("wsemul_vt100_output: kernel output, not console");
    918 #endif
    919 
    920 	if (edp->flags & VTFL_CURSORON)
    921 		(*edp->emulops->cursor)(edp->emulcookie, 0,
    922 					edp->crow, edp->ccol << edp->dw);
    923 	for (; count > 0; data++, count--) {
    924 		if ((*data & 0x7f) < 0x20) {
    925 			wsemul_vt100_output_c0c1(edp, *data, kernel);
    926 			continue;
    927 		}
    928 		if (edp->state == VT100_EMUL_STATE_NORMAL || kernel) {
    929 			wsemul_vt100_output_normal(edp, *data, kernel);
    930 			continue;
    931 		}
    932 #ifdef DIAGNOSTIC
    933 		if (edp->state > sizeof(vt100_output) / sizeof(vt100_output[0]))
    934 			panic("wsemul_vt100: invalid state %d\n", edp->state);
    935 #endif
    936 		edp->state = vt100_output[edp->state - 1](edp, *data);
    937 	}
    938 	if (edp->flags & VTFL_CURSORON)
    939 		(*edp->emulops->cursor)(edp->emulcookie, 1,
    940 					edp->crow, edp->ccol << edp->dw);
    941 }
    942