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