home *** CD-ROM | disk | FTP | other *** search
- /*
- * ansi.c: hterm escape sequence decoder
- *
- * Author: HIRANO Satoshi
- * (C) 1989 Halca Computer Science Laboratory TM
- * University of Tokyo
- *
- * 1.1 86/10/17 Halca.Hirano
- * 1.2 89/05/16 Halca.Hirano add RCS control
- * 2.2 89/05/16 Halca.Hirano
- * ----- V2.2 distribution ----
- * 2.3 89/07/20 Halca.Hirano
- * add EUC/SJIS/JIS (Thank you ohta@ricoh)
- * add UOP_GRAPHICS (Thank you mik@uop)
- * tab editor
- * ---- V2.3.-1 distribution ----
- * 2.4 89/09/01 Halca.Hirano
- * extend UOP_GRAPHICS
- * 2.5 89/09/14 Halca.Hirano
- * fix UOP_GRAPHICS label bug by mik@uop
- * fix binary logging bug
- * ---- V2.4.0 distribution ----
- * 2.6 89/10/10 Halca.Hirano
- * fix CSI bug -> CSI is usable only if JIS mode
- * 2.7 89/11/10 Tominaga@Titech port to J3100
- * 2.8 89/11/19 Halca.Hirano add cursor position reporting (Thanks Tominaga@Titech)
- * 2.9 89/11/30 mik@uop add ACOS kanji code
- * 3.0 89/12/08 Shikita@titech we can read JIS from EUC/SJIS mode
- * 3.1 89/12/24 Halca.Hirano don't display non-supported escape sequences
- * 3.2 90/07/09 Halca.Hirano locate() after receiving non graphic character
- * change IBM-PC character color table
- *
- * $Header: ansi.cv 1.14 90/07/05 08:37:26 hirano Exp $"
- *
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include "config.h"
- #include "hterm.h"
- #include "option.h"
- #include "default.h"
- #include "global.h"
-
- #define MAX_PARAM 20 /* number of parameter in escape seq. */
-
- static short ai;
- static short val[MAX_PARAM]; /* parameter value */
- static int state; /* sequence state */
- static int pageSaved; /* page is saved flag for ESC [ ? 47 h */
-
- #ifdef PC98
- #define CMASK 0x1b
- static short ctab[8] = { 0x00, 0x40, 0x80, 0xc0, 0x20, 0x60, 0xa0, 0xe0 };
- /* black red green yellow blue vioret magenta white */
- #endif /* PC98 */
- #ifdef IBMPC
- #define FMASK 0xf8
- #define BMASK 0x8f
- static short fColor[8] = {0x00, 0x04, 0x02, 0x06, 0x01, 0x03, 0x05, 0x07};
- /* black red green brown blue cyan magenta white */
- static short bColor[8] = {0x00, 0x40, 0x20, 0x60, 0x10, 0x30, 0x50, 0x70};
- #endif /* IBMPC */
-
- /*
- * DEC special character conversion table
- */
- #ifdef J3100
- static unsigned char decConv[128] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
- 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
- 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
- 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
- 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x20,
- 0x04, 0xff, 0x1a, 0x18, 0x1b, 0x19, 0xdf, 0xff,
- 0x19, 0x19, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x7e,
- 0x2d, 0x2d, 0x2d, 0x5f, 0x2b, 0x2b, 0x2b, 0x2b,
- 0x7c, 0x11, 0x10, 0x14, 0xff, 0xff, 0xa5, 0x7f
- };
- #else /* J3100 */
- static unsigned char decConv[128] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
- 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
- 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
- 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
- 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
- 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x20,
- 0xea, 0x87, 0x09, 0x87, 0x0d, 0x0a, 0xdf, 0x87,
- 0x87, 0x87, 0x9b, 0x99, 0x98, 0x9a, 0x8f, 0x95,
- 0x95, 0x95, 0x95, 0x95, 0x93, 0x92, 0x90, 0x91,
- 0x96, 0x87, 0x87, 0x87, 0x87, 0x87, 0xa5, 0x7f
- };
- #endif /* J3100 */
-
- void state1(unsigned short c);
- void state2(unsigned short c);
- void state3(unsigned short c);
- void state5(void );
- void state8(unsigned short c);
- void state16(u_short c);
- void state17(u_short c);
- void state100(unsigned short c);
- void state101(unsigned short c);
- void state102(unsigned short c);
- void state103(void );
- void state104(void );
- void state110(unsigned short c);
- void saveCursor(void );
- void restCursor(void );
- void setAttribute(void );
- void eraseScreen(void );
- void eraseLine(void );
- void cursorPosition(void );
- void scrollRegion(void );
- void downLine(void );
- void upLine(void );
- void cursorReport(void );
-
- void ansiInit()
- {
- setTabs(8);
- }
-
- void ansiSetup()
- {
- kfirstSave = kstateSave = state = ai = kstate = kfirst = saveX = saveY = 0;
- saveAttr = eraseAttr;
- spaceTab = pageSaved = originMode = NO;
- kanjiSave = kanjiCode;
- }
-
- void conPrint(p)
- /*
- * print a string on console with ANSI decoding
- */
- register char *p;
- {
- while (*p)
- decodeAnsi((u_short)(*p++) & 0xff);
- }
-
- void decodeAnsi(c)
- /*
- * decode ANSI escape sequence and reflect it to console
- * in:
- * c if online, char from the remote system
- */
- register u_short c;
- {
- if (logging == LOG_NO_CONVERT)
- logPut(c);
- if (intControl == NO) {
- if (c == CR) {
- wrapPending = NO;
- conWrite(c);
- locate(0, cursorY);
- if (printMode)
- outPrinter(CR);
- if (logging == LOG_CONVERT)
- logPut(c);
- } else if (c == LF) {
- conWrite(c);
- downLine();
- if (printMode)
- outPrinter(LF);
- #ifdef MSDOS
- if (logging == LOG_CONVERT)
- logPut(c);
- #endif
- } else
- #ifdef KANJI
- goto kconvert;
- #else
- conWrite(c);
- #endif /* KANJI */
- return;
- }
- /* short cut */
- if (state == 0 && kstate == 0 && kfirst == 0 && isAscii(c)) {
- conWrite(c);
- return;
- }
- switch(c) {
- case CR:
- locate(0, cursorY);
- wrapPending = NO;
- if (printMode)
- outPrinter(CR);
- if (logging == LOG_CONVERT)
- logPut(c);
- return;
- case FF:
- if (formfeed == YES) {
- clearLine(0, bottomLine);
- locate(0, 0);
- if (printMode)
- outPrinter(FF);
- return;
- } /* else LF */
- case VT:
- case LF:
- downLine();
- if (printMode)
- outPrinter(LF);
- #ifdef MSDOS
- if (logging == LOG_CONVERT)
- logPut(c);
- #endif
- return;
- case BS:
- wrapPending = NO;
- if (cursorX > 0)
- locate(cursorX-1, cursorY);
- if (printMode)
- outPrinter(BS);
- if (logging == LOG_CONVERT)
- logPut(c);
- return;
- case BEL:
- bell();
- if (logging == LOG_CONVERT)
- logPut(c);
- return;
- case HT:
- if (cursorX >= LAST_COLUMN)
- return;
- if (spaceTab) {
- int x = cursorX;
- conWrite(SPACE);
- cursorX = x;
- }
- for (cursorX++; cursorX < LAST_COLUMN && tabTable[cursorX] == 0; cursorX++)
- if (spaceTab) {
- int x = cursorX;
- conWrite(SPACE);
- locate(x, cursorY);
- }
- locate(cursorX, cursorY);
- if (printMode)
- outPrinter(c);
- if (logging == LOG_CONVERT)
- logPut(c);
- return;
- case NUL:
- case DEL:
- return;
- #ifdef KANJI
- case SI: /* invoke G0 to GL */
- kstate = 0; /* enter roman mode */
- return;
- case SO: /* invoke G1 to GL */
- kstate = 1; /* enter kana mode */
- return;
- #else
- case SI:
- case SO:
- return; /* ignored */
- #endif /* KANJI */
- #ifdef NEC_KANJI
- case CAN: /* CAN */
- state = 0;
- return;
- case SUB: /* SUB; NEC kanji in/out marker */
- if (kanjiCode == NECKANJI)
- state = 110;
- else
- state = 0;
- return;
- #else
- case CAN: /* CAN */
- case SUB: /* SUB */
- state = 0;
- return;
- #endif /* NEC_KANJI */
- #ifdef KANJI
- case CSI: /* CSI (ESC [) */
- if (kanjiCode == EUC || kanjiCode == SJIS)
- goto kconvert;
- for (ai = 0; ai < MAX_PARAM; ai++) val[ai] = 0;
- ai = 0;
- state = 2;
- return;
- #endif /* KANJI */
- case ESC:
- for (ai = 0; ai < MAX_PARAM; ai++) val[ai] = 0;
- ai = 0;
- state = 1;
- return;
- #ifdef UOP_GRAPHICS
- case SOH:
- if (state == 17) state17(c);
- return;
- #endif /* UOP_GRAPHICS */
- default:
- if (c < SPACE) /* discard ctrl char */
- return;
- #ifndef KANJI
- if (state == 0) {
- conWrite(c);
- return;
- }
- #else
- if (state)
- break;
- kconvert:
- switch (kstate) {
- case 0: /* roman */
- switch (kanjiCode) {
- case EUC:
- if (kfirst) { /* kfirst = kanji1 or SS2 */
- if (kfirst == SS2)
- conWrite(c); /* kana 00xx */
- else
- conWrite(((kfirst << 8) | c) & 0x7f7f);
- kfirst = 0;
- return;
- }
- if (c & 0x80) { /* kanji 1st or SS2 */
- kfirst = c;
- return;
- }
- conWrite(c); /* ASCII */
- return;
- case SJIS:
- if (isSJIS1(c) && kfirst == 0) {
- kfirst = c;
- return;
- }
- if (isSJIS1(kfirst) && isSJIS2(c)) {
- c = SJIStoJIS(kfirst, c);
- kfirst = 0;
- }
- if (isKana(c))
- kfirst = 0;
- conWrite(c);
- return;
- default: /* NJIS, OJIS, NEC */
- conWrite(c);
- return;
- }
- case 1: /* kana */
- conWrite(c | 0x80); /* note: no SS2 */
- return;
- case 2: /* kanji */
- if (kfirst) {
- conWrite((kfirst << 8) | c);
- kfirst = 0;
- return;
- }
- kfirst = c;
- return;
- case 3: /* DEC special */
- conWrite(decConv[c & 0x7f]);
- return;
- }
- #endif /* KANJI */
- }
- /* if in the state sequence and not controll character */
- switch (state) {
- case 1: /* ESC */
- state1(c);
- break;
- case 2: /* ESC [ */
- state2(c);
- break;
- case 3: /* ESC [ > */
- state3(c);
- break;
- case 5: /* ESC # */
- state5();
- break;
- case 8: /* ESC [ ? */
- state8(c);
- break;
- #ifdef UOP_GRAPHICS
- /* expand for graphics */
- case 16: /* ESC R */
- state16(c);
- break;
- case 17: /* ESC R t */
- state17(c);
- break;
- #endif /* UOP_GRAPHICS */
- #ifdef KANJI
- case 100: /* ESC $ */
- state100(c);
- break;
- case 101: /* ESC $ ( */
- state101(c);
- break;
- case 102: /* ESC ( */
- state102(c);
- break;
- case 103: /* ESC $ ) */
- state103();
- break;
- case 104: /* ESC ) */
- state104();
- break;
- #ifdef NEC_KANJI
- case 110: /* SUB */
- state110(c);
- break;
- #endif /* NEC_KANJI */
- #endif /* KANJI */
- }
- }
-
- /*
- * after ESC
- */
- static void state1(c)
- register u_short c;
- {
- switch(c) {
- case '[': state = 2; break;
- case '#': state = 5; break;
- case '>': state = 0; applKeypad = NO; break;
- case '=': state = 0; applKeypad = YES; break;
- case '7': saveCursor(); /* ESC 7 cursor save */
- state = 0;
- locate(cursorX, cursorY);
- break;
- case '8': restCursor(); /* ESC 8 cursor restore */
- wrapPending = NO;
- state = 0;
- break;
- case 'H': /* ESC H set horizontal tab stop */
- if (cursorX < MAX_COLUMN)
- tabTable[cursorX] = 1;
- state = 0;
- break;
- case 'D': downLine(); state = 0; break; /* IND */
- case 'M': upLine(); state = 0; break; /* RI */
- case 'E': locate(0, cursorY); /* NEL */
- downLine();
- state = 0;
- break;
- #ifdef UOP_GRAPHICS
- case 'R': state = 16; break;
- #endif /* UOP_GRAPHICS */
- #ifdef KANJI
- case '$': state = 100; break;
- case '(': state = 102; break;
- case ')': state = 103; break;
- #endif /* KANJI */
- default: state = 0; /* undefined sequence */
- }
- }
-
- static void state2(c)
- /* ESC [ or CSI */
- register u_short c;
- {
- if (isdigit(c)) { /* ESC [ Pn */
- if (ai < MAX_PARAM)
- val[ai] = val[ai]*10+c-'0';
- else
- state = 0;
- return;
- } else if (c == ';') { /* ESC [ Pn ; */
- ai++;
- return;
- } else {
- switch(c) { /* ESC [ Pm ; Pn c */
- case '=':
- case '?':
- state = 8;
- return;
- case 'H': /* Horizontal and vertical positioning (not recommended */
- case 'f': /* cursor position */
- cursorPosition();
- break;
- case 'm': /* set attribute */
- setAttribute();
- locate(cursorX, cursorY);
- break;
- case 'K': /* erase within line */
- eraseLine();
- break;
- case 'J': /* erase within screen */
- eraseScreen();
- break;
- case 'M': /* delete line */
- if (val[0] == 0) val[0]++;
- wrapPending = NO;
- deleteLine(val[0]);
- locate(0, cursorY);
- break;
- case 'L': /* insert line */
- if (val[0] == 0) val[0]++;
- wrapPending = NO;
- insertLine(val[0]);
- locate(0, cursorY);
- break;
- case 's': /* save cursor pos. */
- saveCursor();
- break;
- case 'u': /* restore cursor position */
- restCursor();
- break;
- case 'P': /* ESC [ val P del char */
- if (val[0] == 0) val[0] = 1;
- wrapPending = NO;
- deleteChar(cursorX, val[0]);
- locate(cursorX, cursorY);
- wrapPending = NO;
- break;
- case '@': /* ESC [ val @ insert char */
- if (val[0] == 0) val[0] = 1;
- wrapPending = NO;
- insertChar(cursorX, val[0]);
- locate(cursorX, cursorY);
- wrapPending = NO;
- break;
- case 'A': /* cursor up */
- moveCursor(UP, val[0]);
- break;
- case 'B': /* cursor down */
- moveCursor(DOWN, val[0]);
- break;
- case 'C': /* cursor right */
- moveCursor(RIGHT, val[0]);
- break;
- case 'D': /* cursor left */
- moveCursor(LEFT, val[0]);
- break;
- case 'r': /* set scrole region */
- scrollRegion();
- break;
- case '>': /* set/reset mode */
- val[0] = 0;
- state = 3;
- return;
- case 'g': /* clear tab stops */
- if (val[0] == 0 && cursorX < MAX_COLUMN)
- tabTable[cursorX] = 0;
- else if (val[0] == 3)
- clearAllTabs();
- break;
- case 'n': /* cursor reporting function */
- cursorReport();
- break;
- }
- }
- state = 0;
- }
-
- static void state3(c)
- /* ESC [ > */
- register u_short c;
- {
- if (isdigit(c)) {
- if (ai < MAX_PARAM)
- val[ai] = val[ai]*10+c-'0';
- else
- state = 0;
- return;
- }
- switch (val[0]) {
- case 1:
- #if 0
- if (c == 'h')
- enable25(); /* enable bottom line */
- else if (c == 'l')
- disable25(); /* disable bottom line */
- #endif
- break;
- }
- state = 0;
- }
-
- static void state5()
- /* ESC # */
- {
- /* do nothing */
- state = 0;
- }
-
- static void state8(c)
- /* ESC [ ? */
- register u_short c;
- {
- if (isdigit(c)) { /* ESC [ ? Pn */
- if (ai < MAX_PARAM)
- val[ai] = val[ai]*10+c-'0';
- else
- state = 0;
- return;
- }
- switch (val[0]) {
- case 1:
- if (c == 'h') /* ESC [ ? 1 h */
- applCursor = YES; /* appl. curosr mode */
- else if (c == 'l')
- applCursor = NO; /* normal curosr mode */
- break;
- /* case 2: vt52mode(); break;*/
- /* case 3: 132/80column(); break;*/
- case 4:
- switch (c) { /* ESC [ ? 4 h */
- case 'h': /* smoothMode(YES); */ break; /* smooth scroll */
- case 'l': /* smoothMode(NO); */ break; /* jump scroll */
- case 'i': printMode = NO; break;
- }
- break;
- case 5:
- switch (c) {
- case 'h': /* reverseScreen(); */
- break;
- case 'l': /* normalScreen(); */
- break;
- case 'i': /* auto print mode */
- printMode = YES;
- printOn();
- break;
- }
- break;
- case 6:
- if (c == 'h') /* ESC [ ? 6 h */
- originMode = YES;
- else if (c == 'l')
- originMode = NO;
- break;
- case 7:
- if (c == 'h') /* ESC [ ? 7 h */
- awMode(YES); /* auto wrap */
- else if (c == 'l')
- awMode(NO); /* no auto wrap */
- break;
- /* case 8: autoRepeatMode(); break;*/
- case 25:
- if (c == 'h') { /* ESC [ ? 25 h */
- cursor = YES;
- cursorOnOff(YES); /* cursor on */
- } else if (c == 'l') { /* ESC [ ? 25 l */
- cursor = NO;
- cursorOnOff(NO); /* cursor off */
- }
- break;
- case 47:
- if (c == 'h') { /* ESC [ ? 47 h */
- if (pageSaved == NO)
- savePage(ansiVramSave);
- pageSaved = YES;
- } else if (c == 'l') {
- if (pageSaved)
- restorePage(ansiVramSave);
- pageSaved = NO;
- }
- break;
- #ifdef FEP
- case 98:
- if (c == 'h') /* ESC [ ? 98 h */
- fep_force_on();
- else if (c == 'l')
- fep_force_off();
- break;
- case 99:
- if (c == 'h') /* ESC [ ? 99 h */
- fep_on();
- else if (c == 'l')
- fep_off();
- break;
- #endif /* FEP */
- }
- locate(cursorX, cursorY);
- state = 0;
- }
-
- #ifdef UOP_GRAPHICS
- /* expand for graphics */
- static void state16(c)
- /* after ESC R */
- u_short c;
- {
- if (isdigit((char)c)) { /* ESC R Pn */
- if (ai < MAX_PARAM)
- val[ai] = val[ai]*10+(char)c-'0';
- else
- state = 0;
- return;
- } else if (c == ';') { /* ESC R Pn ; */
- ai++;
- return;
- } else {
- switch(c) { /* ESC R Pm ; Pn c */
- case 't': /* plot label */
- state = 17;
- return;
- case 'm': /* plot move */
- plot_move(val[0],val[1]);
- break;
- case 'n': /* plot cont */
- plot_cont(val[0],val[1]);
- break;
- case 'p': /* plot point */
- plot_point(val[0],val[1]);
- break;
- case 'l': /* plot line */
- plot_line(val[0],val[1],val[2],val[3]);
- break;
- case 'b': /* plot box */
- plot_box(val[0],val[1],val[2],val[3]);
- break;
- case 'r': /* plot box fill */
- plot_boxfill(val[0],val[1],val[2],val[3]);
- break;
- case 'a': /* plot arc */
- plot_arc(val[0],val[1],val[2],val[3],val[4],val[5]);
- break;
- case 'c': /* plot circle */
- plot_circle(val[0],val[1],val[2]);
- break;
- case 'f': /* plot linemod */
- plot_linemod(val[0]);
- break;
- case 'x': /* plot change color */
- plot_colormod(val[0]);
- break;
- case 's': /* plot space */
- plot_space(val[0],val[1],val[2],val[3]);
- break;
- case 'e': /* plot erase */
- plot_erase();
- break;
- }
- ai = 0;
- }
- state = 0;
- }
-
- static void state17(c)
- /* after ESC R t */
- u_short c;
- {
- static int i = 0;
- static u_char buff[256];
-
- if(c == 0x01) {
- force:
- buff[i] = (u_char)'\0';
- plot_label(buff);
- state = 0;
- i = 0;
- } else {
- if (i > 254)
- goto force;
- buff[i++] = (u_char)c;
- }
- }
-
- #endif /* UOP_GRAPHICS */
-
- #ifdef KANJI
-
- static void state100(c)
- /* ESC $ */
- register u_short c;
- {
- switch (c) {
- case '@': /* designate jis kanji 78 to G0 */
- case 'B': /* designate jis kanji 83 to G0 */
- case '1': /* designate DEC kanji to G0 */
- state = 0;
- kstate = 2; /* enter kanji mode */
- kfirst = 0; /* clear first byte of kanji */
- break;
- case '(':
- state = 101;
- break;
- case ')':
- state = 103;
- break;
- default:
- state = 0;
- break;
- }
- }
-
- static void state101(c)
- /* ESC $ ( */
- register u_short c;
- {
- switch (c) {
- case '@': /* designate jis kanji 78 to G0 */
- case 'B': /* designate jis kanji 83 to G0 */
- case '1': /* designate DEC kanji to G0 */
- kstate = 2; /* enter kanji mode */
- kfirst = 0; /* clear first byte of kanji */
- break;
- }
- state = 0;
- locate(cursorX, cursorY);
- }
-
- static void state102(c)
- /* ESC ( */
- register u_short c;
- {
- switch (c) {
- case 'H': /* designate Swedish to G0 */
- case 'J': /* designate jis roman to G0 */
- case 'B': /* designate ascii to G0 */
- kstate = 0; /* enter alpha mode */
- break;
- case 'I': /* designate jis kana to G0 */
- kstate = 1; /* enter kana mode */
- break;
- case '0': /* designate DEC special to G0 */
- kstate = 3; /* enter DEC special mode */
- break;
- }
- locate(cursorX, cursorY);
- state = 0;
- }
-
- static void state103()
- /* ESC $ ) */
- {
- /* designate 2byte code to G1 (ignored) */
- state = 0;
- }
-
- static void state104()
- /* ESC ) */
- {
- /* designate 1byte code to G1 (ignored) */
- state = 0;
- }
-
- #ifdef NEC_KANJI
-
- static void state110(c)
- /* SUB */
- register u_short c;
- {
- switch (c) {
- case 'p': /* designate nec kanji */
- kstate = 2; /* enter kanji mode */
- kfirst = 0; /* clear first byte of kanji */
- break;
- case 'q': /* designate nec alpha */
- kstate = 0; /* enter alpha mode */
- break;
- }
- state = 0;
- }
- #endif /* NEC_KANJI */
-
- #endif /* KANJI */
-
- static void saveCursor()
- /* save Cursor position */
- {
- saveX = cursorX;
- saveY = cursorY;
- saveAttr = attrib;
- locate(cursorX, cursorY);
- }
-
- static void restCursor()
- /* restore cursor pos. */
- {
- attrib = saveAttr;
- locate(saveX, saveY);
- }
-
- static void setAttribute()
- {
- register int i, c;
-
- for (i = ai + 1, ai = 0; i > 0; --i) {
- switch(c = val[ai++]) {
- case 0: /* normal mode */
- normalMode();
- break;
- case 1: /* high mode */
- #ifdef PC98
- attrib &= 0xcf;
- attrib |= 0xc0;
- #else
- attrib |= _HIGH;
- #endif
- break;
- case 4: /* underlined */
- #ifdef PC98
- attrib |= B_UNDER;
- #else
- attrib |= _UNDER;
- #endif
- break;
- case 5: /* blink */
- #ifdef PC98
- attrib |= B_BLINK;
- #else
- attrib |= _BLINK;
- #endif
- break;
- case 7: /* reverse mode */
- reverseMode();
- break;
- default:
- if (softFont)
- break;
- #ifdef PC98
- if (30 <= c && c <= 37) { /* set foreground color */
- attrib &= CMASK;
- attrib |= ctab[c - 30];
- break;
- }
- if (40 <= c && c <= 47) { /* set background color */
- attrib &= CMASK;
- attrib |= ctab[c - 40] | _REVER;
- break;
- }
- #endif /* PC98 */
- #ifdef IBMPC
- if (30 <= c && c <= 37) { /* set foreground color */
- attrib &= FMASK;
- attrib |= fColor[c - 30];
- break;
- }
- if (40 <= c && c <= 47) { /* set background color */
- attrib &= BMASK;
- attrib |= bColor[c - 40];
- break;
- }
- #endif /* IBMPC */
- /*
- * else ignore
- */
- break;
- }
- }
- }
-
- /*
- ** normalMode()
- *
- * set attribute normal mode
- */
- void normalMode()
- {
- attrib = eraseAttr;
- }
-
- /*
- ** reverseMode()
- *
- * set attribute reverse mode
- */
- void reverseMode()
- {
- #ifdef IBMPC
- /*
- * back color to foreground color, foreground color to back ground color
- */
- if (softFont)
- attrib |= 0x10;
- else {
- attrib &= 0x88;
- attrib |= ((eraseAttr & 0x70)>>4 | (eraseAttr & 0x07) << 4);
- }
- #endif /* IBMPC */
- #ifdef J3100
- attrib |= _REVER;
- #endif /* J3100 */
- #ifdef PC98
- if (eraseAttr & B_REVER)
- attrib &= ~B_REVER;
- else
- attrib |= B_REVER;
- #endif /* PC98 */
- }
-
- static void eraseScreen()
- /*
- * erase within screen
- */
- {
- register u_short tmpX, tmpY;
-
- wrapPending = NO;
- tmpX = cursorX;
- tmpY = cursorY;
- switch(val[0]) {
- case 0: /* erase from cursor to end of screen */
- clearColumn(cursorX, MAX_COLUMN);
- if (cursorY < bottomLine)
- clearLine(cursorY+1, bottomLine);
- break;
- case 1: /* erase from top of screen to cursor */
- if (cursorY > 0)
- clearLine(0, cursorY-1);
- clearColumn(0, cursorX+1); /* erase column on cursor too */
- break;
- case 2: /* erase entire screen */
- clearLine(0, bottomLine);
- tmpX = tmpY = 0;
- break;
- }
- locate(tmpX, tmpY);
- }
-
- static void eraseLine()
- /*
- * erase within line
- */
- {
- register u_short tmpX, tmpY;
-
- wrapPending = NO;
- tmpX = cursorX;
- tmpY = cursorY;
- switch(val[0]) {
- case 0: /* erase from cursor to end of line */
- clearColumn(cursorX, MAX_COLUMN);
- break;
- case 1: /* erase from start of line to cursor */
- clearColumn(0, cursorX+1);
- break;
- case 2: /* erase entire line */
- clearColumn(0, MAX_COLUMN);
- break;
- }
- locate(tmpX, tmpY);
- }
-
- static void cursorPosition()
- /* set direct cursor position */
- {
- register short x = val[1] - 1;
- register short y = val[0] - 1;
-
- wrapPending = NO;
- if (x <= 0)
- x = 0;
- if (x > LAST_COLUMN)
- x = LAST_COLUMN;
- if (y <= 0)
- y = 0;
- if (originMode) {
- if ((y += upScrRegion) > lowScrRegion)
- y = lowScrRegion;
- } else {
- if (y > bottomLine)
- y = bottomLine;
- }
- locate(x, y);
- }
-
- static void scrollRegion()
- /* set scroll region */
- {
- register short up = val[0] - 1;
- register short low = val[1] - 1;
-
- if (up <= 0)
- up = 0;
- else if (up > bottomLine)
- up = bottomLine;
- if (low < 0 || low > bottomLine)
- low = bottomLine;
- if (low < up)
- return; /* illegal request, ignore ??? */
- upScrRegion = up; lowScrRegion = low;
- savePage(page0Save);
- restorePage(page0Save);
- if (originMode)
- locate(upScrRegion, 0);
- else
- locate(0, 0);
- cursorOnOff(cursor);
- }
-
- static void downLine()
- {
- wrapPending = NO;
- if (cursorX > LAST_COLUMN)
- cursorX = 0;
- if (cursorY < lowScrRegion)
- cursorY++;
- else if (cursorY == lowScrRegion)
- scrlUp();
- else if (cursorY < bottomLine)
- cursorY++;
- locate(cursorX, cursorY);
- }
-
- static void upLine()
- {
- wrapPending = NO;
- if (cursorX > LAST_COLUMN)
- cursorX = 0;
- if (cursorY > upScrRegion)
- cursorY--;
- else if (cursorY == upScrRegion) {
- insertLine(1);
- } else if (cursorY > 0)
- cursorY--;
- locate(cursorX, cursorY);
- }
-
- static void cursorReport()
- {
- int i;
-
- for (i = ai + 1, ai = 0; i > 0; --i) {
- switch (val[ai++]) {
- case 5: /* are you OK? */
- outESC("[0n"); /* always OK */
- break;
- case 6: /* cursor position report */
- sprintf(tmpBuf, "[%d;%dR", cursorY+1, cursorX+1);
- outESC(tmpBuf);
- break;
- }
- /* else ignore */
- }
- }
-