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 2553 - (hide annotations) (download) (as text)
Sat Jul 5 13:21:42 2008 UTC (15 years, 9 months ago) by doda
Original Path: teraterm/trunk/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 68998 byte(s)
CHT制御シーケンスに対応。

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 doda 2553 CursorForwardTab(1, AutoWrapMode);
360 maya 2476 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 doda 2553 void CSForwardTab()
1359 doda 2551 {
1360     if (Param[1]<1) Param[1] = 1;
1361 doda 2553 CursorForwardTab(Param[1], AutoWrapMode);
1362 doda 2551 }
1363    
1364 doda 2553 void CSBackwardTab()
1365     {
1366     if (Param[1]<1) Param[1] = 1;
1367     CursorBackwardTab(Param[1]);
1368     }
1369    
1370 maya 2476 void CSMoveToColumnN()
1371     {
1372     if (Param[1]<1) Param[1] = 1;
1373     Param[1]--;
1374     if (Param[1] < 0) Param[1] = 0;
1375     if (Param[1] > NumOfColumns-1) Param[1] = NumOfColumns-1;
1376     MoveCursor(Param[1],CursorY);
1377     }
1378    
1379     void CSCursorRight()
1380     {
1381     if (Param[1]<1) Param[1] = 1;
1382     if (CursorX + Param[1] > NumOfColumns-1)
1383     MoveCursor(NumOfColumns-1,CursorY);
1384     else
1385     MoveCursor(CursorX+Param[1],CursorY);
1386     }
1387    
1388     void CSCursorLeft()
1389     {
1390     if (Param[1]<1) Param[1] = 1;
1391     if (CursorX-Param[1] < 0)
1392     MoveCursor(0,CursorY);
1393     else
1394     MoveCursor(CursorX-Param[1],CursorY);
1395     }
1396    
1397     void CSMoveToLineN()
1398     {
1399     if (Param[1]<1) Param[1] = 1;
1400     if (RelativeOrgMode)
1401     {
1402     if (CursorTop+Param[1]-1 > CursorBottom)
1403     MoveCursor(CursorX,CursorBottom);
1404     else
1405     MoveCursor(CursorX,CursorTop+Param[1]-1);
1406     }
1407     else {
1408     if (Param[1] > NumOfLines-StatusLine)
1409     MoveCursor(CursorX,NumOfLines-1-StatusLine);
1410     else
1411     MoveCursor(CursorX,Param[1]-1);
1412     }
1413     }
1414    
1415     void CSMoveToXY()
1416     {
1417     int NewX, NewY;
1418    
1419     if (Param[1]<1) Param[1] = 1;
1420     if ((NParam < 2) || (Param[2]<1)) Param[2] = 1;
1421     NewX = Param[2] - 1;
1422     if (NewX > NumOfColumns-1) NewX = NumOfColumns-1;
1423    
1424     if ((StatusLine>0) && (CursorY==NumOfLines-1))
1425     NewY = CursorY;
1426     else if (RelativeOrgMode)
1427     {
1428     NewY = CursorTop + Param[1] - 1;
1429     if (NewY > CursorBottom) NewY = CursorBottom;
1430     }
1431     else {
1432     NewY = Param[1] - 1;
1433     if (NewY > NumOfLines-1-StatusLine)
1434     NewY = NumOfLines-1-StatusLine;
1435     }
1436     MoveCursor(NewX,NewY);
1437     }
1438    
1439     void CSDeleteTabStop()
1440     {
1441     if (Param[1]==-1) Param[1] = 0;
1442     ClearTabStop(Param[1]);
1443     }
1444    
1445     void CS_h_Mode()
1446     {
1447     switch (Param[1]) {
1448     case 2: KeybEnabled = FALSE; break;
1449     case 4: InsertMode = TRUE; break;
1450     case 12:
1451     ts.LocalEcho = 0;
1452     if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1453     TelChangeEcho();
1454     break;
1455     case 20:
1456     LFMode = TRUE;
1457     ts.CRSend = IdCRLF;
1458     cv.CRSend = IdCRLF;
1459     break;
1460     }
1461     }
1462    
1463     void CS_i_Mode()
1464     {
1465     if (Param[1]==-1) Param[1] = 0;
1466     switch (Param[1]) {
1467     /* print screen */
1468     // PrintEX -- TRUE: print screen
1469     // FALSE: scroll region
1470     case 0: BuffPrint(! PrintEX); break;
1471     /* printer controller mode off */
1472     case 4: break; /* See PrnParseCS() */
1473     /* printer controller mode on */
1474     case 5:
1475     if (! AutoPrintMode)
1476     OpenPrnFile();
1477     DirectPrn = (ts.PrnDev[0]!=0);
1478     PrinterMode = TRUE;
1479     break;
1480     }
1481     }
1482    
1483     void CS_l_Mode()
1484     {
1485     switch (Param[1]) {
1486     case 2: KeybEnabled = TRUE; break;
1487     case 4: InsertMode = FALSE; break;
1488     case 12:
1489     ts.LocalEcho = 1;
1490     if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1491     TelChangeEcho();
1492     break;
1493     case 20:
1494     LFMode = FALSE;
1495     ts.CRSend = IdCR;
1496     cv.CRSend = IdCR;
1497     break;
1498     }
1499     }
1500    
1501     void CS_n_Mode()
1502     {
1503     char Report[16];
1504     int Y;
1505    
1506     switch (Param[1]) {
1507     case 5:
1508     if (Send8BitMode)
1509     CommBinaryOut(&cv,"\2330n",3); /* Device Status Report -> Ready */
1510     else
1511     CommBinaryOut(&cv,"\033[0n",4); /* Device Status Report -> Ready */
1512     break;
1513     case 6:
1514     /* Cursor Position Report */
1515     Y = CursorY+1;
1516     if ((StatusLine>0) &&
1517     (Y==NumOfLines))
1518     Y = 1;
1519     if (Send8BitMode)
1520     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\233%u;%uR", CLocale, Y, CursorX+1);
1521     else
1522     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[%u;%uR", CLocale, Y, CursorX+1);
1523     CommBinaryOut(&cv,Report,strlen(Report));
1524     break;
1525     }
1526     }
1527    
1528     void CSSetAttr()
1529     {
1530     int i, P;
1531    
1532     UpdateStr();
1533     for (i=1 ; i<=NParam ; i++)
1534     {
1535     P = Param[i];
1536     if (P<0) P = 0;
1537     switch (P) {
1538     case 0: /* Clear all */
1539     CharAttr = DefCharAttr;
1540     BuffSetCurCharAttr(CharAttr);
1541     break;
1542    
1543     case 1: /* Bold */
1544     CharAttr.Attr |= AttrBold;
1545     BuffSetCurCharAttr(CharAttr);
1546     break;
1547    
1548     case 4: /* Under line */
1549     CharAttr.Attr |= AttrUnder;
1550     BuffSetCurCharAttr(CharAttr);
1551     break;
1552    
1553     case 5: /* Blink */
1554     CharAttr.Attr |= AttrBlink;
1555     BuffSetCurCharAttr(CharAttr);
1556     break;
1557    
1558     case 7: /* Reverse */
1559     CharAttr.Attr |= AttrReverse;
1560     BuffSetCurCharAttr(CharAttr);
1561     break;
1562    
1563     case 22: /* Bold off */
1564     CharAttr.Attr &= ~ AttrBold;
1565     BuffSetCurCharAttr(CharAttr);
1566     break;
1567    
1568     case 24: /* Under line off */
1569     CharAttr.Attr &= ~ AttrUnder;
1570     BuffSetCurCharAttr(CharAttr);
1571     break;
1572    
1573     case 25: /* Blink off */
1574     CharAttr.Attr &= ~ AttrBlink;
1575     BuffSetCurCharAttr(CharAttr);
1576     break;
1577    
1578     case 27: /* Reverse off */
1579     CharAttr.Attr &= ~ AttrReverse;
1580     BuffSetCurCharAttr(CharAttr);
1581     break;
1582    
1583     case 30:
1584     case 31:
1585     case 32:
1586     case 33:
1587     case 34:
1588     case 35:
1589     case 36:
1590     case 37: /* text color */
1591     CharAttr.Attr2 |= Attr2Fore;
1592     CharAttr.Fore = P - 30;
1593     BuffSetCurCharAttr(CharAttr);
1594     break;
1595    
1596     case 38: /* text color (256color mode) */
1597     if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1598     i++;
1599     if (i < NParam) {
1600     P = Param[++i];
1601     if (P<0) {
1602     P = 0;
1603     }
1604     CharAttr.Attr2 |= Attr2Fore;
1605     CharAttr.Fore = P;
1606     BuffSetCurCharAttr(CharAttr);
1607     }
1608     }
1609     break;
1610    
1611     case 39: /* Reset text color */
1612     CharAttr.Attr2 &= ~ Attr2Fore;
1613     CharAttr.Fore = AttrDefaultFG;
1614     BuffSetCurCharAttr(CharAttr);
1615     break;
1616    
1617     case 40:
1618     case 41:
1619     case 42:
1620     case 43:
1621     case 44:
1622     case 45:
1623     case 46:
1624     case 47: /* Back color */
1625     CharAttr.Attr2 |= Attr2Back;
1626     CharAttr.Back = P - 40;
1627     BuffSetCurCharAttr(CharAttr);
1628     break;
1629    
1630     case 48: /* Back color (256color mode) */
1631     if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1632     i++;
1633     if (i < NParam) {
1634     P = Param[++i];
1635     if (P<0) {
1636     P = 0;
1637     }
1638     CharAttr.Attr2 |= Attr2Back;
1639     CharAttr.Back = P;
1640     BuffSetCurCharAttr(CharAttr);
1641     }
1642     }
1643     break;
1644    
1645     case 49: /* Reset back color */
1646     CharAttr.Attr2 &= ~ Attr2Back;
1647     CharAttr.Back = AttrDefaultBG;
1648     BuffSetCurCharAttr(CharAttr);
1649     break;
1650    
1651     case 90:
1652     case 91:
1653     case 92:
1654     case 93:
1655     case 94:
1656     case 95:
1657     case 96:
1658     case 97: /* aixterm style text color */
1659     if (ts.ColorFlag & CF_AIXTERM16) {
1660     CharAttr.Attr2 |= Attr2Fore;
1661     CharAttr.Fore = P - 90 + 8;
1662     BuffSetCurCharAttr(CharAttr);
1663     }
1664     break;
1665    
1666     case 100:
1667     if (! (ts.ColorFlag & CF_AIXTERM16)) {
1668     /* Reset text and back color */
1669     CharAttr.Attr2 &= ~ (Attr2Fore | Attr2Back);
1670     CharAttr.Fore = AttrDefaultFG;
1671     CharAttr.Back = AttrDefaultBG;
1672     BuffSetCurCharAttr(CharAttr);
1673     break;
1674     }
1675     /* fall through to aixterm style back color */
1676    
1677     case 101:
1678     case 102:
1679     case 103:
1680     case 104:
1681     case 105:
1682     case 106:
1683     case 107: /* aixterm style back color */
1684     if (ts.ColorFlag & CF_AIXTERM16) {
1685     CharAttr.Attr2 |= Attr2Back;
1686     CharAttr.Back = P - 100 + 8;
1687     BuffSetCurCharAttr(CharAttr);
1688     }
1689     break;
1690     }
1691     }
1692     }
1693    
1694     void CSSetScrollRegion()
1695     {
1696     if ((StatusLine>0) &&
1697     (CursorY==NumOfLines-1))
1698     {
1699     MoveCursor(0,CursorY);
1700     return;
1701     }
1702 doda 2484 if (Param[1]<1) Param[1] =1;
1703 maya 2476 if ((NParam < 2) | (Param[2]<1))
1704     Param[2] = NumOfLines-StatusLine;
1705     Param[1]--;
1706     Param[2]--;
1707     if (Param[1] > NumOfLines-1-StatusLine)
1708     Param[1] = NumOfLines-1-StatusLine;
1709     if (Param[2] > NumOfLines-1-StatusLine)
1710     Param[2] = NumOfLines-1-StatusLine;
1711     if (Param[1] >= Param[2]) return;
1712     CursorTop = Param[1];
1713     CursorBottom = Param[2];
1714     if (RelativeOrgMode) MoveCursor(0,CursorTop);
1715     else MoveCursor(0,0);
1716     }
1717    
1718     void CSSunSequence() /* Sun terminal private sequences */
1719     {
1720     char Report[16];
1721    
1722     switch (Param[1]) {
1723     case 8: /* set terminal size */
1724     if ((Param[2]<=1) || (NParam<2)) Param[2] = 24;
1725     if ((Param[3]<=1) || (NParam<3)) Param[3] = 80;
1726     ChangeTerminalSize(Param[3],Param[2]);
1727     break;
1728     case 14: /* get window size??? */
1729     /* this is not actual window size */
1730     if (Send8BitMode)
1731     CommBinaryOut(&cv,"\2334;640;480t",11);
1732     else
1733     CommBinaryOut(&cv,"\033[4;640;480t",12);
1734     break;
1735     case 18: /* get terminal size */
1736     if (Send8BitMode)
1737     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\2338;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1738     else
1739     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[8;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1740     CommBinaryOut(&cv,Report,strlen(Report));
1741     break;
1742     }
1743     }
1744    
1745     void CSGT(BYTE b)
1746     {
1747     switch (b) {
1748     case 'c': /* second terminal report */
1749     if (Send8BitMode)
1750     CommBinaryOut(&cv,"\233>32;10;2c",11); /* VT382 */
1751     else
1752     CommBinaryOut(&cv,"\033[>32;10;2c",11); /* VT382 */
1753     break;
1754     case 'J':
1755     if (Param[1]==3) // IO-8256 terminal
1756     {
1757     if (Param[2]<1) Param[2]=1;
1758     if (Param[3]<1) Param[3]=1;
1759     if (Param[4]<1) Param[4]=1;
1760     if (Param[5]<1) Param[5]=1;
1761     BuffEraseBox(Param[3]-1,Param[2]-1,
1762     Param[5]-1,Param[4]-1);
1763     }
1764     break;
1765     case 'K':
1766     if ((NParam>=2) && (Param[1]==5))
1767     { // IO-8256 terminal
1768     switch (Param[2]) {
1769     case 3:
1770     case 4:
1771     case 5:
1772     case 6:
1773     BuffDrawLine(CharAttr, Param[2], Param[3]);
1774     break;
1775     case 12:
1776     /* Text color */
1777     if ((Param[3]>=0) && (Param[3]<=7))
1778     {
1779     switch (Param[3]) {
1780     case 3: CharAttr.Fore = IdBlue; break;
1781     case 4: CharAttr.Fore = IdCyan; break;
1782     case 5: CharAttr.Fore = IdYellow; break;
1783     case 6: CharAttr.Fore = IdMagenta; break;
1784     default: CharAttr.Fore = Param[3]; break;
1785     }
1786     CharAttr.Attr2 |= Attr2Fore;
1787     BuffSetCurCharAttr(CharAttr);
1788     }
1789     break;
1790     }
1791     }
1792     else if (Param[1]==3)
1793     {// IO-8256 terminal
1794     if (Param[2]<1) Param[2] = 1;
1795     if (Param[3]<1) Param[2] = 1;
1796     BuffEraseCharsInLine(Param[2]-1,Param[3]-Param[2]+1);
1797     }
1798     break;
1799     }
1800     }
1801    
1802     void CSQExchangeColor()
1803     {
1804     COLORREF ColorRef;
1805    
1806     BuffUpdateScroll();
1807    
1808     ColorRef = ts.VTColor[0];
1809     ts.VTColor[0] = ts.VTColor[1];
1810     ts.VTColor[1] = ColorRef;
1811     #ifdef ALPHABLEND_TYPE2
1812     BGInitialize();
1813     #endif
1814     DispChangeBackground();
1815     }
1816    
1817     void CSQ_h_Mode()
1818     {
1819     int i;
1820    
1821     for (i = 1 ; i<=NParam ; i++)
1822     switch (Param[i]) {
1823     case 1: AppliCursorMode = TRUE; break;
1824     case 3:
1825     ChangeTerminalSize(132,NumOfLines-StatusLine);
1826     break;
1827     case 5:
1828     if (ReverseColor) return;
1829     ReverseColor = TRUE;
1830     /* Exchange text/back color */
1831     CSQExchangeColor();
1832     break;
1833     case 6:
1834     if ((StatusLine>0) &&
1835     (CursorY==NumOfLines-1))
1836     MoveCursor(0,CursorY);
1837     else {
1838     RelativeOrgMode = TRUE;
1839     MoveCursor(0,CursorTop);
1840     }
1841     break;
1842     case 7: AutoWrapMode = TRUE; break;
1843     case 8: AutoRepeatMode = TRUE; break;
1844     case 9:
1845     if (ts.MouseEventTracking)
1846     MouseReportMode = IdMouseTrackX10;
1847     break;
1848     case 19: PrintEX = TRUE; break;
1849     case 25: DispEnableCaret(TRUE); break; // cursor on
1850     case 38:
1851     if (ts.AutoWinSwitch>0)
1852     ChangeEmu = IdTEK; /* Enter TEK Mode */
1853     break;
1854     case 59:
1855     if (ts.Language==IdJapanese)
1856     { /* kanji terminal */
1857     Gn[0] = IdASCII;
1858     Gn[1] = IdKatakana;
1859     Gn[2] = IdKatakana;
1860     Gn[3] = IdKanji;
1861     Glr[0] = 0;
1862     if ((ts.KanjiCode==IdJIS) &&
1863     (ts.JIS7Katakana==0))
1864     Glr[1] = 2; // 8-bit katakana
1865     else
1866     Glr[1] = 3;
1867     }
1868     break;
1869     case 66: AppliKeyMode = TRUE; break;
1870     case 67: ts.BSKey = IdBS; break;
1871     case 1000:
1872     if (ts.MouseEventTracking)
1873     MouseReportMode = IdMouseTrackVT200;
1874     break;
1875     case 1001:
1876     if (ts.MouseEventTracking)
1877     MouseReportMode = IdMouseTrackVT200Hl;
1878     break;
1879     case 1002:
1880     if (ts.MouseEventTracking)
1881     MouseReportMode = IdMouseTrackBtnEvent;
1882     break;
1883     case 1003:
1884     if (ts.MouseEventTracking)
1885     MouseReportMode = IdMouseTrackAllEvent;
1886     break;
1887     case 1004:
1888     if (ts.MouseEventTracking)
1889     FocusReportMode = TRUE;
1890     break;
1891     }
1892     }
1893    
1894     void CSQ_i_Mode()
1895     {
1896     if (Param[1]==-1) Param[1] = 0;
1897     switch (Param[1]) {
1898     case 1:
1899     OpenPrnFile();
1900     BuffDumpCurrentLine(LF);
1901     if (! AutoPrintMode)
1902     ClosePrnFile();
1903     break;
1904     /* auto print mode off */
1905     case 4:
1906     if (AutoPrintMode)
1907     {
1908     ClosePrnFile();
1909     AutoPrintMode = FALSE;
1910     }
1911     break;
1912     /* auto print mode on */
1913     case 5:
1914     if (! AutoPrintMode)
1915     {
1916     OpenPrnFile();
1917     AutoPrintMode = TRUE;
1918     }
1919     break;
1920     }
1921     }
1922    
1923     void CSQ_l_Mode()
1924     {
1925     int i;
1926    
1927     for (i = 1 ; i <= NParam ; i++)
1928     switch (Param[i]) {
1929     case 1: AppliCursorMode = FALSE; break;
1930     case 3:
1931     ChangeTerminalSize(80,NumOfLines-StatusLine);
1932     break;
1933     case 5:
1934     if (! ReverseColor) return;
1935     ReverseColor = FALSE;
1936     /* Exchange text/back color */
1937     CSQExchangeColor();
1938     break;
1939     case 6:
1940     if ((StatusLine>0) &&
1941     (CursorY==NumOfLines-1))
1942     MoveCursor(0,CursorY);
1943     else {
1944     RelativeOrgMode = FALSE;
1945     MoveCursor(0,0);
1946     }
1947     break;
1948     case 7: AutoWrapMode = FALSE; break;
1949     case 8: AutoRepeatMode = FALSE; break;
1950     case 9: MouseReportMode = IdMouseTrackNone; break;
1951     case 19: PrintEX = FALSE; break;
1952     case 25: DispEnableCaret(FALSE); break; // cursor off
1953     case 59:
1954     if (ts.Language==IdJapanese)
1955     { /* katakana terminal */
1956     Gn[0] = IdASCII;
1957     Gn[1] = IdKatakana;
1958     Gn[2] = IdKatakana;
1959     Gn[3] = IdKanji;
1960     Glr[0] = 0;
1961     if ((ts.KanjiCode==IdJIS) &&
1962     (ts.JIS7Katakana==0))
1963     Glr[1] = 2; // 8-bit katakana
1964     else
1965     Glr[1] = 3;
1966     }
1967     break;
1968     case 66: AppliKeyMode = FALSE; break;
1969     case 67: ts.BSKey = IdDEL; break;
1970     case 1000:
1971     case 1001:
1972     case 1002:
1973     case 1003: MouseReportMode = IdMouseTrackNone; break;
1974     case 1004: FocusReportMode = FALSE; break;
1975     }
1976     }
1977    
1978     void CSQ_n_Mode()
1979     {
1980     }
1981    
1982     void CSQuest(BYTE b)
1983     {
1984     switch (b) {
1985 doda 2484 case 'K': CSLineErase(); break;
1986 maya 2476 case 'h': CSQ_h_Mode(); break;
1987     case 'i': CSQ_i_Mode(); break;
1988     case 'l': CSQ_l_Mode(); break;
1989     case 'n': CSQ_n_Mode(); break;
1990     }
1991     }
1992    
1993     void SoftReset()
1994     // called by software-reset escape sequence handler
1995     {
1996     UpdateStr();
1997     AutoRepeatMode = TRUE;
1998     DispEnableCaret(TRUE); // cursor on
1999     InsertMode = FALSE;
2000     RelativeOrgMode = FALSE;
2001     AppliKeyMode = FALSE;
2002     AppliCursorMode = FALSE;
2003     if ((StatusLine>0) &&
2004     (CursorY == NumOfLines-1))
2005     MoveToMainScreen();
2006     CursorTop = 0;
2007     CursorBottom = NumOfLines-1-StatusLine;
2008     ResetCharSet();
2009    
2010     Send8BitMode = ts.Send8BitCtrl;
2011    
2012     /* Attribute */
2013     CharAttr = DefCharAttr;
2014     Special = FALSE;
2015     BuffSetCurCharAttr(CharAttr);
2016    
2017     // status buffers
2018     ResetSBuffers();
2019     }
2020    
2021     void CSExc(BYTE b)
2022     {
2023     switch (b) {
2024     case 'p':
2025     /* Software reset */
2026     SoftReset();
2027     break;
2028     }
2029     }
2030    
2031     void CSDouble(BYTE b)
2032     {
2033     switch (b) {
2034     case 'p':
2035     /* Select terminal mode (software reset) */
2036     SoftReset();
2037     if (NParam > 0) {
2038     switch (Param[1]) {
2039     case 61: // VT100 Mode
2040     Send8BitMode = FALSE; break;
2041     case 62: // VT200 Mode
2042     case 63: // VT300 Mode
2043     case 64: // VT400 Mode
2044     if (NParam > 1 && Param[2] == 1)
2045     Send8BitMode = FALSE;
2046     else
2047     Send8BitMode = TRUE;
2048     break;
2049     }
2050     }
2051     break;
2052     }
2053     }
2054    
2055     void CSDol(BYTE b)
2056     {
2057     switch (b) {
2058     case '}':
2059     if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2060     if (StatusLine==0) return;
2061     if ((Param[1]<1) && (CursorY==NumOfLines-1))
2062     MoveToMainScreen();
2063     else if ((Param[1]==1) && (CursorY<NumOfLines-1))
2064     MoveToStatusLine();
2065     break;
2066     case '~':
2067     if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2068     if (Param[1]<=1)
2069     HideStatusLine();
2070     else if ((StatusLine==0) && (Param[1]==2))
2071     ShowStatusLine(1); // show
2072     break;
2073     }
2074     }
2075    
2076     void PrnParseCS(BYTE b) // printer mode
2077     {
2078     ParseMode = ModeFirst;
2079     switch (ICount) {
2080     /* no intermediate char */
2081     case 0:
2082     switch (Prv) {
2083     /* no private parameter */
2084     case 0:
2085     switch (b) {
2086     case 'i':
2087     if (Param[1]==4)
2088     {
2089     PrinterMode = FALSE;
2090     // clear prn buff
2091     WriteToPrnFile(0,FALSE);
2092     if (! AutoPrintMode)
2093     ClosePrnFile();
2094     return;
2095     }
2096     break;
2097     } /* of case Prv=0 */
2098     break;
2099     }
2100     break;
2101     /* one intermediate char */
2102     case 1: break;
2103     } /* of case Icount */
2104    
2105     WriteToPrnFile(b,TRUE);
2106     }
2107    
2108     void ParseCS(BYTE b) /* b is the final char */
2109     {
2110     if (PrinterMode) { // printer mode
2111     PrnParseCS(b);
2112     return;
2113     }
2114    
2115     switch (ICount) {
2116     /* no intermediate char */
2117     case 0:
2118     switch (Prv) {
2119     /* no private parameter */
2120     case 0:
2121     switch (b) {
2122     case '@': CSInsertCharacter(); break;
2123     case 'A': CSCursorUp(); break;
2124     case 'B': CSCursorDown(); break;
2125     case 'C': CSCursorRight(); break;
2126     case 'D': CSCursorLeft(); break;
2127     case 'E': CSCursorDown1(); break;
2128     case 'F': CSCursorUp1(); break;
2129     case 'G': CSMoveToColumnN(); break;
2130     case 'H': CSMoveToXY(); break;
2131 doda 2553 case 'I': CSForwardTab(); break; // CHT
2132 maya 2476 case 'J': CSScreenErase(); break;
2133     case 'K': CSLineErase(); break;
2134     case 'L': CSInsertLine(); break;
2135     case 'M': CSDeleteNLines(); break;
2136     case 'P': CSDeleteCharacter(); break;
2137     case 'S': CSScrollUP(); break; // SU
2138     case 'T': CSScrollDown(); break; // SD
2139     case 'X': CSEraseCharacter(); break;
2140 doda 2553 case 'Z': CSBackwardTab(); break; // CBT
2141 maya 2476 case '`': CSMoveToColumnN(); break;
2142     case 'a': CSCursorRight(); break;
2143     case 'c': AnswerTerminalType(); break;
2144     case 'd': CSMoveToLineN(); break;
2145     case 'e': CSCursorUp(); break;
2146     case 'f': CSMoveToXY(); break;
2147     case 'g': CSDeleteTabStop(); break;
2148     case 'h': CS_h_Mode(); break;
2149     case 'i': CS_i_Mode(); break;
2150     case 'l': CS_l_Mode(); break;
2151     case 'm': CSSetAttr(); break;
2152     case 'n': CS_n_Mode(); break;
2153     case 'r': CSSetScrollRegion(); break;
2154     case 's': SaveCursor(); break;
2155     case 't': CSSunSequence(); break;
2156     case 'u': RestoreCursor(); break;
2157     } /* of case Prv=0 */
2158     break;
2159     /* private parameter = '>' */
2160     case '>': CSGT(b); break;
2161     /* private parameter = '?' */
2162     case '?': CSQuest(b); break;
2163     }
2164     break;
2165     /* one intermediate char */
2166     case 1:
2167     switch (IntChar[1]) {
2168     /* intermediate char = '!' */
2169     case '!': CSExc(b); break;
2170     /* intermediate char = '"' */
2171     case '"': CSDouble(b); break;
2172     /* intermediate char = '$' */
2173     case '$': CSDol(b); break;
2174     }
2175     break;
2176     } /* of case Icount */
2177    
2178     ParseMode = ModeFirst;
2179     }
2180    
2181     void ControlSequence(BYTE b)
2182     {
2183     if ((b<=US) || (b>=0x80) && (b<=0x9F))
2184     ParseControl(b); /* ctrl char */
2185     else if ((b>=0x40) && (b<=0x7E))
2186     ParseCS(b); /* terminate char */
2187     else {
2188     if (PrinterMode)
2189     WriteToPrnFile(b,FALSE);
2190    
2191     if ((b>=0x20) && (b<=0x2F))
2192     { /* intermediate char */
2193     if (ICount<IntCharMax) ICount++;
2194     IntChar[ICount] = b;
2195     }
2196     else if ((b>=0x30) && (b<=0x39))
2197     {
2198     if (Param[NParam] < 0)
2199 doda 2484 Param[NParam] = 0;
2200 maya 2476 if (Param[NParam]<1000)
2201     Param[NParam] = Param[NParam]*10 + b - 0x30;
2202     }
2203     else if (b==0x3B)
2204     {
2205     if (NParam < NParamMax)
2206     {
2207     NParam++;
2208     Param[NParam] = -1;
2209     }
2210     }
2211     else if ((b>=0x3C) && (b<=0x3F))
2212     { /* private char */
2213     if (FirstPrm) Prv = b;
2214     }
2215     }
2216     FirstPrm = FALSE;
2217     }
2218    
2219     void DeviceControl(BYTE b)
2220     {
2221 doda 2528 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2222 maya 2476 {
2223     ESCFlag = FALSE;
2224     ParseMode = SavedMode;
2225     return;
2226     }
2227    
2228     if (b==ESC)
2229     {
2230     ESCFlag = TRUE;
2231     return;
2232     }
2233     else ESCFlag = FALSE;
2234    
2235 doda 2487 if (b<=US)
2236 maya 2476 ParseControl(b);
2237     else if ((b>=0x30) && (b<=0x39))
2238     {
2239 doda 2484 if (Param[NParam] < 0) Param[NParam] = 0;
2240 maya 2476 if (Param[NParam]<1000)
2241     Param[NParam] = Param[NParam]*10 + b - 0x30;
2242     }
2243     else if (b==0x3B)
2244     {
2245     if (NParam < NParamMax)
2246     {
2247     NParam++;
2248     Param[NParam] = -1;
2249     }
2250     }
2251     else if ((b>=0x40) && (b<=0x7E))
2252     {
2253     if (b=='|')
2254     {
2255     ParseMode = ModeDCUserKey;
2256     if (Param[1] < 1) ClearUserKey();
2257     WaitKeyId = TRUE;
2258     NewKeyId = 0;
2259     }
2260     else ParseMode = ModeSOS;
2261     }
2262     }
2263    
2264     void DCUserKey(BYTE b)
2265     {
2266 doda 2528 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2267 maya 2476 {
2268     if (! WaitKeyId) DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2269     ESCFlag = FALSE;
2270     ParseMode = SavedMode;
2271     return;
2272     }
2273    
2274     if (b==ESC)
2275     {
2276     ESCFlag = TRUE;
2277     return;
2278     }
2279     else ESCFlag = FALSE;
2280    
2281     if (WaitKeyId)
2282     {
2283     if ((b>=0x30) && (b<=0x39))
2284     {
2285     if (NewKeyId<1000)
2286     NewKeyId = NewKeyId*10 + b - 0x30;
2287     }
2288     else if (b==0x2F)
2289     {
2290     WaitKeyId = FALSE;
2291     WaitHi = TRUE;
2292     NewKeyLen = 0;
2293     }
2294     }
2295     else {
2296     if (b==0x3B)
2297     {
2298     DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2299     WaitKeyId = TRUE;
2300     NewKeyId = 0;
2301     }
2302     else {
2303     if (NewKeyLen < FuncKeyStrMax)
2304     {
2305     if (WaitHi)
2306     {
2307     NewKeyStr[NewKeyLen] = ConvHexChar(b) << 4;
2308     WaitHi = FALSE;
2309     }
2310     else {
2311     NewKeyStr[NewKeyLen] = NewKeyStr[NewKeyLen] +
2312     ConvHexChar(b);
2313     WaitHi = TRUE;
2314     NewKeyLen++;
2315     }
2316     }
2317     }
2318     }
2319     }
2320    
2321     void IgnoreString(BYTE b)
2322     {
2323 doda 2528 if ((ESCFlag && (b=='\\')) ||
2324     (b<=US && b!=ESC && b!=HT) ||
2325     (b==ST && ts.KanjiCode!=IdSJIS))
2326 maya 2476 ParseMode = SavedMode;
2327    
2328     if (b==ESC) ESCFlag = TRUE;
2329     else ESCFlag = FALSE;
2330     }
2331    
2332     BOOL XsParseColor(char *colspec, COLORREF *color)
2333     {
2334 doda 2486 unsigned int r, g, b;
2335 doda 2484 // double dr, dg, db;
2336 maya 2476
2337 doda 2484 r = g = b = 255;
2338 maya 2476
2339 doda 2484 if (colspec == NULL || color == NULL) {
2340     return FALSE;
2341     }
2342 maya 2476
2343 doda 2484 if (_strnicmp(colspec, "rgb:", 4) == 0) {
2344     switch (strlen(colspec)) {
2345     case 9: // rgb:R/G/B
2346     if (sscanf(colspec, "rgb:%1x/%1x/%1x", &r, &g, &b) != 3) {
2347     return FALSE;
2348     }
2349     r *= 17; g *= 17; b *= 17;
2350     break;
2351     case 12: // rgb:RR/GG/BB
2352     if (sscanf(colspec, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {
2353     return FALSE;
2354     }
2355     break;
2356     case 15: // rgb:RRR/GGG/BBB
2357     if (sscanf(colspec, "rgb:%3x/%3x/%3x", &r, &g, &b) != 3) {
2358     return FALSE;
2359     }
2360     r >>= 4; g >>= 4; b >>= 4;
2361     break;
2362     case 18: // rgb:RRRR/GGGG/BBBB
2363     if (sscanf(colspec, "rgb:%4x/%4x/%4x", &r, &g, &b) != 3) {
2364     return FALSE;
2365     }
2366     r >>= 8; g >>= 8; b >>= 8;
2367     break;
2368     default:
2369     return FALSE;
2370     }
2371 maya 2476 }
2372 doda 2484 // else if (_strnicmp(colspec, "rgbi:", 5) == 0) {
2373     // ; /* nothing to do */
2374     // }
2375     else if (colspec[0] == '#') {
2376     switch (strlen(colspec)) {
2377     case 4: // #RGB
2378     if (sscanf(colspec, "#%1x%1x%1x", &r, &g, &b) != 3) {
2379     return FALSE;
2380     }
2381     r <<= 4; g <<= 4; b <<= 4;
2382     break;
2383     case 7: // #RRGGBB
2384     if (sscanf(colspec, "#%2x%2x%2x", &r, &g, &b) != 3) {
2385     return FALSE;
2386     }
2387     break;
2388     case 10: // #RRRGGGBBB
2389     if (sscanf(colspec, "#%3x%3x%3x", &r, &g, &b) != 3) {
2390     return FALSE;
2391     }
2392     r >>= 4; g >>= 4; b >>= 4;
2393     break;
2394     case 13: // #RRRRGGGGBBBB
2395     if (sscanf(colspec, "#%4x%4x%4x", &r, &g, &b) != 3) {
2396     return FALSE;
2397     }
2398     r >>= 8; g >>= 8; b >>= 8;
2399     break;
2400     default:
2401     return FALSE;
2402     }
2403 maya 2476 }
2404 doda 2484 else {
2405     return FALSE;
2406 maya 2476 }
2407 doda 2484
2408 doda 2486 if (r > 255 || g > 255 || b > 255) {
2409 doda 2484 return FALSE;
2410 maya 2476 }
2411    
2412 doda 2484 *color = RGB(r, g, b);
2413     return TRUE;
2414 maya 2476 }
2415    
2416     #define ModeXsFirst 1
2417     #define ModeXsString 2
2418     #define ModeXsColorNum 3
2419     #define ModeXsColorSpec 4
2420     #define ModeXsEsc 5
2421     void XSequence(BYTE b)
2422     {
2423 doda 2484 static BYTE XsParseMode = ModeXsFirst, PrevMode;
2424     static char StrBuff[sizeof(ts.Title)];
2425 doda 2486 static unsigned int ColorNumber, StrLen;
2426 doda 2484 COLORREF color;
2427 maya 2476
2428 doda 2484 switch (XsParseMode) {
2429     case ModeXsFirst:
2430     if (isdigit(b)) {
2431     if (Param[1] < 1000) {
2432     Param[1] = Param[1]*10 + b - '0';
2433     }
2434     }
2435     else if (b == ';') {
2436     StrBuff[0] = '\0';
2437     StrLen = 0;
2438     if (Param[1] == 4) {
2439     ColorNumber = 0;
2440     XsParseMode = ModeXsColorNum;
2441     }
2442     else {
2443     XsParseMode = ModeXsString;
2444     }
2445     }
2446     else {
2447     ParseMode = ModeFirst;
2448     }
2449     break;
2450     case ModeXsString:
2451 doda 2528 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2452 doda 2484 StrBuff[StrLen] = '\0';
2453     switch (Param[1]) {
2454     case 0: /* Change window title and icon name */
2455     case 1: /* Change icon name */
2456     case 2: /* Change window title */
2457     strncpy_s(ts.Title, sizeof(ts.Title), StrBuff, _TRUNCATE);
2458     // (2006.6.15 maya) �^�C�g�����n����������SJIS������
2459     ConvertToCP932(ts.Title, sizeof(ts.Title));
2460     ChangeTitle();
2461     break;
2462     default:
2463     /* nothing to do */;
2464     }
2465     ParseMode = ModeFirst;
2466     XsParseMode = ModeXsFirst;
2467     }
2468     else if (b == ESC) { /* Escape */
2469     PrevMode = ModeXsString;
2470     XsParseMode = ModeXsEsc;
2471     }
2472     else if (b <= US) { /* Other control character -- invalid sequence */
2473     ParseMode = ModeFirst;
2474     XsParseMode = ModeXsFirst;
2475     }
2476     else if (StrLen < sizeof(StrBuff) - 1) {
2477     StrBuff[StrLen++] = b;
2478     }
2479     break;
2480     case ModeXsColorNum:
2481     if (isdigit(b)) {
2482     ColorNumber = ColorNumber*10 + b - '0';
2483     }
2484     else if (b == ';') {
2485     XsParseMode = ModeXsColorSpec;
2486     StrBuff[0] = '\0';
2487     StrLen = 0;
2488     }
2489     else {
2490     ParseMode = ModeFirst;
2491     XsParseMode = ModeXsFirst;
2492     }
2493     break;
2494     case ModeXsColorSpec:
2495 doda 2528 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2496 doda 2484 StrBuff[StrLen] = '\0';
2497 doda 2486 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2498 doda 2484 if (strcmp(StrBuff, "?") == 0) {
2499     color = DispGetANSIColor(ColorNumber);
2500     if (Send8BitMode) {
2501     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2502     "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2503     GetRValue(color), GetGValue(color), GetBValue(color));
2504     }
2505     else {
2506     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2507     "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2508     GetRValue(color), GetGValue(color), GetBValue(color));
2509     }
2510     ParseMode = ModeFirst;
2511     XsParseMode = ModeXsFirst;
2512     CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2513     break;
2514     }
2515     else if (XsParseColor(StrBuff, &color)) {
2516     DispSetANSIColor(ColorNumber, color);
2517     }
2518     }
2519     ParseMode = ModeFirst;
2520     XsParseMode = ModeXsFirst;
2521     }
2522     else if (b == ESC) {
2523     PrevMode = ModeXsColorSpec;
2524     XsParseMode = ModeXsEsc;
2525     }
2526     else if (b <= US) { /* Other control character -- invalid sequence */
2527     ParseMode = ModeFirst;
2528     XsParseMode = ModeXsFirst;
2529     }
2530     else if (b == ';') {
2531 doda 2486 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2532 doda 2484 if (strcmp(StrBuff, "?") == 0) {
2533     color = DispGetANSIColor(ColorNumber);
2534     if (Send8BitMode) {
2535     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2536     "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2537     GetRValue(color), GetGValue(color), GetBValue(color));
2538     }
2539     else {
2540     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2541     "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2542     GetRValue(color), GetGValue(color), GetBValue(color));
2543     }
2544     XsParseMode = ModeXsColorNum;
2545     CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2546     }
2547     else if (XsParseColor(StrBuff, &color)) {
2548     DispSetANSIColor(ColorNumber, color);
2549     }
2550     }
2551     ColorNumber = 0;
2552     StrBuff[0] = '\0';
2553     StrLen = 0;
2554     XsParseMode = ModeXsColorNum;
2555     }
2556     else if (StrLen < sizeof(StrBuff) - 1) {
2557     StrBuff[StrLen++] = b;
2558     }
2559     break;
2560     case ModeXsEsc:
2561     if (b == '\\') { /* String Terminator */
2562     XsParseMode = PrevMode;
2563 doda 2528 // XSequence(ST);
2564     XSequence(BEL);
2565 doda 2484 }
2566     else { /* Other character -- invalid sequence */
2567     ParseMode = ModeFirst;
2568     XsParseMode = ModeXsFirst;
2569     }
2570     break;
2571     // default:
2572     // ParseMode = ModeFirst;
2573     // XsParseMode = ModeXsFirst;
2574 maya 2476 }
2575     }
2576    
2577     void DLESeen(BYTE b)
2578     {
2579     ParseMode = ModeFirst;
2580     if (((ts.FTFlag & FT_BPAUTO)!=0) && (b=='B'))
2581     BPStart(IdBPAuto); /* Auto B-Plus activation */
2582     ChangeEmu = -1;
2583     }
2584    
2585     void CANSeen(BYTE b)
2586     {
2587     ParseMode = ModeFirst;
2588     if (((ts.FTFlag & FT_ZAUTO)!=0) && (b=='B'))
2589     ZMODEMStart(IdZAuto); /* Auto ZMODEM activation */
2590     ChangeEmu = -1;
2591     }
2592    
2593     BOOL CheckKanji(BYTE b)
2594     {
2595     BOOL Check;
2596    
2597     if (ts.Language!=IdJapanese) return FALSE;
2598    
2599     ConvJIS = FALSE;
2600    
2601     if (ts.KanjiCode==IdSJIS)
2602     {
2603     if ((0x80<b) && (b<0xa0) || (0xdf<b) && (b<0xfd))
2604     return TRUE; // SJIS kanji
2605     if ((0xa1<=b) && (b<=0xdf))
2606     return FALSE; // SJIS katakana
2607     }
2608    
2609     if ((b>=0x21) && (b<=0x7e))
2610     {
2611     Check = (Gn[Glr[0]]==IdKanji);
2612     ConvJIS = Check;
2613     }
2614     else if ((b>=0xA1) &&