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