Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/ttcomtester/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2535 - (hide annotations) (download) (as text)
Fri May 30 12:36:43 2008 UTC (15 years, 10 months ago) by doda
Original Path: teraterm/trunk/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 68689 byte(s)
UnicodeからDEC特殊文字へのマッピングで、マップされる文字を幾つかの種類に分類し、種類毎に変換するか選択できるようにした。

種類A: 罫線
  Box drawings(U+2500-U+257F)

種類B: 4.58迄は??と表示された文字(英語環境では何も表示されない物も含む)
  Bullet (U+2022)
  Hyphenation point (U+2027)
  Light shade (25%) (U+2591)
  Medium shade (50%) (U+2592)
  Dark shade (75%) (U+2593)
  Black small square (U+25AA)
  Black vertical rectangle (U+25AE)
  Black verty small square (U+2B1D)

種類C: 日本語環境では中点、英語環境ではMiddle dotとして表示される文字
  Middle dot (U+00B7)
  One dot leader (U+2024)
  Bullet operator (U+2219)

種類Aは1,種類Bは2,種類Cは4とし、複数指定する場合は値の和をUnicodeToDecSpMappingに指定する。

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