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