Open-Source-Software-Entwicklung und Downloads

Browse Subversion Repository

Diff of /trunk/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3226 by maya, Tue Mar 24 09:37:20 2009 UTC revision 3227 by maya, Tue Mar 24 15:10:33 2009 UTC
# Line 1  Line 1 
1  /* Tera Term  /* Tera Term
2   Copyright(C) 1994-1998 T. Teranishi   Copyright(C) 1994-1998 T. Teranishi
3   All rights reserved. */   All rights reserved. */
4    
5  /* TERATERM.EXE, VT terminal emulation */  /* TERATERM.EXE, VT terminal emulation */
6  #include "teraterm.h"  #include "teraterm.h"
7  #include "tttypes.h"  #include "tttypes.h"
8  #include <stdio.h>  #include <stdio.h>
9  #include <string.h>  #include <string.h>
10  #include <stdlib.h>  #include <stdlib.h>
11  #include <mbstring.h>  #include <mbstring.h>
12  #include <locale.h>  #include <locale.h>
13    
14  #include "buffer.h"  #include "buffer.h"
15  #include "ttwinman.h"  #include "ttwinman.h"
16  #include "ttcommon.h"  #include "ttcommon.h"
17  #include "commlib.h"  #include "commlib.h"
18  #include "vtdisp.h"  #include "vtdisp.h"
19  #include "keyboard.h"  #include "keyboard.h"
20  #include "ttlib.h"  #include "ttlib.h"
21  #include "ttftypes.h"  #include "ttftypes.h"
22  #include "filesys.h"  #include "filesys.h"
23  #include "teraprn.h"  #include "teraprn.h"
24  #include "telnet.h"  #include "telnet.h"
25    
26  #include "vtterm.h"  #include "vtterm.h"
27    
28    /* Parsing modes */    /* Parsing modes */
29  #define ModeFirst 0  #define ModeFirst 0
30  #define ModeESC   1  #define ModeESC   1
31  #define ModeDCS   2  #define ModeDCS   2
32  #define ModeDCUserKey 3  #define ModeDCUserKey 3
33  #define ModeSOS   4  #define ModeSOS   4
34  #define ModeCSI   5  #define ModeCSI   5
35  #define ModeXS    6  #define ModeXS    6
36  #define ModeDLE   7  #define ModeDLE   7
37  #define ModeCAN   8  #define ModeCAN   8
38    
39  #define NParamMax 16  #define NParamMax 16
40  #define IntCharMax 5  #define IntCharMax 5
41    
42  void VisualBell();  void VisualBell();
43    
44  /* character attribute */  /* character attribute */
45  static TCharAttr CharAttr;  static TCharAttr CharAttr;
46    
47  /* various modes of VT emulation */  /* various modes of VT emulation */
48  static BOOL RelativeOrgMode;  static BOOL RelativeOrgMode;
49  static BOOL InsertMode;  static BOOL InsertMode;
50  static BOOL LFMode;  static BOOL LFMode;
51  static BOOL AutoWrapMode;  static BOOL AutoWrapMode;
52  static BOOL FocusReportMode;  static BOOL FocusReportMode;
53  int MouseReportMode;  int MouseReportMode;
54    
55  // save/restore cursor  // save/restore cursor
56  typedef struct {  typedef struct {
57    int CursorX, CursorY;    int CursorX, CursorY;
58    TCharAttr Attr;    TCharAttr Attr;
59    int Glr[2], Gn[4]; // G0-G3, GL & GR    int Glr[2], Gn[4]; // G0-G3, GL & GR
60    BOOL AutoWrapMode;    BOOL AutoWrapMode;
61    BOOL RelativeOrgMode;    BOOL RelativeOrgMode;
62  } TStatusBuff;  } TStatusBuff;
63  typedef TStatusBuff *PStatusBuff;  typedef TStatusBuff *PStatusBuff;
64    
65  // status buffer for main screen & status line  // status buffer for main screen & status line
66  static TStatusBuff SBuff1, SBuff2;  static TStatusBuff SBuff1, SBuff2;
67    
68  static BOOL ESCFlag, JustAfterESC;  static BOOL ESCFlag, JustAfterESC;
69  static BOOL KanjiIn;  static BOOL KanjiIn;
70  static BOOL EUCkanaIn, EUCsupIn;  static BOOL EUCkanaIn, EUCsupIn;
71  static int  EUCcount;  static int  EUCcount;
72  static BOOL Special;  static BOOL Special;
73    
74  static int Param[NParamMax+1];  static int Param[NParamMax+1];
75  static int NParam;  static int NParam;
76  static BOOL FirstPrm;  static BOOL FirstPrm;
77  static BYTE IntChar[IntCharMax+1];  static BYTE IntChar[IntCharMax+1];
78  static int ICount;  static int ICount;
79  static BYTE Prv;  static BYTE Prv;
80  static int ParseMode, SavedMode;  static int ParseMode, SavedMode;
81  static int ChangeEmu;  static int ChangeEmu;
82    
83  /* user defined keys */  /* user defined keys */
84  static BOOL WaitKeyId, WaitHi;  static BOOL WaitKeyId, WaitHi;
85    
86  /* GL, GR code group */  /* GL, GR code group */
87  static int Glr[2];  static int Glr[2];
88  /* G0, G1, G2, G3 code group */  /* G0, G1, G2, G3 code group */
89  static int Gn[4];  static int Gn[4];
90  /* GL for single shift 2/3 */  /* GL for single shift 2/3 */
91  static int GLtmp;  static int GLtmp;
92  /* single shift 2/3 flag */  /* single shift 2/3 flag */
93  static BOOL SSflag;  static BOOL SSflag;
94  /* JIS -> SJIS conversion flag */  /* JIS -> SJIS conversion flag */
95  static BOOL ConvJIS;  static BOOL ConvJIS;
96  static WORD Kanji;  static WORD Kanji;
97    
98  // variables for status line mode  // variables for status line mode
99  static int StatusX=0;  static int StatusX=0;
100  static BOOL StatusWrap=FALSE;  static BOOL StatusWrap=FALSE;
101  static BOOL StatusCursor=TRUE;  static BOOL StatusCursor=TRUE;
102  static int MainX, MainY; //cursor registers  static int MainX, MainY; //cursor registers
103  static int MainTop, MainBottom; // scroll region registers  static int MainTop, MainBottom; // scroll region registers
104  static BOOL MainWrap;  static BOOL MainWrap;
105  static BOOL MainCursor=TRUE;  static BOOL MainCursor=TRUE;
106    
107  /* status for printer escape sequences */  /* status for printer escape sequences */
108  static BOOL PrintEX = TRUE;  // printing extent  static BOOL PrintEX = TRUE;  // printing extent
109                              // (TRUE: screen, FALSE: scroll region)                              // (TRUE: screen, FALSE: scroll region)
110  static BOOL AutoPrintMode = FALSE;  static BOOL AutoPrintMode = FALSE;
111  static BOOL PrinterMode = FALSE;  static BOOL PrinterMode = FALSE;
112  static BOOL DirectPrn = FALSE;  static BOOL DirectPrn = FALSE;
113    
114  /* User key */  /* User key */
115  static BYTE NewKeyStr[FuncKeyStrMax];  static BYTE NewKeyStr[FuncKeyStrMax];
116  static int NewKeyId, NewKeyLen;  static int NewKeyId, NewKeyLen;
117    
118  static _locale_t CLocale;  static _locale_t CLocale;
119    
120  void ResetSBuffers()  void ResetSBuffers()
121  {  {
122    SBuff1.CursorX = 0;    SBuff1.CursorX = 0;
123    SBuff1.CursorY = 0;    SBuff1.CursorY = 0;
124    SBuff1.Attr = DefCharAttr;    SBuff1.Attr = DefCharAttr;
125    if (ts.Language==IdJapanese)    if (ts.Language==IdJapanese)
126    {    {
127      SBuff1.Gn[0] = IdASCII;      SBuff1.Gn[0] = IdASCII;
128      SBuff1.Gn[1] = IdKatakana;      SBuff1.Gn[1] = IdKatakana;
129      SBuff1.Gn[2] = IdKatakana;      SBuff1.Gn[2] = IdKatakana;
130      SBuff1.Gn[3] = IdKanji;      SBuff1.Gn[3] = IdKanji;
131      SBuff1.Glr[0] = 0;      SBuff1.Glr[0] = 0;
132      if ((ts.KanjiCode==IdJIS) &&      if ((ts.KanjiCode==IdJIS) &&
133          (ts.JIS7Katakana==0))          (ts.JIS7Katakana==0))
134        SBuff1.Glr[1] = 2;  // 8-bit katakana        SBuff1.Glr[1] = 2;  // 8-bit katakana
135      else      else
136        SBuff1.Glr[1] = 3;        SBuff1.Glr[1] = 3;
137    }    }
138    else {    else {
139      SBuff1.Gn[0] = IdASCII;      SBuff1.Gn[0] = IdASCII;
140      SBuff1.Gn[1] = IdSpecial;      SBuff1.Gn[1] = IdSpecial;
141      SBuff1.Gn[2] = IdASCII;      SBuff1.Gn[2] = IdASCII;
142      SBuff1.Gn[3] = IdASCII;      SBuff1.Gn[3] = IdASCII;
143      SBuff1.Glr[0] = 0;      SBuff1.Glr[0] = 0;
144      SBuff1.Glr[1] = 0;      SBuff1.Glr[1] = 0;
145    }    }
146    SBuff1.AutoWrapMode = TRUE;    SBuff1.AutoWrapMode = TRUE;
147    SBuff1.RelativeOrgMode = FALSE;    SBuff1.RelativeOrgMode = FALSE;
148    // copy SBuff1 to SBuff2    // copy SBuff1 to SBuff2
149    SBuff2 = SBuff1;    SBuff2 = SBuff1;
150  }  }
151    
152  void ResetTerminal() /*reset variables but don't update screen */  void ResetTerminal() /*reset variables but don't update screen */
153  {  {
154    DispReset();    DispReset();
155    BuffReset();    BuffReset();
156    
157    /* Attribute */    /* Attribute */
158    CharAttr = DefCharAttr;    CharAttr = DefCharAttr;
159    Special = FALSE;    Special = FALSE;
160    BuffSetCurCharAttr(CharAttr);    BuffSetCurCharAttr(CharAttr);
161    
162    /* Various modes */    /* Various modes */
163    InsertMode = FALSE;    InsertMode = FALSE;
164    LFMode = (ts.CRSend == IdCRLF);    LFMode = (ts.CRSend == IdCRLF);
165    AutoWrapMode = TRUE;    AutoWrapMode = TRUE;
166    AppliKeyMode = FALSE;    AppliKeyMode = FALSE;
167    AppliCursorMode = FALSE;    AppliCursorMode = FALSE;
168    RelativeOrgMode = FALSE;    RelativeOrgMode = FALSE;
169    ts.ColorFlag &= ~CF_REVERSEVIDEO;    ts.ColorFlag &= ~CF_REVERSEVIDEO;
170    AutoRepeatMode = TRUE;    AutoRepeatMode = TRUE;
171    Send8BitMode = ts.Send8BitCtrl;    Send8BitMode = ts.Send8BitCtrl;
172    FocusReportMode = FALSE;    FocusReportMode = FALSE;
173    MouseReportMode = IdMouseTrackNone;    MouseReportMode = IdMouseTrackNone;
174    
175    CLocale = _create_locale(LC_ALL, "C");    CLocale = _create_locale(LC_ALL, "C");
176    
177    /* Character sets */    /* Character sets */
178    ResetCharSet();    ResetCharSet();
179    
180    /* ESC flag for device control sequence */    /* ESC flag for device control sequence */
181    ESCFlag = FALSE;    ESCFlag = FALSE;
182    /* for TEK sequence */    /* for TEK sequence */
183    JustAfterESC = FALSE;    JustAfterESC = FALSE;
184    
185    /* Parse mode */    /* Parse mode */
186    ParseMode = ModeFirst;    ParseMode = ModeFirst;
187    
188    /* Clear printer mode */    /* Clear printer mode */
189    PrinterMode = FALSE;    PrinterMode = FALSE;
190    
191    // status buffers    // status buffers
192    ResetSBuffers();    ResetSBuffers();
193  }  }
194    
195  void ResetCharSet()  void ResetCharSet()
196  {  {
197    if (ts.Language==IdJapanese)    if (ts.Language==IdJapanese)
198    {    {
199      Gn[0] = IdASCII;      Gn[0] = IdASCII;
200      Gn[1] = IdKatakana;      Gn[1] = IdKatakana;
201      Gn[2] = IdKatakana;      Gn[2] = IdKatakana;
202      Gn[3] = IdKanji;      Gn[3] = IdKanji;
203      Glr[0] = 0;      Glr[0] = 0;
204      if ((ts.KanjiCode==IdJIS) &&      if ((ts.KanjiCode==IdJIS) &&
205          (ts.JIS7Katakana==0))          (ts.JIS7Katakana==0))
206        Glr[1] = 2;  // 8-bit katakana        Glr[1] = 2;  // 8-bit katakana
207      else      else
208        Glr[1] = 3;        Glr[1] = 3;
209    }    }
210    else {    else {
211      Gn[0] = IdASCII;      Gn[0] = IdASCII;
212      Gn[1] = IdSpecial;      Gn[1] = IdSpecial;
213      Gn[2] = IdASCII;      Gn[2] = IdASCII;
214      Gn[3] = IdASCII;      Gn[3] = IdASCII;
215      Glr[0] = 0;      Glr[0] = 0;
216      Glr[1] = 0;      Glr[1] = 0;
217      cv.SendCode = IdASCII;      cv.SendCode = IdASCII;
218      cv.SendKanjiFlag = FALSE;      cv.SendKanjiFlag = FALSE;
219      cv.EchoCode = IdASCII;      cv.EchoCode = IdASCII;
220      cv.EchoKanjiFlag = FALSE;      cv.EchoKanjiFlag = FALSE;
221    }    }
222    /* Kanji flag */    /* Kanji flag */
223    KanjiIn = FALSE;    KanjiIn = FALSE;
224    EUCkanaIn = FALSE;    EUCkanaIn = FALSE;
225    EUCsupIn = FALSE;    EUCsupIn = FALSE;
226    SSflag = FALSE;    SSflag = FALSE;
227    
228    cv.Language = ts.Language;    cv.Language = ts.Language;
229    cv.CRSend = ts.CRSend;    cv.CRSend = ts.CRSend;
230    cv.KanjiCodeEcho = ts.KanjiCode;    cv.KanjiCodeEcho = ts.KanjiCode;
231    cv.JIS7KatakanaEcho = ts.JIS7Katakana;    cv.JIS7KatakanaEcho = ts.JIS7Katakana;
232    cv.KanjiCodeSend = ts.KanjiCodeSend;    cv.KanjiCodeSend = ts.KanjiCodeSend;
233    cv.JIS7KatakanaSend = ts.JIS7KatakanaSend;    cv.JIS7KatakanaSend = ts.JIS7KatakanaSend;
234    cv.KanjiIn = ts.KanjiIn;    cv.KanjiIn = ts.KanjiIn;
235    cv.KanjiOut = ts.KanjiOut;    cv.KanjiOut = ts.KanjiOut;
236  }  }
237    
238  void ResetKeypadMode(BOOL DisabledModeOnly)  void ResetKeypadMode(BOOL DisabledModeOnly)
239  {  {
240    if (!DisabledModeOnly || ts.DisableAppKeypad) AppliKeyMode = FALSE;    if (!DisabledModeOnly || ts.DisableAppKeypad) AppliKeyMode = FALSE;
241    if (!DisabledModeOnly || ts.DisableAppCursor) AppliCursorMode = FALSE;    if (!DisabledModeOnly || ts.DisableAppCursor) AppliCursorMode = FALSE;
242  }  }
243    
244  void MoveToMainScreen()  void MoveToMainScreen()
245  {  {
246    StatusX = CursorX;    StatusX = CursorX;
247    StatusWrap = Wrap;    StatusWrap = Wrap;
248    StatusCursor = IsCaretEnabled();    StatusCursor = IsCaretEnabled();
249    
250    CursorTop = MainTop;    CursorTop = MainTop;
251    CursorBottom = MainBottom;    CursorBottom = MainBottom;
252    Wrap = MainWrap;    Wrap = MainWrap;
253    DispEnableCaret(MainCursor);    DispEnableCaret(MainCursor);
254    MoveCursor(MainX,MainY); // move to main screen    MoveCursor(MainX,MainY); // move to main screen
255  }  }
256    
257  void MoveToStatusLine()  void MoveToStatusLine()
258  {  {
259    MainX = CursorX;    MainX = CursorX;
260    MainY = CursorY;    MainY = CursorY;
261    MainTop = CursorTop;    MainTop = CursorTop;
262    MainBottom = CursorBottom;    MainBottom = CursorBottom;
263    MainWrap = Wrap;    MainWrap = Wrap;
264    MainCursor = IsCaretEnabled();    MainCursor = IsCaretEnabled();
265    
266    DispEnableCaret(StatusCursor);    DispEnableCaret(StatusCursor);
267    MoveCursor(StatusX,NumOfLines-1); // move to status line    MoveCursor(StatusX,NumOfLines-1); // move to status line
268    CursorTop = NumOfLines-1;    CursorTop = NumOfLines-1;
269    CursorBottom = CursorTop;    CursorBottom = CursorTop;
270    Wrap = StatusWrap;    Wrap = StatusWrap;
271  }  }
272    
273  void HideStatusLine()  void HideStatusLine()
274  {  {
275    if ((StatusLine>0) &&    if ((StatusLine>0) &&
276        (CursorY==NumOfLines-1))        (CursorY==NumOfLines-1))
277      MoveToMainScreen();      MoveToMainScreen();
278    StatusX = 0;    StatusX = 0;
279    StatusWrap = FALSE;    StatusWrap = FALSE;
280    StatusCursor = TRUE;    StatusCursor = TRUE;
281    ShowStatusLine(0); //hide    ShowStatusLine(0); //hide
282  }  }
283    
284  void ChangeTerminalSize(int Nx, int Ny)  void ChangeTerminalSize(int Nx, int Ny)
285  {  {
286    BuffChangeTerminalSize(Nx,Ny);    BuffChangeTerminalSize(Nx,Ny);
287    StatusX = 0;    StatusX = 0;
288    MainX = 0;    MainX = 0;
289    MainY = 0;    MainY = 0;
290    MainTop = 0;    MainTop = 0;
291    MainBottom = NumOfColumns-1;    MainBottom = NumOfColumns-1;
292  }  }
293    
294  void BackSpace()  void BackSpace()
295  {  {
296    if (CursorX == 0)    if (CursorX == 0)
297    {    {
298      if ((CursorY>0) &&      if ((CursorY>0) &&
299          ((ts.TermFlag & TF_BACKWRAP)!=0))          ((ts.TermFlag & TF_BACKWRAP)!=0))
300      {      {
301        MoveCursor(NumOfColumns-1,CursorY-1);        MoveCursor(NumOfColumns-1,CursorY-1);
302  //      if (cv.HLogBuf!=0) Log1Byte(BS);  //      if (cv.HLogBuf!=0) Log1Byte(BS);
303  // (2005.2.20 yutaka)  // (2005.2.20 yutaka)
304            if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);            if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);
305      }      }
306    }    }
307    else if (CursorX > 0)    else if (CursorX > 0)
308    {    {
309      MoveCursor(CursorX-1,CursorY);      MoveCursor(CursorX-1,CursorY);
310  //    if (cv.HLogBuf!=0) Log1Byte(BS);  //    if (cv.HLogBuf!=0) Log1Byte(BS);
311  // (2005.2.20 yutaka)  // (2005.2.20 yutaka)
312            if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);            if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);
313    }    }
314  }  }
315    
316  void CarriageReturn(BOOL logFlag)  void CarriageReturn(BOOL logFlag)
317  {  {
318  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
319          if (!ts.EnableContinuedLineCopy || logFlag)          if (!ts.EnableContinuedLineCopy || logFlag)
320  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
321          if (cv.HLogBuf!=0) Log1Byte(CR);          if (cv.HLogBuf!=0) Log1Byte(CR);
322    
323          if (CursorX>0)          if (CursorX>0)
324                  MoveCursor(0,CursorY);                  MoveCursor(0,CursorY);
325  }  }
326    
327  void LineFeed(BYTE b, BOOL logFlag)  void LineFeed(BYTE b, BOOL logFlag)
328  {  {
329          /* for auto print mode */          /* for auto print mode */
330          if ((AutoPrintMode) &&          if ((AutoPrintMode) &&
331                  (b>=LF) && (b<=FF))                  (b>=LF) && (b<=FF))
332                  BuffDumpCurrentLine(b);                  BuffDumpCurrentLine(b);
333    
334  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
335          if (!ts.EnableContinuedLineCopy || logFlag)          if (!ts.EnableContinuedLineCopy || logFlag)
336  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
337          if (cv.HLogBuf!=0) Log1Byte(LF);          if (cv.HLogBuf!=0) Log1Byte(LF);
338    
339          if (CursorY < CursorBottom)          if (CursorY < CursorBottom)
340                  MoveCursor(CursorX,CursorY+1);                  MoveCursor(CursorX,CursorY+1);
341          else if (CursorY == CursorBottom) BuffScrollNLines(1);          else if (CursorY == CursorBottom) BuffScrollNLines(1);
342          else if (CursorY < NumOfLines-StatusLine-1)          else if (CursorY < NumOfLines-StatusLine-1)
343                  MoveCursor(CursorX,CursorY+1);                  MoveCursor(CursorX,CursorY+1);
344    
345          if (LFMode) CarriageReturn(logFlag);          if (LFMode) CarriageReturn(logFlag);
346  }  }
347    
348  void Tab()  void Tab()
349  {  {
350    if (Wrap && !ts.VTCompatTab) {    if (Wrap && !ts.VTCompatTab) {
351        CarriageReturn(FALSE);        CarriageReturn(FALSE);
352        LineFeed(LF,FALSE);        LineFeed(LF,FALSE);
353  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
354        if (ts.EnableContinuedLineCopy) {        if (ts.EnableContinuedLineCopy) {
355          SetLineContinued();          SetLineContinued();
356        }        }
357  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
358        Wrap = FALSE;        Wrap = FALSE;
359    }    }
360    CursorForwardTab(1, AutoWrapMode);    CursorForwardTab(1, AutoWrapMode);
361    if (cv.HLogBuf!=0) Log1Byte(HT);    if (cv.HLogBuf!=0) Log1Byte(HT);
362  }  }
363    
364  void PutChar(BYTE b)  void PutChar(BYTE b)
365  {  {
366    BOOL SpecialNew;    BOOL SpecialNew;
367    TCharAttr CharAttrTmp;    TCharAttr CharAttrTmp;
368    
369    CharAttrTmp = CharAttr;    CharAttrTmp = CharAttr;
370    
371    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
372      WriteToPrnFile(b,TRUE);      WriteToPrnFile(b,TRUE);
373      return;      return;
374    }    }
375    
376    if (Wrap)    if (Wrap)
377    {    {
378      CarriageReturn(FALSE);      CarriageReturn(FALSE);
379      LineFeed(LF,FALSE);      LineFeed(LF,FALSE);
380  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
381      CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;      CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;
382  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
383    }    }
384    
385  //  if (cv.HLogBuf!=0) Log1Byte(b);  //  if (cv.HLogBuf!=0) Log1Byte(b);
386  // (2005.2.20 yutaka)  // (2005.2.20 yutaka)
387    if (ts.LogTypePlainText) {    if (ts.LogTypePlainText) {
388            if (__isascii(b) && !isprint(b)) {            if (__isascii(b) && !isprint(b)) {
389                    // ASCII文字で、非表示な文字はログ採取しない。                    // ASCII文字で、非表示な文字はログ採取しない。
390            } else {            } else {
391                  if (cv.HLogBuf!=0) Log1Byte(b);                  if (cv.HLogBuf!=0) Log1Byte(b);
392            }            }
393    } else {    } else {
394            if (cv.HLogBuf!=0) Log1Byte(b);            if (cv.HLogBuf!=0) Log1Byte(b);
395    }    }
396    
397    Wrap = FALSE;    Wrap = FALSE;
398    
399    SpecialNew = FALSE;    SpecialNew = FALSE;
400    if ((b>0x5F) && (b<0x80))    if ((b>0x5F) && (b<0x80))
401    {    {
402      if (SSflag)      if (SSflag)
403        SpecialNew = (Gn[GLtmp]==IdSpecial);        SpecialNew = (Gn[GLtmp]==IdSpecial);
404      else      else
405        SpecialNew = (Gn[Glr[0]]==IdSpecial);        SpecialNew = (Gn[Glr[0]]==IdSpecial);
406    }    }
407    else if (b>0xDF)    else if (b>0xDF)
408    {    {
409      if (SSflag)      if (SSflag)
410        SpecialNew = (Gn[GLtmp]==IdSpecial);        SpecialNew = (Gn[GLtmp]==IdSpecial);
411      else      else
412        SpecialNew = (Gn[Glr[1]]==IdSpecial);        SpecialNew = (Gn[Glr[1]]==IdSpecial);
413    }    }
414    
415    if (SpecialNew != Special)    if (SpecialNew != Special)
416    {    {
417      UpdateStr();      UpdateStr();
418      Special = SpecialNew;      Special = SpecialNew;
419    }    }
420    
421    if (Special)    if (Special)
422    {    {
423      b = b & 0x7F;      b = b & 0x7F;
424      CharAttrTmp.Attr |= AttrSpecial;      CharAttrTmp.Attr |= AttrSpecial;
425    }    }
426    else    else
427      CharAttrTmp.Attr |= CharAttr.Attr;      CharAttrTmp.Attr |= CharAttr.Attr;
428    
429    BuffPutChar(b, CharAttrTmp, InsertMode);    BuffPutChar(b, CharAttrTmp, InsertMode);
430    
431    if (CursorX < NumOfColumns-1)    if (CursorX < NumOfColumns-1)
432      MoveRight();      MoveRight();
433    else {    else {
434      UpdateStr();      UpdateStr();
435      Wrap = AutoWrapMode;      Wrap = AutoWrapMode;
436    }    }
437  }  }
438    
439  void PutDecSp(BYTE b)  void PutDecSp(BYTE b)
440  {  {
441    TCharAttr CharAttrTmp;    TCharAttr CharAttrTmp;
442    
443    CharAttrTmp = CharAttr;    CharAttrTmp = CharAttr;
444    
445    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
446      WriteToPrnFile(b, TRUE);      WriteToPrnFile(b, TRUE);
447      return;      return;
448    }    }
449    
450    if (Wrap) {    if (Wrap) {
451      CarriageReturn(FALSE);      CarriageReturn(FALSE);
452      LineFeed(LF, FALSE);      LineFeed(LF, FALSE);
453  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
454      CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;      CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;
455  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
456    }    }
457    
458    if (cv.HLogBuf!=0) Log1Byte(b);    if (cv.HLogBuf!=0) Log1Byte(b);
459  /*  /*
460    if (ts.LogTypePlainText && __isascii(b) && !isprint(b)) {    if (ts.LogTypePlainText && __isascii(b) && !isprint(b)) {
461      // ASCII文字で、非表示な文字はログ採取しない。      // ASCII文字で、非表示な文字はログ採取しない。
462    } else {    } else {
463      if (cv.HLogBuf!=0) Log1Byte(b);      if (cv.HLogBuf!=0) Log1Byte(b);
464    }    }
465   */   */
466    
467    Wrap = FALSE;    Wrap = FALSE;
468    
469    if (!Special) {    if (!Special) {
470      UpdateStr();      UpdateStr();
471      Special = TRUE;      Special = TRUE;
472    }    }
473    
474    CharAttrTmp.Attr |= AttrSpecial;    CharAttrTmp.Attr |= AttrSpecial;
475    BuffPutChar(b, CharAttrTmp, InsertMode);    BuffPutChar(b, CharAttrTmp, InsertMode);
476    
477    if (CursorX < NumOfColumns-1)    if (CursorX < NumOfColumns-1)
478      MoveRight();      MoveRight();
479    else {    else {
480      UpdateStr();      UpdateStr();
481      Wrap = AutoWrapMode;      Wrap = AutoWrapMode;
482    }    }
483  }  }
484    
485  void PutKanji(BYTE b)  void PutKanji(BYTE b)
486  {  {
487  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
488    TCharAttr CharAttrTmp;    TCharAttr CharAttrTmp;
489    
490    CharAttrTmp = CharAttr;    CharAttrTmp = CharAttr;
491  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
492    Kanji = Kanji + b;    Kanji = Kanji + b;
493    
494    if (PrinterMode && DirectPrn)    if (PrinterMode && DirectPrn)
495    {    {
496      WriteToPrnFile(HIBYTE(Kanji),FALSE);      WriteToPrnFile(HIBYTE(Kanji),FALSE);
497      WriteToPrnFile(LOBYTE(Kanji),TRUE);      WriteToPrnFile(LOBYTE(Kanji),TRUE);
498      return;      return;
499    }    }
500    
501    if (ConvJIS)    if (ConvJIS)
502      Kanji = JIS2SJIS((WORD)(Kanji & 0x7f7f));      Kanji = JIS2SJIS((WORD)(Kanji & 0x7f7f));
503    
504    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
505      WriteToPrnFile(HIBYTE(Kanji),FALSE);      WriteToPrnFile(HIBYTE(Kanji),FALSE);
506      WriteToPrnFile(LOBYTE(Kanji),TRUE);      WriteToPrnFile(LOBYTE(Kanji),TRUE);
507      return;      return;
508    }    }
509    
510    if (Wrap)    if (Wrap)
511    {    {
512      CarriageReturn(FALSE);      CarriageReturn(FALSE);
513      LineFeed(LF,FALSE);      LineFeed(LF,FALSE);
514  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
515      if (ts.EnableContinuedLineCopy)      if (ts.EnableContinuedLineCopy)
516        CharAttrTmp.Attr |= AttrLineContinued;        CharAttrTmp.Attr |= AttrLineContinued;
517  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
518    }    }
519    else if (CursorX > NumOfColumns-2)    else if (CursorX > NumOfColumns-2)
520      if (AutoWrapMode)      if (AutoWrapMode)
521      {      {
522  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
523        if (ts.EnableContinuedLineCopy)        if (ts.EnableContinuedLineCopy)
524        {        {
525        CharAttrTmp.Attr |= AttrLineContinued;        CharAttrTmp.Attr |= AttrLineContinued;
526        if (CursorX == NumOfColumns-1)        if (CursorX == NumOfColumns-1)
527          BuffPutChar(0x20, CharAttr, FALSE);          BuffPutChar(0x20, CharAttr, FALSE);
528        }        }
529  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
530        CarriageReturn(FALSE);        CarriageReturn(FALSE);
531        LineFeed(LF,FALSE);        LineFeed(LF,FALSE);
532      }      }
533      else return;      else return;
534    
535    Wrap = FALSE;    Wrap = FALSE;
536    
537    if (cv.HLogBuf!=0)    if (cv.HLogBuf!=0)
538    {    {
539      Log1Byte(HIBYTE(Kanji));      Log1Byte(HIBYTE(Kanji));
540      Log1Byte(LOBYTE(Kanji));      Log1Byte(LOBYTE(Kanji));
541    }    }
542    
543    if (Special)    if (Special)
544    {    {
545      UpdateStr();      UpdateStr();
546      Special = FALSE;      Special = FALSE;
547    }    }
548    
549  #ifndef NO_COPYLINE_FIX  #ifndef NO_COPYLINE_FIX
550    BuffPutKanji(Kanji, CharAttrTmp, InsertMode);    BuffPutKanji(Kanji, CharAttrTmp, InsertMode);
551  #else  #else
552    BuffPutKanji(Kanji, CharAttr, InsertMode);    BuffPutKanji(Kanji, CharAttr, InsertMode);
553  #endif /* NO_COPYLINE_FIX */  #endif /* NO_COPYLINE_FIX */
554    
555    if (CursorX < NumOfColumns-2)    if (CursorX < NumOfColumns-2)
556    {    {
557      MoveRight();      MoveRight();
558      MoveRight();      MoveRight();
559    }    }
560    else {    else {
561      UpdateStr();      UpdateStr();
562      Wrap = AutoWrapMode;      Wrap = AutoWrapMode;
563    }    }
564  }  }
565    
566  void PutDebugChar(BYTE b)  void PutDebugChar(BYTE b)
567  {  {
568    InsertMode = FALSE;    InsertMode = FALSE;
569    AutoWrapMode = TRUE;    AutoWrapMode = TRUE;
570    
571    if ((b & 0x80) == 0x80)    if ((b & 0x80) == 0x80)
572    {    {
573      UpdateStr();      UpdateStr();
574      CharAttr.Attr = AttrReverse;      CharAttr.Attr = AttrReverse;
575      b = b & 0x7f;      b = b & 0x7f;
576    }    }
577    
578    if (b<=US)    if (b<=US)
579    {    {
580      PutChar('^');      PutChar('^');
581      PutChar((char)(b+0x40));      PutChar((char)(b+0x40));
582    }    }
583    else if (b==DEL)    else if (b==DEL)
584    {    {
585      PutChar('<');      PutChar('<');
586      PutChar('D');      PutChar('D');
587      PutChar('E');      PutChar('E');
588      PutChar('L');      PutChar('L');
589      PutChar('>');      PutChar('>');
590    }    }
591    else    else
592      PutChar(b);      PutChar(b);
593    
594    if (CharAttr.Attr != AttrDefault)    if (CharAttr.Attr != AttrDefault)
595    {    {
596      UpdateStr();      UpdateStr();
597      CharAttr.Attr = AttrDefault;      CharAttr.Attr = AttrDefault;
598    }    }
599  }  }
600    
601  void PrnParseControl(BYTE b) // printer mode  void PrnParseControl(BYTE b) // printer mode
602  {  {
603    switch (b) {    switch (b) {
604      case NUL: return;      case NUL: return;
605      case SO:      case SO:
606        if (! DirectPrn)        if (! DirectPrn)
607        {        {
608          if ((ts.Language==IdJapanese) &&          if ((ts.Language==IdJapanese) &&
609              (ts.KanjiCode==IdJIS) &&              (ts.KanjiCode==IdJIS) &&
610              (ts.JIS7Katakana==1) &&              (ts.JIS7Katakana==1) &&
611              ((ts.TermFlag & TF_FIXEDJIS)!=0))              ((ts.TermFlag & TF_FIXEDJIS)!=0))
612            Gn[1] = IdKatakana;            Gn[1] = IdKatakana;
613          Glr[0] = 1; /* LS1 */          Glr[0] = 1; /* LS1 */
614          return;          return;
615        }        }
616        break;        break;
617      case SI:      case SI:
618        if (! DirectPrn)        if (! DirectPrn)
619        {        {
620          Glr[0] = 0; /* LS0 */          Glr[0] = 0; /* LS0 */
621          return;          return;
622        }        }
623        break;        break;
624      case DC1:      case DC1:
625      case DC3: return;      case DC3: return;
626      case ESC:      case ESC:
627        ICount = 0;        ICount = 0;
628        JustAfterESC = TRUE;        JustAfterESC = TRUE;
629        ParseMode = ModeESC;        ParseMode = ModeESC;
630        WriteToPrnFile(0,TRUE); // flush prn buff        WriteToPrnFile(0,TRUE); // flush prn buff
631        return;        return;
632      case CSI:      case CSI:
633        if ((ts.TerminalID<IdVT220J) ||        if ((ts.TerminalID<IdVT220J) ||
634            ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))            ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))
635        {        {
636          PutChar(b); /* Disp C1 char in VT100 mode */          PutChar(b); /* Disp C1 char in VT100 mode */
637          return;          return;
638        }        }
639        ICount = 0;        ICount = 0;
640        FirstPrm = TRUE;        FirstPrm = TRUE;
641        NParam = 1;        NParam = 1;
642        Param[1] = -1;        Param[1] = -1;
643        Prv = 0;        Prv = 0;
644        ParseMode = ModeCSI;        ParseMode = ModeCSI;
645        WriteToPrnFile(0,TRUE); // flush prn buff        WriteToPrnFile(0,TRUE); // flush prn buff
646        WriteToPrnFile(b,FALSE);        WriteToPrnFile(b,FALSE);
647        return;        return;
648    }    }
649    /* send the uninterpreted character to printer */    /* send the uninterpreted character to printer */
650    WriteToPrnFile(b,TRUE);    WriteToPrnFile(b,TRUE);
651  }  }
652    
653  void ParseControl(BYTE b)  void ParseControl(BYTE b)
654  {  {
655    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
656      PrnParseControl(b);      PrnParseControl(b);
657      return;      return;
658    }    }
659    
660    if (b>=0x80) /* C1 char */    if (b>=0x80) /* C1 char */
661    {    {
662      /* English mode */      /* English mode */
663      if (ts.Language==IdEnglish)      if (ts.Language==IdEnglish)
664      {      {
665        if ((ts.TerminalID<IdVT220J) ||        if ((ts.TerminalID<IdVT220J) ||
666            ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))            ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))
667        {        {
668          PutChar(b); /* Disp C1 char in VT100 mode */          PutChar(b); /* Disp C1 char in VT100 mode */
669          return;          return;
670        }        }
671      }      }
672      else { /* Japanese mode */      else { /* Japanese mode */
673        if ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0)        if ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0)
674          return; /* ignore C1 char */          return; /* ignore C1 char */
675        /* C1 chars are interpreted as C0 chars in VT100 mode */        /* C1 chars are interpreted as C0 chars in VT100 mode */
676        if (ts.TerminalID<IdVT220J)        if (ts.TerminalID<IdVT220J)
677          b = b & 0x7F;          b = b & 0x7F;
678      }      }
679    }    }
680    switch (b) {    switch (b) {
681      /* C0 group */      /* C0 group */
682      case ENQ:      case ENQ:
683        CommBinaryOut(&cv,&(ts.Answerback[0]),ts.AnswerbackLen);        CommBinaryOut(&cv,&(ts.Answerback[0]),ts.AnswerbackLen);
684        break;        break;
685      case BEL:      case BEL:
686        switch (ts.Beep) {        switch (ts.Beep) {
687        case IdBeepOff:        case IdBeepOff:
688          /* nothing to do */          /* nothing to do */
689          break;          break;
690        case IdBeepOn:        case IdBeepOn:
691          MessageBeep(0);          MessageBeep(0);
692          break;          break;
693        case IdBeepVisual:        case IdBeepVisual:
694          VisualBell();          VisualBell();
695          break;          break;
696        }        }
697        break;        break;
698      case BS: BackSpace(); break;      case BS: BackSpace(); break;
699      case HT: Tab(); break;      case HT: Tab(); break;
700    
701      case LF:      case LF:
702                  // 受信時の改行コードが LF の場合は、サーバから LF のみが送られてくると仮定し、                  // 受信時の改行コードが LF の場合は、サーバから LF のみが送られてくると仮定し、
703                  // CR+LFとして扱うようにする。                  // CR+LFとして扱うようにする。
704                  // cf. http://www.neocom.ca/forum/viewtopic.php?t=216                  // cf. http://www.neocom.ca/forum/viewtopic.php?t=216
705                  // (2007.1.21 yutaka)                  // (2007.1.21 yutaka)
706                  if (ts.CRReceive == IdLF) {                  if (ts.CRReceive == IdLF) {
707                          CarriageReturn(TRUE);                          CarriageReturn(TRUE);
708                          LineFeed(b, TRUE);                          LineFeed(b, TRUE);
709                          break;                          break;
710                  }                  }
711    
712      case VT: LineFeed(b,TRUE); break;      case VT: LineFeed(b,TRUE); break;
713    
714      case FF:      case FF:
715        if ((ts.AutoWinSwitch>0) && JustAfterESC)        if ((ts.AutoWinSwitch>0) && JustAfterESC)
716        {        {
717          CommInsert1Byte(&cv,b);          CommInsert1Byte(&cv,b);
718          CommInsert1Byte(&cv,ESC);          CommInsert1Byte(&cv,ESC);
719          ChangeEmu = IdTEK;  /* Enter TEK Mode */          ChangeEmu = IdTEK;  /* Enter TEK Mode */
720        }        }
721        else        else
722          LineFeed(b,TRUE);          LineFeed(b,TRUE);
723        break;        break;
724      case CR:      case CR:
725        CarriageReturn(TRUE);        CarriageReturn(TRUE);
726        if (ts.CRReceive==IdCRLF)        if (ts.CRReceive==IdCRLF)
727          CommInsert1Byte(&cv,LF);          CommInsert1Byte(&cv,LF);
728        break;        break;
729      case SO:      case SO:
730        if ((ts.Language==IdJapanese) &&        if ((ts.Language==IdJapanese) &&
731            (ts.KanjiCode==IdJIS) &&            (ts.KanjiCode==IdJIS) &&
732            (ts.JIS7Katakana==1) &&            (ts.JIS7Katakana==1) &&
733            ((ts.TermFlag & TF_FIXEDJIS)!=0))            ((ts.TermFlag & TF_FIXEDJIS)!=0))
734          Gn[1] = IdKatakana;          Gn[1] = IdKatakana;
735    
736        Glr[0] = 1; /* LS1 */        Glr[0] = 1; /* LS1 */
737        break;        break;
738      case SI: Glr[0] = 0; break; /* LS0 */      case SI: Glr[0] = 0; break; /* LS0 */
739      case DLE:      case DLE:
740        if ((ts.FTFlag & FT_BPAUTO)!=0)        if ((ts.FTFlag & FT_BPAUTO)!=0)
741          ParseMode = ModeDLE; /* Auto B-Plus activation */          ParseMode = ModeDLE; /* Auto B-Plus activation */
742        break;        break;
743      case CAN:      case CAN:
744        if ((ts.FTFlag & FT_ZAUTO)!=0)        if ((ts.FTFlag & FT_ZAUTO)!=0)
745          ParseMode = ModeCAN; /* Auto ZMODEM activation */          ParseMode = ModeCAN; /* Auto ZMODEM activation */
746  //      else if (ts.AutoWinSwitch>0)  //      else if (ts.AutoWinSwitch>0)
747  //              ChangeEmu = IdTEK;  /* Enter TEK Mode */  //              ChangeEmu = IdTEK;  /* Enter TEK Mode */
748        else        else
749          ParseMode = ModeFirst;          ParseMode = ModeFirst;
750        break;        break;
751      case SUB: ParseMode = ModeFirst; break;      case SUB: ParseMode = ModeFirst; break;
752      case ESC:      case ESC:
753        ICount = 0;        ICount = 0;
754        JustAfterESC = TRUE;        JustAfterESC = TRUE;
755        ParseMode = ModeESC;        ParseMode = ModeESC;
756        break;        break;
757      case FS:      case FS:
758      case GS:      case GS:
759      case RS:      case RS:
760      case US:      case US:
761        if (ts.AutoWinSwitch>0)        if (ts.AutoWinSwitch>0)
762        {        {
763          CommInsert1Byte(&cv,b);          CommInsert1Byte(&cv,b);
764          ChangeEmu = IdTEK;  /* Enter TEK Mode */          ChangeEmu = IdTEK;  /* Enter TEK Mode */
765        }        }
766        break;        break;
767    
768      /* C1 char */      /* C1 char */
769      case IND: LineFeed(0,TRUE); break;      case IND: LineFeed(0,TRUE); break;
770      case NEL:      case NEL:
771        LineFeed(0,TRUE);        LineFeed(0,TRUE);
772        CarriageReturn(TRUE);        CarriageReturn(TRUE);
773        break;        break;
774      case HTS: SetTabStop(); break;      case HTS: SetTabStop(); break;
775      case RI: CursorUpWithScroll(); break;      case RI: CursorUpWithScroll(); break;
776      case SS2:      case SS2:
777        GLtmp = 2;        GLtmp = 2;
778        SSflag = TRUE;        SSflag = TRUE;
779        break;        break;
780      case SS3:      case SS3:
781        GLtmp = 3;        GLtmp = 3;
782        SSflag = TRUE;        SSflag = TRUE;
783        break;        break;
784      case DCS:      case DCS:
785        SavedMode = ParseMode;        SavedMode = ParseMode;
786        ESCFlag = FALSE;        ESCFlag = FALSE;
787        NParam = 1;        NParam = 1;
788        Param[1] = -1;        Param[1] = -1;
789        ParseMode = ModeDCS;        ParseMode = ModeDCS;
790        break;        break;
791      case SOS:      case SOS:
792        SavedMode = ParseMode;        SavedMode = ParseMode;
793        ESCFlag = FALSE;        ESCFlag = FALSE;
794        ParseMode = ModeSOS;        ParseMode = ModeSOS;
795        break;        break;
796      case CSI:      case CSI:
797        ICount = 0;        ICount = 0;
798        FirstPrm = TRUE;        FirstPrm = TRUE;
799        NParam = 1;        NParam = 1;
800        Param[1] = -1;        Param[1] = -1;
801        Prv = 0;        Prv = 0;
802        ParseMode = ModeCSI;        ParseMode = ModeCSI;
803        break;        break;
804      case OSC:      case OSC:
805        Param[1] = 0;        Param[1] = 0;
806        ParseMode = ModeXS;        ParseMode = ModeXS;
807        break;        break;
808      case PM:      case PM:
809      case APC:      case APC:
810        SavedMode = ParseMode;        SavedMode = ParseMode;
811        ESCFlag = FALSE;        ESCFlag = FALSE;
812        ParseMode = ModeSOS;        ParseMode = ModeSOS;
813        break;        break;
814    }    }
815  }  }
816    
817  void SaveCursor()  void SaveCursor()
818  {  {
819    int i;    int i;
820    PStatusBuff Buff;    PStatusBuff Buff;
821    
822    if ((StatusLine>0) &&    if ((StatusLine>0) &&
823        (CursorY==NumOfLines-1))        (CursorY==NumOfLines-1))
824      Buff = &SBuff2; // for status line      Buff = &SBuff2; // for status line
825    else    else
826      Buff = &SBuff1; // for main screen      Buff = &SBuff1; // for main screen
827    
828    Buff->CursorX = CursorX;    Buff->CursorX = CursorX;
829    Buff->CursorY = CursorY;    Buff->CursorY = CursorY;
830    Buff->Attr = CharAttr;    Buff->Attr = CharAttr;
831    Buff->Glr[0] = Glr[0];    Buff->Glr[0] = Glr[0];
832    Buff->Glr[1] = Glr[1];    Buff->Glr[1] = Glr[1];
833    for (i=0 ; i<=3; i++)    for (i=0 ; i<=3; i++)
834      Buff->Gn[i] = Gn[i];      Buff->Gn[i] = Gn[i];
835    Buff->AutoWrapMode = AutoWrapMode;    Buff->AutoWrapMode = AutoWrapMode;
836    Buff->RelativeOrgMode = RelativeOrgMode;    Buff->RelativeOrgMode = RelativeOrgMode;
837  }  }
838    
839  void  RestoreCursor()  void  RestoreCursor()
840  {  {
841    int i;    int i;
842    PStatusBuff Buff;    PStatusBuff Buff;
843    UpdateStr();    UpdateStr();
844    
845    if ((StatusLine>0) &&    if ((StatusLine>0) &&
846        (CursorY==NumOfLines-1))        (CursorY==NumOfLines-1))
847      Buff = &SBuff2; // for status line      Buff = &SBuff2; // for status line
848    else    else
849      Buff = &SBuff1; // for main screen      Buff = &SBuff1; // for main screen
850    
851    if (Buff->CursorX > NumOfColumns-1)    if (Buff->CursorX > NumOfColumns-1)
852      Buff->CursorX = NumOfColumns-1;      Buff->CursorX = NumOfColumns-1;
853    if (Buff->CursorY > NumOfLines-1-StatusLine)    if (Buff->CursorY > NumOfLines-1-StatusLine)
854      Buff->CursorY = NumOfLines-1-StatusLine;      Buff->CursorY = NumOfLines-1-StatusLine;
855    MoveCursor(Buff->CursorX,Buff->CursorY);    MoveCursor(Buff->CursorX,Buff->CursorY);
856    CharAttr = Buff->Attr;    CharAttr = Buff->Attr;
857    Glr[0] = Buff->Glr[0];    Glr[0] = Buff->Glr[0];
858    Glr[1] = Buff->Glr[1];    Glr[1] = Buff->Glr[1];
859    for (i=0 ; i<=3; i++)    for (i=0 ; i<=3; i++)
860      Gn[i] = Buff->Gn[i];      Gn[i] = Buff->Gn[i];
861    AutoWrapMode = Buff->AutoWrapMode;    AutoWrapMode = Buff->AutoWrapMode;
862    RelativeOrgMode = Buff->RelativeOrgMode;    RelativeOrgMode = Buff->RelativeOrgMode;
863  }  }
864    
865  void AnswerTerminalType()  void AnswerTerminalType()
866  {  {
867    char Tmp[31];    char Tmp[31];
868    
869    if (ts.TerminalID<IdVT320)    if (ts.TerminalID<IdVT320)
870      strncpy_s(Tmp, sizeof(Tmp),"\033[?", _TRUNCATE);      strncpy_s(Tmp, sizeof(Tmp),"\033[?", _TRUNCATE);
871    else    else
872      strncpy_s(Tmp, sizeof(Tmp),"\233?", _TRUNCATE);      strncpy_s(Tmp, sizeof(Tmp),"\233?", _TRUNCATE);
873    
874    switch (ts.TerminalID) {    switch (ts.TerminalID) {
875      case IdVT100:      case IdVT100:
876        strncat_s(Tmp,sizeof(Tmp),"1;2",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"1;2",_TRUNCATE);
877        break;        break;
878      case IdVT100J:      case IdVT100J:
879        strncat_s(Tmp,sizeof(Tmp),"5;2",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"5;2",_TRUNCATE);
880        break;        break;
881      case IdVT101:      case IdVT101:
882        strncat_s(Tmp,sizeof(Tmp),"1;0",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"1;0",_TRUNCATE);
883        break;        break;
884      case IdVT102:      case IdVT102:
885        strncat_s(Tmp,sizeof(Tmp),"6",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"6",_TRUNCATE);
886        break;        break;
887      case IdVT102J:      case IdVT102J:
888        strncat_s(Tmp,sizeof(Tmp),"15",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"15",_TRUNCATE);
889        break;        break;
890      case IdVT220J:      case IdVT220J:
891        strncat_s(Tmp,sizeof(Tmp),"62;1;2;5;6;7;8",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"62;1;2;5;6;7;8",_TRUNCATE);
892        break;        break;
893      case IdVT282:      case IdVT282:
894        strncat_s(Tmp,sizeof(Tmp),"62;1;2;4;5;6;7;8;10;11",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"62;1;2;4;5;6;7;8;10;11",_TRUNCATE);
895        break;        break;
896      case IdVT320:      case IdVT320:
897        strncat_s(Tmp,sizeof(Tmp),"63;1;2;6;7;8",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"63;1;2;6;7;8",_TRUNCATE);
898        break;        break;
899      case IdVT382:      case IdVT382:
900        strncat_s(Tmp,sizeof(Tmp),"63;1;2;4;5;6;7;8;10;15",_TRUNCATE);        strncat_s(Tmp,sizeof(Tmp),"63;1;2;4;5;6;7;8;10;15",_TRUNCATE);
901        break;        break;
902    }    }
903    strncat_s(Tmp,sizeof(Tmp),"c",_TRUNCATE);    strncat_s(Tmp,sizeof(Tmp),"c",_TRUNCATE);
904    
905    CommBinaryOut(&cv,Tmp,strlen(Tmp)); /* Report terminal ID */    CommBinaryOut(&cv,Tmp,strlen(Tmp)); /* Report terminal ID */
906  }  }
907    
908  void ESCSpace(BYTE b)  void ESCSpace(BYTE b)
909  {  {
910    switch (b) {    switch (b) {
911      case 'F': Send8BitMode = FALSE; break;      // S7C1T      case 'F': Send8BitMode = FALSE; break;      // S7C1T
912      case 'G': Send8BitMode = TRUE; break;       // S8C1T      case 'G': Send8BitMode = TRUE; break;       // S8C1T
913    }    }
914  }  }
915    
916  void ESCSharp(BYTE b)  void ESCSharp(BYTE b)
917  {  {
918    switch (b) {    switch (b) {
919      case '8':  /* Fill screen with "E" */      case '8':  /* Fill screen with "E" */
920        BuffUpdateScroll();        BuffUpdateScroll();
921        BuffFillWithE();        BuffFillWithE();
922        MoveCursor(0,0);        MoveCursor(0,0);
923        ParseMode = ModeFirst;        ParseMode = ModeFirst;
924        break;        break;
925    }    }
926  }  }
927    
928  /* select double byte code set */  /* select double byte code set */
929  void ESCDBCSSelect(BYTE b)  void ESCDBCSSelect(BYTE b)
930  {  {
931    int Dist;    int Dist;
932    
933    if (ts.Language!=IdJapanese) return;    if (ts.Language!=IdJapanese) return;
934    
935    switch (ICount) {    switch (ICount) {
936      case 1:      case 1:
937        if ((b=='@') || (b=='B'))        if ((b=='@') || (b=='B'))
938        {        {
939          Gn[0] = IdKanji; /* Kanji -> G0 */          Gn[0] = IdKanji; /* Kanji -> G0 */
940          if ((ts.TermFlag & TF_AUTOINVOKE)!=0)          if ((ts.TermFlag & TF_AUTOINVOKE)!=0)
941            Glr[0] = 0; /* G0->GL */            Glr[0] = 0; /* G0->GL */
942        }        }
943        break;        break;
944      case 2:      case 2:
945        /* Second intermediate char must be        /* Second intermediate char must be
946           '(' or ')' or '*' or '+'. */           '(' or ')' or '*' or '+'. */
947        Dist = (IntChar[2]-'(') & 3; /* G0 - G3 */        Dist = (IntChar[2]-'(') & 3; /* G0 - G3 */
948        if ((b=='1') || (b=='3') ||        if ((b=='1') || (b=='3') ||
949            (b=='@') || (b=='B'))            (b=='@') || (b=='B'))
950        {        {
951          Gn[Dist] = IdKanji; /* Kanji -> G0-3 */          Gn[Dist] = IdKanji; /* Kanji -> G0-3 */
952          if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&          if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
953              (Dist==0))              (Dist==0))
954            Glr[0] = 0; /* G0->GL */            Glr[0] = 0; /* G0->GL */
955        }        }
956        break;        break;
957    }    }
958  }  }
959    
960  void ESCSelectCode(BYTE b)  void ESCSelectCode(BYTE b)
961  {  {
962    switch (b) {    switch (b) {
963      case '0':      case '0':
964        if (ts.AutoWinSwitch>0)        if (ts.AutoWinSwitch>0)
965          ChangeEmu = IdTEK; /* enter TEK mode */          ChangeEmu = IdTEK; /* enter TEK mode */
966        break;        break;
967    }    }
968  }  }
969    
970    /* select single byte code set */    /* select single byte code set */
971  void ESCSBCSSelect(BYTE b)  void ESCSBCSSelect(BYTE b)
972  {  {
973    int Dist;    int Dist;
974    
975    /* Intermediate char must be    /* Intermediate char must be
976       '(' or ')' or '*' or '+'.  */       '(' or ')' or '*' or '+'.  */
977    Dist = (IntChar[1]-'(') & 3; /* G0 - G3 */    Dist = (IntChar[1]-'(') & 3; /* G0 - G3 */
978    
979    switch (b) {    switch (b) {
980      case '0': Gn[Dist] = IdSpecial; break;      case '0': Gn[Dist] = IdSpecial; break;
981      case '<': Gn[Dist] = IdASCII; break;      case '<': Gn[Dist] = IdASCII; break;
982      case '>': Gn[Dist] = IdASCII; break;      case '>': Gn[Dist] = IdASCII; break;
983      case 'A': Gn[Dist] = IdASCII; break;      case 'A': Gn[Dist] = IdASCII; break;
984      case 'B': Gn[Dist] = IdASCII; break;      case 'B': Gn[Dist] = IdASCII; break;
985      case 'H': Gn[Dist] = IdASCII; break;      case 'H': Gn[Dist] = IdASCII; break;
986      case 'I':      case 'I':
987        if (ts.Language==IdJapanese)        if (ts.Language==IdJapanese)
988          Gn[Dist] = IdKatakana;          Gn[Dist] = IdKatakana;
989        break;        break;
990      case 'J': Gn[Dist] = IdASCII; break;      case 'J': Gn[Dist] = IdASCII; break;
991    }    }
992    
993    if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&    if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
994        (Dist==0))        (Dist==0))
995      Glr[0] = 0;  /* G0->GL */      Glr[0] = 0;  /* G0->GL */
996  }  }
997    
998  void PrnParseEscape(BYTE b) // printer mode  void PrnParseEscape(BYTE b) // printer mode
999  {  {
1000    int i;    int i;
1001    
1002    ParseMode = ModeFirst;    ParseMode = ModeFirst;
1003    switch (ICount) {    switch (ICount) {
1004      /* no intermediate char */      /* no intermediate char */
1005      case 0:      case 0:
1006        switch (b) {        switch (b) {
1007          case '[': /* CSI */          case '[': /* CSI */
1008            ICount = 0;            ICount = 0;
1009            FirstPrm = TRUE;            FirstPrm = TRUE;
1010            NParam = 1;            NParam = 1;
1011            Param[1] = -1;            Param[1] = -1;
1012            Prv = 0;            Prv = 0;
1013            WriteToPrnFile(ESC,FALSE);            WriteToPrnFile(ESC,FALSE);
1014            WriteToPrnFile('[',FALSE);            WriteToPrnFile('[',FALSE);
1015            ParseMode = ModeCSI;            ParseMode = ModeCSI;
1016            return;            return;
1017        } /* end of case Icount=0 */        } /* end of case Icount=0 */
1018        break;        break;
1019      /* one intermediate char */      /* one intermediate char */
1020      case 1:      case 1:
1021        switch (IntChar[1]) {        switch (IntChar[1]) {
1022          case '$':          case '$':
1023            if (! DirectPrn)            if (! DirectPrn)
1024            {            {
1025              ESCDBCSSelect(b);              ESCDBCSSelect(b);
1026              return;              return;
1027            }            }
1028            break;            break;
1029          case '(':          case '(':
1030          case ')':          case ')':
1031          case '*':          case '*':
1032          case '+':          case '+':
1033            if (! DirectPrn)            if (! DirectPrn)
1034            {            {
1035              ESCSBCSSelect(b);              ESCSBCSSelect(b);
1036              return;              return;
1037            }            }
1038            break;            break;
1039        }        }
1040        break;        break;
1041      /* two intermediate char */      /* two intermediate char */
1042      case 2:      case 2:
1043        if ((! DirectPrn) &&        if ((! DirectPrn) &&
1044            (IntChar[1]=='$') &&            (IntChar[1]=='$') &&
1045            ('('<=IntChar[2]) &&            ('('<=IntChar[2]) &&
1046            (IntChar[2]<='+'))            (IntChar[2]<='+'))
1047        {        {
1048          ESCDBCSSelect(b);          ESCDBCSSelect(b);
1049          return;          return;
1050        }        }
1051        break;        break;
1052    }    }
1053    // send the uninterpreted sequence to printer    // send the uninterpreted sequence to printer
1054    WriteToPrnFile(ESC,FALSE);    WriteToPrnFile(ESC,FALSE);
1055    for (i=1; i<=ICount; i++)    for (i=1; i<=ICount; i++)
1056      WriteToPrnFile(IntChar[i],FALSE);      WriteToPrnFile(IntChar[i],FALSE);
1057    WriteToPrnFile(b,TRUE);    WriteToPrnFile(b,TRUE);
1058  }  }
1059    
1060  void ParseEscape(BYTE b) /* b is the final char */  void ParseEscape(BYTE b) /* b is the final char */
1061  {  {
1062    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
1063      PrnParseEscape(b);      PrnParseEscape(b);
1064      return;      return;
1065    }    }
1066    
1067    switch (ICount) {    switch (ICount) {
1068      /* no intermediate char */      /* no intermediate char */
1069      case 0:      case 0:
1070        switch (b) {        switch (b) {
1071          case '7': SaveCursor(); break;          case '7': SaveCursor(); break;
1072          case '8': RestoreCursor(); break;          case '8': RestoreCursor(); break;
1073          case '=': AppliKeyMode = TRUE; break;          case '=': AppliKeyMode = TRUE; break;
1074          case '>': AppliKeyMode = FALSE; break;          case '>': AppliKeyMode = FALSE; break;
1075          case 'D': /* IND */          case 'D': /* IND */
1076            LineFeed(0,TRUE);            LineFeed(0,TRUE);
1077            break;            break;
1078          case 'E': /* NEL */          case 'E': /* NEL */
1079            MoveCursor(0,CursorY);            MoveCursor(0,CursorY);
1080            LineFeed(0,TRUE);            LineFeed(0,TRUE);
1081            break;            break;
1082          case 'H': /* HTS */          case 'H': /* HTS */
1083            SetTabStop();            SetTabStop();
1084            break;            break;
1085          case 'M': /* RI */          case 'M': /* RI */
1086            CursorUpWithScroll();            CursorUpWithScroll();
1087            break;            break;
1088          case 'N': /* SS2 */          case 'N': /* SS2 */
1089            GLtmp = 2;            GLtmp = 2;
1090            SSflag = TRUE;            SSflag = TRUE;
1091            break;            break;
1092          case 'O': /* SS3 */          case 'O': /* SS3 */
1093            GLtmp = 3;            GLtmp = 3;
1094            SSflag = TRUE;            SSflag = TRUE;
1095            break;            break;
1096          case 'P': /* DCS */          case 'P': /* DCS */
1097            SavedMode = ParseMode;            SavedMode = ParseMode;
1098            ESCFlag = FALSE;            ESCFlag = FALSE;
1099            NParam = 1;            NParam = 1;
1100            Param[1] = -1;            Param[1] = -1;
1101            ParseMode = ModeDCS;            ParseMode = ModeDCS;
1102            return;            return;
1103          case 'X': /* SOS */          case 'X': /* SOS */
1104            SavedMode = ParseMode;            SavedMode = ParseMode;
1105            ESCFlag = FALSE;            ESCFlag = FALSE;
1106            ParseMode = ModeSOS;            ParseMode = ModeSOS;
1107            return;            return;
1108          case 'Z': AnswerTerminalType(); break;          case 'Z': AnswerTerminalType(); break;
1109          case '[': /* CSI */          case '[': /* CSI */
1110            ICount = 0;            ICount = 0;
1111            FirstPrm = TRUE;            FirstPrm = TRUE;
1112            NParam = 1;            NParam = 1;
1113            Param[1] = -1;            Param[1] = -1;
1114            Prv = 0;            Prv = 0;
1115            ParseMode = ModeCSI;            ParseMode = ModeCSI;
1116            return;            return;
1117          case '\\': break; /* ST */          case '\\': break; /* ST */
1118          case ']': /* XTERM sequence (OSC) */          case ']': /* XTERM sequence (OSC) */
1119            NParam = 1;            NParam = 1;
1120            Param[1] = 0;            Param[1] = 0;
1121            ParseMode = ModeXS;            ParseMode = ModeXS;
1122            return;            return;
1123          case '^':          case '^':
1124          case '_': /* PM, APC */          case '_': /* PM, APC */
1125            SavedMode = ParseMode;            SavedMode = ParseMode;
1126            ESCFlag = FALSE;            ESCFlag = FALSE;
1127            ParseMode = ModeSOS;            ParseMode = ModeSOS;
1128            return;            return;
1129          case 'c': /* Hardware reset */          case 'c': /* Hardware reset */
1130            HideStatusLine();            HideStatusLine();
1131            ResetTerminal();            ResetTerminal();
1132            ClearUserKey();            ClearUserKey();
1133            ClearBuffer();            ClearBuffer();
1134            if (ts.PortType==IdSerial) // reset serial port            if (ts.PortType==IdSerial) // reset serial port
1135              CommResetSerial(&ts, &cv, TRUE);              CommResetSerial(&ts, &cv, TRUE);
1136            break;            break;
1137          case 'g': /* Visual Bell (screen original?) */          case 'g': /* Visual Bell (screen original?) */
1138            VisualBell();            VisualBell();
1139            break;            break;
1140          case 'n': Glr[0] = 2; break; /* LS2 */          case 'n': Glr[0] = 2; break; /* LS2 */
1141          case 'o': Glr[0] = 3; break; /* LS3 */          case 'o': Glr[0] = 3; break; /* LS3 */
1142          case '|': Glr[1] = 3; break; /* LS3R */          case '|': Glr[1] = 3; break; /* LS3R */
1143          case '}': Glr[1] = 2; break; /* LS2R */          case '}': Glr[1] = 2; break; /* LS2R */
1144          case '~': Glr[1] = 1; break; /* LS1R */          case '~': Glr[1] = 1; break; /* LS1R */
1145        } /* end of case Icount=0 */        } /* end of case Icount=0 */
1146        break;        break;
1147      /* one intermediate char */      /* one intermediate char */
1148      case 1:      case 1:
1149        switch (IntChar[1]) {        switch (IntChar[1]) {
1150          case ' ': ESCSpace(b); break;          case ' ': ESCSpace(b); break;
1151          case '#': ESCSharp(b); break;          case '#': ESCSharp(b); break;
1152          case '$': ESCDBCSSelect(b); break;          case '$': ESCDBCSSelect(b); break;
1153          case '%': break;          case '%': break;
1154          case '(':          case '(':
1155          case ')':          case ')':
1156          case '*':          case '*':
1157          case '+':          case '+':
1158            ESCSBCSSelect(b);            ESCSBCSSelect(b);
1159            break;            break;
1160        }        }
1161        break;        break;
1162      /* two intermediate char */      /* two intermediate char */
1163      case 2:      case 2:
1164        if ((IntChar[1]=='$') &&        if ((IntChar[1]=='$') &&
1165            ('('<=IntChar[2]) &&            ('('<=IntChar[2]) &&
1166            (IntChar[2]<='+'))            (IntChar[2]<='+'))
1167          ESCDBCSSelect(b);          ESCDBCSSelect(b);
1168        else if ((IntChar[1]=='%') &&        else if ((IntChar[1]=='%') &&
1169                 (IntChar[2]=='!'))                 (IntChar[2]=='!'))
1170          ESCSelectCode(b);          ESCSelectCode(b);
1171        break;        break;
1172    }    }
1173    ParseMode = ModeFirst;    ParseMode = ModeFirst;
1174  }  }
1175    
1176  void EscapeSequence(BYTE b)  void EscapeSequence(BYTE b)
1177  {  {
1178    if (b<=US)    if (b<=US)
1179      ParseControl(b);      ParseControl(b);
1180    else if ((b>=0x20) && (b<=0x2F))    else if ((b>=0x20) && (b<=0x2F))
1181    {    {
1182      if (ICount<IntCharMax) ICount++;      if (ICount<IntCharMax) ICount++;
1183      IntChar[ICount] = b;      IntChar[ICount] = b;
1184    }    }
1185    else if ((b>=0x30) && (b<=0x7E))    else if ((b>=0x30) && (b<=0x7E))
1186      ParseEscape(b);      ParseEscape(b);
1187    else if ((b>=0x80) && (b<=0x9F))    else if ((b>=0x80) && (b<=0x9F))
1188      ParseControl(b);      ParseControl(b);
1189    
1190    JustAfterESC = FALSE;    JustAfterESC = FALSE;
1191  }  }
1192    
1193    void CSInsertCharacter()    void CSInsertCharacter()
1194    {    {
1195    // Insert space characters at cursor    // Insert space characters at cursor
1196      int Count;      int Count;
1197    
1198      BuffUpdateScroll();      BuffUpdateScroll();
1199      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1200      Count = Param[1];      Count = Param[1];
1201      BuffInsertSpace(Count);      BuffInsertSpace(Count);
1202    }    }
1203    
1204    void CSCursorUp()    void CSCursorUp()
1205    {    {
1206      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1207    
1208      if (CursorY >= CursorTop)      if (CursorY >= CursorTop)
1209      {      {
1210        if (CursorY-Param[1] > CursorTop)        if (CursorY-Param[1] > CursorTop)
1211          MoveCursor(CursorX,CursorY-Param[1]);          MoveCursor(CursorX,CursorY-Param[1]);
1212        else        else
1213          MoveCursor(CursorX,CursorTop);          MoveCursor(CursorX,CursorTop);
1214      }      }
1215      else {      else {
1216        if (CursorY > 0)        if (CursorY > 0)
1217          MoveCursor(CursorX,CursorY-Param[1]);          MoveCursor(CursorX,CursorY-Param[1]);
1218        else        else
1219          MoveCursor(CursorX,0);          MoveCursor(CursorX,0);
1220      }      }
1221    }    }
1222    
1223    void CSCursorUp1()    void CSCursorUp1()
1224    {    {
1225      MoveCursor(0,CursorY);      MoveCursor(0,CursorY);
1226      CSCursorUp();      CSCursorUp();
1227    }    }
1228    
1229    void CSCursorDown()    void CSCursorDown()
1230    {    {
1231      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1232    
1233      if (CursorY <= CursorBottom)      if (CursorY <= CursorBottom)
1234      {      {
1235        if (CursorY+Param[1] < CursorBottom)        if (CursorY+Param[1] < CursorBottom)
1236          MoveCursor(CursorX,CursorY+Param[1]);          MoveCursor(CursorX,CursorY+Param[1]);
1237        else        else
1238          MoveCursor(CursorX,CursorBottom);          MoveCursor(CursorX,CursorBottom);
1239      }      }
1240      else {      else {
1241        if (CursorY < NumOfLines-StatusLine-1)        if (CursorY < NumOfLines-StatusLine-1)
1242          MoveCursor(CursorX,CursorY+Param[1]);          MoveCursor(CursorX,CursorY+Param[1]);
1243        else        else
1244          MoveCursor(CursorX,NumOfLines-StatusLine);          MoveCursor(CursorX,NumOfLines-StatusLine);
1245      }      }
1246    }    }
1247    
1248    void CSCursorDown1()    void CSCursorDown1()
1249    {    {
1250      MoveCursor(0,CursorY);      MoveCursor(0,CursorY);
1251      CSCursorDown();      CSCursorDown();
1252    }    }
1253    
1254  void CSScreenErase()  void CSScreenErase()
1255  {  {
1256          if (Param[1] == -1) Param[1] = 0;          if (Param[1] == -1) Param[1] = 0;
1257          BuffUpdateScroll();          BuffUpdateScroll();
1258          switch (Param[1]) {          switch (Param[1]) {
1259          case 0:          case 0:
1260                  // <ESC>[H(Cursor in left upper corner)によりカーソルが左上隅を指している場合、                  // <ESC>[H(Cursor in left upper corner)によりカーソルが左上隅を指している場合、
1261                  // <ESC>[Jは<ESC>[2Jと同じことなので、処理を分け、現行バッファをスクロールアウト                  // <ESC>[Jは<ESC>[2Jと同じことなので、処理を分け、現行バッファをスクロールアウト
1262                  // させるようにする。(2005.5.29 yutaka)                  // させるようにする。(2005.5.29 yutaka)
1263                  // コンフィグレーションで切り替えられるようにした。(2008.5.3 yutaka)                  // コンフィグレーションで切り替えられるようにした。(2008.5.3 yutaka)
1264                  if (ts.ScrollWindowClearScreen &&                  if (ts.ScrollWindowClearScreen &&
1265                          (CursorX == 0 && CursorY == 0)) {                          (CursorX == 0 && CursorY == 0)) {
1266                          //      Erase screen (scroll out)                          //      Erase screen (scroll out)
1267                          BuffClearScreen();                          BuffClearScreen();
1268                          UpdateWindow(HVTWin);                          UpdateWindow(HVTWin);
1269    
1270                  } else {                  } else {
1271                          //      Erase characters from cursor to the end of screen                          //      Erase characters from cursor to the end of screen
1272                          BuffEraseCurToEnd();                          BuffEraseCurToEnd();
1273                  }                  }
1274                  break;                  break;
1275    
1276          case 1:          case 1:
1277                  //      Erase characters from home to cursor                  //      Erase characters from home to cursor
1278                  BuffEraseHomeToCur();                  BuffEraseHomeToCur();
1279                  break;                  break;
1280    
1281          case 2:          case 2:
1282                  //      Erase screen (scroll out)                  //      Erase screen (scroll out)
1283                  BuffClearScreen();                  BuffClearScreen();
1284                  UpdateWindow(HVTWin);                  UpdateWindow(HVTWin);
1285                  break;                  break;
1286          }          }
1287  }  }
1288    
1289    void CSInsertLine()    void CSInsertLine()
1290    {    {
1291    // Insert lines at current position    // Insert lines at current position
1292      int Count, YEnd;      int Count, YEnd;
1293    
1294      if (CursorY < CursorTop) return;      if (CursorY < CursorTop) return;
1295      if (CursorY > CursorBottom) return;      if (CursorY > CursorBottom) return;
1296      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1297      Count = Param[1];      Count = Param[1];
1298    
1299      YEnd = CursorBottom;      YEnd = CursorBottom;
1300      if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;      if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1301      if (Count > YEnd+1 - CursorY) Count = YEnd+1 - CursorY;      if (Count > YEnd+1 - CursorY) Count = YEnd+1 - CursorY;
1302    
1303      BuffInsertLines(Count,YEnd);      BuffInsertLines(Count,YEnd);
1304    }    }
1305    
1306    void CSLineErase()    void CSLineErase()
1307    {    {
1308      if (Param[1] == -1) Param[1] = 0;      if (Param[1] == -1) Param[1] = 0;
1309      BuffUpdateScroll();      BuffUpdateScroll();
1310      switch (Param[1]) {      switch (Param[1]) {
1311        /* erase char from cursor to end of line */        /* erase char from cursor to end of line */
1312        case 0:        case 0:
1313          BuffEraseCharsInLine(CursorX,NumOfColumns-CursorX);          BuffEraseCharsInLine(CursorX,NumOfColumns-CursorX);
1314          break;          break;
1315        /* erase char from start of line to cursor */        /* erase char from start of line to cursor */
1316        case 1:        case 1:
1317          BuffEraseCharsInLine(0,CursorX+1);          BuffEraseCharsInLine(0,CursorX+1);
1318          break;          break;
1319        /* erase entire line */        /* erase entire line */
1320        case 2:        case 2:
1321          BuffEraseCharsInLine(0,NumOfColumns);          BuffEraseCharsInLine(0,NumOfColumns);
1322          break;          break;
1323      }      }
1324    }    }
1325    
1326    void CSDeleteNLines()    void CSDeleteNLines()
1327    // Delete lines from current line    // Delete lines from current line
1328    {    {
1329      int Count, YEnd;      int Count, YEnd;
1330    
1331      if (CursorY < CursorTop) return;      if (CursorY < CursorTop) return;
1332      if (CursorY > CursorBottom) return;      if (CursorY > CursorBottom) return;
1333      Count = Param[1];      Count = Param[1];
1334      if (Count<1) Count = 1;      if (Count<1) Count = 1;
1335    
1336      YEnd = CursorBottom;      YEnd = CursorBottom;
1337      if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;      if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1338      if (Count > YEnd+1-CursorY) Count = YEnd+1-CursorY;      if (Count > YEnd+1-CursorY) Count = YEnd+1-CursorY;
1339      BuffDeleteLines(Count,YEnd);      BuffDeleteLines(Count,YEnd);
1340    }    }
1341    
1342    void CSDeleteCharacter()    void CSDeleteCharacter()
1343    {    {
1344    // Delete characters in current line from cursor    // Delete characters in current line from cursor
1345    
1346      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1347      BuffUpdateScroll();      BuffUpdateScroll();
1348      BuffDeleteChars(Param[1]);      BuffDeleteChars(Param[1]);
1349    }    }
1350    
1351    void CSEraseCharacter()    void CSEraseCharacter()
1352    {    {
1353      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1354      BuffUpdateScroll();      BuffUpdateScroll();
1355      BuffEraseChars(Param[1]);      BuffEraseChars(Param[1]);
1356    }    }
1357    
1358    void CSScrollUP()    void CSScrollUP()
1359    {    {
1360      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1361      BuffUpdateScroll();      BuffUpdateScroll();
1362      BuffRegionScrollUpNLines(Param[1]);      BuffRegionScrollUpNLines(Param[1]);
1363    }    }
1364    
1365    void CSScrollDown()    void CSScrollDown()
1366    {    {
1367      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1368      BuffUpdateScroll();      BuffUpdateScroll();
1369      BuffRegionScrollDownNLines(Param[1]);      BuffRegionScrollDownNLines(Param[1]);
1370    }    }
1371    
1372    void CSForwardTab()    void CSForwardTab()
1373    {    {
1374      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1375      CursorForwardTab(Param[1], AutoWrapMode);      CursorForwardTab(Param[1], AutoWrapMode);
1376    }    }
1377    
1378    void CSBackwardTab()    void CSBackwardTab()
1379    {    {
1380      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1381      CursorBackwardTab(Param[1]);      CursorBackwardTab(Param[1]);
1382    }    }
1383    
1384    void CSMoveToColumnN()    void CSMoveToColumnN()
1385    {    {
1386      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1387      Param[1]--;      Param[1]--;
1388      if (Param[1] < 0) Param[1] = 0;      if (Param[1] < 0) Param[1] = 0;
1389      if (Param[1] > NumOfColumns-1) Param[1] = NumOfColumns-1;      if (Param[1] > NumOfColumns-1) Param[1] = NumOfColumns-1;
1390      MoveCursor(Param[1],CursorY);      MoveCursor(Param[1],CursorY);
1391    }    }
1392    
1393    void CSCursorRight()    void CSCursorRight()
1394    {    {
1395      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1396      if (CursorX + Param[1] > NumOfColumns-1)      if (CursorX + Param[1] > NumOfColumns-1)
1397        MoveCursor(NumOfColumns-1,CursorY);        MoveCursor(NumOfColumns-1,CursorY);
1398      else      else
1399        MoveCursor(CursorX+Param[1],CursorY);        MoveCursor(CursorX+Param[1],CursorY);
1400    }    }
1401    
1402    void CSCursorLeft()    void CSCursorLeft()
1403    {    {
1404      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1405      if (CursorX-Param[1] < 0)      if (CursorX-Param[1] < 0)
1406        MoveCursor(0,CursorY);        MoveCursor(0,CursorY);
1407      else      else
1408        MoveCursor(CursorX-Param[1],CursorY);        MoveCursor(CursorX-Param[1],CursorY);
1409    }    }
1410    
1411    void CSMoveToLineN()    void CSMoveToLineN()
1412    {    {
1413      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1414      if (RelativeOrgMode)      if (RelativeOrgMode)
1415      {      {
1416        if (CursorTop+Param[1]-1 > CursorBottom)        if (CursorTop+Param[1]-1 > CursorBottom)
1417          MoveCursor(CursorX,CursorBottom);          MoveCursor(CursorX,CursorBottom);
1418        else        else
1419          MoveCursor(CursorX,CursorTop+Param[1]-1);          MoveCursor(CursorX,CursorTop+Param[1]-1);
1420      }      }
1421      else {      else {
1422        if (Param[1] > NumOfLines-StatusLine)        if (Param[1] > NumOfLines-StatusLine)
1423          MoveCursor(CursorX,NumOfLines-1-StatusLine);          MoveCursor(CursorX,NumOfLines-1-StatusLine);
1424        else        else
1425          MoveCursor(CursorX,Param[1]-1);          MoveCursor(CursorX,Param[1]-1);
1426      }      }
1427    }    }
1428    
1429    void CSMoveToXY()    void CSMoveToXY()
1430    {    {
1431      int NewX, NewY;      int NewX, NewY;
1432    
1433      if (Param[1]<1) Param[1] = 1;      if (Param[1]<1) Param[1] = 1;
1434      if ((NParam < 2) || (Param[2]<1)) Param[2] = 1;      if ((NParam < 2) || (Param[2]<1)) Param[2] = 1;
1435      NewX = Param[2] - 1;      NewX = Param[2] - 1;
1436      if (NewX > NumOfColumns-1) NewX = NumOfColumns-1;      if (NewX > NumOfColumns-1) NewX = NumOfColumns-1;
1437    
1438      if ((StatusLine>0) && (CursorY==NumOfLines-1))      if ((StatusLine>0) && (CursorY==NumOfLines-1))
1439        NewY = CursorY;        NewY = CursorY;
1440      else if (RelativeOrgMode)      else if (RelativeOrgMode)
1441      {      {
1442        NewY = CursorTop + Param[1] - 1;        NewY = CursorTop + Param[1] - 1;
1443        if (NewY > CursorBottom) NewY = CursorBottom;        if (NewY > CursorBottom) NewY = CursorBottom;
1444      }      }
1445      else {      else {
1446        NewY = Param[1] - 1;        NewY = Param[1] - 1;
1447        if (NewY > NumOfLines-1-StatusLine)        if (NewY > NumOfLines-1-StatusLine)
1448          NewY = NumOfLines-1-StatusLine;          NewY = NumOfLines-1-StatusLine;
1449      }      }
1450      MoveCursor(NewX,NewY);      MoveCursor(NewX,NewY);
1451    }    }
1452    
1453    void CSDeleteTabStop()    void CSDeleteTabStop()
1454    {    {
1455      if (Param[1]==-1) Param[1] = 0;      if (Param[1]==-1) Param[1] = 0;
1456      ClearTabStop(Param[1]);      ClearTabStop(Param[1]);
1457    }    }
1458    
1459    void CS_h_Mode()    void CS_h_Mode()
1460    {    {
1461      switch (Param[1]) {      switch (Param[1]) {
1462        case 2: KeybEnabled = FALSE; break;        case 2: KeybEnabled = FALSE; break;
1463        case 4: InsertMode = TRUE; break;        case 4: InsertMode = TRUE; break;
1464        case 12:        case 12:
1465          ts.LocalEcho = 0;          ts.LocalEcho = 0;
1466          if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))          if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1467            TelChangeEcho();            TelChangeEcho();
1468          break;          break;
1469        case 20:        case 20:
1470          LFMode = TRUE;          LFMode = TRUE;
1471          ts.CRSend = IdCRLF;          ts.CRSend = IdCRLF;
1472          cv.CRSend = IdCRLF;          cv.CRSend = IdCRLF;
1473          break;          break;
1474      }      }
1475    }    }
1476    
1477    void CS_i_Mode()    void CS_i_Mode()
1478    {    {
1479      if (Param[1]==-1) Param[1] = 0;      if (Param[1]==-1) Param[1] = 0;
1480      switch (Param[1]) {      switch (Param[1]) {
1481        /* print screen */        /* print screen */
1482          //  PrintEX --  TRUE: print screen          //  PrintEX --  TRUE: print screen
1483          //              FALSE: scroll region          //              FALSE: scroll region
1484        case 0: BuffPrint(! PrintEX); break;        case 0: BuffPrint(! PrintEX); break;
1485        /* printer controller mode off */        /* printer controller mode off */
1486        case 4: break; /* See PrnParseCS() */        case 4: break; /* See PrnParseCS() */
1487        /* printer controller mode on */        /* printer controller mode on */
1488        case 5:        case 5:
1489          if (! AutoPrintMode)          if (! AutoPrintMode)
1490            OpenPrnFile();            OpenPrnFile();
1491          DirectPrn = (ts.PrnDev[0]!=0);          DirectPrn = (ts.PrnDev[0]!=0);
1492          PrinterMode = TRUE;          PrinterMode = TRUE;
1493          break;          break;
1494      }      }
1495    }    }
1496    
1497    void CS_l_Mode()    void CS_l_Mode()
1498    {    {
1499      switch (Param[1]) {      switch (Param[1]) {
1500        case 2: KeybEnabled = TRUE; break;        case 2: KeybEnabled = TRUE; break;
1501        case 4: InsertMode = FALSE; break;        case 4: InsertMode = FALSE; break;
1502        case 12:        case 12:
1503          ts.LocalEcho = 1;          ts.LocalEcho = 1;
1504          if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))          if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1505            TelChangeEcho();            TelChangeEcho();
1506          break;          break;
1507        case 20:        case 20:
1508          LFMode = FALSE;          LFMode = FALSE;
1509          ts.CRSend = IdCR;          ts.CRSend = IdCR;
1510          cv.CRSend = IdCR;          cv.CRSend = IdCR;
1511          break;          break;
1512      }      }
1513    }    }
1514    
1515    void CS_n_Mode()    void CS_n_Mode()
1516    {    {
1517      char Report[16];      char Report[16];
1518      int Y;      int Y;
1519    
1520      switch (Param[1]) {      switch (Param[1]) {
1521        case 5:        case 5:
1522          if (Send8BitMode)          if (Send8BitMode)
1523            CommBinaryOut(&cv,"\2330n",3); /* Device Status Report -> Ready */            CommBinaryOut(&cv,"\2330n",3); /* Device Status Report -> Ready */
1524          else          else
1525            CommBinaryOut(&cv,"\033[0n",4); /* Device Status Report -> Ready */            CommBinaryOut(&cv,"\033[0n",4); /* Device Status Report -> Ready */
1526          break;          break;
1527        case 6:        case 6:
1528          /* Cursor Position Report */          /* Cursor Position Report */
1529          Y = CursorY+1;          Y = CursorY+1;
1530          if ((StatusLine>0) &&          if ((StatusLine>0) &&
1531              (Y==NumOfLines))              (Y==NumOfLines))
1532            Y = 1;            Y = 1;
1533          if (Send8BitMode)          if (Send8BitMode)
1534            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\233%u;%uR", CLocale, Y, CursorX+1);            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\233%u;%uR", CLocale, Y, CursorX+1);
1535          else          else
1536            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[%u;%uR", CLocale, Y, CursorX+1);            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[%u;%uR", CLocale, Y, CursorX+1);
1537          CommBinaryOut(&cv,Report,strlen(Report));          CommBinaryOut(&cv,Report,strlen(Report));
1538          break;          break;
1539      }      }
1540    }    }
1541    
1542  void CSSetAttr()  void CSSetAttr()
1543  {  {
1544          int i, P;          int i, P;
1545    
1546          UpdateStr();          UpdateStr();
1547          for (i=1 ; i<=NParam ; i++)          for (i=1 ; i<=NParam ; i++)
1548          {          {
1549                  P = Param[i];                  P = Param[i];
1550                  if (P<0) P = 0;                  if (P<0) P = 0;
1551                  switch (P) {                  switch (P) {
1552                  case   0:       /* Clear all */                  case   0:       /* Clear all */
1553                          CharAttr = DefCharAttr;                          CharAttr = DefCharAttr;
1554                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1555                          break;                          break;
1556    
1557                  case   1:       /* Bold */                  case   1:       /* Bold */
1558                          CharAttr.Attr |= AttrBold;                          CharAttr.Attr |= AttrBold;
1559                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1560                          break;                          break;
1561    
1562                  case   4:       /* Under line */                  case   4:       /* Under line */
1563                          CharAttr.Attr |= AttrUnder;                          CharAttr.Attr |= AttrUnder;
1564                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1565                          break;                          break;
1566    
1567                  case   5:       /* Blink */                  case   5:       /* Blink */
1568                          CharAttr.Attr |= AttrBlink;                          CharAttr.Attr |= AttrBlink;
1569                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1570                          break;                          break;
1571    
1572                  case   7:       /* Reverse */                  case   7:       /* Reverse */
1573                          CharAttr.Attr |= AttrReverse;                          CharAttr.Attr |= AttrReverse;
1574                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1575                          break;                          break;
1576    
1577                  case  22:       /* Bold off */                  case  22:       /* Bold off */
1578                          CharAttr.Attr &= ~ AttrBold;                          CharAttr.Attr &= ~ AttrBold;
1579                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1580                          break;                          break;
1581    
1582                  case  24:       /* Under line off */                  case  24:       /* Under line off */
1583                          CharAttr.Attr &= ~ AttrUnder;                          CharAttr.Attr &= ~ AttrUnder;
1584                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1585                          break;                          break;
1586    
1587                  case  25:       /* Blink off */                  case  25:       /* Blink off */
1588                          CharAttr.Attr &= ~ AttrBlink;                          CharAttr.Attr &= ~ AttrBlink;
1589                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1590                          break;                          break;
1591    
1592                  case  27:       /* Reverse off */                  case  27:       /* Reverse off */
1593                          CharAttr.Attr &= ~ AttrReverse;                          CharAttr.Attr &= ~ AttrReverse;
1594                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1595                          break;                          break;
1596    
1597                  case  30:                  case  30:
1598                  case  31:                  case  31:
1599                  case  32:                  case  32:
1600                  case  33:                  case  33:
1601                  case  34:                  case  34:
1602                  case  35:                  case  35:
1603                  case  36:                  case  36:
1604                  case  37:       /* text color */                  case  37:       /* text color */
1605                          CharAttr.Attr2 |= Attr2Fore;                          CharAttr.Attr2 |= Attr2Fore;
1606                          CharAttr.Fore = P - 30;                          CharAttr.Fore = P - 30;
1607                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1608                          break;                          break;
1609    
1610                  case  38:       /* text color (256color mode) */                  case  38:       /* text color (256color mode) */
1611                          if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {                          if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1612                                  i++;                                  i++;
1613                                  if (i < NParam) {                                  if (i < NParam) {
1614                                          P = Param[++i];                                          P = Param[++i];
1615                                          if (P<0) {                                          if (P<0) {
1616                                                  P = 0;                                                  P = 0;
1617                                          }                                          }
1618                                          CharAttr.Attr2 |= Attr2Fore;                                          CharAttr.Attr2 |= Attr2Fore;
1619                                          CharAttr.Fore = P;                                          CharAttr.Fore = P;
1620                                          BuffSetCurCharAttr(CharAttr);                                          BuffSetCurCharAttr(CharAttr);
1621                                  }                                  }
1622                          }                          }
1623                          break;                          break;
1624    
1625                  case  39:       /* Reset text color */                  case  39:       /* Reset text color */
1626                          CharAttr.Attr2 &= ~ Attr2Fore;                          CharAttr.Attr2 &= ~ Attr2Fore;
1627                          CharAttr.Fore = AttrDefaultFG;                          CharAttr.Fore = AttrDefaultFG;
1628                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1629                          break;                          break;
1630    
1631                  case  40:                  case  40:
1632                  case  41:                  case  41:
1633                  case  42:                  case  42:
1634                  case  43:                  case  43:
1635                  case  44:                  case  44:
1636                  case  45:                  case  45:
1637                  case  46:                  case  46:
1638                  case  47:       /* Back color */                  case  47:       /* Back color */
1639                          CharAttr.Attr2 |= Attr2Back;                          CharAttr.Attr2 |= Attr2Back;
1640                          CharAttr.Back = P - 40;                          CharAttr.Back = P - 40;
1641                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1642                          break;                          break;
1643    
1644                  case  48:       /* Back color (256color mode) */                  case  48:       /* Back color (256color mode) */
1645                          if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {                          if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1646                                  i++;                                  i++;
1647                                  if (i < NParam) {                                  if (i < NParam) {
1648                                          P = Param[++i];                                          P = Param[++i];
1649                                          if (P<0) {                                          if (P<0) {
1650                                                  P = 0;                                                  P = 0;
1651                                          }                                          }
1652                                          CharAttr.Attr2 |= Attr2Back;                                          CharAttr.Attr2 |= Attr2Back;
1653                                          CharAttr.Back = P;                                          CharAttr.Back = P;
1654                                          BuffSetCurCharAttr(CharAttr);                                          BuffSetCurCharAttr(CharAttr);
1655                                  }                                  }
1656                          }                          }
1657                          break;                          break;
1658    
1659                  case  49:       /* Reset back color */                  case  49:       /* Reset back color */
1660                          CharAttr.Attr2 &= ~ Attr2Back;                          CharAttr.Attr2 &= ~ Attr2Back;
1661                          CharAttr.Back = AttrDefaultBG;                          CharAttr.Back = AttrDefaultBG;
1662                          BuffSetCurCharAttr(CharAttr);                          BuffSetCurCharAttr(CharAttr);
1663                          break;                          break;
1664    
1665                  case 90:                  case 90:
1666                  case 91:                  case 91:
1667                  case 92:                  case 92:
1668                  case 93:                  case 93:
1669                  case 94:                  case 94:
1670                  case 95:                  case 95:
1671                  case 96:                  case 96:
1672                  case 97:        /* aixterm style text color */                  case 97:        /* aixterm style text color */
1673                          if (ts.ColorFlag & CF_AIXTERM16) {                          if (ts.ColorFlag & CF_AIXTERM16) {
1674                                  CharAttr.Attr2 |= Attr2Fore;                                  CharAttr.Attr2 |= Attr2Fore;
1675                                  CharAttr.Fore = P - 90 + 8;                                  CharAttr.Fore = P - 90 + 8;
1676                                  BuffSetCurCharAttr(CharAttr);                                  BuffSetCurCharAttr(CharAttr);
1677                          }                          }
1678                          break;                          break;
1679    
1680                  case 100:                  case 100:
1681                          if (! (ts.ColorFlag & CF_AIXTERM16)) {                          if (! (ts.ColorFlag & CF_AIXTERM16)) {
1682                                  /* Reset text and back color */                                  /* Reset text and back color */
1683                                  CharAttr.Attr2 &= ~ (Attr2Fore | Attr2Back);                                  CharAttr.Attr2 &= ~ (Attr2Fore | Attr2Back);
1684                                  CharAttr.Fore = AttrDefaultFG;                                  CharAttr.Fore = AttrDefaultFG;
1685                                  CharAttr.Back = AttrDefaultBG;                                  CharAttr.Back = AttrDefaultBG;
1686                                  BuffSetCurCharAttr(CharAttr);                                  BuffSetCurCharAttr(CharAttr);
1687                                  break;                                  break;
1688                          }                          }
1689                          /* fall through to aixterm style back color */                          /* fall through to aixterm style back color */
1690    
1691                  case 101:                  case 101:
1692                  case 102:                  case 102:
1693                  case 103:                  case 103:
1694                  case 104:                  case 104:
1695                  case 105:                  case 105:
1696                  case 106:                  case 106:
1697                  case 107:       /* aixterm style back color */                  case 107:       /* aixterm style back color */
1698                          if (ts.ColorFlag & CF_AIXTERM16) {                          if (ts.ColorFlag & CF_AIXTERM16) {
1699                                  CharAttr.Attr2 |= Attr2Back;                                  CharAttr.Attr2 |= Attr2Back;
1700                                  CharAttr.Back = P - 100 + 8;                                  CharAttr.Back = P - 100 + 8;
1701                                  BuffSetCurCharAttr(CharAttr);                                  BuffSetCurCharAttr(CharAttr);
1702                          }                          }
1703                          break;                          break;
1704                  }                  }
1705          }          }
1706  }  }
1707    
1708    void CSSetScrollRegion()    void CSSetScrollRegion()
1709    {    {
1710      if ((StatusLine>0) &&      if ((StatusLine>0) &&
1711          (CursorY==NumOfLines-1))          (CursorY==NumOfLines-1))
1712      {      {
1713        MoveCursor(0,CursorY);        MoveCursor(0,CursorY);
1714        return;        return;
1715      }      }
1716      if (Param[1]<1) Param[1] =1;      if (Param[1]<1) Param[1] =1;
1717      if ((NParam < 2) | (Param[2]<1))      if ((NParam < 2) | (Param[2]<1))
1718        Param[2] = NumOfLines-StatusLine;        Param[2] = NumOfLines-StatusLine;
1719      Param[1]--;      Param[1]--;
1720      Param[2]--;      Param[2]--;
1721      if (Param[1] > NumOfLines-1-StatusLine)      if (Param[1] > NumOfLines-1-StatusLine)
1722        Param[1] = NumOfLines-1-StatusLine;        Param[1] = NumOfLines-1-StatusLine;
1723      if (Param[2] > NumOfLines-1-StatusLine)      if (Param[2] > NumOfLines-1-StatusLine)
1724        Param[2] = NumOfLines-1-StatusLine;        Param[2] = NumOfLines-1-StatusLine;
1725      if (Param[1] >= Param[2]) return;      if (Param[1] >= Param[2]) return;
1726      CursorTop = Param[1];      CursorTop = Param[1];
1727      CursorBottom = Param[2];      CursorBottom = Param[2];
1728      if (RelativeOrgMode) MoveCursor(0,CursorTop);      if (RelativeOrgMode) MoveCursor(0,CursorTop);
1729                      else MoveCursor(0,0);                      else MoveCursor(0,0);
1730    }    }
1731    
1732    void CSSunSequence() /* Sun terminal private sequences */    void CSSunSequence() /* Sun terminal private sequences */
1733    {    {
1734      char Report[16];      char Report[16];
1735    
1736      switch (Param[1]) {      switch (Param[1]) {
1737        case 8: /* set terminal size */        case 8: /* set terminal size */
1738          if ((Param[2]<=1) || (NParam<2)) Param[2] = 24;          if ((Param[2]<=1) || (NParam<2)) Param[2] = 24;
1739          if ((Param[3]<=1) || (NParam<3)) Param[3] = 80;          if ((Param[3]<=1) || (NParam<3)) Param[3] = 80;
1740          ChangeTerminalSize(Param[3],Param[2]);          ChangeTerminalSize(Param[3],Param[2]);
1741          break;          break;
1742        case 14: /* get window size??? */        case 14: /* get window size??? */
1743          /* this is not actual window size */          /* this is not actual window size */
1744          if (Send8BitMode)          if (Send8BitMode)
1745            CommBinaryOut(&cv,"\2334;640;480t",11);            CommBinaryOut(&cv,"\2334;640;480t",11);
1746          else          else
1747            CommBinaryOut(&cv,"\033[4;640;480t",12);            CommBinaryOut(&cv,"\033[4;640;480t",12);
1748          break;          break;
1749        case 18: /* get terminal size */        case 18: /* get terminal size */
1750          if (Send8BitMode)          if (Send8BitMode)
1751            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\2338;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\2338;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1752          else          else
1753            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[8;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);            _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[8;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1754          CommBinaryOut(&cv,Report,strlen(Report));          CommBinaryOut(&cv,Report,strlen(Report));
1755          break;          break;
1756      }      }
1757    }    }
1758    
1759    void CSGT(BYTE b)    void CSGT(BYTE b)
1760    {    {
1761      switch (b) {      switch (b) {
1762        case 'c': /* second terminal report */        case 'c': /* second terminal report */
1763          if (Send8BitMode)          if (Send8BitMode)
1764            CommBinaryOut(&cv,"\233>32;10;2c",11); /* VT382 */            CommBinaryOut(&cv,"\233>32;10;2c",11); /* VT382 */
1765          else          else
1766            CommBinaryOut(&cv,"\033[>32;10;2c",11); /* VT382 */            CommBinaryOut(&cv,"\033[>32;10;2c",11); /* VT382 */
1767          break;          break;
1768        case 'J':        case 'J':
1769          if (Param[1]==3) // IO-8256 terminal          if (Param[1]==3) // IO-8256 terminal
1770          {          {
1771            if (Param[2]<1) Param[2]=1;            if (Param[2]<1) Param[2]=1;
1772            if (Param[3]<1) Param[3]=1;            if (Param[3]<1) Param[3]=1;
1773            if (Param[4]<1) Param[4]=1;            if (Param[4]<1) Param[4]=1;
1774            if (Param[5]<1) Param[5]=1;            if (Param[5]<1) Param[5]=1;
1775            BuffEraseBox(Param[3]-1,Param[2]-1,            BuffEraseBox(Param[3]-1,Param[2]-1,
1776                         Param[5]-1,Param[4]-1);                         Param[5]-1,Param[4]-1);
1777          }          }
1778          break;          break;
1779        case 'K':        case 'K':
1780          if ((NParam>=2) && (Param[1]==5))          if ((NParam>=2) && (Param[1]==5))
1781          {       // IO-8256 terminal          {       // IO-8256 terminal
1782            switch (Param[2]) {            switch (Param[2]) {
1783              case 3:              case 3:
1784              case 4:              case 4:
1785              case 5:              case 5:
1786              case 6:              case 6:
1787                BuffDrawLine(CharAttr, Param[2], Param[3]);                BuffDrawLine(CharAttr, Param[2], Param[3]);
1788                break;                break;
1789              case 12:              case 12:
1790                /* Text color */                /* Text color */
1791                if ((Param[3]>=0) && (Param[3]<=7))                if ((Param[3]>=0) && (Param[3]<=7))
1792                {                {
1793                  switch (Param[3]) {                  switch (Param[3]) {
1794                    case 3: CharAttr.Fore = IdBlue; break;                    case 3: CharAttr.Fore = IdBlue; break;
1795                    case 4: CharAttr.Fore = IdCyan; break;                    case 4: CharAttr.Fore = IdCyan; break;
1796                    case 5: CharAttr.Fore = IdYellow; break;                    case 5: CharAttr.Fore = IdYellow; break;
1797                    case 6: CharAttr.Fore = IdMagenta; break;                    case 6: CharAttr.Fore = IdMagenta; break;
1798                    default: CharAttr.Fore = Param[3]; break;                    default: CharAttr.Fore = Param[3]; break;
1799                  }                  }
1800                  CharAttr.Attr2 |= Attr2Fore;                  CharAttr.Attr2 |= Attr2Fore;
1801                  BuffSetCurCharAttr(CharAttr);                  BuffSetCurCharAttr(CharAttr);
1802                }                }
1803                break;                break;
1804            }            }
1805          }          }
1806          else if (Param[1]==3)          else if (Param[1]==3)
1807          {// IO-8256 terminal          {// IO-8256 terminal
1808            if (Param[2]<1) Param[2] = 1;            if (Param[2]<1) Param[2] = 1;
1809            if (Param[3]<1) Param[2] = 1;            if (Param[3]<1) Param[2] = 1;
1810            BuffEraseCharsInLine(Param[2]-1,Param[3]-Param[2]+1);            BuffEraseCharsInLine(Param[2]-1,Param[3]-Param[2]+1);
1811          }          }
1812          break;          break;
1813      }      }
1814    }    }
1815    
1816      void CSQExchangeColor()      void CSQExchangeColor()
1817      {      {
1818        COLORREF ColorRef;        COLORREF ColorRef;
1819    
1820        BuffUpdateScroll();        BuffUpdateScroll();
1821    
1822        if (ts.ColorFlag & CF_REVERSECOLOR) {        if (ts.ColorFlag & CF_REVERSECOLOR) {
1823          ColorRef = ts.VTColor[0];          ColorRef = ts.VTColor[0];
1824          ts.VTColor[0] = ts.VTReverseColor[0];          ts.VTColor[0] = ts.VTReverseColor[0];
1825          ts.VTReverseColor[0] = ColorRef;          ts.VTReverseColor[0] = ColorRef;
1826          ColorRef = ts.VTColor[1];          ColorRef = ts.VTColor[1];
1827          ts.VTColor[1] = ts.VTReverseColor[1];          ts.VTColor[1] = ts.VTReverseColor[1];
1828          ts.VTReverseColor[1] = ColorRef;          ts.VTReverseColor[1] = ColorRef;
1829        }        }
1830        else {        else {
1831          ColorRef = ts.VTColor[0];          ColorRef = ts.VTColor[0];
1832          ts.VTColor[0] = ts.VTColor[1];          ts.VTColor[0] = ts.VTColor[1];
1833          ts.VTColor[1] = ColorRef;          ts.VTColor[1] = ColorRef;
1834        }        }
1835    
1836        ColorRef = ts.VTBoldColor[0];        ColorRef = ts.VTBoldColor[0];
1837        ts.VTBoldColor[0] = ts.VTBoldColor[1];        ts.VTBoldColor[0] = ts.VTBoldColor[1];
1838        ts.VTBoldColor[1] = ColorRef;        ts.VTBoldColor[1] = ColorRef;
1839    
1840        ColorRef = ts.VTBlinkColor[0];        ColorRef = ts.VTBlinkColor[0];
1841        ts.VTBlinkColor[0] = ts.VTBlinkColor[1];        ts.VTBlinkColor[0] = ts.VTBlinkColor[1];
1842        ts.VTBlinkColor[1] = ColorRef;        ts.VTBlinkColor[1] = ColorRef;
1843    
1844        ColorRef = ts.URLColor[0];        ColorRef = ts.URLColor[0];
1845        ts.URLColor[0] = ts.URLColor[1];        ts.URLColor[0] = ts.URLColor[1];
1846        ts.URLColor[1] = ColorRef;        ts.URLColor[1] = ColorRef;
1847    
1848        ts.ColorFlag ^= CF_REVERSEVIDEO;        ts.ColorFlag ^= CF_REVERSEVIDEO;
1849    
1850  #ifdef ALPHABLEND_TYPE2  #ifdef ALPHABLEND_TYPE2
1851        BGInitialize();        BGInitialize();
1852  #endif  #endif
1853        DispChangeBackground();        DispChangeBackground();
1854        UpdateWindow(HVTWin);        UpdateWindow(HVTWin);
1855      }      }
1856    
1857      void CSQ_h_Mode()      void CSQ_h_Mode()
1858      {      {
1859        int i;        int i;
1860    
1861        for (i = 1 ; i<=NParam ; i++)        for (i = 1 ; i<=NParam ; i++)
1862          switch (Param[i]) {          switch (Param[i]) {
1863            case 1: AppliCursorMode = TRUE; break;            case 1: AppliCursorMode = TRUE; break;
1864            case 3:            case 3:
1865              ChangeTerminalSize(132,NumOfLines-StatusLine);              ChangeTerminalSize(132,NumOfLines-StatusLine);
1866              break;              break;
1867            case 5: /* Reverse Video */            case 5: /* Reverse Video */
1868              if (!(ts.ColorFlag & CF_REVERSEVIDEO))              if (!(ts.ColorFlag & CF_REVERSEVIDEO))
1869                CSQExchangeColor(); /* Exchange text/back color */                CSQExchangeColor(); /* Exchange text/back color */
1870              break;              break;
1871            case 6:            case 6:
1872              if ((StatusLine>0) &&              if ((StatusLine>0) &&
1873                  (CursorY==NumOfLines-1))                  (CursorY==NumOfLines-1))
1874                MoveCursor(0,CursorY);                MoveCursor(0,CursorY);
1875              else {              else {
1876                RelativeOrgMode = TRUE;                RelativeOrgMode = TRUE;
1877                MoveCursor(0,CursorTop);                MoveCursor(0,CursorTop);
1878              }              }
1879              break;              break;
1880            case 7: AutoWrapMode = TRUE; break;            case 7: AutoWrapMode = TRUE; break;
1881            case 8: AutoRepeatMode = TRUE; break;            case 8: AutoRepeatMode = TRUE; break;
1882            case 9:            case 9:
1883              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1884                MouseReportMode = IdMouseTrackX10;                MouseReportMode = IdMouseTrackX10;
1885              break;              break;
1886            case 19: PrintEX = TRUE; break;            case 19: PrintEX = TRUE; break;
1887            case 25: DispEnableCaret(TRUE); break; // cursor on            case 25: DispEnableCaret(TRUE); break; // cursor on
1888            case 38:            case 38:
1889              if (ts.AutoWinSwitch>0)              if (ts.AutoWinSwitch>0)
1890                ChangeEmu = IdTEK; /* Enter TEK Mode */                ChangeEmu = IdTEK; /* Enter TEK Mode */
1891              break;              break;
1892            case 59:            case 59:
1893              if (ts.Language==IdJapanese)              if (ts.Language==IdJapanese)
1894              { /* kanji terminal */              { /* kanji terminal */
1895                Gn[0] = IdASCII;                Gn[0] = IdASCII;
1896                Gn[1] = IdKatakana;                Gn[1] = IdKatakana;
1897                Gn[2] = IdKatakana;                Gn[2] = IdKatakana;
1898                Gn[3] = IdKanji;                Gn[3] = IdKanji;
1899                Glr[0] = 0;                Glr[0] = 0;
1900                if ((ts.KanjiCode==IdJIS) &&                if ((ts.KanjiCode==IdJIS) &&
1901                    (ts.JIS7Katakana==0))                    (ts.JIS7Katakana==0))
1902                  Glr[1] = 2;  // 8-bit katakana                  Glr[1] = 2;  // 8-bit katakana
1903                else                else
1904                  Glr[1] = 3;                  Glr[1] = 3;
1905              }              }
1906              break;              break;
1907            case 66: AppliKeyMode = TRUE; break;            case 66: AppliKeyMode = TRUE; break;
1908            case 67: ts.BSKey = IdBS; break;            case 67: ts.BSKey = IdBS; break;
1909            case 1000:            case 1000:
1910              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1911                MouseReportMode = IdMouseTrackVT200;                MouseReportMode = IdMouseTrackVT200;
1912              break;              break;
1913            case 1001:            case 1001:
1914              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1915                MouseReportMode = IdMouseTrackVT200Hl;                MouseReportMode = IdMouseTrackVT200Hl;
1916              break;              break;
1917            case 1002:            case 1002:
1918              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1919                MouseReportMode = IdMouseTrackBtnEvent;                MouseReportMode = IdMouseTrackBtnEvent;
1920              break;              break;
1921            case 1003:            case 1003:
1922              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1923                MouseReportMode = IdMouseTrackAllEvent;                MouseReportMode = IdMouseTrackAllEvent;
1924              break;              break;
1925            case 1004:            case 1004:
1926              if (ts.MouseEventTracking)              if (ts.MouseEventTracking)
1927                FocusReportMode = TRUE;                FocusReportMode = TRUE;
1928              break;              break;
1929        }        }
1930      }      }
1931    
1932      void CSQ_i_Mode()      void CSQ_i_Mode()
1933      {      {
1934        if (Param[1]==-1) Param[1] = 0;        if (Param[1]==-1) Param[1] = 0;
1935        switch (Param[1]) {        switch (Param[1]) {
1936          case 1:          case 1:
1937            OpenPrnFile();            OpenPrnFile();
1938            BuffDumpCurrentLine(LF);            BuffDumpCurrentLine(LF);
1939            if (! AutoPrintMode)            if (! AutoPrintMode)
1940              ClosePrnFile();              ClosePrnFile();
1941            break;            break;
1942          /* auto print mode off */          /* auto print mode off */
1943          case 4:          case 4:
1944            if (AutoPrintMode)            if (AutoPrintMode)
1945            {            {
1946              ClosePrnFile();              ClosePrnFile();
1947              AutoPrintMode = FALSE;              AutoPrintMode = FALSE;
1948            }            }
1949            break;            break;
1950          /* auto print mode on */          /* auto print mode on */
1951          case 5:          case 5:
1952            if (! AutoPrintMode)            if (! AutoPrintMode)
1953            {            {
1954              OpenPrnFile();              OpenPrnFile();
1955              AutoPrintMode = TRUE;              AutoPrintMode = TRUE;
1956            }            }
1957            break;            break;
1958        }        }
1959      }      }
1960    
1961      void CSQ_l_Mode()      void CSQ_l_Mode()
1962      {      {
1963        int i;        int i;
1964    
1965        for (i = 1 ; i <= NParam ; i++)        for (i = 1 ; i <= NParam ; i++)
1966          switch (Param[i]) {          switch (Param[i]) {
1967            case 1: AppliCursorMode = FALSE; break;            case 1: AppliCursorMode = FALSE; break;
1968            case 3:            case 3:
1969              ChangeTerminalSize(80,NumOfLines-StatusLine);              ChangeTerminalSize(80,NumOfLines-StatusLine);
1970              break;              break;
1971            case 5: /* Normal Video */            case 5: /* Normal Video */
1972              if (ts.ColorFlag & CF_REVERSEVIDEO)              if (ts.ColorFlag & CF_REVERSEVIDEO)
1973                CSQExchangeColor(); /* Exchange text/back color */                CSQExchangeColor(); /* Exchange text/back color */
1974              break;              break;
1975            case 6:            case 6:
1976              if ((StatusLine>0) &&              if ((StatusLine>0) &&
1977                  (CursorY==NumOfLines-1))                  (CursorY==NumOfLines-1))
1978                MoveCursor(0,CursorY);                MoveCursor(0,CursorY);
1979              else {              else {
1980                RelativeOrgMode = FALSE;                RelativeOrgMode = FALSE;
1981                MoveCursor(0,0);                MoveCursor(0,0);
1982              }              }
1983              break;              break;
1984            case 7: AutoWrapMode = FALSE; break;            case 7: AutoWrapMode = FALSE; break;
1985            case 8: AutoRepeatMode = FALSE; break;            case 8: AutoRepeatMode = FALSE; break;
1986            case 9: MouseReportMode = IdMouseTrackNone; break;            case 9: MouseReportMode = IdMouseTrackNone; break;
1987            case 19: PrintEX = FALSE; break;            case 19: PrintEX = FALSE; break;
1988            case 25: DispEnableCaret(FALSE); break; // cursor off            case 25: DispEnableCaret(FALSE); break; // cursor off
1989            case 59:            case 59:
1990              if (ts.Language==IdJapanese)              if (ts.Language==IdJapanese)
1991              { /* katakana terminal */              { /* katakana terminal */
1992                Gn[0] = IdASCII;                Gn[0] = IdASCII;
1993                Gn[1] = IdKatakana;                Gn[1] = IdKatakana;
1994                Gn[2] = IdKatakana;                Gn[2] = IdKatakana;
1995                Gn[3] = IdKanji;                Gn[3] = IdKanji;
1996                Glr[0] = 0;                Glr[0] = 0;
1997                if ((ts.KanjiCode==IdJIS) &&                if ((ts.KanjiCode==IdJIS) &&
1998                    (ts.JIS7Katakana==0))                    (ts.JIS7Katakana==0))
1999                  Glr[1] = 2;  // 8-bit katakana                  Glr[1] = 2;  // 8-bit katakana
2000                else                else
2001                  Glr[1] = 3;                  Glr[1] = 3;
2002              }              }
2003              break;              break;
2004            case 66: AppliKeyMode = FALSE; break;            case 66: AppliKeyMode = FALSE; break;
2005            case 67: ts.BSKey = IdDEL; break;            case 67: ts.BSKey = IdDEL; break;
2006            case 1000:            case 1000:
2007            case 1001:            case 1001:
2008            case 1002:            case 1002:
2009            case 1003: MouseReportMode = IdMouseTrackNone; break;            case 1003: MouseReportMode = IdMouseTrackNone; break;
2010            case 1004: FocusReportMode = FALSE; break;            case 1004: FocusReportMode = FALSE; break;
2011          }          }
2012      }      }
2013    
2014      void CSQ_n_Mode()      void CSQ_n_Mode()
2015      {      {
2016      }      }
2017    
2018    void CSQuest(BYTE b)    void CSQuest(BYTE b)
2019    {    {
2020      switch (b) {      switch (b) {
2021        case 'K': CSLineErase(); break;        case 'K': CSLineErase(); break;
2022        case 'h': CSQ_h_Mode(); break;        case 'h': CSQ_h_Mode(); break;
2023        case 'i': CSQ_i_Mode(); break;        case 'i': CSQ_i_Mode(); break;
2024        case 'l': CSQ_l_Mode(); break;        case 'l': CSQ_l_Mode(); break;
2025        case 'n': CSQ_n_Mode(); break;        case 'n': CSQ_n_Mode(); break;
2026      }      }
2027    }    }
2028    
2029    void SoftReset()    void SoftReset()
2030    // called by software-reset escape sequence handler    // called by software-reset escape sequence handler
2031    {    {
2032      UpdateStr();      UpdateStr();
2033      AutoRepeatMode = TRUE;      AutoRepeatMode = TRUE;
2034      DispEnableCaret(TRUE); // cursor on      DispEnableCaret(TRUE); // cursor on
2035      InsertMode = FALSE;      InsertMode = FALSE;
2036      RelativeOrgMode = FALSE;      RelativeOrgMode = FALSE;
2037      AppliKeyMode = FALSE;      AppliKeyMode = FALSE;
2038      AppliCursorMode = FALSE;      AppliCursorMode = FALSE;
2039      if ((StatusLine>0) &&      if ((StatusLine>0) &&
2040          (CursorY == NumOfLines-1))          (CursorY == NumOfLines-1))
2041        MoveToMainScreen();        MoveToMainScreen();
2042      CursorTop = 0;      CursorTop = 0;
2043      CursorBottom = NumOfLines-1-StatusLine;      CursorBottom = NumOfLines-1-StatusLine;
2044      ResetCharSet();      ResetCharSet();
2045    
2046      Send8BitMode = ts.Send8BitCtrl;      Send8BitMode = ts.Send8BitCtrl;
2047    
2048      /* Attribute */      /* Attribute */
2049      CharAttr = DefCharAttr;      CharAttr = DefCharAttr;
2050      Special = FALSE;      Special = FALSE;
2051      BuffSetCurCharAttr(CharAttr);      BuffSetCurCharAttr(CharAttr);
2052    
2053      // status buffers      // status buffers
2054      ResetSBuffers();      ResetSBuffers();
2055    }    }
2056    
2057    void CSExc(BYTE b)    void CSExc(BYTE b)
2058    {    {
2059      switch (b) {      switch (b) {
2060        case 'p':        case 'p':
2061          /* Software reset */          /* Software reset */
2062          SoftReset();          SoftReset();
2063          break;          break;
2064      }      }
2065    }    }
2066    
2067    void CSDouble(BYTE b)    void CSDouble(BYTE b)
2068    {    {
2069      switch (b) {      switch (b) {
2070        case 'p':        case 'p':
2071          /* Select terminal mode (software reset) */          /* Select terminal mode (software reset) */
2072          SoftReset();          SoftReset();
2073          if (NParam > 0) {          if (NParam > 0) {
2074            switch (Param[1]) {            switch (Param[1]) {
2075              case 61: // VT100 Mode              case 61: // VT100 Mode
2076                Send8BitMode = FALSE; break;                Send8BitMode = FALSE; break;
2077              case 62: // VT200 Mode              case 62: // VT200 Mode
2078              case 63: // VT300 Mode              case 63: // VT300 Mode
2079              case 64: // VT400 Mode              case 64: // VT400 Mode
2080                if (NParam > 1 && Param[2] == 1)                if (NParam > 1 && Param[2] == 1)
2081                  Send8BitMode = FALSE;                  Send8BitMode = FALSE;
2082                else                else
2083                  Send8BitMode = TRUE;                  Send8BitMode = TRUE;
2084                break;                break;
2085            }            }
2086          }          }
2087          break;          break;
2088      }      }
2089    }    }
2090    
2091    void CSDol(BYTE b)    void CSDol(BYTE b)
2092    {    {
2093      switch (b) {      switch (b) {
2094        case '}':        case '}':
2095          if ((ts.TermFlag & TF_ENABLESLINE)==0) return;          if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2096          if (StatusLine==0) return;          if (StatusLine==0) return;
2097          if ((Param[1]<1) && (CursorY==NumOfLines-1))          if ((Param[1]<1) && (CursorY==NumOfLines-1))
2098            MoveToMainScreen();            MoveToMainScreen();
2099          else if ((Param[1]==1) && (CursorY<NumOfLines-1))          else if ((Param[1]==1) && (CursorY<NumOfLines-1))
2100            MoveToStatusLine();            MoveToStatusLine();
2101          break;          break;
2102        case '~':        case '~':
2103          if ((ts.TermFlag & TF_ENABLESLINE)==0) return;          if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2104          if (Param[1]<=1)          if (Param[1]<=1)
2105            HideStatusLine();            HideStatusLine();
2106          else if ((StatusLine==0) && (Param[1]==2))          else if ((StatusLine==0) && (Param[1]==2))
2107            ShowStatusLine(1); // show            ShowStatusLine(1); // show
2108          break;          break;
2109      }      }
2110    }    }
2111    
2112  void PrnParseCS(BYTE b) // printer mode  void PrnParseCS(BYTE b) // printer mode
2113  {  {
2114    ParseMode = ModeFirst;    ParseMode = ModeFirst;
2115    switch (ICount) {    switch (ICount) {
2116      /* no intermediate char */      /* no intermediate char */
2117      case 0:      case 0:
2118        switch (Prv) {        switch (Prv) {
2119          /* no private parameter */          /* no private parameter */
2120          case 0:          case 0:
2121            switch (b) {            switch (b) {
2122              case 'i':              case 'i':
2123                if (Param[1]==4)                if (Param[1]==4)
2124                {                {
2125                  PrinterMode = FALSE;                  PrinterMode = FALSE;
2126                  // clear prn buff                  // clear prn buff
2127                  WriteToPrnFile(0,FALSE);                  WriteToPrnFile(0,FALSE);
2128                  if (! AutoPrintMode)                  if (! AutoPrintMode)
2129                    ClosePrnFile();                    ClosePrnFile();
2130                  return;                  return;
2131                }                }
2132                break;                break;
2133            } /* of case Prv=0 */            } /* of case Prv=0 */
2134            break;            break;
2135        }        }
2136        break;        break;
2137      /* one intermediate char */      /* one intermediate char */
2138      case 1: break;      case 1: break;
2139    } /* of case Icount */    } /* of case Icount */
2140    
2141    WriteToPrnFile(b,TRUE);    WriteToPrnFile(b,TRUE);
2142  }  }
2143    
2144  void ParseCS(BYTE b) /* b is the final char */  void ParseCS(BYTE b) /* b is the final char */
2145  {  {
2146    if (PrinterMode) { // printer mode    if (PrinterMode) { // printer mode
2147      PrnParseCS(b);      PrnParseCS(b);
2148      return;      return;
2149    }    }
2150    
2151    switch (ICount) {    switch (ICount) {
2152      /* no intermediate char */      /* no intermediate char */
2153      case 0:      case 0:
2154        switch (Prv) {        switch (Prv) {
2155          /* no private parameter */          /* no private parameter */
2156          case 0:          case 0:
2157            switch (b) {            switch (b) {
2158              case '@': CSInsertCharacter(); break;              case '@': CSInsertCharacter(); break;
2159              case 'A': CSCursorUp(); break;              case 'A': CSCursorUp(); break;
2160              case 'B': CSCursorDown(); break;              case 'B': CSCursorDown(); break;
2161              case 'C': CSCursorRight(); break;              case 'C': CSCursorRight(); break;
2162              case 'D': CSCursorLeft(); break;              case 'D': CSCursorLeft(); break;
2163              case 'E': CSCursorDown1(); break;              case 'E': CSCursorDown1(); break;
2164              case 'F': CSCursorUp1(); break;              case 'F': CSCursorUp1(); break;
2165              case 'G': CSMoveToColumnN(); break;              case 'G': CSMoveToColumnN(); break;
2166              case 'H': CSMoveToXY(); break;              case 'H': CSMoveToXY(); break;
2167              case 'I': CSForwardTab(); break;            // CHT              case 'I': CSForwardTab(); break;            // CHT
2168              case 'J': CSScreenErase(); break;              case 'J': CSScreenErase(); break;
2169              case 'K': CSLineErase(); break;              case 'K': CSLineErase(); break;
2170              case 'L': CSInsertLine(); break;              case 'L': CSInsertLine(); break;
2171              case 'M': CSDeleteNLines(); break;              case 'M': CSDeleteNLines(); break;
2172              case 'P': CSDeleteCharacter(); break;              case 'P': CSDeleteCharacter(); break;
2173              case 'S': CSScrollUP(); break;              // SU              case 'S': CSScrollUP(); break;              // SU
2174              case 'T': CSScrollDown(); break;            // SD              case 'T': CSScrollDown(); break;            // SD
2175              case 'X': CSEraseCharacter(); break;              case 'X': CSEraseCharacter(); break;
2176              case 'Z': CSBackwardTab(); break;           // CBT              case 'Z': CSBackwardTab(); break;           // CBT
2177              case '`': CSMoveToColumnN(); break;              case '`': CSMoveToColumnN(); break;
2178              case 'a': CSCursorRight(); break;              case 'a': CSCursorRight(); break;
2179              case 'c': AnswerTerminalType(); break;              case 'c': AnswerTerminalType(); break;
2180              case 'd': CSMoveToLineN(); break;              case 'd': CSMoveToLineN(); break;
2181              case 'e': CSCursorUp(); break;              case 'e': CSCursorUp(); break;
2182              case 'f': CSMoveToXY(); break;              case 'f': CSMoveToXY(); break;
2183              case 'g': CSDeleteTabStop(); break;              case 'g': CSDeleteTabStop(); break;
2184              case 'h': CS_h_Mode(); break;              case 'h': CS_h_Mode(); break;
2185              case 'i': CS_i_Mode(); break;              case 'i': CS_i_Mode(); break;
2186              case 'l': CS_l_Mode(); break;              case 'l': CS_l_Mode(); break;
2187              case 'm': CSSetAttr(); break;              case 'm': CSSetAttr(); break;
2188              case 'n': CS_n_Mode(); break;              case 'n': CS_n_Mode(); break;
2189              case 'r': CSSetScrollRegion(); break;              case 'r': CSSetScrollRegion(); break;
2190              case 's': SaveCursor(); break;              case 's': SaveCursor(); break;
2191              case 't': CSSunSequence(); break;              case 't': CSSunSequence(); break;
2192              case 'u': RestoreCursor(); break;              case 'u': RestoreCursor(); break;
2193            } /* of case Prv=0 */            } /* of case Prv=0 */
2194            break;            break;
2195          /* private parameter = '>' */          /* private parameter = '>' */
2196          case '>': CSGT(b); break;          case '>': CSGT(b); break;
2197          /* private parameter = '?' */          /* private parameter = '?' */
2198          case '?': CSQuest(b); break;          case '?': CSQuest(b); break;
2199        }        }
2200        break;        break;
2201      /* one intermediate char */      /* one intermediate char */
2202      case 1:      case 1:
2203        switch (IntChar[1]) {        switch (IntChar[1]) {
2204          /* intermediate char = '!' */          /* intermediate char = '!' */
2205          case '!': CSExc(b); break;          case '!': CSExc(b); break;
2206          /* intermediate char = '"' */          /* intermediate char = '"' */
2207          case '"': CSDouble(b); break;          case '"': CSDouble(b); break;
2208          /* intermediate char = '$' */          /* intermediate char = '$' */
2209          case '$': CSDol(b); break;          case '$': CSDol(b); break;
2210        }        }
2211        break;        break;
2212    } /* of case Icount */    } /* of case Icount */
2213    
2214    ParseMode = ModeFirst;    ParseMode = ModeFirst;
2215  }  }
2216    
2217  void ControlSequence(BYTE b)  void ControlSequence(BYTE b)
2218  {  {
2219    if ((b<=US) || (b>=0x80) && (b<=0x9F))    if ((b<=US) || (b>=0x80) && (b<=0x9F))
2220      ParseControl(b); /* ctrl char */      ParseControl(b); /* ctrl char */
2221    else if ((b>=0x40) && (b<=0x7E))    else if ((b>=0x40) && (b<=0x7E))
2222      ParseCS(b); /* terminate char */      ParseCS(b); /* terminate char */
2223    else {    else {
2224      if (PrinterMode)      if (PrinterMode)
2225        WriteToPrnFile(b,FALSE);        WriteToPrnFile(b,FALSE);
2226    
2227      if ((b>=0x20) && (b<=0x2F))      if ((b>=0x20) && (b<=0x2F))
2228      { /* intermediate char */      { /* intermediate char */
2229        if (ICount<IntCharMax) ICount++;        if (ICount<IntCharMax) ICount++;
2230        IntChar[ICount] = b;        IntChar[ICount] = b;
2231      }      }
2232      else if ((b>=0x30) && (b<=0x39))      else if ((b>=0x30) && (b<=0x39))
2233      {      {
2234        if (Param[NParam] < 0)        if (Param[NParam] < 0)
2235          Param[NParam] = 0;          Param[NParam] = 0;
2236        if (Param[NParam]<1000)        if (Param[NParam]<1000)
2237         Param[NParam] = Param[NParam]*10 + b - 0x30;         Param[NParam] = Param[NParam]*10 + b - 0x30;
2238      }      }
2239      else if (b==0x3B)      else if (b==0x3B)
2240      {      {
2241        if (NParam < NParamMax)        if (NParam < NParamMax)
2242        {        {
2243          NParam++;          NParam++;
2244          Param[NParam] = -1;          Param[NParam] = -1;
2245        }        }
2246      }      }
2247      else if ((b>=0x3C) && (b<=0x3F))      else if ((b>=0x3C) && (b<=0x3F))
2248      { /* private char */      { /* private char */
2249        if (FirstPrm) Prv = b;        if (FirstPrm) Prv = b;
2250      }      }
2251    }    }
2252    FirstPrm = FALSE;    FirstPrm = FALSE;
2253  }  }
2254    
2255  void DeviceControl(BYTE b)  void DeviceControl(BYTE b)
2256  {  {
2257    if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))    if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2258    {    {
2259      ESCFlag = FALSE;      ESCFlag = FALSE;
2260      ParseMode = SavedMode;      ParseMode = SavedMode;
2261      return;      return;
2262    }    }
2263    
2264    if (b==ESC)    if (b==ESC)
2265    {    {
2266      ESCFlag = TRUE;      ESCFlag = TRUE;
2267      return;      return;
2268    }    }
2269    else ESCFlag = FALSE;    else ESCFlag = FALSE;
2270    
2271    if (b<=US)    if (b<=US)
2272      ParseControl(b);      ParseControl(b);
2273    else if ((b>=0x30) && (b<=0x39))    else if ((b>=0x30) && (b<=0x39))
2274    {    {
2275      if (Param[NParam] < 0) Param[NParam] = 0;      if (Param[NParam] < 0) Param[NParam] = 0;
2276      if (Param[NParam]<1000)      if (Param[NParam]<1000)
2277        Param[NParam] = Param[NParam]*10 + b - 0x30;        Param[NParam] = Param[NParam]*10 + b - 0x30;
2278    }    }
2279    else if (b==0x3B)    else if (b==0x3B)
2280    {    {
2281      if (NParam < NParamMax)      if (NParam < NParamMax)
2282      {      {
2283        NParam++;        NParam++;
2284        Param[NParam] = -1;        Param[NParam] = -1;
2285      }      }
2286    }    }
2287    else if ((b>=0x40) && (b<=0x7E))    else if ((b>=0x40) && (b<=0x7E))
2288    {    {
2289      if (b=='|')      if (b=='|')
2290      {      {
2291        ParseMode = ModeDCUserKey;        ParseMode = ModeDCUserKey;
2292        if (Param[1] < 1) ClearUserKey();        if (Param[1] < 1) ClearUserKey();
2293        WaitKeyId = TRUE;        WaitKeyId = TRUE;
2294        NewKeyId = 0;        NewKeyId = 0;
2295      }      }
2296      else ParseMode = ModeSOS;      else ParseMode = ModeSOS;
2297    }    }
2298  }  }
2299    
2300  void DCUserKey(BYTE b)  void DCUserKey(BYTE b)
2301  {  {
2302    if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))    if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2303    {    {
2304      if (! WaitKeyId) DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);      if (! WaitKeyId) DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2305      ESCFlag = FALSE;      ESCFlag = FALSE;
2306      ParseMode = SavedMode;      ParseMode = SavedMode;
2307      return;      return;
2308    }    }
2309    
2310    if (b==ESC)    if (b==ESC)
2311    {    {
2312      ESCFlag = TRUE;      ESCFlag = TRUE;
2313      return;      return;
2314    }    }
2315    else ESCFlag = FALSE;    else ESCFlag = FALSE;
2316    
2317    if (WaitKeyId)    if (WaitKeyId)
2318    {    {
2319      if ((b>=0x30) && (b<=0x39))      if ((b>=0x30) && (b<=0x39))
2320      {      {
2321        if (NewKeyId<1000)        if (NewKeyId<1000)
2322          NewKeyId = NewKeyId*10 + b - 0x30;          NewKeyId = NewKeyId*10 + b - 0x30;
2323      }      }
2324      else if (b==0x2F)      else if (b==0x2F)
2325      {      {
2326        WaitKeyId = FALSE;        WaitKeyId = FALSE;
2327        WaitHi = TRUE;        WaitHi = TRUE;
2328        NewKeyLen = 0;        NewKeyLen = 0;
2329      }      }
2330    }    }
2331    else {    else {
2332      if (b==0x3B)      if (b==0x3B)
2333      {      {
2334        DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);        DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2335        WaitKeyId = TRUE;        WaitKeyId = TRUE;
2336        NewKeyId = 0;        NewKeyId = 0;
2337      }      }
2338      else {      else {
2339        if (NewKeyLen < FuncKeyStrMax)        if (NewKeyLen < FuncKeyStrMax)
2340        {        {
2341          if (WaitHi)          if (WaitHi)
2342          {          {
2343            NewKeyStr[NewKeyLen] = ConvHexChar(b) << 4;            NewKeyStr[NewKeyLen] = ConvHexChar(b) << 4;
2344            WaitHi = FALSE;            WaitHi = FALSE;
2345          }          }
2346          else {          else {
2347            NewKeyStr[NewKeyLen] = NewKeyStr[NewKeyLen] +            NewKeyStr[NewKeyLen] = NewKeyStr[NewKeyLen] +
2348                                   ConvHexChar(b);                                   ConvHexChar(b);
2349            WaitHi = TRUE;            WaitHi = TRUE;
2350            NewKeyLen++;            NewKeyLen++;
2351          }          }
2352        }        }
2353      }      }
2354    }    }
2355  }  }
2356    
2357  void IgnoreString(BYTE b)  void IgnoreString(BYTE b)
2358  {  {
2359    if ((ESCFlag && (b=='\\')) ||    if ((ESCFlag && (b=='\\')) ||
2360        (b<=US && b!=ESC && b!=HT) ||        (b<=US && b!=ESC && b!=HT) ||
2361        (b==ST && ts.KanjiCode!=IdSJIS))        (b==ST && ts.KanjiCode!=IdSJIS))
2362      ParseMode = SavedMode;      ParseMode = SavedMode;
2363    
2364    if (b==ESC) ESCFlag = TRUE;    if (b==ESC) ESCFlag = TRUE;
2365           else ESCFlag = FALSE;           else ESCFlag = FALSE;
2366  }  }
2367    
2368  BOOL XsParseColor(char *colspec, COLORREF *color)  BOOL XsParseColor(char *colspec, COLORREF *color)
2369  {  {
2370          unsigned int r, g, b;          unsigned int r, g, b;
2371  //      double dr, dg, db;  //      double dr, dg, db;
2372    
2373          r = g = b = 255;          r = g = b = 255;
2374    
2375          if (colspec == NULL || color == NULL) {          if (colspec == NULL || color == NULL) {
2376                  return FALSE;                  return FALSE;
2377          }          }
2378    
2379          if (_strnicmp(colspec, "rgb:", 4) == 0) {          if (_strnicmp(colspec, "rgb:", 4) == 0) {
2380                  switch (strlen(colspec)) {                  switch (strlen(colspec)) {
2381                    case  9:      // rgb:R/G/B                    case  9:      // rgb:R/G/B
2382                          if (sscanf(colspec, "rgb:%1x/%1x/%1x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "rgb:%1x/%1x/%1x", &r, &g, &b) != 3) {
2383                                  return FALSE;                                  return FALSE;
2384                          }                          }
2385                          r *= 17; g *= 17; b *= 17;                          r *= 17; g *= 17; b *= 17;
2386                          break;                          break;
2387                    case 12:      // rgb:RR/GG/BB                    case 12:      // rgb:RR/GG/BB
2388                          if (sscanf(colspec, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {
2389                                  return FALSE;                                  return FALSE;
2390                          }                          }
2391                          break;                          break;
2392                    case 15:      // rgb:RRR/GGG/BBB                    case 15:      // rgb:RRR/GGG/BBB
2393                          if (sscanf(colspec, "rgb:%3x/%3x/%3x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "rgb:%3x/%3x/%3x", &r, &g, &b) != 3) {
2394                                  return FALSE;                                  return FALSE;
2395                          }                          }
2396                          r >>= 4; g >>= 4; b >>= 4;                          r >>= 4; g >>= 4; b >>= 4;
2397                          break;                          break;
2398                    case 18:      // rgb:RRRR/GGGG/BBBB                    case 18:      // rgb:RRRR/GGGG/BBBB
2399                          if (sscanf(colspec, "rgb:%4x/%4x/%4x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "rgb:%4x/%4x/%4x", &r, &g, &b) != 3) {
2400                                  return FALSE;                                  return FALSE;
2401                          }                          }
2402                          r >>= 8; g >>= 8; b >>= 8;                          r >>= 8; g >>= 8; b >>= 8;
2403                          break;                          break;
2404                    default:                    default:
2405                          return FALSE;                          return FALSE;
2406                  }                  }
2407          }          }
2408  //      else if (_strnicmp(colspec, "rgbi:", 5) == 0) {  //      else if (_strnicmp(colspec, "rgbi:", 5) == 0) {
2409  //              ; /* nothing to do */  //              ; /* nothing to do */
2410  //      }  //      }
2411          else if (colspec[0] == '#') {          else if (colspec[0] == '#') {
2412                  switch (strlen(colspec)) {                  switch (strlen(colspec)) {
2413                    case  4:      // #RGB                    case  4:      // #RGB
2414                          if (sscanf(colspec, "#%1x%1x%1x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "#%1x%1x%1x", &r, &g, &b) != 3) {
2415                                  return FALSE;                                  return FALSE;
2416                          }                          }
2417                          r <<= 4; g <<= 4; b <<= 4;                          r <<= 4; g <<= 4; b <<= 4;
2418                          break;                          break;
2419                    case  7:      // #RRGGBB                    case  7:      // #RRGGBB
2420                          if (sscanf(colspec, "#%2x%2x%2x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "#%2x%2x%2x", &r, &g, &b) != 3) {
2421                                  return FALSE;                                  return FALSE;
2422                          }                          }
2423                          break;                          break;
2424                    case 10:      // #RRRGGGBBB                    case 10:      // #RRRGGGBBB
2425                          if (sscanf(colspec, "#%3x%3x%3x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "#%3x%3x%3x", &r, &g, &b) != 3) {
2426                                  return FALSE;                                  return FALSE;
2427                          }                          }
2428                          r >>= 4; g >>= 4; b >>= 4;                          r >>= 4; g >>= 4; b >>= 4;
2429                          break;                          break;
2430                    case 13:      // #RRRRGGGGBBBB                    case 13:      // #RRRRGGGGBBBB
2431                          if (sscanf(colspec, "#%4x%4x%4x", &r, &g, &b) != 3) {                          if (sscanf(colspec, "#%4x%4x%4x", &r, &g, &b) != 3) {
2432                                  return FALSE;                                  return FALSE;
2433                          }                          }
2434                          r >>= 8; g >>= 8; b >>= 8;                          r >>= 8; g >>= 8; b >>= 8;
2435                          break;                          break;
2436                    default:                    default:
2437                          return FALSE;                          return FALSE;
2438                  }                  }
2439          }          }
2440          else {          else {
2441                  return FALSE;                  return FALSE;
2442          }          }
2443    
2444          if (r > 255 || g > 255 || b > 255) {          if (r > 255 || g > 255 || b > 255) {
2445                  return FALSE;                  return FALSE;
2446          }          }
2447    
2448          *color = RGB(r, g, b);          *color = RGB(r, g, b);
2449          return TRUE;          return TRUE;
2450  }  }
2451    
2452  #define ModeXsFirst     1  #define ModeXsFirst     1
2453  #define ModeXsString    2  #define ModeXsString    2
2454  #define ModeXsColorNum  3  #define ModeXsColorNum  3
2455  #define ModeXsColorSpec 4  #define ModeXsColorSpec 4
2456  #define ModeXsEsc       5  #define ModeXsEsc       5
2457  void XSequence(BYTE b)  void XSequence(BYTE b)
2458  {  {
2459          static BYTE XsParseMode = ModeXsFirst, PrevMode;          static BYTE XsParseMode = ModeXsFirst, PrevMode;
2460          static char StrBuff[sizeof(ts.Title)];          static char StrBuff[sizeof(ts.Title)];
2461          static unsigned int ColorNumber, StrLen;          static unsigned int ColorNumber, StrLen;
2462          COLORREF color;          COLORREF color;
2463    
2464          switch (XsParseMode) {          switch (XsParseMode) {
2465            case ModeXsFirst:            case ModeXsFirst:
2466                  if (isdigit(b)) {                  if (isdigit(b)) {
2467                          if (Param[1] < 1000) {                          if (Param[1] < 1000) {
2468                                  Param[1] = Param[1]*10 + b - '0';                                  Param[1] = Param[1]*10 + b - '0';
2469                          }                          }
2470                  }                  }
2471                  else if (b == ';') {                  else if (b == ';') {
2472                          StrBuff[0] = '\0';                          StrBuff[0] = '\0';
2473                          StrLen = 0;                          StrLen = 0;
2474                          if (Param[1] == 4) {                          if (Param[1] == 4) {
2475                                  ColorNumber = 0;                                  ColorNumber = 0;
2476                                  XsParseMode = ModeXsColorNum;                                  XsParseMode = ModeXsColorNum;
2477                          }                          }
2478                          else {                          else {
2479                                  XsParseMode = ModeXsString;                                  XsParseMode = ModeXsString;
2480                          }                          }
2481                  }                  }
2482                  else {                  else {
2483                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2484                  }                  }
2485                  break;                  break;
2486            case ModeXsString:            case ModeXsString:
2487                  if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */                  if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2488                          StrBuff[StrLen] = '\0';                          StrBuff[StrLen] = '\0';
2489                          switch (Param[1]) {                          switch (Param[1]) {
2490                            case 0: /* Change window title and icon name */                            case 0: /* Change window title and icon name */
2491                            case 1: /* Change icon name */                            case 1: /* Change icon name */
2492                            case 2: /* Change window title */                            case 2: /* Change window title */
2493                                  if (ts.AcceptTitleChangeRequest) {                                  if (ts.AcceptTitleChangeRequest) {
2494                                          strncpy_s(cv.TitleRemote, sizeof(cv.TitleRemote), StrBuff, _TRUNCATE);                                          strncpy_s(cv.TitleRemote, sizeof(cv.TitleRemote), StrBuff, _TRUNCATE);
2495                                          // (2006.6.15 maya) タイトルに渡す文字列をSJISに変換                                          // (2006.6.15 maya) タイトルに渡す文字列をSJISに変換
2496                                          ConvertToCP932(cv.TitleRemote, sizeof(cv.TitleRemote));                                          ConvertToCP932(cv.TitleRemote, sizeof(cv.TitleRemote));
2497                                          ChangeTitle();                                          ChangeTitle();
2498                                  }                                  }
2499                                  break;                                  break;
2500                            default:                            default:
2501                                  /* nothing to do */;                                  /* nothing to do */;
2502                          }                          }
2503                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2504                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2505                  }                  }
2506                  else if (b == ESC) { /* Escape */                  else if (b == ESC) { /* Escape */
2507                          PrevMode = ModeXsString;                          PrevMode = ModeXsString;
2508                          XsParseMode = ModeXsEsc;                          XsParseMode = ModeXsEsc;
2509                  }                  }
2510                  else if (b <= US) { /* Other control character -- invalid sequence */                  else if (b <= US) { /* Other control character -- invalid sequence */
2511                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2512                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2513                  }                  }
2514                  else if (StrLen < sizeof(StrBuff) - 1) {                  else if (StrLen < sizeof(StrBuff) - 1) {
2515                          StrBuff[StrLen++] = b;                          StrBuff[StrLen++] = b;
2516                  }                  }
2517                  break;                  break;
2518            case ModeXsColorNum:            case ModeXsColorNum:
2519                  if (isdigit(b)) {                  if (isdigit(b)) {
2520                          ColorNumber = ColorNumber*10 + b - '0';                          ColorNumber = ColorNumber*10 + b - '0';
2521                  }                  }
2522                  else if (b == ';') {                  else if (b == ';') {
2523                          XsParseMode = ModeXsColorSpec;                          XsParseMode = ModeXsColorSpec;
2524                          StrBuff[0] = '\0';                          StrBuff[0] = '\0';
2525                          StrLen = 0;                          StrLen = 0;
2526                  }                  }
2527                  else {                  else {
2528                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2529                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2530                  }                  }
2531                  break;                  break;
2532            case ModeXsColorSpec:            case ModeXsColorSpec:
2533                  if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */                  if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2534                          StrBuff[StrLen] = '\0';                          StrBuff[StrLen] = '\0';
2535                          if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {                          if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2536                                  if (strcmp(StrBuff, "?") == 0) {                                  if (strcmp(StrBuff, "?") == 0) {
2537                                          color = DispGetANSIColor(ColorNumber);                                          color = DispGetANSIColor(ColorNumber);
2538                                          if (Send8BitMode) {                                          if (Send8BitMode) {
2539                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2540                                                          "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,                                                          "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2541                                                          GetRValue(color), GetGValue(color), GetBValue(color));                                                          GetRValue(color), GetGValue(color), GetBValue(color));
2542                                          }                                          }
2543                                          else {                                          else {
2544                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2545                                                          "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,                                                          "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2546                                                          GetRValue(color), GetGValue(color), GetBValue(color));                                                          GetRValue(color), GetGValue(color), GetBValue(color));
2547                                          }                                          }
2548                                          ParseMode = ModeFirst;                                          ParseMode = ModeFirst;
2549                                          XsParseMode = ModeXsFirst;                                          XsParseMode = ModeXsFirst;
2550                                          CommBinaryOut(&cv, StrBuff, strlen(StrBuff));                                          CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2551                                          break;                                          break;
2552                                  }                                  }
2553                                  else if (XsParseColor(StrBuff, &color)) {                                  else if (XsParseColor(StrBuff, &color)) {
2554                                          DispSetANSIColor(ColorNumber, color);                                          DispSetANSIColor(ColorNumber, color);
2555                                  }                                  }
2556                          }                          }
2557                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2558                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2559                  }                  }
2560                  else if (b == ESC) {                  else if (b == ESC) {
2561                          PrevMode = ModeXsColorSpec;                          PrevMode = ModeXsColorSpec;
2562                          XsParseMode = ModeXsEsc;                          XsParseMode = ModeXsEsc;
2563                  }                  }
2564                  else if (b <= US) { /* Other control character -- invalid sequence */                  else if (b <= US) { /* Other control character -- invalid sequence */
2565                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2566                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2567                  }                  }
2568                  else if (b == ';') {                  else if (b == ';') {
2569                          if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {                          if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2570                                  if (strcmp(StrBuff, "?") == 0) {                                  if (strcmp(StrBuff, "?") == 0) {
2571                                          color = DispGetANSIColor(ColorNumber);                                          color = DispGetANSIColor(ColorNumber);
2572                                          if (Send8BitMode) {                                          if (Send8BitMode) {
2573                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2574                                                          "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,                                                          "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2575                                                          GetRValue(color), GetGValue(color), GetBValue(color));                                                          GetRValue(color), GetGValue(color), GetBValue(color));
2576                                          }                                          }
2577                                          else {                                          else {
2578                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,                                                  _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2579                                                          "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,                                                          "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2580                                                          GetRValue(color), GetGValue(color), GetBValue(color));                                                          GetRValue(color), GetGValue(color), GetBValue(color));
2581                                          }                                          }
2582                                          XsParseMode = ModeXsColorNum;                                          XsParseMode = ModeXsColorNum;
2583                                          CommBinaryOut(&cv, StrBuff, strlen(StrBuff));                                          CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2584                                  }                                  }
2585                                  else if (XsParseColor(StrBuff, &color)) {                                  else if (XsParseColor(StrBuff, &color)) {
2586                                          DispSetANSIColor(ColorNumber, color);                                          DispSetANSIColor(ColorNumber, color);
2587                                  }                                  }
2588                          }                          }
2589                          ColorNumber = 0;                          ColorNumber = 0;
2590                          StrBuff[0] = '\0';                          StrBuff[0] = '\0';
2591                          StrLen = 0;                          StrLen = 0;
2592                          XsParseMode = ModeXsColorNum;                          XsParseMode = ModeXsColorNum;
2593                  }                  }
2594                  else if (StrLen < sizeof(StrBuff) - 1) {                  else if (StrLen < sizeof(StrBuff) - 1) {
2595                          StrBuff[StrLen++] = b;                          StrBuff[StrLen++] = b;
2596                  }                  }
2597                  break;                  break;
2598            case ModeXsEsc:            case ModeXsEsc:
2599                  if (b == '\\') { /* String Terminator */                  if (b == '\\') { /* String Terminator */
2600                          XsParseMode = PrevMode;                          XsParseMode = PrevMode;
2601  //                      XSequence(ST);  //                      XSequence(ST);
2602                          XSequence(BEL);                          XSequence(BEL);
2603                  }                  }
2604                  else { /* Other character -- invalid sequence */                  else { /* Other character -- invalid sequence */
2605                          ParseMode = ModeFirst;                          ParseMode = ModeFirst;
2606                          XsParseMode = ModeXsFirst;                          XsParseMode = ModeXsFirst;
2607                  }                  }
2608                  break;                  break;
2609  //        default:  //        default:
2610  //              ParseMode = ModeFirst;  //              ParseMode = ModeFirst;
2611  //              XsParseMode = ModeXsFirst;  //              XsParseMode = ModeXsFirst;
2612          }          }
2613  }  }
2614    
2615  void DLESeen(BYTE b)  void DLESeen(BYTE b)
2616  {  {
2617    ParseMode = ModeFirst;    ParseMode = ModeFirst;
2618    if (((ts.FTFlag & FT_BPAUTO)!=0) && (b=='B'))    if (((ts.FTFlag & FT_BPAUTO)!=0) && (b=='B'))
2619      BPStart(IdBPAuto); /* Auto B-Plus activation */      BPStart(IdBPAuto); /* Auto B-Plus activation */
2620    ChangeEmu = -1;    ChangeEmu = -1;
2621  }  }
2622    
2623  void CANSeen(BYTE b)  void CANSeen(BYTE b)
2624  {  {
2625    ParseMode = ModeFirst;    ParseMode = ModeFirst;
2626    if (((ts.FTFlag & FT_ZAUTO)!=0) && (b=='B'))    if (((ts.FTFlag & FT_ZAUTO)!=0) && (b=='B'))
2627      ZMODEMStart(IdZAuto); /* Auto ZMODEM activation */      ZMODEMStart(IdZAuto); /* Auto ZMODEM activation */
2628    ChangeEmu = -1;    ChangeEmu = -1;
2629  }  }
2630    
2631  BOOL CheckKanji(BYTE b)  BOOL CheckKanji(BYTE b)
2632  {  {
2633    BOOL Check;    BOOL Check;
2634    
2635    if (ts.Language!=IdJapanese) return FALSE;    if (ts.Language!=IdJapanese) return FALSE;
2636    
2637    ConvJIS = FALSE;    ConvJIS = FALSE;
2638    
2639    if (ts.KanjiCode==IdSJIS)    if (ts.KanjiCode==IdSJIS)
2640    {    {
2641      if ((0x80<b) && (b<0xa0) || (0xdf<b) && (b<0xfd))      if ((0x80<b) && (b<0xa0) || (0xdf<b) && (b<0xfd))
2642        return TRUE; // SJIS kanji        return TRUE; // SJIS kanji
2643      if ((0xa1<=b) && (b<=0xdf))      if ((0xa1<=b) && (b<=0xdf))
2644        return FALSE; // SJIS katakana        return FALSE; // SJIS katakana
2645    }    }
2646    
2647    if ((b>=0x21) && (b<=0x7e))    if ((b>=0x21) && (b<=0x7e))
2648    {    {
2649      Check = (Gn[Glr[0]]==IdKanji);      Check = (Gn[Glr[0]]==IdKanji);
2650      ConvJIS = Check;      ConvJIS = Check;
2651    }    }
2652    else if ((b>=0xA1) && (b<=0xFE))    else if ((b>=0xA1) && (b<=0xFE))
2653    {    {
2654      Check = (Gn[Glr[1]]==IdKanji);      Check = (Gn[Glr[1]]==IdKanji);
2655      if (ts.KanjiCode==IdEUC)      if (ts.KanjiCode==IdEUC)
2656        Check = TRUE;        Check = TRUE;
2657      else if (ts.KanjiCode==IdJIS)      else if (ts.KanjiCode==IdJIS)
2658      {      {
2659        if (((ts.TermFlag & TF_FIXEDJIS)!=0) &&        if (((ts.TermFlag & TF_FIXEDJIS)!=0) &&
2660            (ts.JIS7Katakana==0))            (ts.JIS7Katakana==0))
2661          Check = FALSE; // 8-bit katakana          Check = FALSE; // 8-bit katakana
2662      }      }
2663      ConvJIS = Check;      ConvJIS = Check;
2664    }    }