Home | History | Annotate | Line # | Download | only in wscons
wsemul_vt100.c revision 1.46
      1  1.46       chs /* $NetBSD: wsemul_vt100.c,v 1.46 2019/11/10 21:16:38 chs Exp $ */
      2   1.1  drochner 
      3   1.1  drochner /*
      4   1.1  drochner  * Copyright (c) 1998
      5   1.1  drochner  *	Matthias Drochner.  All rights reserved.
      6   1.1  drochner  *
      7   1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8   1.1  drochner  * modification, are permitted provided that the following conditions
      9   1.1  drochner  * are met:
     10   1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11   1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     12   1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15   1.1  drochner  *
     16   1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  drochner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  drochner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  drochner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  drochner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  drochner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  drochner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  drochner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  drochner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  drochner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  drochner  *
     27   1.1  drochner  */
     28  1.16     lukem 
     29  1.16     lukem #include <sys/cdefs.h>
     30  1.46       chs __KERNEL_RCSID(0, "$NetBSD: wsemul_vt100.c,v 1.46 2019/11/10 21:16:38 chs Exp $");
     31  1.26      jmmv 
     32  1.37     pooka #ifdef _KERNEL_OPT
     33  1.26      jmmv #include "opt_wsmsgattrs.h"
     34  1.37     pooka #endif
     35   1.1  drochner 
     36   1.1  drochner #include <sys/param.h>
     37   1.1  drochner #include <sys/systm.h>
     38   1.1  drochner #include <sys/time.h>
     39   1.1  drochner #include <sys/malloc.h>
     40   1.1  drochner #include <sys/fcntl.h>
     41   1.1  drochner 
     42   1.1  drochner #include <dev/wscons/wsconsio.h>
     43   1.1  drochner #include <dev/wscons/wsdisplayvar.h>
     44   1.1  drochner #include <dev/wscons/wsemulvar.h>
     45   1.1  drochner #include <dev/wscons/wsemul_vt100var.h>
     46   1.1  drochner #include <dev/wscons/ascii.h>
     47   1.1  drochner 
     48  1.15  augustss void	*wsemul_vt100_cnattach(const struct wsscreen_descr *, void *,
     49  1.15  augustss 			       int, int, long);
     50  1.15  augustss void	*wsemul_vt100_attach(int console, const struct wsscreen_descr *,
     51  1.15  augustss 			     void *, int, int, void *, long);
     52  1.15  augustss void	wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int);
     53  1.15  augustss void	wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp);
     54  1.15  augustss void	wsemul_vt100_resetop(void *, enum wsemul_resetops);
     55  1.26      jmmv #ifdef WSDISPLAY_CUSTOM_OUTPUT
     56  1.26      jmmv static void wsemul_vt100_getmsgattrs(void *, struct wsdisplay_msgattrs *);
     57  1.26      jmmv static void wsemul_vt100_setmsgattrs(void *, const struct wsscreen_descr *,
     58  1.26      jmmv                                      const struct wsdisplay_msgattrs *);
     59  1.26      jmmv #endif /* WSDISPLAY_CUSTOM_OUTPUT */
     60  1.38  macallan static void wsemul_vt100_resize(void *, const struct wsscreen_descr *);
     61   1.1  drochner 
     62   1.1  drochner const struct wsemul_ops wsemul_vt100_ops = {
     63  1.40      maya 	.name = "vt100",
     64  1.40      maya 	.cnattach = wsemul_vt100_cnattach,
     65  1.40      maya 	.attach = wsemul_vt100_attach,
     66  1.40      maya 	.output = wsemul_vt100_output,
     67  1.40      maya 	.translate = wsemul_vt100_translate,
     68  1.40      maya 	.detach = wsemul_vt100_detach,
     69  1.40      maya 	.reset = wsemul_vt100_resetop,
     70  1.26      jmmv #ifdef WSDISPLAY_CUSTOM_OUTPUT
     71  1.40      maya 	.getmsgattrs = wsemul_vt100_getmsgattrs,
     72  1.40      maya 	.setmsgattrs = wsemul_vt100_setmsgattrs,
     73  1.26      jmmv #else
     74  1.40      maya 	.getmsgattrs = NULL,
     75  1.40      maya 	.setmsgattrs = NULL,
     76  1.26      jmmv #endif
     77  1.38  macallan 	.resize = wsemul_vt100_resize
     78   1.1  drochner };
     79   1.1  drochner 
     80   1.1  drochner struct wsemul_vt100_emuldata wsemul_vt100_console_emuldata;
     81   1.1  drochner 
     82  1.15  augustss static void wsemul_vt100_init(struct wsemul_vt100_emuldata *,
     83  1.15  augustss 			      const struct wsscreen_descr *,
     84  1.15  augustss 			      void *, int, int, long);
     85  1.15  augustss 
     86  1.15  augustss static void wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *,
     87  1.15  augustss 				       u_char, int);
     88  1.15  augustss static void wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *,
     89  1.15  augustss 				     u_char, int);
     90  1.21  sommerfe static void wsemul_vt100_nextline(struct wsemul_vt100_emuldata *);
     91  1.15  augustss typedef u_int vt100_handler(struct wsemul_vt100_emuldata *, u_char);
     92  1.21  sommerfe 
     93   1.1  drochner static vt100_handler
     94   1.1  drochner wsemul_vt100_output_esc,
     95   1.1  drochner wsemul_vt100_output_csi,
     96   1.1  drochner wsemul_vt100_output_scs94,
     97   1.1  drochner wsemul_vt100_output_scs94_percent,
     98   1.1  drochner wsemul_vt100_output_scs96,
     99   1.1  drochner wsemul_vt100_output_scs96_percent,
    100   1.3  drochner wsemul_vt100_output_esc_hash,
    101   1.1  drochner wsemul_vt100_output_esc_spc,
    102   1.1  drochner wsemul_vt100_output_string,
    103   1.1  drochner wsemul_vt100_output_string_esc,
    104   1.1  drochner wsemul_vt100_output_dcs,
    105   1.1  drochner wsemul_vt100_output_dcs_dollar;
    106   1.1  drochner 
    107   1.1  drochner #define	VT100_EMUL_STATE_NORMAL		0	/* normal processing */
    108   1.1  drochner #define	VT100_EMUL_STATE_ESC		1	/* got ESC */
    109   1.1  drochner #define	VT100_EMUL_STATE_CSI		2	/* got CSI (ESC[) */
    110   1.3  drochner #define	VT100_EMUL_STATE_SCS94		3	/* got ESC{()*+} */
    111   1.3  drochner #define	VT100_EMUL_STATE_SCS94_PERCENT	4	/* got ESC{()*+}% */
    112   1.3  drochner #define	VT100_EMUL_STATE_SCS96		5	/* got ESC{-./} */
    113   1.3  drochner #define	VT100_EMUL_STATE_SCS96_PERCENT	6	/* got ESC{-./}% */
    114   1.3  drochner #define	VT100_EMUL_STATE_ESC_HASH	7	/* got ESC# */
    115   1.3  drochner #define	VT100_EMUL_STATE_ESC_SPC	8	/* got ESC<SPC> */
    116   1.3  drochner #define	VT100_EMUL_STATE_STRING		9	/* waiting for ST (ESC\) */
    117   1.3  drochner #define	VT100_EMUL_STATE_STRING_ESC	10	/* waiting for ST, got ESC */
    118   1.3  drochner #define	VT100_EMUL_STATE_DCS		11	/* got DCS (ESC P) */
    119   1.3  drochner #define	VT100_EMUL_STATE_DCS_DOLLAR	12	/* got DCS<p>$ */
    120   1.1  drochner 
    121   1.1  drochner vt100_handler *vt100_output[] = {
    122   1.1  drochner 	wsemul_vt100_output_esc,
    123   1.1  drochner 	wsemul_vt100_output_csi,
    124   1.1  drochner 	wsemul_vt100_output_scs94,
    125   1.1  drochner 	wsemul_vt100_output_scs94_percent,
    126   1.1  drochner 	wsemul_vt100_output_scs96,
    127   1.1  drochner 	wsemul_vt100_output_scs96_percent,
    128   1.3  drochner 	wsemul_vt100_output_esc_hash,
    129   1.1  drochner 	wsemul_vt100_output_esc_spc,
    130   1.1  drochner 	wsemul_vt100_output_string,
    131   1.1  drochner 	wsemul_vt100_output_string_esc,
    132   1.1  drochner 	wsemul_vt100_output_dcs,
    133   1.1  drochner 	wsemul_vt100_output_dcs_dollar,
    134   1.1  drochner };
    135   1.1  drochner 
    136   1.1  drochner static void
    137  1.27     perry wsemul_vt100_init(struct wsemul_vt100_emuldata *edp,
    138  1.15  augustss 	const struct wsscreen_descr *type, void *cookie, int ccol, int crow,
    139  1.15  augustss 	long defattr)
    140   1.1  drochner {
    141  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    142  1.26      jmmv 	int error;
    143  1.26      jmmv 
    144  1.32  drochner 	vd->emulops = type->textops;
    145  1.32  drochner 	vd->emulcookie = cookie;
    146  1.32  drochner 	vd->scrcapabilities = type->capabilities;
    147  1.32  drochner 	vd->nrows = type->nrows;
    148  1.32  drochner 	vd->ncols = type->ncols;
    149  1.32  drochner 	vd->crow = crow;
    150  1.32  drochner 	vd->ccol = ccol;
    151  1.26      jmmv 
    152  1.26      jmmv 	/* The underlying driver has already allocated a default and simple
    153  1.26      jmmv 	 * attribute for us, which is stored in defattr.  We try to set the
    154  1.26      jmmv 	 * values specified by the kernel options below, but in case of
    155  1.26      jmmv 	 * failure we fallback to the value given by the driver. */
    156  1.26      jmmv 
    157  1.26      jmmv 	if (type->capabilities & WSSCREEN_WSCOLORS) {
    158  1.32  drochner 		vd->msgattrs.default_attrs = WS_DEFAULT_COLATTR |
    159  1.26      jmmv 		    WSATTR_WSCOLORS;
    160  1.32  drochner 		vd->msgattrs.default_bg = WS_DEFAULT_BG;
    161  1.32  drochner 		vd->msgattrs.default_fg = WS_DEFAULT_FG;
    162  1.26      jmmv 
    163  1.32  drochner 		vd->msgattrs.kernel_attrs = WS_KERNEL_COLATTR |
    164  1.26      jmmv 		    WSATTR_WSCOLORS;
    165  1.32  drochner 		vd->msgattrs.kernel_bg = WS_KERNEL_BG;
    166  1.32  drochner 		vd->msgattrs.kernel_fg = WS_KERNEL_FG;
    167  1.26      jmmv 	} else {
    168  1.32  drochner 		vd->msgattrs.default_attrs = WS_DEFAULT_MONOATTR;
    169  1.32  drochner 		vd->msgattrs.default_bg = vd->msgattrs.default_fg = 0;
    170  1.26      jmmv 
    171  1.32  drochner 		vd->msgattrs.kernel_attrs = WS_KERNEL_MONOATTR;
    172  1.32  drochner 		vd->msgattrs.kernel_bg = vd->msgattrs.kernel_fg = 0;
    173  1.26      jmmv 	}
    174  1.26      jmmv 
    175  1.32  drochner 	error = (*vd->emulops->allocattr)(cookie,
    176  1.32  drochner 					   vd->msgattrs.default_fg,
    177  1.32  drochner 					   vd->msgattrs.default_bg,
    178  1.32  drochner 					   vd->msgattrs.default_attrs,
    179  1.32  drochner 					   &vd->defattr);
    180  1.26      jmmv 	if (error) {
    181  1.32  drochner 		vd->defattr = defattr;
    182  1.26      jmmv 		/* XXX This assumes the driver has allocated white on black
    183  1.26      jmmv 		 * XXX as the default attribute, which is not always true.
    184  1.26      jmmv 		 * XXX Maybe we need an emulop that, given an attribute,
    185  1.26      jmmv 		 * XXX (defattr) returns its flags and colors? */
    186  1.32  drochner 		vd->msgattrs.default_attrs = 0;
    187  1.32  drochner 		vd->msgattrs.default_bg = WSCOL_BLACK;
    188  1.32  drochner 		vd->msgattrs.default_fg = WSCOL_WHITE;
    189  1.26      jmmv 	} else {
    190  1.32  drochner 		if (vd->emulops->replaceattr != NULL)
    191  1.32  drochner 			(*vd->emulops->replaceattr)(cookie, defattr,
    192  1.32  drochner 			                             vd->defattr);
    193  1.26      jmmv 	}
    194  1.26      jmmv 
    195  1.26      jmmv #if defined(WS_KERNEL_CUSTOMIZED)
    196  1.26      jmmv 	/* Set up kernel colors, in case they were customized by the user;
    197  1.26      jmmv 	 * otherwise default to the colors specified for the console.
    198  1.26      jmmv 	 * In case of failure, we use console colors too; we can assume
    199  1.26      jmmv 	 * they are good as they have been previously allocated and
    200  1.26      jmmv 	 * verified. */
    201  1.32  drochner 	error = (*vd->emulops->allocattr)(cookie,
    202  1.32  drochner 					   vd->msgattrs.kernel_fg,
    203  1.32  drochner 					   vd->msgattrs.kernel_bg,
    204  1.32  drochner 					   vd->msgattrs.kernel_attrs,
    205  1.26      jmmv 					   &edp->kernattr);
    206  1.26      jmmv 	if (error)
    207  1.26      jmmv #endif
    208  1.32  drochner 	edp->kernattr = vd->defattr;
    209   1.1  drochner }
    210   1.1  drochner 
    211   1.1  drochner void *
    212  1.15  augustss wsemul_vt100_cnattach(const struct wsscreen_descr *type, void *cookie,
    213  1.15  augustss 	int ccol, int crow, long defattr)
    214   1.1  drochner {
    215   1.1  drochner 	struct wsemul_vt100_emuldata *edp;
    216  1.32  drochner 	struct vt100base_data *vd;
    217   1.1  drochner 
    218   1.1  drochner 	edp = &wsemul_vt100_console_emuldata;
    219  1.32  drochner 	vd = &edp->bd;
    220   1.1  drochner 	wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    221   1.1  drochner #ifdef DIAGNOSTIC
    222   1.1  drochner 	edp->console = 1;
    223   1.1  drochner #endif
    224  1.32  drochner 	vd->cbcookie = NULL;
    225   1.1  drochner 
    226  1.32  drochner 	vd->tabs = 0;
    227  1.32  drochner 	vd->dblwid = 0;
    228  1.32  drochner 	vd->dw = 0;
    229  1.32  drochner 	vd->dcsarg = 0;
    230   1.3  drochner 	edp->isolatin1tab = edp->decgraphtab = edp->dectechtab = 0;
    231   1.3  drochner 	edp->nrctab = 0;
    232   1.1  drochner 	wsemul_vt100_reset(edp);
    233  1.43  christos 	return edp;
    234   1.1  drochner }
    235   1.1  drochner 
    236   1.1  drochner void *
    237  1.15  augustss wsemul_vt100_attach(int console, const struct wsscreen_descr *type,
    238  1.15  augustss 	void *cookie, int ccol, int crow, void *cbcookie, long defattr)
    239   1.1  drochner {
    240   1.1  drochner 	struct wsemul_vt100_emuldata *edp;
    241  1.32  drochner 	struct vt100base_data *vd;
    242   1.1  drochner 
    243   1.1  drochner 	if (console) {
    244   1.1  drochner 		edp = &wsemul_vt100_console_emuldata;
    245   1.1  drochner 		KASSERT(edp->console == 1);
    246   1.1  drochner 	} else {
    247   1.1  drochner 		edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
    248   1.1  drochner 		wsemul_vt100_init(edp, type, cookie, ccol, crow, defattr);
    249   1.1  drochner #ifdef DIAGNOSTIC
    250   1.1  drochner 		edp->console = 0;
    251   1.1  drochner #endif
    252   1.1  drochner 	}
    253  1.32  drochner 	vd = &edp->bd;
    254  1.32  drochner 	vd->cbcookie = cbcookie;
    255   1.1  drochner 
    256  1.46       chs 	vd->tabs = malloc(1024, M_DEVBUF, M_WAITOK);
    257  1.46       chs 	vd->dblwid = malloc(1024, M_DEVBUF, M_WAITOK|M_ZERO);
    258  1.32  drochner 	vd->dw = 0;
    259  1.46       chs 	vd->dcsarg = malloc(DCS_MAXLEN, M_DEVBUF, M_WAITOK);
    260  1.46       chs 	edp->isolatin1tab = malloc(128 * sizeof(int), M_DEVBUF, M_WAITOK);
    261  1.46       chs 	edp->decgraphtab = malloc(128 * sizeof(int), M_DEVBUF, M_WAITOK);
    262  1.46       chs 	edp->dectechtab = malloc(128 * sizeof(int), M_DEVBUF, M_WAITOK);
    263  1.46       chs 	edp->nrctab = malloc(128 * sizeof(int), M_DEVBUF, M_WAITOK);
    264   1.3  drochner 	vt100_initchartables(edp);
    265   1.1  drochner 	wsemul_vt100_reset(edp);
    266  1.43  christos 	return edp;
    267   1.1  drochner }
    268   1.1  drochner 
    269   1.1  drochner void
    270  1.15  augustss wsemul_vt100_detach(void *cookie, u_int *crowp, u_int *ccolp)
    271   1.1  drochner {
    272   1.1  drochner 	struct wsemul_vt100_emuldata *edp = cookie;
    273  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    274   1.1  drochner 
    275  1.32  drochner 	*crowp = vd->crow;
    276  1.32  drochner 	*ccolp = vd->ccol;
    277   1.3  drochner #define f(ptr) if (ptr) {free(ptr, M_DEVBUF); ptr = 0;}
    278  1.32  drochner 	f(vd->tabs)
    279  1.32  drochner 	f(vd->dblwid)
    280  1.32  drochner 	f(vd->dcsarg)
    281   1.3  drochner 	f(edp->isolatin1tab)
    282   1.3  drochner 	f(edp->decgraphtab)
    283   1.3  drochner 	f(edp->dectechtab)
    284   1.3  drochner 	f(edp->nrctab)
    285   1.3  drochner #undef f
    286   1.1  drochner 	if (edp != &wsemul_vt100_console_emuldata)
    287   1.1  drochner 		free(edp, M_DEVBUF);
    288   1.9  drochner }
    289   1.9  drochner 
    290  1.38  macallan static void
    291  1.38  macallan wsemul_vt100_resize(void * cookie, const struct wsscreen_descr *type)
    292  1.38  macallan {
    293  1.38  macallan 	struct wsemul_vt100_emuldata *edp = cookie;
    294  1.38  macallan 
    295  1.38  macallan 	edp->bd.nrows = type->nrows;
    296  1.38  macallan 	edp->bd.ncols = type->ncols;
    297  1.38  macallan 	wsemul_vt100_reset(edp);
    298  1.38  macallan 	wsemul_vt100_resetop(cookie, WSEMUL_CLEARSCREEN);
    299  1.38  macallan }
    300  1.38  macallan 
    301   1.9  drochner void
    302  1.15  augustss wsemul_vt100_resetop(void *cookie, enum wsemul_resetops op)
    303   1.9  drochner {
    304   1.9  drochner 	struct wsemul_vt100_emuldata *edp = cookie;
    305  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    306   1.9  drochner 
    307   1.9  drochner 	switch (op) {
    308   1.9  drochner 	case WSEMUL_RESET:
    309   1.9  drochner 		wsemul_vt100_reset(edp);
    310   1.9  drochner 		break;
    311   1.9  drochner 	case WSEMUL_SYNCFONT:
    312   1.9  drochner 		vt100_initchartables(edp);
    313  1.10  drochner 		break;
    314  1.10  drochner 	case WSEMUL_CLEARSCREEN:
    315  1.32  drochner 		wsemul_vt100_ed(vd, 2);
    316  1.32  drochner 		vd->ccol = vd->crow = 0;
    317  1.32  drochner 		(*vd->emulops->cursor)(vd->emulcookie,
    318  1.32  drochner 					vd->flags & VTFL_CURSORON, 0, 0);
    319  1.10  drochner 		break;
    320  1.10  drochner 	default:
    321   1.9  drochner 		break;
    322   1.9  drochner 	}
    323   1.1  drochner }
    324   1.1  drochner 
    325   1.1  drochner void
    326  1.15  augustss wsemul_vt100_reset(struct wsemul_vt100_emuldata *edp)
    327   1.1  drochner {
    328  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    329   1.1  drochner 	int i;
    330   1.1  drochner 
    331   1.1  drochner 	edp->state = VT100_EMUL_STATE_NORMAL;
    332  1.32  drochner 	vd->flags = VTFL_DECAWM | VTFL_CURSORON;
    333  1.32  drochner 	vd->bkgdattr = vd->curattr = vd->defattr;
    334  1.32  drochner 	vd->attrflags = vd->msgattrs.default_attrs;
    335  1.32  drochner 	vd->fgcol = vd->msgattrs.default_fg;
    336  1.32  drochner 	vd->bgcol = vd->msgattrs.default_bg;
    337  1.32  drochner 	vd->scrreg_startrow = 0;
    338  1.32  drochner 	vd->scrreg_nrows = vd->nrows;
    339  1.32  drochner 	if (vd->tabs) {
    340  1.32  drochner 		memset(vd->tabs, 0, vd->ncols);
    341  1.32  drochner 		for (i = 8; i < vd->ncols; i += 8)
    342  1.32  drochner 			vd->tabs[i] = 1;
    343   1.1  drochner 	}
    344  1.32  drochner 	vd->dcspos = 0;
    345  1.32  drochner 	vd->dcstype = 0;
    346   1.3  drochner 	edp->chartab_G[0] = 0;
    347   1.3  drochner 	edp->chartab_G[1] = edp->nrctab; /* ??? */
    348   1.3  drochner 	edp->chartab_G[2] = edp->isolatin1tab;
    349   1.3  drochner 	edp->chartab_G[3] = edp->isolatin1tab;
    350   1.3  drochner 	edp->chartab0 = 0;
    351   1.3  drochner 	edp->chartab1 = 2;
    352   1.3  drochner 	edp->sschartab = 0;
    353   1.1  drochner }
    354   1.1  drochner 
    355   1.1  drochner /*
    356   1.1  drochner  * now all the state machine bits
    357   1.1  drochner  */
    358   1.1  drochner 
    359  1.21  sommerfe /*
    360  1.21  sommerfe  * Move the cursor to the next line if possible. If the cursor is at
    361  1.21  sommerfe  * the bottom of the scroll area, then scroll it up. If the cursor is
    362  1.21  sommerfe  * at the bottom of the screen then don't move it down.
    363  1.21  sommerfe  */
    364   1.7  drochner static void
    365  1.21  sommerfe wsemul_vt100_nextline(struct wsemul_vt100_emuldata *edp)
    366  1.21  sommerfe {
    367  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    368  1.32  drochner 
    369  1.32  drochner 	if (ROWS_BELOW(vd) == 0) {
    370  1.21  sommerfe 		/* Bottom of the scroll region. */
    371  1.32  drochner 	  	wsemul_vt100_scrollup(vd, 1);
    372  1.21  sommerfe 	} else {
    373  1.32  drochner 		if ((vd->crow+1) < vd->nrows)
    374  1.21  sommerfe 			/* Cursor not at the bottom of the screen. */
    375  1.32  drochner 			vd->crow++;
    376  1.32  drochner 		CHECK_DW(vd);
    377  1.21  sommerfe 	}
    378  1.27     perry }
    379  1.21  sommerfe 
    380  1.22  sommerfe static void
    381  1.15  augustss wsemul_vt100_output_normal(struct wsemul_vt100_emuldata *edp, u_char c,
    382  1.15  augustss 	int kernel)
    383   1.1  drochner {
    384  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    385   1.7  drochner 	u_int *ct, dc;
    386   1.7  drochner 
    387  1.32  drochner 	if ((vd->flags & (VTFL_LASTCHAR | VTFL_DECAWM)) ==
    388   1.7  drochner 	    (VTFL_LASTCHAR | VTFL_DECAWM)) {
    389  1.21  sommerfe 		wsemul_vt100_nextline(edp);
    390  1.32  drochner 		vd->ccol = 0;
    391  1.32  drochner 		vd->flags &= ~VTFL_LASTCHAR;
    392   1.7  drochner 	}
    393   1.7  drochner 
    394   1.7  drochner 	if (c & 0x80) {
    395   1.7  drochner 		c &= 0x7f;
    396   1.7  drochner 		ct = edp->chartab_G[edp->chartab1];
    397   1.7  drochner 	} else {
    398   1.7  drochner 		if (edp->sschartab) {
    399   1.7  drochner 			ct = edp->chartab_G[edp->sschartab];
    400   1.7  drochner 			edp->sschartab = 0;
    401   1.7  drochner 		} else
    402   1.7  drochner 			ct = edp->chartab_G[edp->chartab0];
    403   1.7  drochner 	}
    404   1.7  drochner 	dc = (ct ? ct[c] : c);
    405   1.7  drochner 
    406  1.32  drochner 	if ((vd->flags & VTFL_INSERTMODE) && COLS_LEFT(vd))
    407  1.32  drochner 		COPYCOLS(vd, vd->ccol, vd->ccol + 1, COLS_LEFT(vd));
    408   1.7  drochner 
    409  1.32  drochner 	(*vd->emulops->putchar)(vd->emulcookie, vd->crow,
    410  1.32  drochner 				 vd->ccol << vd->dw, dc,
    411  1.32  drochner 				 kernel ? edp->kernattr : vd->curattr);
    412   1.7  drochner 
    413  1.32  drochner 	if (COLS_LEFT(vd))
    414  1.32  drochner 		vd->ccol++;
    415   1.7  drochner 	else
    416  1.32  drochner 		vd->flags |= VTFL_LASTCHAR;
    417   1.7  drochner }
    418   1.7  drochner 
    419   1.7  drochner static void
    420  1.27     perry wsemul_vt100_output_c0c1(struct wsemul_vt100_emuldata *edp, u_char c,
    421  1.15  augustss 	int kernel)
    422   1.7  drochner {
    423  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    424   1.7  drochner 	u_int n;
    425   1.1  drochner 
    426   1.1  drochner 	switch (c) {
    427  1.19  junyoung 	case ASCII_NUL:
    428  1.19  junyoung 	default:
    429   1.1  drochner 		/* ignore */
    430   1.1  drochner 		break;
    431  1.19  junyoung 	case ASCII_BEL:
    432  1.32  drochner 		wsdisplay_emulbell(vd->cbcookie);
    433   1.1  drochner 		break;
    434  1.19  junyoung 	case ASCII_BS:
    435  1.32  drochner 		if (vd->ccol > 0) {
    436  1.32  drochner 			vd->ccol--;
    437  1.32  drochner 			vd->flags &= ~VTFL_LASTCHAR;
    438   1.1  drochner 		}
    439   1.1  drochner 		break;
    440  1.19  junyoung 	case ASCII_CR:
    441  1.32  drochner 		vd->ccol = 0;
    442  1.32  drochner 		vd->flags &= ~VTFL_LASTCHAR;
    443   1.1  drochner 		break;
    444  1.19  junyoung 	case ASCII_HT:
    445  1.32  drochner 		if (vd->tabs) {
    446  1.32  drochner 			if (!COLS_LEFT(vd))
    447   1.1  drochner 				break;
    448  1.32  drochner 			for (n = vd->ccol + 1; n < NCOLS(vd) - 1; n++)
    449  1.32  drochner 				if (vd->tabs[n])
    450   1.1  drochner 					break;
    451   1.1  drochner 		} else {
    452  1.45  riastrad 			n = vd->ccol + uimin(8 - (vd->ccol & 7), COLS_LEFT(vd));
    453   1.1  drochner 		}
    454  1.32  drochner 		vd->ccol = n;
    455   1.1  drochner 		break;
    456  1.19  junyoung 	case ASCII_SO: /* LS1 */
    457   1.3  drochner 		edp->chartab0 = 1;
    458   1.3  drochner 		break;
    459  1.19  junyoung 	case ASCII_SI: /* LS0 */
    460   1.3  drochner 		edp->chartab0 = 0;
    461   1.1  drochner 		break;
    462  1.19  junyoung 	case ASCII_ESC:
    463  1.14  jdolecek 		if (kernel) {
    464  1.43  christos 			printf("%s: ESC in kernel output ignored\n", __func__);
    465  1.14  jdolecek 			break;	/* ignore the ESC */
    466  1.14  jdolecek 		}
    467  1.14  jdolecek 
    468   1.7  drochner 		if (edp->state == VT100_EMUL_STATE_STRING) {
    469   1.7  drochner 			/* might be a string end */
    470   1.7  drochner 			edp->state = VT100_EMUL_STATE_STRING_ESC;
    471   1.7  drochner 		} else {
    472   1.7  drochner 			/* XXX cancel current escape sequence */
    473   1.7  drochner 			edp->state = VT100_EMUL_STATE_ESC;
    474   1.7  drochner 		}
    475   1.1  drochner 		break;
    476   1.1  drochner #if 0
    477  1.19  junyoung 	case CSI: /* 8-bit */
    478   1.7  drochner 		/* XXX cancel current escape sequence */
    479   1.1  drochner 		edp->nargs = 0;
    480   1.8  augustss 		memset(edp->args, 0, sizeof (edp->args));
    481   1.3  drochner 		edp->modif1 = edp->modif2 = '\0';
    482   1.7  drochner 		edp->state = VT100_EMUL_STATE_CSI;
    483   1.1  drochner 		break;
    484  1.19  junyoung 	case DCS: /* 8-bit */
    485   1.7  drochner 		/* XXX cancel current escape sequence */
    486   1.1  drochner 		edp->nargs = 0;
    487   1.8  augustss 		memset(edp->args, 0, sizeof (edp->args));
    488   1.7  drochner 		edp->state = VT100_EMUL_STATE_DCS;
    489   1.1  drochner 		break;
    490  1.19  junyoung 	case ST: /* string end 8-bit */
    491   1.7  drochner 		/* XXX only in VT100_EMUL_STATE_STRING */
    492   1.7  drochner 		wsemul_vt100_handle_dcs(edp);
    493  1.43  christos 		edp->state = VT100_EMUL_STATE_NORMAL;
    494  1.43  christos 		break;
    495   1.1  drochner #endif
    496  1.19  junyoung 	case ASCII_LF:
    497  1.19  junyoung 	case ASCII_VT:
    498  1.19  junyoung 	case ASCII_FF:
    499  1.21  sommerfe 		wsemul_vt100_nextline(edp);
    500   1.1  drochner 		break;
    501   1.1  drochner 	}
    502   1.1  drochner }
    503   1.1  drochner 
    504   1.1  drochner static u_int
    505  1.15  augustss wsemul_vt100_output_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    506   1.1  drochner {
    507  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    508   1.3  drochner 	int i;
    509   1.1  drochner 
    510   1.1  drochner 	switch (c) {
    511  1.19  junyoung 	case '[': /* CSI */
    512  1.32  drochner 		vd->nargs = 0;
    513  1.32  drochner 		memset(vd->args, 0, sizeof (vd->args));
    514  1.32  drochner 		vd->modif1 = vd->modif2 = '\0';
    515  1.43  christos 		return VT100_EMUL_STATE_CSI;
    516  1.19  junyoung 	case '7': /* DECSC */
    517  1.32  drochner 		vd->flags |= VTFL_SAVEDCURS;
    518  1.32  drochner 		edp->savedcursor_row = vd->crow;
    519  1.32  drochner 		edp->savedcursor_col = vd->ccol;
    520  1.32  drochner 		edp->savedattr = vd->curattr;
    521  1.32  drochner 		edp->savedbkgdattr = vd->bkgdattr;
    522  1.32  drochner 		edp->savedattrflags = vd->attrflags;
    523  1.32  drochner 		edp->savedfgcol = vd->fgcol;
    524  1.32  drochner 		edp->savedbgcol = vd->bgcol;
    525   1.3  drochner 		for (i = 0; i < 4; i++)
    526   1.3  drochner 			edp->savedchartab_G[i] = edp->chartab_G[i];
    527   1.3  drochner 		edp->savedchartab0 = edp->chartab0;
    528   1.3  drochner 		edp->savedchartab1 = edp->chartab1;
    529   1.1  drochner 		break;
    530  1.19  junyoung 	case '8': /* DECRC */
    531  1.32  drochner 		if ((vd->flags & VTFL_SAVEDCURS) == 0)
    532  1.24  christos 			break;
    533  1.32  drochner 		vd->crow = edp->savedcursor_row;
    534  1.32  drochner 		vd->ccol = edp->savedcursor_col;
    535  1.32  drochner 		vd->curattr = edp->savedattr;
    536  1.32  drochner 		vd->bkgdattr = edp->savedbkgdattr;
    537  1.32  drochner 		vd->attrflags = edp->savedattrflags;
    538  1.32  drochner 		vd->fgcol = edp->savedfgcol;
    539  1.32  drochner 		vd->bgcol = edp->savedbgcol;
    540   1.3  drochner 		for (i = 0; i < 4; i++)
    541   1.3  drochner 			edp->chartab_G[i] = edp->savedchartab_G[i];
    542   1.3  drochner 		edp->chartab0 = edp->savedchartab0;
    543   1.3  drochner 		edp->chartab1 = edp->savedchartab1;
    544   1.1  drochner 		break;
    545  1.19  junyoung 	case '=': /* DECKPAM application mode */
    546  1.32  drochner 		vd->flags |= VTFL_APPLKEYPAD;
    547   1.1  drochner 		break;
    548  1.19  junyoung 	case '>': /* DECKPNM numeric mode */
    549  1.32  drochner 		vd->flags &= ~VTFL_APPLKEYPAD;
    550   1.1  drochner 		break;
    551  1.19  junyoung 	case 'E': /* NEL */
    552  1.32  drochner 		vd->ccol = 0;
    553   1.1  drochner 		/* FALLTHRU */
    554  1.19  junyoung 	case 'D': /* IND */
    555  1.21  sommerfe 		wsemul_vt100_nextline(edp);
    556   1.1  drochner 		break;
    557  1.19  junyoung 	case 'H': /* HTS */
    558  1.32  drochner 		KASSERT(vd->tabs != 0);
    559  1.32  drochner 		vd->tabs[vd->ccol] = 1;
    560   1.1  drochner 		break;
    561  1.19  junyoung 	case '~': /* LS1R */
    562   1.3  drochner 		edp->chartab1 = 1;
    563   1.3  drochner 		break;
    564  1.19  junyoung 	case 'n': /* LS2 */
    565   1.3  drochner 		edp->chartab0 = 2;
    566   1.3  drochner 		break;
    567  1.19  junyoung 	case '}': /* LS2R */
    568   1.3  drochner 		edp->chartab1 = 2;
    569   1.3  drochner 		break;
    570  1.19  junyoung 	case 'o': /* LS3 */
    571   1.3  drochner 		edp->chartab0 = 3;
    572   1.3  drochner 		break;
    573  1.19  junyoung 	case '|': /* LS3R */
    574   1.3  drochner 		edp->chartab1 = 3;
    575   1.1  drochner 		break;
    576  1.19  junyoung 	case 'N': /* SS2 */
    577   1.3  drochner 		edp->sschartab = 2;
    578   1.3  drochner 		break;
    579  1.19  junyoung 	case 'O': /* SS3 */
    580   1.3  drochner 		edp->sschartab = 3;
    581   1.1  drochner 		break;
    582  1.19  junyoung 	case 'M': /* RI */
    583  1.32  drochner 		if (ROWS_ABOVE(vd) > 0) {
    584  1.32  drochner 			vd->crow--;
    585  1.32  drochner 			CHECK_DW(vd);
    586   1.1  drochner 			break;
    587   1.1  drochner 		}
    588  1.32  drochner 		wsemul_vt100_scrolldown(vd, 1);
    589   1.1  drochner 		break;
    590  1.19  junyoung 	case 'P': /* DCS */
    591  1.32  drochner 		vd->nargs = 0;
    592  1.32  drochner 		memset(vd->args, 0, sizeof (vd->args));
    593  1.43  christos 		return VT100_EMUL_STATE_DCS;
    594  1.19  junyoung 	case 'c': /* RIS */
    595   1.1  drochner 		wsemul_vt100_reset(edp);
    596  1.32  drochner 		wsemul_vt100_ed(vd, 2);
    597  1.32  drochner 		vd->ccol = vd->crow = 0;
    598   1.1  drochner 		break;
    599  1.19  junyoung 	case '(': case ')': case '*': case '+': /* SCS */
    600   1.3  drochner 		edp->designating = c - '(';
    601  1.43  christos 		return VT100_EMUL_STATE_SCS94;
    602  1.19  junyoung 	case '-': case '.': case '/': /* SCS */
    603   1.3  drochner 		edp->designating = c - '-' + 1;
    604  1.43  christos 		return VT100_EMUL_STATE_SCS96;
    605  1.19  junyoung 	case '#':
    606  1.43  christos 		return VT100_EMUL_STATE_ESC_HASH;
    607  1.19  junyoung 	case ' ': /* 7/8 bit */
    608  1.43  christos 		return VT100_EMUL_STATE_ESC_SPC;
    609  1.19  junyoung 	case ']': /* OSC operating system command */
    610  1.19  junyoung 	case '^': /* PM privacy message */
    611  1.19  junyoung 	case '_': /* APC application program command */
    612   1.1  drochner 		/* ignored */
    613  1.43  christos 		return VT100_EMUL_STATE_STRING;
    614  1.19  junyoung 	case '<': /* exit VT52 mode - ignored */
    615   1.1  drochner 		break;
    616  1.19  junyoung 	default:
    617   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    618   1.1  drochner 		printf("ESC%c unknown\n", c);
    619   1.1  drochner #endif
    620   1.1  drochner 		break;
    621   1.1  drochner 	}
    622  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    623   1.1  drochner }
    624   1.1  drochner 
    625   1.1  drochner static u_int
    626  1.15  augustss wsemul_vt100_output_scs94(struct wsemul_vt100_emuldata *edp, u_char c)
    627   1.1  drochner {
    628   1.1  drochner 	switch (c) {
    629  1.19  junyoung 	case '%': /* probably DEC supplemental graphic */
    630  1.43  christos 		return VT100_EMUL_STATE_SCS94_PERCENT;
    631  1.19  junyoung 	case 'A': /* british / national */
    632   1.3  drochner 		edp->chartab_G[edp->designating] = edp->nrctab;
    633   1.3  drochner 		break;
    634  1.19  junyoung 	case 'B': /* ASCII */
    635   1.3  drochner 		edp->chartab_G[edp->designating] = 0;
    636   1.3  drochner 		break;
    637  1.19  junyoung 	case '<': /* user preferred supplemental */
    638   1.3  drochner 		/* XXX not really "user" preferred */
    639   1.3  drochner 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    640   1.3  drochner 		break;
    641  1.19  junyoung 	case '0': /* DEC special graphic */
    642   1.3  drochner 		edp->chartab_G[edp->designating] = edp->decgraphtab;
    643   1.3  drochner 		break;
    644  1.19  junyoung 	case '>': /* DEC tech */
    645   1.3  drochner 		edp->chartab_G[edp->designating] = edp->dectechtab;
    646   1.1  drochner 		break;
    647  1.19  junyoung 	default:
    648   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    649   1.3  drochner 		printf("ESC%c%c unknown\n", edp->designating + '(', c);
    650   1.1  drochner #endif
    651   1.1  drochner 		break;
    652   1.1  drochner 	}
    653  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    654   1.1  drochner }
    655   1.1  drochner 
    656   1.1  drochner static u_int
    657  1.15  augustss wsemul_vt100_output_scs94_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    658   1.1  drochner {
    659   1.1  drochner 	switch (c) {
    660  1.19  junyoung 	case '5': /* DEC supplemental graphic */
    661   1.3  drochner 		/* XXX there are differences */
    662   1.3  drochner 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    663   1.1  drochner 		break;
    664  1.19  junyoung 	default:
    665   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    666   1.3  drochner 		printf("ESC%c%%%c unknown\n", edp->designating + '(', c);
    667   1.1  drochner #endif
    668   1.1  drochner 		break;
    669   1.1  drochner 	}
    670  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    671   1.1  drochner }
    672   1.1  drochner 
    673   1.1  drochner static u_int
    674  1.15  augustss wsemul_vt100_output_scs96(struct wsemul_vt100_emuldata *edp, u_char c)
    675   1.1  drochner {
    676   1.3  drochner 	int nrc;
    677   1.1  drochner 
    678   1.1  drochner 	switch (c) {
    679  1.31       snj 	case '%': /* probably portuguese */
    680  1.43  christos 		return VT100_EMUL_STATE_SCS96_PERCENT;
    681  1.19  junyoung 	case 'A': /* ISO-latin-1 supplemental */
    682   1.3  drochner 		edp->chartab_G[edp->designating] = edp->isolatin1tab;
    683   1.3  drochner 		break;
    684  1.19  junyoung 	case '4': /* dutch */
    685   1.3  drochner 		nrc = 1;
    686   1.3  drochner 		goto setnrc;
    687  1.19  junyoung 	case '5': case 'C': /* finnish */
    688   1.3  drochner 		nrc = 2;
    689   1.3  drochner 		goto setnrc;
    690  1.19  junyoung 	case 'R': /* french */
    691   1.3  drochner 		nrc = 3;
    692   1.3  drochner 		goto setnrc;
    693  1.19  junyoung 	case 'Q': /* french canadian */
    694   1.3  drochner 		nrc = 4;
    695   1.3  drochner 		goto setnrc;
    696  1.19  junyoung 	case 'K': /* german */
    697   1.3  drochner 		nrc = 5;
    698   1.3  drochner 		goto setnrc;
    699  1.19  junyoung 	case 'Y': /* italian */
    700   1.3  drochner 		nrc = 6;
    701   1.3  drochner 		goto setnrc;
    702  1.19  junyoung 	case 'E': case '6': /* norwegian / danish */
    703   1.3  drochner 		nrc = 7;
    704   1.3  drochner 		goto setnrc;
    705  1.19  junyoung 	case 'Z': /* spanish */
    706   1.3  drochner 		nrc = 9;
    707   1.3  drochner 		goto setnrc;
    708  1.19  junyoung 	case '7': case 'H': /* swedish */
    709   1.3  drochner 		nrc = 10;
    710   1.3  drochner 		goto setnrc;
    711  1.19  junyoung 	case '=': /* swiss */
    712   1.3  drochner 		nrc = 11;
    713   1.3  drochner setnrc:
    714   1.3  drochner 		vt100_setnrc(edp, nrc); /* what table ??? */
    715   1.1  drochner 		break;
    716  1.19  junyoung 	default:
    717   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    718   1.3  drochner 		printf("ESC%c%c unknown\n", edp->designating + '-' - 1, c);
    719   1.1  drochner #endif
    720   1.1  drochner 		break;
    721   1.1  drochner 	}
    722  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    723   1.1  drochner }
    724   1.1  drochner 
    725   1.1  drochner static u_int
    726  1.15  augustss wsemul_vt100_output_scs96_percent(struct wsemul_vt100_emuldata *edp, u_char c)
    727   1.1  drochner {
    728   1.1  drochner 	switch (c) {
    729  1.31       snj 	case '6': /* portuguese */
    730   1.3  drochner 		vt100_setnrc(edp, 8);
    731   1.1  drochner 		break;
    732  1.19  junyoung 	default:
    733   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    734   1.3  drochner 		printf("ESC%c%%%c unknown\n", edp->designating + '-', c);
    735   1.1  drochner #endif
    736   1.1  drochner 		break;
    737   1.1  drochner 	}
    738  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    739   1.1  drochner }
    740   1.1  drochner 
    741   1.1  drochner static u_int
    742  1.30  christos wsemul_vt100_output_esc_spc(struct wsemul_vt100_emuldata *edp,
    743  1.29  christos     u_char c)
    744   1.1  drochner {
    745   1.1  drochner 	switch (c) {
    746  1.19  junyoung 	case 'F': /* 7-bit controls */
    747  1.19  junyoung 	case 'G': /* 8-bit controls */
    748   1.1  drochner #ifdef VT100_PRINTNOTIMPL
    749   1.1  drochner 		printf("ESC<SPC>%c ignored\n", c);
    750   1.1  drochner #endif
    751   1.1  drochner 		break;
    752  1.19  junyoung 	default:
    753   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    754   1.1  drochner 		printf("ESC<SPC>%c unknown\n", c);
    755   1.1  drochner #endif
    756   1.1  drochner 		break;
    757   1.1  drochner 	}
    758  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    759   1.1  drochner }
    760   1.1  drochner 
    761   1.1  drochner static u_int
    762  1.15  augustss wsemul_vt100_output_string(struct wsemul_vt100_emuldata *edp, u_char c)
    763   1.1  drochner {
    764  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    765  1.32  drochner 
    766  1.32  drochner 	if (vd->dcstype && vd->dcspos < DCS_MAXLEN)
    767  1.32  drochner 		vd->dcsarg[vd->dcspos++] = c;
    768  1.43  christos 	return VT100_EMUL_STATE_STRING;
    769   1.1  drochner }
    770   1.1  drochner 
    771   1.1  drochner static u_int
    772  1.15  augustss wsemul_vt100_output_string_esc(struct wsemul_vt100_emuldata *edp, u_char c)
    773   1.1  drochner {
    774  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    775  1.32  drochner 
    776   1.1  drochner 	if (c == '\\') { /* ST complete */
    777  1.32  drochner 		wsemul_vt100_handle_dcs(vd);
    778  1.43  christos 		return VT100_EMUL_STATE_NORMAL;
    779   1.1  drochner 	} else
    780  1.43  christos 		return VT100_EMUL_STATE_STRING;
    781   1.1  drochner }
    782   1.1  drochner 
    783   1.1  drochner static u_int
    784  1.15  augustss wsemul_vt100_output_dcs(struct wsemul_vt100_emuldata *edp, u_char c)
    785   1.1  drochner {
    786  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    787   1.1  drochner 
    788   1.1  drochner 	switch (c) {
    789  1.19  junyoung 	case '0': case '1': case '2': case '3': case '4':
    790  1.19  junyoung 	case '5': case '6': case '7': case '8': case '9':
    791   1.1  drochner 		/* argument digit */
    792  1.32  drochner 		if (vd->nargs > VT100_EMUL_NARGS - 1)
    793   1.1  drochner 			break;
    794  1.32  drochner 		vd->args[vd->nargs] = (vd->args[vd->nargs] * 10) +
    795   1.1  drochner 		    (c - '0');
    796   1.1  drochner 		break;
    797  1.19  junyoung 	case ';': /* argument terminator */
    798  1.32  drochner 		vd->nargs++;
    799   1.1  drochner 		break;
    800  1.19  junyoung 	default:
    801  1.32  drochner 		vd->nargs++;
    802  1.32  drochner 		if (vd->nargs > VT100_EMUL_NARGS) {
    803   1.1  drochner #ifdef VT100_DEBUG
    804   1.1  drochner 			printf("vt100: too many arguments\n");
    805   1.1  drochner #endif
    806  1.32  drochner 			vd->nargs = VT100_EMUL_NARGS;
    807   1.1  drochner 		}
    808   1.1  drochner 		switch (c) {
    809  1.19  junyoung 		case '$':
    810  1.43  christos 			return VT100_EMUL_STATE_DCS_DOLLAR;
    811  1.19  junyoung 		case '{': /* DECDLD soft charset */
    812  1.19  junyoung 		case '!': /* DECRQUPSS user preferred supplemental set */
    813   1.1  drochner 			/* 'u' must follow - need another state */
    814  1.19  junyoung 		case '|': /* DECUDK program F6..F20 */
    815   1.1  drochner #ifdef VT100_PRINTNOTIMPL
    816   1.1  drochner 			printf("DCS%c ignored\n", c);
    817   1.1  drochner #endif
    818   1.1  drochner 			break;
    819  1.19  junyoung 		default:
    820   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    821  1.32  drochner 			printf("DCS%c (%d, %d) unknown\n", c, ARG(vd, 0), ARG(vd, 1));
    822   1.1  drochner #endif
    823   1.1  drochner 			break;
    824   1.1  drochner 		}
    825  1.44    martin 		return VT100_EMUL_STATE_STRING;
    826   1.1  drochner 	}
    827   1.1  drochner 
    828  1.43  christos 	return VT100_EMUL_STATE_DCS;
    829   1.1  drochner }
    830   1.1  drochner 
    831   1.1  drochner static u_int
    832  1.15  augustss wsemul_vt100_output_dcs_dollar(struct wsemul_vt100_emuldata *edp, u_char c)
    833   1.1  drochner {
    834  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    835  1.32  drochner 
    836   1.1  drochner 	switch (c) {
    837  1.19  junyoung 	case 'p': /* DECRSTS terminal state restore */
    838  1.19  junyoung 	case 'q': /* DECRQSS control function request */
    839   1.1  drochner #ifdef VT100_PRINTNOTIMPL
    840   1.1  drochner 		printf("DCS$%c ignored\n", c);
    841   1.1  drochner #endif
    842   1.1  drochner 		break;
    843  1.19  junyoung 	case 't': /* DECRSPS restore presentation state */
    844  1.32  drochner 		switch (ARG(vd, 0)) {
    845  1.19  junyoung 		case 0: /* error */
    846   1.1  drochner 			break;
    847  1.19  junyoung 		case 1: /* cursor information restore */
    848   1.1  drochner #ifdef VT100_PRINTNOTIMPL
    849   1.1  drochner 			printf("DCS1$t ignored\n");
    850   1.1  drochner #endif
    851   1.1  drochner 			break;
    852  1.19  junyoung 		case 2: /* tab stop restore */
    853  1.32  drochner 			vd->dcspos = 0;
    854  1.32  drochner 			vd->dcstype = DCSTYPE_TABRESTORE;
    855   1.1  drochner 			break;
    856  1.19  junyoung 		default:
    857   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    858  1.32  drochner 			printf("DCS%d$t unknown\n", ARG(vd, 0));
    859   1.1  drochner #endif
    860   1.1  drochner 			break;
    861   1.1  drochner 		}
    862   1.1  drochner 		break;
    863  1.19  junyoung 	default:
    864   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    865  1.32  drochner 		printf("DCS$%c (%d, %d) unknown\n", c, ARG(vd, 0), ARG(vd, 1));
    866   1.1  drochner #endif
    867   1.1  drochner 		break;
    868   1.1  drochner 	}
    869  1.43  christos 	return VT100_EMUL_STATE_STRING;
    870   1.1  drochner }
    871   1.1  drochner 
    872   1.1  drochner static u_int
    873  1.15  augustss wsemul_vt100_output_esc_hash(struct wsemul_vt100_emuldata *edp, u_char c)
    874   1.1  drochner {
    875  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    876  1.23  drochner 	int i, j;
    877   1.1  drochner 
    878   1.1  drochner 	switch (c) {
    879  1.19  junyoung 	case '5': /*  DECSWL single width, single height */
    880  1.32  drochner 		if (vd->dw) {
    881  1.32  drochner 			for (i = 0; i < vd->ncols / 2; i++)
    882  1.32  drochner 				(*vd->emulops->copycols)(vd->emulcookie,
    883  1.32  drochner 							  vd->crow,
    884   1.3  drochner 							  2 * i, i, 1);
    885  1.32  drochner 			(*vd->emulops->erasecols)(vd->emulcookie, vd->crow,
    886  1.32  drochner 						   i, vd->ncols - i,
    887  1.32  drochner 						   vd->bkgdattr);
    888  1.32  drochner 			vd->dblwid[vd->crow] = 0;
    889  1.32  drochner 			vd->dw = 0;
    890   1.1  drochner 		}
    891   1.1  drochner 		break;
    892  1.19  junyoung 	case '6': /*  DECDWL double width, single height */
    893  1.19  junyoung 	case '3': /*  DECDHL double width, double height, top half */
    894  1.19  junyoung 	case '4': /*  DECDHL double width, double height, bottom half */
    895  1.32  drochner 		if (!vd->dw) {
    896  1.32  drochner 			for (i = vd->ncols / 2 - 1; i >= 0; i--)
    897  1.32  drochner 				(*vd->emulops->copycols)(vd->emulcookie,
    898  1.32  drochner 							  vd->crow,
    899   1.3  drochner 							  i, 2 * i, 1);
    900  1.32  drochner 			for (i = 0; i < vd->ncols / 2; i++)
    901  1.32  drochner 				(*vd->emulops->erasecols)(vd->emulcookie,
    902  1.32  drochner 							   vd->crow,
    903   1.3  drochner 							   2 * i + 1, 1,
    904  1.32  drochner 							   vd->bkgdattr);
    905  1.32  drochner 			vd->dblwid[vd->crow] = 1;
    906  1.32  drochner 			vd->dw = 1;
    907  1.32  drochner 			if (vd->ccol > (vd->ncols >> 1) - 1)
    908  1.32  drochner 				vd->ccol = (vd->ncols >> 1) - 1;
    909   1.3  drochner 		}
    910   1.1  drochner 		break;
    911  1.23  drochner 	case '8': /* DECALN */
    912  1.32  drochner 		for (i = 0; i < vd->nrows; i++)
    913  1.32  drochner 			for (j = 0; j < vd->ncols; j++)
    914  1.32  drochner 				(*vd->emulops->putchar)(vd->emulcookie, i, j,
    915  1.32  drochner 							 'E', vd->curattr);
    916  1.32  drochner 		vd->ccol = 0;
    917  1.32  drochner 		vd->crow = 0;
    918   1.1  drochner 		break;
    919  1.19  junyoung 	default:
    920   1.1  drochner #ifdef VT100_PRINTUNKNOWN
    921   1.1  drochner 		printf("ESC#%c unknown\n", c);
    922   1.1  drochner #endif
    923   1.1  drochner 		break;
    924   1.1  drochner 	}
    925  1.43  christos 	return VT100_EMUL_STATE_NORMAL;
    926   1.1  drochner }
    927   1.1  drochner 
    928   1.1  drochner static u_int
    929  1.15  augustss wsemul_vt100_output_csi(struct wsemul_vt100_emuldata *edp, u_char c)
    930   1.1  drochner {
    931  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    932   1.1  drochner 
    933   1.1  drochner 	switch (c) {
    934  1.19  junyoung 	case '0': case '1': case '2': case '3': case '4':
    935  1.19  junyoung 	case '5': case '6': case '7': case '8': case '9':
    936   1.1  drochner 		/* argument digit */
    937  1.32  drochner 		if (vd->nargs > VT100_EMUL_NARGS - 1)
    938   1.1  drochner 			break;
    939  1.32  drochner 		vd->args[vd->nargs] = (vd->args[vd->nargs] * 10) +
    940   1.1  drochner 		    (c - '0');
    941   1.1  drochner 		break;
    942  1.19  junyoung 	case ';': /* argument terminator */
    943  1.32  drochner 		vd->nargs++;
    944   1.1  drochner 		break;
    945  1.19  junyoung 	case '?': /* DEC specific */
    946  1.19  junyoung 	case '>': /* DA query */
    947  1.32  drochner 		vd->modif1 = c;
    948   1.1  drochner 		break;
    949  1.19  junyoung 	case '!':
    950  1.19  junyoung 	case '"':
    951  1.19  junyoung 	case '$':
    952  1.19  junyoung 	case '&':
    953  1.32  drochner 		vd->modif2 = c;
    954   1.1  drochner 		break;
    955  1.19  junyoung 	default: /* end of escape sequence */
    956  1.32  drochner 		vd->nargs++;
    957  1.32  drochner 		if (vd->nargs > VT100_EMUL_NARGS) {
    958   1.1  drochner #ifdef VT100_DEBUG
    959   1.1  drochner 			printf("vt100: too many arguments\n");
    960   1.1  drochner #endif
    961  1.32  drochner 			vd->nargs = VT100_EMUL_NARGS;
    962   1.1  drochner 		}
    963  1.32  drochner 		wsemul_vt100_handle_csi(vd, c);
    964  1.43  christos 		return VT100_EMUL_STATE_NORMAL;
    965   1.1  drochner 	}
    966  1.43  christos 	return VT100_EMUL_STATE_CSI;
    967   1.1  drochner }
    968   1.1  drochner 
    969   1.1  drochner void
    970  1.15  augustss wsemul_vt100_output(void *cookie, const u_char *data, u_int count, int kernel)
    971   1.1  drochner {
    972   1.1  drochner 	struct wsemul_vt100_emuldata *edp = cookie;
    973  1.32  drochner 	struct vt100base_data *vd = &edp->bd;
    974   1.1  drochner 
    975   1.1  drochner #ifdef DIAGNOSTIC
    976   1.1  drochner 	if (kernel && !edp->console)
    977  1.43  christos 		panic("%s: kernel output, not console", __func__);
    978   1.1  drochner #endif
    979   1.1  drochner 
    980  1.32  drochner 	if (vd->flags & VTFL_CURSORON)
    981  1.32  drochner 		(*vd->emulops->cursor)(vd->emulcookie, 0,
    982  1.32  drochner 					vd->crow, vd->ccol << vd->dw);
    983   1.1  drochner 	for (; count > 0; data++, count--) {
    984   1.7  drochner 		if ((*data & 0x7f) < 0x20) {
    985   1.7  drochner 			wsemul_vt100_output_c0c1(edp, *data, kernel);
    986   1.7  drochner 			continue;
    987   1.7  drochner 		}
    988   1.1  drochner 		if (edp->state == VT100_EMUL_STATE_NORMAL || kernel) {
    989   1.7  drochner 			wsemul_vt100_output_normal(edp, *data, kernel);
    990   1.1  drochner 			continue;
    991   1.1  drochner 		}
    992  1.43  christos 		int state = edp->state - 1;
    993  1.43  christos 		KASSERT(state < __arraycount(vt100_output));
    994  1.43  christos 		edp->state = vt100_output[state](edp, *data);
    995   1.1  drochner 	}
    996  1.32  drochner 	if (vd->flags & VTFL_CURSORON)
    997  1.32  drochner 		(*vd->emulops->cursor)(vd->emulcookie, 1,
    998  1.43  christos 		    vd->crow, vd->ccol << vd->dw);
    999   1.1  drochner }
   1000  1.26      jmmv 
   1001  1.26      jmmv #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1002  1.26      jmmv static void
   1003  1.26      jmmv wsemul_vt100_getmsgattrs(void *cookie, struct wsdisplay_msgattrs *ma)
   1004  1.26      jmmv {
   1005  1.26      jmmv 	struct wsemul_vt100_emuldata *edp = cookie;
   1006  1.35  drochner 	struct vt100base_data *vd = &edp->bd;
   1007  1.26      jmmv 
   1008  1.32  drochner 	*ma = vd->msgattrs;
   1009  1.26      jmmv }
   1010  1.26      jmmv 
   1011  1.26      jmmv static void
   1012  1.26      jmmv wsemul_vt100_setmsgattrs(void *cookie, const struct wsscreen_descr *type,
   1013  1.26      jmmv                          const struct wsdisplay_msgattrs *ma)
   1014  1.26      jmmv {
   1015  1.26      jmmv 	int error;
   1016  1.26      jmmv 	long tmp;
   1017  1.26      jmmv 	struct wsemul_vt100_emuldata *edp = cookie;
   1018  1.35  drochner 	struct vt100base_data *vd = &edp->bd;
   1019  1.26      jmmv 
   1020  1.32  drochner 	vd->msgattrs = *ma;
   1021  1.26      jmmv 	if (type->capabilities & WSSCREEN_WSCOLORS) {
   1022  1.32  drochner 		vd->msgattrs.default_attrs |= WSATTR_WSCOLORS;
   1023  1.32  drochner 		vd->msgattrs.kernel_attrs |= WSATTR_WSCOLORS;
   1024  1.26      jmmv 	} else {
   1025  1.32  drochner 		vd->msgattrs.default_bg = vd->msgattrs.kernel_bg = 0;
   1026  1.32  drochner 		vd->msgattrs.default_fg = vd->msgattrs.kernel_fg = 0;
   1027  1.26      jmmv 	}
   1028  1.26      jmmv 
   1029  1.32  drochner 	error = (*vd->emulops->allocattr)(vd->emulcookie,
   1030  1.32  drochner 	                                   vd->msgattrs.default_fg,
   1031  1.32  drochner 					   vd->msgattrs.default_bg,
   1032  1.32  drochner 	                                   vd->msgattrs.default_attrs,
   1033  1.26      jmmv 	                                   &tmp);
   1034  1.36       mrg #ifndef VT100_DEBUG
   1035  1.36       mrg 	__USE(error);
   1036  1.36       mrg #else
   1037  1.26      jmmv 	if (error)
   1038  1.26      jmmv 		printf("vt100: failed to allocate attribute for default "
   1039  1.26      jmmv 		       "messages\n");
   1040  1.26      jmmv 	else
   1041  1.26      jmmv #endif
   1042  1.26      jmmv 	{
   1043  1.32  drochner 		if (vd->curattr == vd->defattr) {
   1044  1.32  drochner 			vd->bkgdattr = vd->curattr = tmp;
   1045  1.32  drochner 			vd->attrflags = vd->msgattrs.default_attrs;
   1046  1.32  drochner 			vd->bgcol = vd->msgattrs.default_bg;
   1047  1.32  drochner 			vd->fgcol = vd->msgattrs.default_fg;
   1048  1.26      jmmv 		} else {
   1049  1.26      jmmv 			edp->savedbkgdattr = edp->savedattr = tmp;
   1050  1.32  drochner 			edp->savedattrflags = vd->msgattrs.default_attrs;
   1051  1.32  drochner 			edp->savedbgcol = vd->msgattrs.default_bg;
   1052  1.32  drochner 			edp->savedfgcol = vd->msgattrs.default_fg;
   1053  1.26      jmmv 		}
   1054  1.32  drochner 		if (vd->emulops->replaceattr != NULL)
   1055  1.32  drochner 			(*vd->emulops->replaceattr)(vd->emulcookie,
   1056  1.32  drochner 			                             vd->defattr, tmp);
   1057  1.32  drochner 		vd->defattr = tmp;
   1058  1.26      jmmv 	}
   1059  1.26      jmmv 
   1060  1.32  drochner 	error = (*vd->emulops->allocattr)(vd->emulcookie,
   1061  1.32  drochner 	                                   vd->msgattrs.kernel_fg,
   1062  1.32  drochner 					   vd->msgattrs.kernel_bg,
   1063  1.32  drochner 	                                   vd->msgattrs.kernel_attrs,
   1064  1.26      jmmv 	                                   &tmp);
   1065  1.26      jmmv #ifdef VT100_DEBUG
   1066  1.26      jmmv 	if (error)
   1067  1.26      jmmv 		printf("vt100: failed to allocate attribute for kernel "
   1068  1.26      jmmv 		       "messages\n");
   1069  1.26      jmmv 	else
   1070  1.26      jmmv #endif
   1071  1.26      jmmv 	{
   1072  1.32  drochner 		if (vd->emulops->replaceattr != NULL)
   1073  1.32  drochner 			(*vd->emulops->replaceattr)(vd->emulcookie,
   1074  1.26      jmmv 			                             edp->kernattr, tmp);
   1075  1.26      jmmv 		edp->kernattr = tmp;
   1076  1.26      jmmv 	}
   1077  1.26      jmmv }
   1078  1.26      jmmv #endif /* WSDISPLAY_CUSTOM_OUTPUT */
   1079