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