/* $NetBSD: wsemul_sun.c,v 1.1 1998/03/22 14:24:03 drochner Exp $ */ /* * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Christopher G. Demetriou * for the NetBSD Project. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ static const char _copyright[] __attribute__ ((unused)) = "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved."; static const char _rcsid[] __attribute__ ((unused)) = "$NetBSD: wsemul_sun.c,v 1.1 1998/03/22 14:24:03 drochner Exp $"; /* XXX DESCRIPTION/SOURCE OF INFORMATION */ #include #include #include #include #include #include #include #include #include void *wsemul_sun_cnattach __P((const struct wsscreen_descr *, void *, int, int)); void *wsemul_sun_attach __P((int console, const struct wsscreen_descr *, void *, int, int, void *)); void wsemul_sun_output __P((void *cookie, const u_char *data, u_int count)); void wsemul_sun_detach __P((void *cookie, u_int *crowp, u_int *ccolp)); const struct wsemul_ops wsemul_sun_ops = { "sun", wsemul_sun_cnattach, wsemul_sun_attach, wsemul_sun_output, wsemul_sun_detach, }; #define SUN_EMUL_STATE_NORMAL 0 /* normal processing */ #define SUN_EMUL_STATE_HAVEESC 1 /* seen start of ctl seq */ #define SUN_EMUL_STATE_CONTROL 2 /* processing ctl seq */ #define SUN_EMUL_NARGS 2 /* max # of args to a command */ struct wsemul_sun_emuldata { const struct wsdisplay_emulops *emulops; void *emulcookie; void *cbcookie; u_int nrows, ncols, crow, ccol; u_int state; /* processing state */ u_int args[SUN_EMUL_NARGS]; /* command args, if CONTROL */ u_int scrolldist; /* distance to scroll */ }; static u_int wsemul_sun_output_normal __P((struct wsemul_sun_emuldata *, u_char)); static u_int wsemul_sun_output_haveesc __P((struct wsemul_sun_emuldata *, u_char)); static u_int wsemul_sun_output_control __P((struct wsemul_sun_emuldata *, u_char)); static void wsemul_sun_control __P((struct wsemul_sun_emuldata *, u_char)); struct wsemul_sun_emuldata wsemul_sun_console_emuldata; /* some useful utility macros */ #define ARG(n) (edp->args[(n)]) #define NORMALIZE_ARG(n) (ARG(n) ? ARG(n) : 1) #define COLS_LEFT (edp->ncols - edp->ccol - 1) #define ROWS_LEFT (edp->nrows - edp->crow - 1) void * wsemul_sun_cnattach(type, cookie, ccol, crow) const struct wsscreen_descr *type; void *cookie; int ccol, crow; { struct wsemul_sun_emuldata *edp; edp = &wsemul_sun_console_emuldata; edp->emulops = type->textops; edp->emulcookie = cookie; edp->nrows = type->nrows; edp->ncols = type->ncols; edp->crow = crow; edp->ccol = ccol; edp->cbcookie = NULL; edp->state = SUN_EMUL_STATE_NORMAL; edp->scrolldist = 1; return (edp); } void * wsemul_sun_attach(console, type, cookie, ccol, crow, cbcookie) int console; const struct wsscreen_descr *type; void *cookie; int ccol, crow; void *cbcookie; { struct wsemul_sun_emuldata *edp; if (console) edp = &wsemul_sun_console_emuldata; else { edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK); edp->emulops = type->textops; edp->emulcookie = cookie; edp->nrows = type->nrows; edp->ncols = type->ncols; edp->crow = crow; edp->ccol = ccol; edp->state = SUN_EMUL_STATE_NORMAL; edp->scrolldist = 1; } edp->cbcookie = cbcookie; return (edp); } static inline u_int wsemul_sun_output_normal(edp, c) struct wsemul_sun_emuldata *edp; u_char c; { u_int newstate = SUN_EMUL_STATE_NORMAL; u_int n; switch (c) { case ASCII_BEL: /* "Bell (BEL)" */ wsdisplay_emulbell(edp->cbcookie); break; case ASCII_BS: /* "Backspace (BS)" */ if (edp->ccol > 0) edp->ccol--; break; case ASCII_CR: /* "Return (CR)" */ edp->ccol = 0; break; case ASCII_HT: /* "Tab (TAB)" */ n = max(8 - (edp->ccol & 7), COLS_LEFT); (*edp->emulops->erasecols)(edp->emulcookie, edp->crow, edp->ccol, n); edp->ccol += n; break; case ASCII_NP: /* "Form Feed (FF)" */ (*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows); edp->ccol = 0; edp->crow = 0; break; case ASCII_VT: /* "Reverse Line Feed" */ if (edp->crow > 0) edp->crow--; break; case ASCII_ESC: /* "Escape (ESC)" */ if (edp->state == SUN_EMUL_STATE_NORMAL) { newstate = SUN_EMUL_STATE_HAVEESC; break; } /* special case: fall through, we're printing one out */ /* FALLTHRU */ default: /* normal character */ (*edp->emulops->putstr)(edp->emulcookie, edp->crow, edp->ccol, &c, 1); edp->ccol++; /* if cur col is still on cur line, done. */ if (edp->ccol < edp->ncols) break; /* wrap the column around. */ edp->ccol = 0; /* FALLTHRU */ case ASCII_LF: /* "Line Feed (LF)" */ /* if the cur line isn't the last, incr and leave. */ if (edp->crow < edp->nrows - 1) { edp->crow++; break; } /* * if we're in wrap-around mode, go to the first * line and clear it. */ if (edp->scrolldist == 0) { edp->crow = 0; (*edp->emulops->eraserows)(edp->emulcookie, 0, 1); break; } /* scroll by the scrolling distance. */ (*edp->emulops->copyrows)(edp->emulcookie, edp->scrolldist, 0, edp->nrows - edp->scrolldist); (*edp->emulops->eraserows)(edp->emulcookie, edp->nrows - edp->scrolldist, edp->scrolldist); edp->crow -= edp->scrolldist - 1; break; } return (newstate); } static inline u_int wsemul_sun_output_haveesc(edp, c) struct wsemul_sun_emuldata *edp; u_char c; { u_int newstate; switch (c) { case '[': /* continuation of multi-char sequence */ bzero(edp->args, sizeof (edp->args)); newstate = SUN_EMUL_STATE_CONTROL; break; default: /* spit out the escape char (???), then the new character */ wsemul_sun_output_normal(edp, ASCII_ESC); /* ??? */ newstate = wsemul_sun_output_normal(edp, c); break; } return (newstate); } static inline void wsemul_sun_control(edp, c) struct wsemul_sun_emuldata *edp; u_char c; { u_int n, src, dst; switch (c) { case '@': /* "Insert Character (ICH)" */ n = min(NORMALIZE_ARG(0), COLS_LEFT + 1); src = edp->ccol; dst = edp->ccol + n; if (dst < edp->ncols) { (*edp->emulops->copycols)(edp->emulcookie, edp->crow, src, dst, edp->ncols - dst); } (*edp->emulops->erasecols)(edp->emulcookie, edp->crow, src, dst - src); break; case 'E': /* "Cursor Next Line (CNL)" */ edp->ccol = 0; /* FALLTHRU */ case 'A': /* "Cursor Up (CUU)" */ edp->crow -= min(NORMALIZE_ARG(0), edp->crow); break; case 'B': /* "Cursor Down (CUD)" */ edp->crow += min(NORMALIZE_ARG(0), ROWS_LEFT); break; case 'C': /* "Cursor Forward (CUF)" */ edp->ccol += min(NORMALIZE_ARG(0), COLS_LEFT); break; case 'D': /* "Cursor Backward (CUB)" */ edp->ccol -= min(NORMALIZE_ARG(0), edp->ccol); break; case 'f': /* "Horizontal And Vertical Position (HVP)" */ case 'H': /* "Cursor Position (CUP)" */ edp->crow = min(NORMALIZE_ARG(1), edp->nrows) - 1; edp->ccol = min(NORMALIZE_ARG(0), edp->ncols) - 1; break; case 'J': /* "Erase in Display (ED)" */ if (ROWS_LEFT > 0) { (*edp->emulops->eraserows)(edp->emulcookie, edp->crow + 1, ROWS_LEFT); } /* FALLTHRU */ case 'K': /* "Erase in Line (EL)" */ (*edp->emulops->erasecols)(edp->emulcookie, edp->crow, edp->ccol, COLS_LEFT + 1); break; case 'L': /* "Insert Line (IL)" */ n = min(NORMALIZE_ARG(0), ROWS_LEFT + 1); src = edp->crow; dst = edp->crow + n; if (dst < edp->nrows) { (*edp->emulops->copyrows)(edp->emulcookie, src, dst, edp->nrows - dst); } (*edp->emulops->eraserows)(edp->emulcookie, src, dst - src); break; case 'M': /* "Delete Line (DL)" */ n = min(NORMALIZE_ARG(0), ROWS_LEFT + 1); src = edp->crow + n; dst = edp->crow; if (src < edp->nrows) { (*edp->emulops->copyrows)(edp->emulcookie, src, dst, edp->nrows - src); } (*edp->emulops->eraserows)(edp->emulcookie, dst + edp->nrows - src, src - dst); break; case 'P': /* "Delete Character (DCH)" */ n = min(NORMALIZE_ARG(0), COLS_LEFT + 1); src = edp->ccol + n; dst = edp->ccol; if (src < edp->ncols) { (*edp->emulops->copycols)(edp->emulcookie, edp->crow, src, dst, edp->ncols - src); } (*edp->emulops->erasecols)(edp->emulcookie, edp->crow, dst + edp->ncols - src, src - dst); break; case 'm': /* "Select Graphic Rendition (SGR)" */ /* XXX rendition currently ignored */ break; case 'p': /* "Black On White (SUNBOW)" */ /* XXX rendition currently ignored */ break; case 'q': /* "White On Black (SUNWOB)" */ /* XXX rendition currently ignored */ break; case 'r': /* "Set Scrolling (SUNSCRL)" */ edp->scrolldist = max(ARG(0), edp->nrows); break; case 's': /* "Reset Terminal Emulator (SUNRESET)" */ edp->scrolldist = 1; /* XXX rendition aspects of this currently ignored */ break; } } static inline u_int wsemul_sun_output_control(edp, c) struct wsemul_sun_emuldata *edp; u_char c; { u_int newstate = SUN_EMUL_STATE_CONTROL; u_int i; switch (c) { case '0': case '1': case '2': case '3': case '4': /* argument digit */ case '5': case '6': case '7': case '8': case '9': edp->args[0] = (edp->args[0] * 10) + (c - '0'); break; case ';': /* argument terminator */ for (i = 1; i < SUN_EMUL_NARGS; i++) edp->args[i] = edp->args[i - 1]; edp->args[0] = 0; break; default: /* end of escape sequence */ wsemul_sun_control(edp, c); newstate = SUN_EMUL_STATE_NORMAL; break; } return (newstate); } void wsemul_sun_output(cookie, data, count) void *cookie; const u_char *data; u_int count; { struct wsemul_sun_emuldata *edp = cookie; u_int newstate; /* XXX */ (*edp->emulops->cursor)(edp->emulcookie, 0, edp->crow, edp->ccol); for (; count > 0; data++, count--) { switch (edp->state) { case SUN_EMUL_STATE_NORMAL: /* XXX SCAN INPUT FOR NEWLINES, DO PRESCROLLING */ newstate = wsemul_sun_output_normal(edp, *data); break; case SUN_EMUL_STATE_HAVEESC: newstate = wsemul_sun_output_haveesc(edp, *data); break; case SUN_EMUL_STATE_CONTROL: newstate = wsemul_sun_output_control(edp, *data); break; default: #ifdef DIAGNOSTIC panic("wsemul_sun: invalid state %d\n", edp->state); #endif /* try to recover, if things get screwed up... */ newstate = wsemul_sun_output_normal(edp, *data); break; } edp->state = newstate; } /* XXX */ (*edp->emulops->cursor)(edp->emulcookie, 1, edp->crow, edp->ccol); } void wsemul_sun_detach(cookie, crowp, ccolp) void *cookie; u_int *crowp, *ccolp; { struct wsemul_sun_emuldata *edp = cookie; *crowp = edp->crow; *ccolp = edp->ccol; if (edp != &wsemul_sun_console_emuldata) free(edp, M_DEVBUF); }