Home | History | Annotate | Line # | Download | only in wscons
wsemul_vt100_subr.c revision 1.1
      1 /* $NetBSD: wsemul_vt100_subr.c,v 1.1 1998/06/20 19:17:47 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 
     38 #include <dev/wscons/wsksymvar.h>
     39 #include <dev/wscons/wsdisplayvar.h>
     40 #include <dev/wscons/wsemulvar.h>
     41 #include <dev/wscons/wsemul_vt100var.h>
     42 
     43 static int vt100_selectattribute __P((struct wsemul_vt100_emuldata *,
     44 				      int, int, int, long *));
     45 static int vt100_ansimode __P((struct wsemul_vt100_emuldata *, int, int));
     46 static int vt100_decmode __P((struct wsemul_vt100_emuldata *, int, int));
     47 #define MODE_SET 33
     48 #define MODE_RESET 44
     49 #define MODE_REPORT 55
     50 
     51 /*
     52  * scroll up within scrolling region
     53  */
     54 void
     55 wsemul_vt100_scrollup(edp, n)
     56 	struct wsemul_vt100_emuldata *edp;
     57 	int n;
     58 {
     59 	int help;
     60 
     61 	if (n > edp->scrreg_nrows)
     62 		n = edp->scrreg_nrows;
     63 
     64 	help = edp->scrreg_nrows - n;
     65 	if (help > 0)
     66 		(*edp->emulops->copyrows)(edp->emulcookie,
     67 					  edp->scrreg_startrow + n,
     68 					  edp->scrreg_startrow,
     69 					  help);
     70 	(*edp->emulops->eraserows)(edp->emulcookie,
     71 				   edp->scrreg_startrow + help, n,
     72 				   edp->curattr);
     73 }
     74 
     75 /*
     76  * scroll down within scrolling region
     77  */
     78 void
     79 wsemul_vt100_scrolldown(edp, n)
     80 	struct wsemul_vt100_emuldata *edp;
     81 	int n;
     82 {
     83 	int help;
     84 
     85 	if (n > edp->scrreg_nrows)
     86 		n = edp->scrreg_nrows;
     87 
     88 	help = edp->scrreg_nrows - n;
     89 	if (help > 0)
     90 		(*edp->emulops->copyrows)(edp->emulcookie,
     91 					  edp->scrreg_startrow,
     92 					  edp->scrreg_startrow + n,
     93 					  help);
     94 	(*edp->emulops->eraserows)(edp->emulcookie,
     95 				   edp->scrreg_startrow, n,
     96 				   edp->curattr);
     97 }
     98 
     99 /*
    100  * erase in display
    101  */
    102 void
    103 wsemul_vt100_ed(edp, arg)
    104 	struct wsemul_vt100_emuldata *edp;
    105 	int arg;
    106 {
    107 	int n;
    108 
    109 	switch (arg) {
    110 	    case 0: /* cursor to end */
    111 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    112 					   edp->ccol, COLS_LEFT + 1,
    113 					   edp->curattr);
    114 		n = edp->nrows - edp->crow - 1;
    115 		if (n > 0) {
    116 			(*edp->emulops->eraserows)(edp->emulcookie,
    117 						   edp->crow + 1, n,
    118 						   edp->curattr);
    119 		}
    120 		break;
    121 	    case 1: /* beginning to cursor */
    122 		if (edp->crow > 0) {
    123 			(*edp->emulops->eraserows)(edp->emulcookie,
    124 						   0, edp->crow,
    125 						   edp->curattr);
    126 		}
    127 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    128 					   0, edp->ccol + 1,
    129 					   edp->curattr);
    130 		break;
    131 	    case 2: /* complete display */
    132 		(*edp->emulops->eraserows)(edp->emulcookie,
    133 					   0, edp->nrows,
    134 					   edp->curattr);
    135 		break;
    136 	    default:
    137 #ifdef VT100_PRINTUNKNOWN
    138 		printf("ed(%d) unknown\n", arg);
    139 #endif
    140 		break;
    141 	}
    142 }
    143 
    144 /*
    145  * erase in line
    146  */
    147 void
    148 wsemul_vt100_el(edp, arg)
    149 	struct wsemul_vt100_emuldata *edp;
    150 	int arg;
    151 {
    152 	switch (arg) {
    153 	    case 0: /* cursor to end */
    154 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    155 					   edp->ccol, COLS_LEFT + 1,
    156 					   edp->curattr);
    157 		break;
    158 	    case 1: /* beginning to cursor */
    159 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    160 					   0, edp->ccol + 1,
    161 					   edp->curattr);
    162 		break;
    163 	    case 2: /* complete line */
    164 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    165 					   0, edp->ncols,
    166 					   edp->curattr);
    167 		break;
    168 	    default:
    169 #ifdef VT100_PRINTUNKNOWN
    170 		printf("el(%d) unknown\n", arg);
    171 #endif
    172 		break;
    173 	}
    174 }
    175 
    176 /*
    177  * handle commands after CSI (ESC[)
    178  */
    179 void
    180 wsemul_vt100_handle_csi(edp, c)
    181 	struct wsemul_vt100_emuldata *edp;
    182 	u_char c;
    183 {
    184 	int n, help, flags, fgcol, bgcol;
    185 	long attr;
    186 
    187 	switch (c) {
    188 	    case '@': /* ICH insert character VT300 only */
    189 		n = min(DEF1_ARG(0), COLS_LEFT + 1);
    190 		help = edp->ncols - (edp->ccol + n);
    191 		if (help > 0)
    192 			(*edp->emulops->copycols)(edp->emulcookie, edp->crow,
    193 						  edp->ccol, edp->ccol + n,
    194 						  help);
    195 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    196 					   edp->ccol, n, edp->curattr);
    197 		break;
    198 	    case 'A': /* CUU */
    199 		edp->crow -= min(DEF1_ARG(0), ROWS_ABOVE);
    200 		break;
    201 	    case 'B': /* CUD */
    202 		edp->crow += min(DEF1_ARG(0), ROWS_BELOW);
    203 		break;
    204 	    case 'C': /* CUF */
    205 		edp->ccol += min(DEF1_ARG(0), COLS_LEFT);
    206 		break;
    207 	    case 'D': /* CUB */
    208 		edp->ccol -= min(DEF1_ARG(0), edp->ccol);
    209 		edp->flags &= ~VTFL_LASTCHAR;
    210 		break;
    211 	    case 'H': /* CUP */
    212 	    case 'f': /* HVP */
    213 		if (edp->flags & VTFL_DECOM)
    214 			edp->crow = edp->scrreg_startrow +
    215 			    min(DEF1_ARG(0), edp->scrreg_nrows) - 1;
    216 		else
    217 			edp->crow = min(DEF1_ARG(0), edp->nrows) - 1;
    218 		edp->ccol = min(DEF1_ARG(1), edp->ncols) - 1;
    219 		edp->flags &= ~VTFL_LASTCHAR;
    220 		break;
    221 	    case 'J': /* ED erase in display */
    222 		wsemul_vt100_ed(edp, ARG(0));
    223 		break;
    224 	    case 'K': /* EL erase in line */
    225 		wsemul_vt100_el(edp, ARG(0));
    226 		break;
    227 	    case 'L': /* IL insert line */
    228 	    case 'M': /* DL delete line */
    229 		n = min(DEF1_ARG(0), ROWS_BELOW + 1);
    230 		{
    231 			int savscrstartrow, savscrnrows;
    232 			savscrstartrow = edp->scrreg_startrow;
    233 			savscrnrows = edp->scrreg_nrows;
    234 			edp->scrreg_nrows -= ROWS_ABOVE;
    235 			edp->scrreg_startrow = edp->crow;
    236 			if (c == 'L')
    237 				wsemul_vt100_scrolldown(edp, n);
    238 			else
    239 				wsemul_vt100_scrollup(edp, n);
    240 			edp->scrreg_startrow = savscrstartrow;
    241 			edp->scrreg_nrows = savscrnrows;
    242 		}
    243 		break;
    244 	    case 'P': /* DCH delete character */
    245 		n = min(DEF1_ARG(0), COLS_LEFT + 1);
    246 		help = edp->ncols - (edp->ccol + n);
    247 		if (help > 0)
    248 			(*edp->emulops->copycols)(edp->emulcookie, edp->crow,
    249 						  edp->ccol + n, edp->ccol,
    250 						  help);
    251 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    252 					   edp->ncols - n, n, edp->curattr);
    253 		break;
    254 	    case 'X': /* ECH erase character */
    255 		n = min(DEF1_ARG(0), COLS_LEFT + 1);
    256 		(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
    257 					   edp->ccol, n,
    258 					   edp->curattr);
    259 		break;
    260 	    case 'c': /* DA primary */
    261 		if (ARG(0) == 0)
    262 			wsdisplay_emulinput(edp->cbcookie, WSEMUL_VT_ID1,
    263 					    sizeof(WSEMUL_VT_ID1));
    264 		break;
    265 	    case 'g': /* TBC */
    266 		KASSERT(edp->tabs != 0);
    267 		switch (ARG(0)) {
    268 		    case 0:
    269 			edp->tabs[edp->ccol] = 0;
    270 			break;
    271 		    case 3:
    272 			bzero(edp->tabs, edp->ncols);
    273 			break;
    274 		    default:
    275 #ifdef VT100_PRINTUNKNOWN
    276 			printf("CSI%dg unknown\n", ARG(0));
    277 #endif
    278 			break;
    279 		}
    280 		break;
    281 	    case 'h': /* SM */
    282 		vt100_setmode(edp, ARG(0), 0);
    283 		break;
    284 	    case 'i': /* MC printer controller mode */
    285 		switch (ARG(0)) {
    286 		    case 0: /* print screen */
    287 			break;
    288 		    case 4: /* off */
    289 		    case 5: /* on */
    290 			break;
    291 		    default:
    292 #ifdef VT100_PRINTUNKNOWN
    293 			printf("CSI%di unknown\n", ARG(0));
    294 #endif
    295 			break;
    296 		}
    297 		break;
    298 	    case 'l': /* RM */
    299 		vt100_resetmode(edp, ARG(0), 0);
    300 		break;
    301 	    case 'm': /* SGR select graphic rendition */
    302 		flags = edp->attrflags;
    303 		fgcol = edp->fgcol;
    304 		bgcol = edp->bgcol;
    305 		for (n = 0; n < edp->nargs; n++) {
    306 			switch (ARG(n)) {
    307 			    case 0: /* reset */
    308 				edp->attrflags = 0;
    309 				edp->fgcol = WSCOL_WHITE;
    310 				edp->bgcol = WSCOL_BLACK;
    311 				if (n == edp->nargs - 1) {
    312 					edp->curattr = edp->defattr;
    313 					return;
    314 				}
    315 				break;
    316 			    case 1: /* bold */
    317 				flags |= WSATTR_HILIT;
    318 				break;
    319 			    case 4: /* underline */
    320 				flags |= WSATTR_UNDERLINE;
    321 				break;
    322 			    case 5: /* blink */
    323 				flags |= WSATTR_BLINK;
    324 				break;
    325 			    case 7: /* reverse */
    326 				flags |= WSATTR_REVERSE;
    327 				break;
    328 			    case 22: /* ~bold VT300 only */
    329 				flags &= ~WSATTR_HILIT;
    330 				break;
    331 			    case 24: /* ~underline VT300 only */
    332 				flags &= ~WSATTR_UNDERLINE;
    333 				break;
    334 			    case 25: /* ~blink VT300 only */
    335 				flags &= ~WSATTR_BLINK;
    336 				break;
    337 			    case 27: /* ~reverse VT300 only */
    338 				flags &= ~WSATTR_REVERSE;
    339 				break;
    340 			    case 30: case 31: case 32: case 33:
    341 			    case 34: case 35: case 36: case 37:
    342 				/* fg color */
    343 				flags |= WSATTR_WSCOLORS;
    344 				fgcol = ARG(n) - 30;
    345 				break;
    346 			    case 40: case 41: case 42: case 43:
    347 			    case 44: case 45: case 46: case 47:
    348 				/* bg color */
    349 				flags |= WSATTR_WSCOLORS;
    350 				bgcol = ARG(n) - 40;
    351 				break;
    352 			    default:
    353 #ifdef VT100_PRINTUNKNOWN
    354 				printf("CSI%dm unknown\n", ARG(n));
    355 #endif
    356 			}
    357 		}
    358 		if (vt100_selectattribute(edp, flags, fgcol, bgcol, &attr)) {
    359 #ifdef VT100_DEBUG
    360 			printf("error allocating attr %d/%d/%x\n",
    361 			       fgcol, bgcol, flags);
    362 #endif
    363 		} else {
    364 			edp->curattr = attr;
    365 			edp->attrflags = flags;
    366 			edp->fgcol = fgcol;
    367 			edp->bgcol = bgcol;
    368 		}
    369 		break;
    370 	    case 'n': /* reports */
    371 		switch (ARG(0)) {
    372 		    case 5: /* DSR operating status */
    373 			/* 0 = OK, 3 = malfunction */
    374 			wsdisplay_emulinput(edp->cbcookie, "\033[0n", 4);
    375 			break;
    376 		    case 6: { /* DSR cursor position report */
    377 			char buf[20];
    378 			int row;
    379 			if (edp->flags & VTFL_DECOM)
    380 				row = ROWS_ABOVE;
    381 			else
    382 				row = edp->crow;
    383 			n = sprintf(buf, "\033[%d;%dR",
    384 				    row + 1, edp->ccol + 1);
    385 			wsdisplay_emulinput(edp->cbcookie, buf, n);
    386 			}
    387 			break;
    388 		    case 15: /* DSR printer status */
    389 			/* 13 = no printer, 10 = ready, 11 = not ready */
    390 			wsdisplay_emulinput(edp->cbcookie, "\033[?13n", 6);
    391 			break;
    392 		    case 25: /* UDK status - VT300 only */
    393 			/* 20 = locked, 21 = unlocked */
    394 			wsdisplay_emulinput(edp->cbcookie, "\033[?21n", 6);
    395 			break;
    396 		    case 26: /* keyboard dialect */
    397 			/* 1 = north american , 7 = german */
    398 			wsdisplay_emulinput(edp->cbcookie, "\033[?27;1n", 8);
    399 			break;
    400 		    default:
    401 #ifdef VT100_PRINTUNKNOWN
    402 			printf("CSI%dn unknown\n", ARG(0));
    403 #endif
    404 			break;
    405 		}
    406 		break;
    407 	    case 'r': /* DECSTBM set top/bottom margins */
    408 		if (ARG(0) == 0 && ARG(1) == 0) {
    409 			edp->scrreg_startrow = 0;
    410 			edp->scrreg_nrows = edp->nrows;
    411 		} else {
    412 			edp->scrreg_startrow = min(DEF1_ARG(0),
    413 						   edp->nrows) - 1;
    414 			edp->scrreg_nrows = min(DEF1_ARG(1), edp->nrows) -
    415 			    edp->scrreg_startrow;
    416 		}
    417 		edp->crow = ((edp->flags & VTFL_DECOM) ?
    418 			     edp->scrreg_startrow : 0);
    419 		edp->ccol = 0;
    420 		break;
    421 	    case 'y':
    422 		switch (ARG(0)) {
    423 		    case 4: /* DECTST invoke confidence test */
    424 			/* ignore */
    425 			break;
    426 		    default:
    427 #ifdef VT100_PRINTUNKNOWN
    428 			printf("CSI%dy unknown\n", ARG(0));
    429 #endif
    430 			break;
    431 		}
    432 		break;
    433 	    default:
    434 #ifdef VT100_PRINTUNKNOWN
    435 		printf("CSI%c (%d, %d) unknown\n", c, ARG(0), ARG(1));
    436 #endif
    437 	}
    438 }
    439 
    440 /*
    441  * handle commands after CSI?
    442  */
    443 void
    444 wsemul_vt100_handle_csi_qm(edp, c)
    445 	struct wsemul_vt100_emuldata *edp;
    446 	u_char c;
    447 {
    448 	switch (c) {
    449 	    case 'J': /* DECSED selective erase in display */
    450 		wsemul_vt100_ed(edp, ARG(0));
    451 		break;
    452 	    case 'K': /* DECSEL selective erase in line */
    453 		wsemul_vt100_el(edp, ARG(0));
    454 		break;
    455 	    case 'h': /* SM, DEC private */
    456 		vt100_setmode(edp, ARG(0), 1);
    457 		break;
    458 	    case 'i': /* MC printer controller mode */
    459 		switch (ARG(0)) {
    460 		    case 1: /* print cursor line */
    461 		    case 4: /* off */
    462 		    case 5: /* on */
    463 #ifdef VT100_PRINTNOTIMPL
    464 			printf("CSI?%di ignored\n", ARG(0));
    465 #endif
    466 			break;
    467 		    default:
    468 #ifdef VT100_PRINTUNKNOWN
    469 			printf("CSI?%di unknown\n", ARG(0));
    470 #endif
    471 			break;
    472 		}
    473 		break;
    474 	    case 'l': /* RM, DEC private */
    475 		vt100_resetmode(edp, ARG(0), 1);
    476 		break;
    477 	    default:
    478 #ifdef VT100_PRINTUNKNOWN
    479 		printf("CSI?%c (%d, %d) unknown\n", c, ARG(0), ARG(1));
    480 #endif
    481 		break;
    482 	}
    483 }
    484 
    485 /*
    486  * get an attribute from the graphics driver,
    487  * try to find replacements if the desired appearance
    488  * is not supported
    489  */
    490 static int
    491 vt100_selectattribute(edp, flags, fgcol, bgcol, attr)
    492 	struct wsemul_vt100_emuldata *edp;
    493 	int flags, fgcol, bgcol;
    494 	long *attr;
    495 {
    496 	if ((flags & WSATTR_HILIT) &&
    497 	    !(edp->scrcapabilities & WSSCREEN_HILIT)) {
    498 		flags &= ~WSATTR_HILIT;
    499 		if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
    500 			fgcol = WSCOL_RED;
    501 			flags |= WSATTR_WSCOLORS;
    502 		} else {
    503 #ifdef VT100_DEBUG
    504 			printf("bold ignored (impossible)\n");
    505 #endif
    506 		}
    507 	}
    508 	if ((flags & WSATTR_UNDERLINE) &&
    509 	    !(edp->scrcapabilities & WSSCREEN_UNDERLINE)) {
    510 		flags &= ~WSATTR_UNDERLINE;
    511 		if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
    512 			bgcol = WSCOL_BROWN;
    513 			flags &= ~WSATTR_UNDERLINE;
    514 			flags |= WSATTR_WSCOLORS;
    515 		} else {
    516 #ifdef VT100_DEBUG
    517 			printf("underline ignored (impossible)\n");
    518 #endif
    519 		}
    520 	}
    521 	if ((flags & WSATTR_BLINK) &&
    522 	    !(edp->scrcapabilities & WSSCREEN_BLINK)) {
    523 		flags &= ~WSATTR_BLINK;
    524 #ifdef VT100_DEBUG
    525 		printf("blink ignored (impossible)\n");
    526 #endif
    527 	}
    528 	if ((flags & WSATTR_REVERSE) &&
    529 	    !(edp->scrcapabilities & WSSCREEN_REVERSE)) {
    530 		flags &= ~WSATTR_REVERSE;
    531 		if (edp->scrcapabilities & WSSCREEN_WSCOLORS) {
    532 			int help;
    533 			help = bgcol;
    534 			bgcol = fgcol;
    535 			fgcol = help;
    536 			flags |= WSATTR_WSCOLORS;
    537 		} else {
    538 #ifdef VT100_DEBUG
    539 			printf("reverse ignored (impossible)\n");
    540 #endif
    541 		}
    542 	}
    543 	if ((flags & WSATTR_WSCOLORS) &&
    544 	    !(edp->scrcapabilities & WSSCREEN_WSCOLORS)) {
    545 		flags &= ~WSATTR_WSCOLORS;
    546 #ifdef VT100_DEBUG
    547 		printf("colors ignored (impossible)\n");
    548 #endif
    549 	}
    550 	return ((*edp->emulops->alloc_attr)(edp->emulcookie,
    551 					    fgcol, bgcol, flags,
    552 					    attr));
    553 }
    554 
    555 /*
    556  * handle device control sequences if the main state machine
    557  * told so by setting edp->dcstype to a nonzero value
    558  */
    559 void
    560 wsemul_vt100_handle_dcs(edp)
    561 	struct wsemul_vt100_emuldata *edp;
    562 {
    563 	int i, pos;
    564 
    565 	switch (edp->dcstype) {
    566 	    case 0: /* not handled */
    567 		return;
    568 	    case DCSTYPE_TABRESTORE:
    569 		KASSERT(edp->tabs != 0);
    570 		bzero(edp->tabs, edp->ncols);
    571 		pos = 0;
    572 		for (i = 0; i < edp->dcspos; i++) {
    573 			char c = edp->dcsarg[i];
    574 			switch (c) {
    575 			    case '0': case '1': case '2': case '3': case '4':
    576 			    case '5': case '6': case '7': case '8': case '9':
    577 				pos = pos * 10 + (edp->dcsarg[i] - '0');
    578 				break;
    579 			    case '/':
    580 				if (pos > 0)
    581 					edp->tabs[pos - 1] = 1;
    582 				pos = 0;
    583 				break;
    584 			    default:
    585 #ifdef VT100_PRINTUNKNOWN
    586 				printf("unknown char %c in DCS\n", c);
    587 #endif
    588 			}
    589 		}
    590 		if (pos > 0)
    591 			edp->tabs[pos - 1] = 1;
    592 		break;
    593 	    default:
    594 		panic("wsemul_vt100_handle_dcs: bad type %d", edp->dcstype);
    595 	}
    596 	edp->dcstype = 0;
    597 }
    598 
    599 static int
    600 vt100_ansimode(edp, nr, op)
    601 	struct wsemul_vt100_emuldata *edp;
    602 	int nr, op;
    603 {
    604 	int res = 0; /* default: unknown */
    605 
    606 	switch (nr) {
    607 	    case 2: /* KAM keyboard locked/unlocked */
    608 		break;
    609 	    case 3: /* CRM control representation */
    610 		break;
    611 	    case 4: /* IRM insert/replace characters */
    612 		if (op == MODE_SET)
    613 			edp->flags |= VTFL_INSERTMODE;
    614 		else if (op == MODE_RESET)
    615 			edp->flags &= ~VTFL_INSERTMODE;
    616 		res = ((edp->flags & VTFL_INSERTMODE) ? 1 : 2);
    617 		break;
    618 	    case 10: /* HEM horizontal editing (permanently reset) */
    619 		res = 4;
    620 		break;
    621 	    case 12: /* SRM local echo off/on */
    622 		res = 4; /* permanently reset ??? */
    623 		break;
    624 	    case 20: /* LNM newline = newline/linefeed */
    625 		break;
    626 	    default:
    627 #ifdef VT100_PRINTUNKNOWN
    628 		printf("ANSI mode %d unknown\n", nr);
    629 #endif
    630 	}
    631 	return (res);
    632 }
    633 
    634 static int
    635 vt100_decmode(edp, nr, op)
    636 	struct wsemul_vt100_emuldata *edp;
    637 	int nr, op;
    638 {
    639 	int res = 0; /* default: unknown */
    640 
    641 	switch (nr) {
    642 	    case 1: /* DECCKM application/nomal cursor keys */
    643 		if (op == MODE_SET)
    644 			edp->flags |= VTFL_APPLCURSOR;
    645 		else if (op == MODE_RESET)
    646 			edp->flags &= ~VTFL_APPLCURSOR;
    647 		res = ((edp->flags & VTFL_APPLCURSOR) ? 1 : 2);
    648 		break;
    649 	    case 2: /* DECANM ANSI vt100/vt52 */
    650 		res = 3; /* permanently set ??? */
    651 		break;
    652 	    case 3: /* DECCOLM 132/80 cols */
    653 	    case 4: /* DECSCLM smooth/jump scroll */
    654 	    case 5: /* DECSCNM light/dark background */
    655 		res = 4; /* all permanently reset ??? */
    656 		break;
    657 	    case 6: /* DECOM move within/outside margins */
    658 		if (op == MODE_SET)
    659 			edp->flags |= VTFL_DECOM;
    660 		else if (op == MODE_RESET)
    661 			edp->flags &= ~VTFL_DECOM;
    662 		res = ((edp->flags & VTFL_DECOM) ? 1 : 2);
    663 		break;
    664 	    case 7: /* DECAWM autowrap */
    665 		if (op == MODE_SET)
    666 			edp->flags |= VTFL_DECAWM;
    667 		else if (op == MODE_RESET)
    668 			edp->flags &= ~VTFL_DECAWM;
    669 		res = ((edp->flags & VTFL_DECAWM) ? 1 : 2);
    670 		break;
    671 	    case 8: /* DECARM keyboard autorepeat */
    672 		break;
    673 	    case 18: /* DECPFF print form feed */
    674 		break;
    675 	    case 19: /* DECPEX printer extent: screen/scrolling region */
    676 		break;
    677 	    case 25: /* DECTCEM text cursor on/off */
    678 		if (op == MODE_SET || op == MODE_RESET) {
    679 			edp->flags = (edp->flags & ~VTFL_CURSORON) |
    680 			    ((op == MODE_SET) ? VTFL_CURSORON : 0);
    681 			(*edp->emulops->cursor)(edp->emulcookie,
    682 						(op == MODE_SET),
    683 						edp->crow, edp->ccol);
    684 		}
    685 		res = ((edp->flags & VTFL_CURSORON) ? 1 : 2);
    686 		break;
    687 	    case 42: /* DECNRCM use 7-bit NRC /
    688 		      7/8 bit from DEC multilingual or ISO-latin-1*/
    689 		break;
    690 	    case 66: /* DECNKM numeric keypad */
    691 		break;
    692 	    case 68: /* DECKBUM keyboard usage data processing/typewriter */
    693 		break;
    694 	    default:
    695 #ifdef VT100_PRINTUNKNOWN
    696 		printf("DEC mode %d unknown\n", nr);
    697 #endif
    698 		break;
    699 	}
    700 	return (res);
    701 }
    702 
    703 void
    704 vt100_setmode(edp, nr, decmode)
    705 	struct wsemul_vt100_emuldata *edp;
    706 	int nr, decmode;
    707 {
    708 	if (decmode)
    709 		vt100_decmode(edp, nr, MODE_SET);
    710 	else
    711 		vt100_ansimode(edp, nr, MODE_SET);
    712 }
    713 
    714 void
    715 vt100_resetmode(edp, nr, decmode)
    716 	struct wsemul_vt100_emuldata *edp;
    717 	int nr, decmode;
    718 {
    719 	if (decmode)
    720 		vt100_decmode(edp, nr, MODE_RESET);
    721 	else
    722 		vt100_ansimode(edp, nr, MODE_RESET);
    723 }
    724 
    725 void
    726 vt100_reportmode(edp, nr, decmode)
    727 	struct wsemul_vt100_emuldata *edp;
    728 	int nr, decmode;
    729 {
    730 	char buf[20];
    731 	int res, n;
    732 
    733 	if (decmode)
    734 		res = vt100_decmode(edp, nr, MODE_REPORT);
    735 	else
    736 		res = vt100_ansimode(edp, nr, MODE_REPORT);
    737 
    738 	n = sprintf(buf, "\033[%s%d;%d$y", (decmode ? "?" : ""), nr, res);
    739 	wsdisplay_emulinput(edp->cbcookie, buf, n);
    740 }
    741