Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2555 - (hide annotations) (download) (as text)
Sat Jul 5 21:55:11 2008 UTC (15 years, 9 months ago) by doda
Original Path: teraterm/trunk/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 69279 byte(s)
環境によってはほとんどフラッシュしていないように見えるため、ビジュアルベルにウェイトを追加した。

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