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 2588 - (hide annotations) (download) (as text)
Sat Sep 20 03:02:36 2008 UTC (15 years, 6 months ago) by maya
Original Path: teraterm/trunk/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 69863 byte(s)
AcceptTitleChangeRequest に表記を統一

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 2561 void VisualBell();
43 doda 2554
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 doda 2561 VisualBell();
696 doda 2554 break;
697     }
698 maya 2476 break;
699     case BS: BackSpace(); break;
700     case HT: Tab(); break;
701    
702     case LF:
703     // ���M�������s�R�[�h�� LF ���������A�T�[�o���� LF ���������������������������A
704     // CR+LF���������������������B
705     // cf. http://www.neocom.ca/forum/viewtopic.php?t=216
706     // (2007.1.21 yutaka)
707     if (ts.CRReceive == IdLF) {
708     CarriageReturn(TRUE);
709     LineFeed(b, TRUE);
710     break;
711     }
712    
713     case VT: LineFeed(b,TRUE); break;
714    
715     case FF:
716     if ((ts.AutoWinSwitch>0) && JustAfterESC)
717     {
718     CommInsert1Byte(&cv,b);
719     CommInsert1Byte(&cv,ESC);
720     ChangeEmu = IdTEK; /* Enter TEK Mode */
721     }
722     else
723     LineFeed(b,TRUE);
724     break;
725     case CR:
726     CarriageReturn(TRUE);
727     if (ts.CRReceive==IdCRLF)
728     CommInsert1Byte(&cv,LF);
729     break;
730     case SO:
731     if ((ts.Language==IdJapanese) &&
732     (ts.KanjiCode==IdJIS) &&
733     (ts.JIS7Katakana==1) &&
734     ((ts.TermFlag & TF_FIXEDJIS)!=0))
735     Gn[1] = IdKatakana;
736    
737     Glr[0] = 1; /* LS1 */
738     break;
739     case SI: Glr[0] = 0; break; /* LS0 */
740     case DLE:
741     if ((ts.FTFlag & FT_BPAUTO)!=0)
742     ParseMode = ModeDLE; /* Auto B-Plus activation */
743     break;
744     case CAN:
745     if ((ts.FTFlag & FT_ZAUTO)!=0)
746     ParseMode = ModeCAN; /* Auto ZMODEM activation */
747     // else if (ts.AutoWinSwitch>0)
748     // ChangeEmu = IdTEK; /* Enter TEK Mode */
749     else
750     ParseMode = ModeFirst;
751     break;
752     case SUB: ParseMode = ModeFirst; break;
753     case ESC:
754     ICount = 0;
755     JustAfterESC = TRUE;
756     ParseMode = ModeESC;
757     break;
758     case FS:
759     case GS:
760     case RS:
761     case US:
762     if (ts.AutoWinSwitch>0)
763     {
764     CommInsert1Byte(&cv,b);
765     ChangeEmu = IdTEK; /* Enter TEK Mode */
766     }
767     break;
768    
769     /* C1 char */
770     case IND: LineFeed(0,TRUE); break;
771     case NEL:
772     LineFeed(0,TRUE);
773     CarriageReturn(TRUE);
774     break;
775     case HTS: SetTabStop(); break;
776     case RI: CursorUpWithScroll(); break;
777     case SS2:
778     GLtmp = 2;
779     SSflag = TRUE;
780     break;
781     case SS3:
782     GLtmp = 3;
783     SSflag = TRUE;
784     break;
785     case DCS:
786     SavedMode = ParseMode;
787     ESCFlag = FALSE;
788     NParam = 1;
789     Param[1] = -1;
790     ParseMode = ModeDCS;
791     break;
792     case SOS:
793     SavedMode = ParseMode;
794     ESCFlag = FALSE;
795     ParseMode = ModeSOS;
796     break;
797     case CSI:
798     ICount = 0;
799     FirstPrm = TRUE;
800     NParam = 1;
801     Param[1] = -1;
802     Prv = 0;
803     ParseMode = ModeCSI;
804     break;
805     case OSC:
806     Param[1] = 0;
807     ParseMode = ModeXS;
808     break;
809     case PM:
810     case APC:
811     SavedMode = ParseMode;
812     ESCFlag = FALSE;
813     ParseMode = ModeSOS;
814     break;
815     }
816     }
817    
818     void SaveCursor()
819     {
820     int i;
821     PStatusBuff Buff;
822    
823     if ((StatusLine>0) &&
824     (CursorY==NumOfLines-1))
825     Buff = &SBuff2; // for status line
826     else
827     Buff = &SBuff1; // for main screen
828    
829     Buff->CursorX = CursorX;
830     Buff->CursorY = CursorY;
831     Buff->Attr = CharAttr;
832     Buff->Glr[0] = Glr[0];
833     Buff->Glr[1] = Glr[1];
834     for (i=0 ; i<=3; i++)
835     Buff->Gn[i] = Gn[i];
836     Buff->AutoWrapMode = AutoWrapMode;
837     Buff->RelativeOrgMode = RelativeOrgMode;
838     }
839    
840     void RestoreCursor()
841     {
842     int i;
843     PStatusBuff Buff;
844     UpdateStr();
845    
846     if ((StatusLine>0) &&
847     (CursorY==NumOfLines-1))
848     Buff = &SBuff2; // for status line
849     else
850     Buff = &SBuff1; // for main screen
851    
852     if (Buff->CursorX > NumOfColumns-1)
853     Buff->CursorX = NumOfColumns-1;
854     if (Buff->CursorY > NumOfLines-1-StatusLine)
855     Buff->CursorY = NumOfLines-1-StatusLine;
856     MoveCursor(Buff->CursorX,Buff->CursorY);
857     CharAttr = Buff->Attr;
858     Glr[0] = Buff->Glr[0];
859     Glr[1] = Buff->Glr[1];
860     for (i=0 ; i<=3; i++)
861     Gn[i] = Buff->Gn[i];
862     AutoWrapMode = Buff->AutoWrapMode;
863     RelativeOrgMode = Buff->RelativeOrgMode;
864     }
865    
866     void AnswerTerminalType()
867     {
868     char Tmp[31];
869    
870     if (ts.TerminalID<IdVT320)
871     strncpy_s(Tmp, sizeof(Tmp),"\033[?", _TRUNCATE);
872     else
873     strncpy_s(Tmp, sizeof(Tmp),"\233?", _TRUNCATE);
874    
875     switch (ts.TerminalID) {
876     case IdVT100:
877     strncat_s(Tmp,sizeof(Tmp),"1;2",_TRUNCATE);
878     break;
879     case IdVT100J:
880     strncat_s(Tmp,sizeof(Tmp),"5;2",_TRUNCATE);
881     break;
882     case IdVT101:
883     strncat_s(Tmp,sizeof(Tmp),"1;0",_TRUNCATE);
884     break;
885     case IdVT102:
886     strncat_s(Tmp,sizeof(Tmp),"6",_TRUNCATE);
887     break;
888     case IdVT102J:
889     strncat_s(Tmp,sizeof(Tmp),"15",_TRUNCATE);
890     break;
891     case IdVT220J:
892     strncat_s(Tmp,sizeof(Tmp),"62;1;2;5;6;7;8",_TRUNCATE);
893     break;
894     case IdVT282:
895     strncat_s(Tmp,sizeof(Tmp),"62;1;2;4;5;6;7;8;10;11",_TRUNCATE);
896     break;
897     case IdVT320:
898     strncat_s(Tmp,sizeof(Tmp),"63;1;2;6;7;8",_TRUNCATE);
899     break;
900     case IdVT382:
901     strncat_s(Tmp,sizeof(Tmp),"63;1;2;4;5;6;7;8;10;15",_TRUNCATE);
902     break;
903     }
904     strncat_s(Tmp,sizeof(Tmp),"c",_TRUNCATE);
905    
906     CommBinaryOut(&cv,Tmp,strlen(Tmp)); /* Report terminal ID */
907     }
908    
909     void ESCSpace(BYTE b)
910     {
911     switch (b) {
912     case 'F': Send8BitMode = FALSE; break; // S7C1T
913     case 'G': Send8BitMode = TRUE; break; // S8C1T
914     }
915     }
916    
917     void ESCSharp(BYTE b)
918     {
919     switch (b) {
920     case '8': /* Fill screen with "E" */
921     BuffUpdateScroll();
922     BuffFillWithE();
923     MoveCursor(0,0);
924     ParseMode = ModeFirst;
925     break;
926     }
927     }
928    
929     /* select double byte code set */
930     void ESCDBCSSelect(BYTE b)
931     {
932     int Dist;
933    
934     if (ts.Language!=IdJapanese) return;
935    
936     switch (ICount) {
937     case 1:
938     if ((b=='@') || (b=='B'))
939     {
940     Gn[0] = IdKanji; /* Kanji -> G0 */
941     if ((ts.TermFlag & TF_AUTOINVOKE)!=0)
942     Glr[0] = 0; /* G0->GL */
943     }
944     break;
945     case 2:
946     /* Second intermediate char must be
947     '(' or ')' or '*' or '+'. */
948     Dist = (IntChar[2]-'(') & 3; /* G0 - G3 */
949     if ((b=='1') || (b=='3') ||
950     (b=='@') || (b=='B'))
951     {
952     Gn[Dist] = IdKanji; /* Kanji -> G0-3 */
953     if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
954     (Dist==0))
955     Glr[0] = 0; /* G0->GL */
956     }
957     break;
958     }
959 doda 2484 }
960 maya 2476
961     void ESCSelectCode(BYTE b)
962     {
963     switch (b) {
964     case '0':
965     if (ts.AutoWinSwitch>0)
966     ChangeEmu = IdTEK; /* enter TEK mode */
967     break;
968     }
969     }
970    
971     /* select single byte code set */
972     void ESCSBCSSelect(BYTE b)
973     {
974     int Dist;
975    
976     /* Intermediate char must be
977     '(' or ')' or '*' or '+'. */
978     Dist = (IntChar[1]-'(') & 3; /* G0 - G3 */
979    
980     switch (b) {
981     case '0': Gn[Dist] = IdSpecial; break;
982     case '<': Gn[Dist] = IdASCII; break;
983     case '>': Gn[Dist] = IdASCII; break;
984     case 'B': Gn[Dist] = IdASCII; break;
985     case 'H': Gn[Dist] = IdASCII; break;
986     case 'I':
987     if (ts.Language==IdJapanese)
988     Gn[Dist] = IdKatakana;
989     break;
990     case 'J': Gn[Dist] = IdASCII; break;
991     }
992    
993     if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
994     (Dist==0))
995     Glr[0] = 0; /* G0->GL */
996     }
997    
998     void PrnParseEscape(BYTE b) // printer mode
999     {
1000     int i;
1001    
1002     ParseMode = ModeFirst;
1003     switch (ICount) {
1004     /* no intermediate char */
1005     case 0:
1006     switch (b) {
1007     case '[': /* CSI */
1008     ICount = 0;
1009     FirstPrm = TRUE;
1010     NParam = 1;
1011     Param[1] = -1;
1012     Prv = 0;
1013     WriteToPrnFile(ESC,FALSE);
1014     WriteToPrnFile('[',FALSE);
1015     ParseMode = ModeCSI;
1016     return;
1017     } /* end of case Icount=0 */
1018     break;
1019     /* one intermediate char */
1020     case 1:
1021     switch (IntChar[1]) {
1022     case '$':
1023     if (! DirectPrn)
1024     {
1025     ESCDBCSSelect(b);
1026     return;
1027     }
1028     break;
1029     case '(':
1030     case ')':
1031     case '*':
1032     case '+':
1033     if (! DirectPrn)
1034     {
1035     ESCSBCSSelect(b);
1036     return;
1037     }
1038     break;
1039     }
1040     break;
1041     /* two intermediate char */
1042     case 2:
1043     if ((! DirectPrn) &&
1044     (IntChar[1]=='$') &&
1045     ('('<=IntChar[2]) &&
1046     (IntChar[2]<='+'))
1047     {
1048     ESCDBCSSelect(b);
1049     return;
1050     }
1051     break;
1052     }
1053     // send the uninterpreted sequence to printer
1054     WriteToPrnFile(ESC,FALSE);
1055     for (i=1; i<=ICount; i++)
1056     WriteToPrnFile(IntChar[i],FALSE);
1057     WriteToPrnFile(b,TRUE);
1058     }
1059    
1060     void ParseEscape(BYTE b) /* b is the final char */
1061     {
1062     if (PrinterMode) { // printer mode
1063     PrnParseEscape(b);
1064     return;
1065     }
1066    
1067     switch (ICount) {
1068     /* no intermediate char */
1069     case 0:
1070     switch (b) {
1071     case '7': SaveCursor(); break;
1072     case '8': RestoreCursor(); break;
1073     case '=': AppliKeyMode = TRUE; break;
1074     case '>': AppliKeyMode = FALSE; break;
1075     case 'D': /* IND */
1076     LineFeed(0,TRUE);
1077     break;
1078     case 'E': /* NEL */
1079     MoveCursor(0,CursorY);
1080     LineFeed(0,TRUE);
1081     break;
1082     case 'H': /* HTS */
1083     SetTabStop();
1084     break;
1085     case 'M': /* RI */
1086     CursorUpWithScroll();
1087     break;
1088     case 'N': /* SS2 */
1089     GLtmp = 2;
1090     SSflag = TRUE;
1091     break;
1092     case 'O': /* SS3 */
1093     GLtmp = 3;
1094     SSflag = TRUE;
1095     break;
1096     case 'P': /* DCS */
1097     SavedMode = ParseMode;
1098     ESCFlag = FALSE;
1099     NParam = 1;
1100     Param[1] = -1;
1101     ParseMode = ModeDCS;
1102     return;
1103     case 'X': /* SOS */
1104     SavedMode = ParseMode;
1105     ESCFlag = FALSE;
1106     ParseMode = ModeSOS;
1107     return;
1108     case 'Z': AnswerTerminalType(); break;
1109     case '[': /* CSI */
1110     ICount = 0;
1111     FirstPrm = TRUE;
1112     NParam = 1;
1113     Param[1] = -1;
1114     Prv = 0;
1115     ParseMode = ModeCSI;
1116     return;
1117     case '\\': break; /* ST */
1118     case ']': /* XTERM sequence (OSC) */
1119     NParam = 1;
1120     Param[1] = 0;
1121     ParseMode = ModeXS;
1122 doda 2484 return;
1123 maya 2476 case '^':
1124     case '_': /* PM, APC */
1125     SavedMode = ParseMode;
1126     ESCFlag = FALSE;
1127     ParseMode = ModeSOS;
1128     return;
1129     case 'c': /* Hardware reset */
1130     HideStatusLine();
1131     ResetTerminal();
1132     ClearUserKey();
1133     ClearBuffer();
1134     if (ts.PortType==IdSerial) // reset serial port
1135     CommResetSerial(&ts, &cv, TRUE);
1136     break;
1137 doda 2561 case 'g': /* Visual Bell (screen original?) */
1138     VisualBell();
1139     break;
1140 maya 2476 case 'n': Glr[0] = 2; break; /* LS2 */
1141     case 'o': Glr[0] = 3; break; /* LS3 */
1142     case '|': Glr[1] = 3; break; /* LS3R */
1143     case '}': Glr[1] = 2; break; /* LS2R */
1144     case '~': Glr[1] = 1; break; /* LS1R */
1145     } /* end of case Icount=0 */
1146     break;
1147     /* one intermediate char */
1148     case 1:
1149     switch (IntChar[1]) {
1150     case ' ': ESCSpace(b); break;
1151     case '#': ESCSharp(b); break;
1152     case '$': ESCDBCSSelect(b); break;
1153     case '%': break;
1154     case '(':
1155     case ')':
1156     case '*':
1157     case '+':
1158     ESCSBCSSelect(b);
1159     break;
1160     }
1161     break;
1162     /* two intermediate char */
1163     case 2:
1164     if ((IntChar[1]=='$') &&
1165     ('('<=IntChar[2]) &&
1166     (IntChar[2]<='+'))
1167     ESCDBCSSelect(b);
1168     else if ((IntChar[1]=='%') &&
1169     (IntChar[2]=='!'))
1170     ESCSelectCode(b);
1171     break;
1172     }
1173     ParseMode = ModeFirst;
1174     }
1175    
1176     void EscapeSequence(BYTE b)
1177     {
1178     if (b<=US)
1179     ParseControl(b);
1180     else if ((b>=0x20) && (b<=0x2F))
1181     {
1182     if (ICount<IntCharMax) ICount++;
1183     IntChar[ICount] = b;
1184     }
1185     else if ((b>=0x30) && (b<=0x7E))
1186     ParseEscape(b);
1187     else if ((b>=0x80) && (b<=0x9F))
1188     ParseControl(b);
1189    
1190     JustAfterESC = FALSE;
1191     }
1192    
1193     void CSInsertCharacter()
1194     {
1195     // Insert space characters at cursor
1196     int Count;
1197    
1198     BuffUpdateScroll();
1199     if (Param[1]<1) Param[1] = 1;
1200     Count = Param[1];
1201     BuffInsertSpace(Count);
1202     }
1203    
1204     void CSCursorUp()
1205     {
1206     if (Param[1]<1) Param[1] = 1;
1207    
1208     if (CursorY >= CursorTop)
1209     {
1210     if (CursorY-Param[1] > CursorTop)
1211     MoveCursor(CursorX,CursorY-Param[1]);
1212     else
1213     MoveCursor(CursorX,CursorTop);
1214     }
1215     else {
1216     if (CursorY > 0)
1217     MoveCursor(CursorX,CursorY-Param[1]);
1218     else
1219     MoveCursor(CursorX,0);
1220     }
1221     }
1222    
1223     void CSCursorUp1()
1224     {
1225     MoveCursor(0,CursorY);
1226     CSCursorUp();
1227     }
1228    
1229     void CSCursorDown()
1230     {
1231     if (Param[1]<1) Param[1] = 1;
1232    
1233     if (CursorY <= CursorBottom)
1234     {
1235     if (CursorY+Param[1] < CursorBottom)
1236     MoveCursor(CursorX,CursorY+Param[1]);
1237     else
1238     MoveCursor(CursorX,CursorBottom);
1239     }
1240     else {
1241     if (CursorY < NumOfLines-StatusLine-1)
1242     MoveCursor(CursorX,CursorY+Param[1]);
1243     else
1244     MoveCursor(CursorX,NumOfLines-StatusLine);
1245     }
1246     }
1247    
1248     void CSCursorDown1()
1249     {
1250     MoveCursor(0,CursorY);
1251     CSCursorDown();
1252     }
1253    
1254     void CSScreenErase()
1255     {
1256     if (Param[1] == -1) Param[1] = 0;
1257     BuffUpdateScroll();
1258     switch (Param[1]) {
1259     case 0:
1260     // <ESC>[H(Cursor in left upper corner)�������J�[�\�������������w�������������A
1261     // <ESC>[J��<ESC>[2J�����������������A�����������A���s�o�b�t�@���X�N���[���A�E�g
1262     // �����������������B(2005.5.29 yutaka)
1263 yutakapon 2498 // �R���t�B�O���[�V�������������������������������B(2008.5.3 yutaka)
1264     if (ts.ScrollWindowClearScreen &&
1265     (CursorX == 0 && CursorY == 0)) {
1266 maya 2476 // Erase screen (scroll out)
1267     BuffClearScreen();
1268     UpdateWindow(HVTWin);
1269    
1270     } else {
1271     // Erase characters from cursor to the end of screen
1272     BuffEraseCurToEnd();
1273     }
1274     break;
1275    
1276     case 1:
1277     // Erase characters from home to cursor
1278     BuffEraseHomeToCur();
1279     break;
1280    
1281     case 2:
1282     // Erase screen (scroll out)
1283     BuffClearScreen();
1284     UpdateWindow(HVTWin);
1285     break;
1286     }
1287     }
1288    
1289     void CSInsertLine()
1290     {
1291     // Insert lines at current position
1292     int Count, YEnd;
1293    
1294     if (CursorY < CursorTop) return;
1295     if (CursorY > CursorBottom) return;
1296     if (Param[1]<1) Param[1] = 1;
1297     Count = Param[1];
1298    
1299     YEnd = CursorBottom;
1300     if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1301     if (Count > YEnd+1 - CursorY) Count = YEnd+1 - CursorY;
1302    
1303     BuffInsertLines(Count,YEnd);
1304     }
1305    
1306     void CSLineErase()
1307     {
1308     if (Param[1] == -1) Param[1] = 0;
1309     BuffUpdateScroll();
1310     switch (Param[1]) {
1311     /* erase char from cursor to end of line */
1312     case 0:
1313     BuffEraseCharsInLine(CursorX,NumOfColumns-CursorX);
1314     break;
1315     /* erase char from start of line to cursor */
1316     case 1:
1317     BuffEraseCharsInLine(0,CursorX+1);
1318     break;
1319     /* erase entire line */
1320     case 2:
1321     BuffEraseCharsInLine(0,NumOfColumns);
1322     break;
1323     }
1324     }
1325    
1326     void CSDeleteNLines()
1327     // Delete lines from current line
1328     {
1329     int Count, YEnd;
1330    
1331     if (CursorY < CursorTop) return;
1332     if (CursorY > CursorBottom) return;
1333     Count = Param[1];
1334     if (Count<1) Count = 1;
1335    
1336     YEnd = CursorBottom;
1337     if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1338     if (Count > YEnd+1-CursorY) Count = YEnd+1-CursorY;
1339     BuffDeleteLines(Count,YEnd);
1340     }
1341    
1342     void CSDeleteCharacter()
1343     {
1344     // Delete characters in current line from cursor
1345    
1346     if (Param[1]<1) Param[1] = 1;
1347     BuffUpdateScroll();
1348     BuffDeleteChars(Param[1]);
1349     }
1350    
1351     void CSEraseCharacter()
1352     {
1353     if (Param[1]<1) Param[1] = 1;
1354     BuffUpdateScroll();
1355     BuffEraseChars(Param[1]);
1356     }
1357    
1358     void CSScrollUP()
1359     {
1360     if (Param[1]<1) Param[1] = 1;
1361     BuffUpdateScroll();
1362     BuffRegionScrollUpNLines(Param[1]);
1363     }
1364    
1365     void CSScrollDown()
1366     {
1367     if (Param[1]<1) Param[1] = 1;
1368     BuffUpdateScroll();
1369     BuffRegionScrollDownNLines(Param[1]);
1370     }
1371    
1372 doda 2553 void CSForwardTab()
1373 doda 2551 {
1374     if (Param[1]<1) Param[1] = 1;
1375 doda 2553 CursorForwardTab(Param[1], AutoWrapMode);
1376 doda 2551 }
1377    
1378 doda 2553 void CSBackwardTab()
1379     {
1380     if (Param[1]<1) Param[1] = 1;
1381     CursorBackwardTab(Param[1]);
1382     }
1383    
1384 maya 2476 void CSMoveToColumnN()
1385     {
1386     if (Param[1]<1) Param[1] = 1;
1387     Param[1]--;
1388     if (Param[1] < 0) Param[1] = 0;
1389     if (Param[1] > NumOfColumns-1) Param[1] = NumOfColumns-1;
1390     MoveCursor(Param[1],CursorY);
1391     }
1392    
1393     void CSCursorRight()
1394     {
1395     if (Param[1]<1) Param[1] = 1;
1396     if (CursorX + Param[1] > NumOfColumns-1)
1397     MoveCursor(NumOfColumns-1,CursorY);
1398     else
1399     MoveCursor(CursorX+Param[1],CursorY);
1400     }
1401    
1402     void CSCursorLeft()
1403     {
1404     if (Param[1]<1) Param[1] = 1;
1405     if (CursorX-Param[1] < 0)
1406     MoveCursor(0,CursorY);
1407     else
1408     MoveCursor(CursorX-Param[1],CursorY);
1409     }
1410    
1411     void CSMoveToLineN()
1412     {
1413     if (Param[1]<1) Param[1] = 1;
1414     if (RelativeOrgMode)
1415     {
1416     if (CursorTop+Param[1]-1 > CursorBottom)
1417     MoveCursor(CursorX,CursorBottom);
1418     else
1419     MoveCursor(CursorX,CursorTop+Param[1]-1);
1420     }
1421     else {
1422     if (Param[1] > NumOfLines-StatusLine)
1423     MoveCursor(CursorX,NumOfLines-1-StatusLine);
1424     else
1425     MoveCursor(CursorX,Param[1]-1);
1426     }
1427     }
1428    
1429     void CSMoveToXY()
1430     {
1431     int NewX, NewY;
1432    
1433     if (Param[1]<1) Param[1] = 1;
1434     if ((NParam < 2) || (Param[2]<1)) Param[2] = 1;
1435     NewX = Param[2] - 1;
1436     if (NewX > NumOfColumns-1) NewX = NumOfColumns-1;
1437    
1438     if ((StatusLine>0) && (CursorY==NumOfLines-1))
1439     NewY = CursorY;
1440     else if (RelativeOrgMode)
1441     {
1442     NewY = CursorTop + Param[1] - 1;
1443     if (NewY > CursorBottom) NewY = CursorBottom;
1444     }
1445     else {
1446     NewY = Param[1] - 1;
1447     if (NewY > NumOfLines-1-StatusLine)
1448     NewY = NumOfLines-1-StatusLine;
1449     }
1450     MoveCursor(NewX,NewY);
1451     }
1452    
1453     void CSDeleteTabStop()
1454     {
1455     if (Param[1]==-1) Param[1] = 0;
1456     ClearTabStop(Param[1]);
1457     }
1458    
1459     void CS_h_Mode()
1460     {
1461     switch (Param[1]) {
1462     case 2: KeybEnabled = FALSE; break;
1463     case 4: InsertMode = TRUE; break;
1464     case 12:
1465     ts.LocalEcho = 0;
1466     if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1467     TelChangeEcho();
1468     break;
1469     case 20:
1470     LFMode = TRUE;
1471     ts.CRSend = IdCRLF;
1472     cv.CRSend = IdCRLF;
1473     break;
1474     }
1475     }
1476    
1477     void CS_i_Mode()
1478     {
1479     if (Param[1]==-1) Param[1] = 0;
1480     switch (Param[1]) {
1481     /* print screen */
1482     // PrintEX -- TRUE: print screen
1483     // FALSE: scroll region
1484     case 0: BuffPrint(! PrintEX); break;
1485     /* printer controller mode off */
1486     case 4: break; /* See PrnParseCS() */
1487     /* printer controller mode on */
1488     case 5:
1489     if (! AutoPrintMode)
1490     OpenPrnFile();
1491     DirectPrn = (ts.PrnDev[0]!=0);
1492     PrinterMode = TRUE;
1493     break;
1494     }
1495     }
1496    
1497     void CS_l_Mode()
1498     {
1499     switch (Param[1]) {
1500     case 2: KeybEnabled = TRUE; break;
1501     case 4: InsertMode = FALSE; break;
1502     case 12:
1503     ts.LocalEcho = 1;
1504     if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1505     TelChangeEcho();
1506     break;
1507     case 20:
1508     LFMode = FALSE;
1509     ts.CRSend = IdCR;
1510     cv.CRSend = IdCR;
1511     break;
1512     }
1513     }
1514    
1515     void CS_n_Mode()
1516     {
1517     char Report[16];
1518     int Y;
1519    
1520     switch (Param[1]) {
1521     case 5:
1522     if (Send8BitMode)
1523     CommBinaryOut(&cv,"\2330n",3); /* Device Status Report -> Ready */
1524     else
1525     CommBinaryOut(&cv,"\033[0n",4); /* Device Status Report -> Ready */
1526     break;
1527     case 6:
1528     /* Cursor Position Report */
1529     Y = CursorY+1;
1530     if ((StatusLine>0) &&
1531     (Y==NumOfLines))
1532     Y = 1;
1533     if (Send8BitMode)
1534     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\233%u;%uR", CLocale, Y, CursorX+1);
1535     else
1536     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[%u;%uR", CLocale, Y, CursorX+1);
1537     CommBinaryOut(&cv,Report,strlen(Report));
1538     break;
1539     }
1540     }
1541    
1542     void CSSetAttr()
1543     {
1544     int i, P;
1545    
1546     UpdateStr();
1547     for (i=1 ; i<=NParam ; i++)
1548     {
1549     P = Param[i];
1550     if (P<0) P = 0;
1551     switch (P) {
1552     case 0: /* Clear all */
1553     CharAttr = DefCharAttr;
1554     BuffSetCurCharAttr(CharAttr);
1555     break;
1556    
1557     case 1: /* Bold */
1558     CharAttr.Attr |= AttrBold;
1559     BuffSetCurCharAttr(CharAttr);
1560     break;
1561    
1562     case 4: /* Under line */
1563     CharAttr.Attr |= AttrUnder;
1564     BuffSetCurCharAttr(CharAttr);
1565     break;
1566    
1567     case 5: /* Blink */
1568     CharAttr.Attr |= AttrBlink;
1569     BuffSetCurCharAttr(CharAttr);
1570     break;
1571    
1572     case 7: /* Reverse */
1573     CharAttr.Attr |= AttrReverse;
1574     BuffSetCurCharAttr(CharAttr);
1575     break;
1576    
1577     case 22: /* Bold off */
1578     CharAttr.Attr &= ~ AttrBold;
1579     BuffSetCurCharAttr(CharAttr);
1580     break;
1581    
1582     case 24: /* Under line off */
1583     CharAttr.Attr &= ~ AttrUnder;
1584     BuffSetCurCharAttr(CharAttr);
1585     break;
1586    
1587     case 25: /* Blink off */
1588     CharAttr.Attr &= ~ AttrBlink;
1589     BuffSetCurCharAttr(CharAttr);
1590     break;
1591    
1592     case 27: /* Reverse off */
1593     CharAttr.Attr &= ~ AttrReverse;
1594     BuffSetCurCharAttr(CharAttr);
1595     break;
1596    
1597     case 30:
1598     case 31:
1599     case 32:
1600     case 33:
1601     case 34:
1602     case 35:
1603     case 36:
1604     case 37: /* text color */
1605     CharAttr.Attr2 |= Attr2Fore;
1606     CharAttr.Fore = P - 30;
1607     BuffSetCurCharAttr(CharAttr);
1608     break;
1609    
1610     case 38: /* text color (256color mode) */
1611     if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1612     i++;
1613     if (i < NParam) {
1614     P = Param[++i];
1615     if (P<0) {
1616     P = 0;
1617     }
1618     CharAttr.Attr2 |= Attr2Fore;
1619     CharAttr.Fore = P;
1620     BuffSetCurCharAttr(CharAttr);
1621     }
1622     }
1623     break;
1624    
1625     case 39: /* Reset text color */
1626     CharAttr.Attr2 &= ~ Attr2Fore;
1627     CharAttr.Fore = AttrDefaultFG;
1628     BuffSetCurCharAttr(CharAttr);
1629     break;
1630    
1631     case 40:
1632     case 41:
1633     case 42:
1634     case 43:
1635     case 44:
1636     case 45:
1637     case 46:
1638     case 47: /* Back color */
1639     CharAttr.Attr2 |= Attr2Back;
1640     CharAttr.Back = P - 40;
1641     BuffSetCurCharAttr(CharAttr);
1642     break;
1643    
1644     case 48: /* Back color (256color mode) */
1645     if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1646     i++;
1647     if (i < NParam) {
1648     P = Param[++i];
1649     if (P<0) {
1650     P = 0;
1651     }
1652     CharAttr.Attr2 |= Attr2Back;
1653     CharAttr.Back = P;
1654     BuffSetCurCharAttr(CharAttr);
1655     }
1656     }
1657     break;
1658    
1659     case 49: /* Reset back color */
1660     CharAttr.Attr2 &= ~ Attr2Back;
1661     CharAttr.Back = AttrDefaultBG;
1662     BuffSetCurCharAttr(CharAttr);
1663     break;
1664    
1665     case 90:
1666     case 91:
1667     case 92:
1668     case 93:
1669     case 94:
1670     case 95:
1671     case 96:
1672     case 97: /* aixterm style text color */
1673     if (ts.ColorFlag & CF_AIXTERM16) {
1674     CharAttr.Attr2 |= Attr2Fore;
1675     CharAttr.Fore = P - 90 + 8;
1676     BuffSetCurCharAttr(CharAttr);
1677     }
1678     break;
1679    
1680     case 100:
1681     if (! (ts.ColorFlag & CF_AIXTERM16)) {
1682     /* Reset text and back color */
1683     CharAttr.Attr2 &= ~ (Attr2Fore | Attr2Back);
1684     CharAttr.Fore = AttrDefaultFG;
1685     CharAttr.Back = AttrDefaultBG;
1686     BuffSetCurCharAttr(CharAttr);
1687     break;
1688     }
1689     /* fall through to aixterm style back color */
1690    
1691     case 101:
1692     case 102:
1693     case 103:
1694     case 104:
1695     case 105:
1696     case 106:
1697     case 107: /* aixterm style back color */
1698     if (ts.ColorFlag & CF_AIXTERM16) {
1699     CharAttr.Attr2 |= Attr2Back;
1700     CharAttr.Back = P - 100 + 8;
1701     BuffSetCurCharAttr(CharAttr);
1702     }
1703     break;
1704     }
1705     }
1706     }
1707    
1708     void CSSetScrollRegion()
1709     {
1710     if ((StatusLine>0) &&
1711     (CursorY==NumOfLines-1))
1712     {
1713     MoveCursor(0,CursorY);
1714     return;
1715     }
1716 doda 2484 if (Param[1]<1) Param[1] =1;
1717 maya 2476 if ((NParam < 2) | (Param[2]<1))
1718     Param[2] = NumOfLines-StatusLine;
1719     Param[1]--;
1720     Param[2]--;
1721     if (Param[1] > NumOfLines-1-StatusLine)
1722     Param[1] = NumOfLines-1-StatusLine;
1723     if (Param[2] > NumOfLines-1-StatusLine)
1724     Param[2] = NumOfLines-1-StatusLine;
1725     if (Param[1] >= Param[2]) return;
1726     CursorTop = Param[1];
1727     CursorBottom = Param[2];
1728     if (RelativeOrgMode) MoveCursor(0,CursorTop);
1729     else MoveCursor(0,0);
1730     }
1731    
1732     void CSSunSequence() /* Sun terminal private sequences */
1733     {
1734     char Report[16];
1735    
1736     switch (Param[1]) {
1737     case 8: /* set terminal size */
1738     if ((Param[2]<=1) || (NParam<2)) Param[2] = 24;
1739     if ((Param[3]<=1) || (NParam<3)) Param[3] = 80;
1740     ChangeTerminalSize(Param[3],Param[2]);
1741     break;
1742     case 14: /* get window size??? */
1743     /* this is not actual window size */
1744     if (Send8BitMode)
1745     CommBinaryOut(&cv,"\2334;640;480t",11);
1746     else
1747     CommBinaryOut(&cv,"\033[4;640;480t",12);
1748     break;
1749     case 18: /* get terminal size */
1750     if (Send8BitMode)
1751     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\2338;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1752     else
1753     _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[8;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1754     CommBinaryOut(&cv,Report,strlen(Report));
1755     break;
1756     }
1757     }
1758    
1759     void CSGT(BYTE b)
1760     {
1761     switch (b) {
1762     case 'c': /* second terminal report */
1763     if (Send8BitMode)
1764     CommBinaryOut(&cv,"\233>32;10;2c",11); /* VT382 */
1765     else
1766     CommBinaryOut(&cv,"\033[>32;10;2c",11); /* VT382 */
1767     break;
1768     case 'J':
1769     if (Param[1]==3) // IO-8256 terminal
1770     {
1771     if (Param[2]<1) Param[2]=1;
1772     if (Param[3]<1) Param[3]=1;
1773     if (Param[4]<1) Param[4]=1;
1774     if (Param[5]<1) Param[5]=1;
1775     BuffEraseBox(Param[3]-1,Param[2]-1,
1776     Param[5]-1,Param[4]-1);
1777     }
1778     break;
1779     case 'K':
1780     if ((NParam>=2) && (Param[1]==5))
1781     { // IO-8256 terminal
1782     switch (Param[2]) {
1783     case 3:
1784     case 4:
1785     case 5:
1786     case 6:
1787     BuffDrawLine(CharAttr, Param[2], Param[3]);
1788     break;
1789     case 12:
1790     /* Text color */
1791     if ((Param[3]>=0) && (Param[3]<=7))
1792     {
1793     switch (Param[3]) {
1794     case 3: CharAttr.Fore = IdBlue; break;
1795     case 4: CharAttr.Fore = IdCyan; break;
1796     case 5: CharAttr.Fore = IdYellow; break;
1797     case 6: CharAttr.Fore = IdMagenta; break;
1798     default: CharAttr.Fore = Param[3]; break;
1799     }
1800     CharAttr.Attr2 |= Attr2Fore;
1801     BuffSetCurCharAttr(CharAttr);
1802     }
1803     break;
1804     }
1805     }
1806     else if (Param[1]==3)
1807     {// IO-8256 terminal
1808     if (Param[2]<1) Param[2] = 1;
1809     if (Param[3]<1) Param[2] = 1;
1810     BuffEraseCharsInLine(Param[2]-1,Param[3]-Param[2]+1);
1811     }
1812     break;
1813     }
1814     }
1815    
1816     void CSQExchangeColor()
1817     {
1818     COLORREF ColorRef;
1819    
1820     BuffUpdateScroll();
1821    
1822     ColorRef = ts.VTColor[0];
1823     ts.VTColor[0] = ts.VTColor[1];
1824     ts.VTColor[1] = ColorRef;
1825 doda 2562
1826     ColorRef = ts.VTBoldColor[0];
1827     ts.VTBoldColor[0] = ts.VTBoldColor[1];
1828     ts.VTBoldColor[1] = ColorRef;
1829    
1830     ColorRef = ts.VTBlinkColor[0];
1831     ts.VTBlinkColor[0] = ts.VTBlinkColor[1];
1832     ts.VTBlinkColor[1] = ColorRef;
1833    
1834     ColorRef = ts.URLColor[0];
1835     ts.URLColor[0] = ts.URLColor[1];
1836     ts.URLColor[1] = ColorRef;
1837    
1838 maya 2476 #ifdef ALPHABLEND_TYPE2
1839     BGInitialize();
1840     #endif
1841     DispChangeBackground();
1842 doda 2554 UpdateWindow(HVTWin);
1843 maya 2476 }
1844    
1845     void CSQ_h_Mode()
1846     {
1847     int i;
1848    
1849     for (i = 1 ; i<=NParam ; i++)
1850     switch (Param[i]) {
1851     case 1: AppliCursorMode = TRUE; break;
1852     case 3:
1853     ChangeTerminalSize(132,NumOfLines-StatusLine);
1854     break;
1855     case 5:
1856     if (ReverseColor) return;
1857     ReverseColor = TRUE;
1858     /* Exchange text/back color */
1859     CSQExchangeColor();
1860     break;
1861     case 6:
1862     if ((StatusLine>0) &&
1863     (CursorY==NumOfLines-1))
1864     MoveCursor(0,CursorY);
1865     else {
1866     RelativeOrgMode = TRUE;
1867     MoveCursor(0,CursorTop);
1868     }
1869     break;
1870     case 7: AutoWrapMode = TRUE; break;
1871     case 8: AutoRepeatMode = TRUE; break;
1872     case 9:
1873     if (ts.MouseEventTracking)
1874     MouseReportMode = IdMouseTrackX10;
1875     break;
1876     case 19: PrintEX = TRUE; break;
1877     case 25: DispEnableCaret(TRUE); break; // cursor on
1878     case 38:
1879     if (ts.AutoWinSwitch>0)
1880     ChangeEmu = IdTEK; /* Enter TEK Mode */
1881     break;
1882     case 59:
1883     if (ts.Language==IdJapanese)
1884     { /* kanji terminal */
1885     Gn[0] = IdASCII;
1886     Gn[1] = IdKatakana;
1887     Gn[2] = IdKatakana;
1888     Gn[3] = IdKanji;
1889     Glr[0] = 0;
1890     if ((ts.KanjiCode==IdJIS) &&
1891     (ts.JIS7Katakana==0))
1892     Glr[1] = 2; // 8-bit katakana
1893     else
1894     Glr[1] = 3;
1895     }
1896     break;
1897     case 66: AppliKeyMode = TRUE; break;
1898     case 67: ts.BSKey = IdBS; break;
1899     case 1000:
1900     if (ts.MouseEventTracking)
1901     MouseReportMode = IdMouseTrackVT200;
1902     break;
1903     case 1001:
1904     if (ts.MouseEventTracking)
1905     MouseReportMode = IdMouseTrackVT200Hl;
1906     break;
1907     case 1002:
1908     if (ts.MouseEventTracking)
1909     MouseReportMode = IdMouseTrackBtnEvent;
1910     break;
1911     case 1003:
1912     if (ts.MouseEventTracking)
1913     MouseReportMode = IdMouseTrackAllEvent;
1914     break;
1915     case 1004:
1916     if (ts.MouseEventTracking)
1917     FocusReportMode = TRUE;
1918     break;
1919     }
1920     }
1921    
1922     void CSQ_i_Mode()
1923     {
1924     if (Param[1]==-1) Param[1] = 0;
1925     switch (Param[1]) {
1926     case 1:
1927     OpenPrnFile();
1928     BuffDumpCurrentLine(LF);
1929     if (! AutoPrintMode)
1930     ClosePrnFile();
1931     break;
1932     /* auto print mode off */
1933     case 4:
1934     if (AutoPrintMode)
1935     {
1936     ClosePrnFile();
1937     AutoPrintMode = FALSE;
1938     }
1939     break;
1940     /* auto print mode on */
1941     case 5:
1942     if (! AutoPrintMode)
1943     {
1944     OpenPrnFile();
1945     AutoPrintMode = TRUE;
1946     }
1947     break;
1948     }
1949     }
1950    
1951     void CSQ_l_Mode()
1952     {
1953     int i;
1954    
1955     for (i = 1 ; i <= NParam ; i++)
1956     switch (Param[i]) {
1957     case 1: AppliCursorMode = FALSE; break;
1958     case 3:
1959     ChangeTerminalSize(80,NumOfLines-StatusLine);
1960     break;
1961     case 5:
1962     if (! ReverseColor) return;
1963     ReverseColor = FALSE;
1964     /* Exchange text/back color */
1965     CSQExchangeColor();
1966     break;
1967     case 6:
1968     if ((StatusLine>0) &&
1969     (CursorY==NumOfLines-1))
1970     MoveCursor(0,CursorY);
1971     else {
1972     RelativeOrgMode = FALSE;
1973     MoveCursor(0,0);
1974     }
1975     break;
1976     case 7: AutoWrapMode = FALSE; break;
1977     case 8: AutoRepeatMode = FALSE; break;
1978     case 9: MouseReportMode = IdMouseTrackNone; break;
1979     case 19: PrintEX = FALSE; break;
1980     case 25: DispEnableCaret(FALSE); break; // cursor off
1981     case 59:
1982     if (ts.Language==IdJapanese)
1983     { /* katakana terminal */
1984     Gn[0] = IdASCII;
1985     Gn[1] = IdKatakana;
1986     Gn[2] = IdKatakana;
1987     Gn[3] = IdKanji;
1988     Glr[0] = 0;
1989     if ((ts.KanjiCode==IdJIS) &&
1990     (ts.JIS7Katakana==0))
1991     Glr[1] = 2; // 8-bit katakana
1992     else
1993     Glr[1] = 3;
1994     }
1995     break;
1996     case 66: AppliKeyMode = FALSE; break;
1997     case 67: ts.BSKey = IdDEL; break;
1998     case 1000:
1999     case 1001:
2000     case 1002:
2001     case 1003: MouseReportMode = IdMouseTrackNone; break;
2002     case 1004: FocusReportMode = FALSE; break;
2003     }
2004     }
2005    
2006     void CSQ_n_Mode()
2007     {
2008     }
2009    
2010     void CSQuest(BYTE b)
2011     {
2012     switch (b) {
2013 doda 2484 case 'K': CSLineErase(); break;
2014 maya 2476 case 'h': CSQ_h_Mode(); break;
2015     case 'i': CSQ_i_Mode(); break;
2016     case 'l': CSQ_l_Mode(); break;
2017     case 'n': CSQ_n_Mode(); break;
2018     }
2019     }
2020    
2021     void SoftReset()
2022     // called by software-reset escape sequence handler
2023     {
2024     UpdateStr();
2025     AutoRepeatMode = TRUE;
2026     DispEnableCaret(TRUE); // cursor on
2027     InsertMode = FALSE;
2028     RelativeOrgMode = FALSE;
2029     AppliKeyMode = FALSE;
2030     AppliCursorMode = FALSE;
2031     if ((StatusLine>0) &&
2032     (CursorY == NumOfLines-1))
2033     MoveToMainScreen();
2034     CursorTop = 0;
2035     CursorBottom = NumOfLines-1-StatusLine;
2036     ResetCharSet();
2037    
2038     Send8BitMode = ts.Send8BitCtrl;
2039    
2040     /* Attribute */
2041     CharAttr = DefCharAttr;
2042     Special = FALSE;
2043     BuffSetCurCharAttr(CharAttr);
2044    
2045     // status buffers
2046     ResetSBuffers();
2047     }
2048    
2049     void CSExc(BYTE b)
2050     {
2051     switch (b) {
2052     case 'p':
2053     /* Software reset */
2054     SoftReset();
2055     break;
2056     }
2057     }
2058    
2059     void CSDouble(BYTE b)
2060     {
2061     switch (b) {
2062     case 'p':
2063     /* Select terminal mode (software reset) */
2064     SoftReset();
2065     if (NParam > 0) {
2066     switch (Param[1]) {
2067     case 61: // VT100 Mode
2068     Send8BitMode = FALSE; break;
2069     case 62: // VT200 Mode
2070     case 63: // VT300 Mode
2071     case 64: // VT400 Mode
2072     if (NParam > 1 && Param[2] == 1)
2073     Send8BitMode = FALSE;
2074     else
2075     Send8BitMode = TRUE;
2076     break;
2077     }
2078     }
2079     break;
2080     }
2081     }
2082    
2083     void CSDol(BYTE b)
2084     {
2085     switch (b) {
2086     case '}':
2087     if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2088     if (StatusLine==0) return;
2089     if ((Param[1]<1) && (CursorY==NumOfLines-1))
2090     MoveToMainScreen();
2091     else if ((Param[1]==1) && (CursorY<NumOfLines-1))
2092     MoveToStatusLine();
2093     break;
2094     case '~':
2095     if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2096     if (Param[1]<=1)
2097     HideStatusLine();
2098     else if ((StatusLine==0) && (Param[1]==2))
2099     ShowStatusLine(1); // show
2100     break;
2101     }
2102     }
2103    
2104     void PrnParseCS(BYTE b) // printer mode
2105     {
2106     ParseMode = ModeFirst;
2107     switch (ICount) {
2108     /* no intermediate char */
2109     case 0:
2110     switch (Prv) {
2111     /* no private parameter */
2112     case 0:
2113     switch (b) {
2114     case 'i':
2115     if (Param[1]==4)
2116     {
2117     PrinterMode = FALSE;
2118     // clear prn buff
2119     WriteToPrnFile(0,FALSE);
2120     if (! AutoPrintMode)
2121     ClosePrnFile();
2122     return;
2123     }
2124     break;
2125     } /* of case Prv=0 */
2126     break;
2127     }
2128     break;
2129     /* one intermediate char */
2130     case 1: break;
2131     } /* of case Icount */
2132    
2133     WriteToPrnFile(b,TRUE);
2134     }
2135    
2136     void ParseCS(BYTE b) /* b is the final char */
2137     {
2138     if (PrinterMode) { // printer mode
2139     PrnParseCS(b);
2140     return;
2141     }
2142    
2143     switch (ICount) {
2144     /* no intermediate char */
2145     case 0:
2146     switch (Prv) {
2147     /* no private parameter */
2148     case 0:
2149     switch (b) {
2150     case '@': CSInsertCharacter(); break;
2151     case 'A': CSCursorUp(); break;
2152     case 'B': CSCursorDown(); break;
2153     case 'C': CSCursorRight(); break;
2154     case 'D': CSCursorLeft(); break;
2155     case 'E': CSCursorDown1(); break;
2156     case 'F': CSCursorUp1(); break;
2157     case 'G': CSMoveToColumnN(); break;
2158     case 'H': CSMoveToXY(); break;
2159 doda 2553 case 'I': CSForwardTab(); break; // CHT
2160 maya 2476 case 'J': CSScreenErase(); break;
2161     case 'K': CSLineErase(); break;
2162     case 'L': CSInsertLine(); break;
2163     case 'M': CSDeleteNLines(); break;
2164     case 'P': CSDeleteCharacter(); break;
2165     case 'S': CSScrollUP(); break; // SU
2166     case 'T': CSScrollDown(); break; // SD
2167     case 'X': CSEraseCharacter(); break;
2168 doda 2553 case 'Z': CSBackwardTab(); break; // CBT
2169 maya 2476 case '`': CSMoveToColumnN(); break;
2170     case 'a': CSCursorRight(); break;
2171     case 'c': AnswerTerminalType(); break;
2172     case 'd': CSMoveToLineN(); break;
2173     case 'e': CSCursorUp(); break;
2174     case 'f': CSMoveToXY(); break;
2175     case 'g': CSDeleteTabStop(); break;
2176     case 'h': CS_h_Mode(); break;
2177     case 'i': CS_i_Mode(); break;
2178     case 'l': CS_l_Mode(); break;
2179     case 'm': CSSetAttr(); break;
2180     case 'n': CS_n_Mode(); break;
2181     case 'r': CSSetScrollRegion(); break;
2182     case 's': SaveCursor(); break;
2183     case 't': CSSunSequence(); break;
2184     case 'u': RestoreCursor(); break;
2185     } /* of case Prv=0 */
2186     break;
2187     /* private parameter = '>' */
2188     case '>': CSGT(b); break;
2189     /* private parameter = '?' */
2190     case '?': CSQuest(b); break;
2191     }
2192     break;
2193     /* one intermediate char */
2194     case 1:
2195     switch (IntChar[1]) {
2196     /* intermediate char = '!' */
2197     case '!': CSExc(b); break;
2198     /* intermediate char = '"' */
2199     case '"': CSDouble(b); break;
2200     /* intermediate char = '$' */
2201     case '$': CSDol(b); break;
2202     }
2203     break;
2204     } /* of case Icount */
2205    
2206     ParseMode = ModeFirst;
2207     }
2208    
2209     void ControlSequence(BYTE b)
2210     {
2211     if ((b<=US) || (b>=0x80) && (b<=0x9F))
2212     ParseControl(b); /* ctrl char */
2213     else if ((b>=0x40) && (b<=0x7E))
2214     ParseCS(b); /* terminate char */
2215     else {
2216     if (PrinterMode)
2217     WriteToPrnFile(b,FALSE);
2218    
2219     if ((b>=0x20) && (b<=0x2F))
2220     { /* intermediate char */
2221     if (ICount<IntCharMax) ICount++;
2222     IntChar[ICount] = b;
2223     }
2224     else if ((b>=0x30) && (b<=0x39))
2225     {
2226     if (Param[NParam] < 0)
2227 doda 2484 Param[NParam] = 0;
2228 maya 2476 if (Param[NParam]<1000)
2229     Param[NParam] = Param[NParam]*10 + b - 0x30;
2230     }
2231     else if (b==0x3B)
2232     {
2233     if (NParam < NParamMax)
2234     {
2235     NParam++;
2236     Param[NParam] = -1;
2237     }
2238     }
2239     else if ((b>=0x3C) && (b<=0x3F))
2240     { /* private char */
2241     if (FirstPrm) Prv = b;
2242     }
2243     }
2244     FirstPrm = FALSE;
2245     }
2246    
2247     void DeviceControl(BYTE b)
2248     {
2249 doda 2528 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2250 maya 2476 {
2251     ESCFlag = FALSE;
2252     ParseMode = SavedMode;
2253     return;
2254     }
2255    
2256     if (b==ESC)
2257     {
2258     ESCFlag = TRUE;
2259     return;
2260     }
2261     else ESCFlag = FALSE;
2262    
2263 doda 2487 if (b<=US)
2264 maya 2476 ParseControl(b);
2265     else if ((b>=0x30) && (b<=0x39))
2266     {
2267 doda 2484 if (Param[NParam] < 0) Param[NParam] = 0;
2268 maya 2476 if (Param[NParam]<1000)
2269     Param[NParam] = Param[NParam]*10 + b - 0x30;
2270     }
2271     else if (b==0x3B)
2272     {
2273     if (NParam < NParamMax)
2274     {
2275     NParam++;
2276     Param[NParam] = -1;
2277     }
2278     }
2279     else if ((b>=0x40) && (b<=0x7E))
2280     {
2281     if (b=='|')
2282     {
2283     ParseMode = ModeDCUserKey;
2284     if (Param[1] < 1) ClearUserKey();
2285     WaitKeyId = TRUE;
2286     NewKeyId = 0;
2287     }
2288     else ParseMode = ModeSOS;
2289     }
2290     }
2291    
2292     void DCUserKey(BYTE b)
2293     {
2294 doda 2528 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2295 maya 2476 {
2296     if (! WaitKeyId) DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2297     ESCFlag = FALSE;
2298     ParseMode = SavedMode;
2299     return;
2300     }
2301    
2302     if (b==ESC)
2303     {
2304     ESCFlag = TRUE;
2305     return;
2306     }
2307     else ESCFlag = FALSE;
2308    
2309     if (WaitKeyId)
2310     {
2311     if ((b>=0x30) && (b<=0x39))
2312     {
2313     if (NewKeyId<1000)
2314     NewKeyId = NewKeyId*10 + b - 0x30;
2315     }
2316     else if (b==0x2F)
2317     {
2318     WaitKeyId = FALSE;
2319     WaitHi = TRUE;
2320     NewKeyLen = 0;
2321     }
2322     }
2323     else {
2324     if (b==0x3B)
2325     {
2326     DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2327     WaitKeyId = TRUE;
2328     NewKeyId = 0;
2329     }
2330     else {
2331     if (NewKeyLen < FuncKeyStrMax)
2332     {
2333     if (WaitHi)
2334     {
2335     NewKeyStr[NewKeyLen] = ConvHexChar(b) << 4;
2336     WaitHi = FALSE;
2337     }
2338     else {
2339     NewKeyStr[NewKeyLen] = NewKeyStr[NewKeyLen] +
2340     ConvHexChar(b);
2341     WaitHi = TRUE;
2342     NewKeyLen++;
2343     }
2344     }
2345     }
2346     }
2347     }
2348    
2349     void IgnoreString(BYTE b)
2350     {
2351 doda 2528 if ((ESCFlag && (b=='\\')) ||
2352     (b<=US && b!=ESC && b!=HT) ||
2353     (b==ST && ts.KanjiCode!=IdSJIS))
2354 maya 2476 ParseMode = SavedMode;
2355    
2356     if (b==ESC) ESCFlag = TRUE;
2357     else ESCFlag = FALSE;
2358     }
2359    
2360     BOOL XsParseColor(char *colspec, COLORREF *color)
2361     {
2362 doda 2486 unsigned int r, g, b;
2363 doda 2484 // double dr, dg, db;
2364 maya 2476
2365 doda 2484 r = g = b = 255;
2366 maya 2476
2367 doda 2484 if (colspec == NULL || color == NULL) {
2368     return FALSE;
2369     }
2370 maya 2476
2371 doda 2484 if (_strnicmp(colspec, "rgb:", 4) == 0) {
2372     switch (strlen(colspec)) {
2373     case 9: // rgb:R/G/B
2374     if (sscanf(colspec, "rgb:%1x/%1x/%1x", &r, &g, &b) != 3) {
2375     return FALSE;
2376     }
2377     r *= 17; g *= 17; b *= 17;
2378     break;
2379     case 12: // rgb:RR/GG/BB
2380     if (sscanf(colspec, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {
2381     return FALSE;
2382     }
2383     break;
2384     case 15: // rgb:RRR/GGG/BBB
2385     if (sscanf(colspec, "rgb:%3x/%3x/%3x", &r, &g, &b) != 3) {
2386     return FALSE;
2387     }
2388     r >>= 4; g >>= 4; b >>= 4;
2389     break;
2390     case 18: // rgb:RRRR/GGGG/BBBB
2391     if (sscanf(colspec, "rgb:%4x/%4x/%4x", &r, &g, &b) != 3) {
2392     return FALSE;
2393     }
2394     r >>= 8; g >>= 8; b >>= 8;
2395     break;
2396     default:
2397     return FALSE;
2398     }
2399 maya 2476 }
2400 doda 2484 // else if (_strnicmp(colspec, "rgbi:", 5) == 0) {
2401     // ; /* nothing to do */
2402     // }
2403     else if (colspec[0] == '#') {
2404     switch (strlen(colspec)) {
2405     case 4: // #RGB
2406     if (sscanf(colspec, "#%1x%1x%1x", &r, &g, &b) != 3) {
2407     return FALSE;
2408     }
2409     r <<= 4; g <<= 4; b <<= 4;
2410     break;
2411     case 7: // #RRGGBB
2412     if (sscanf(colspec, "#%2x%2x%2x", &r, &g, &b) != 3) {
2413     return FALSE;
2414     }
2415     break;
2416     case 10: // #RRRGGGBBB
2417     if (sscanf(colspec, "#%3x%3x%3x", &r, &g, &b) != 3) {
2418     return FALSE;
2419     }
2420     r >>= 4; g >>= 4; b >>= 4;
2421     break;
2422     case 13: // #RRRRGGGGBBBB
2423     if (sscanf(colspec, "#%4x%4x%4x", &r, &g, &b) != 3) {
2424     return FALSE;
2425     }
2426     r >>= 8; g >>= 8; b >>= 8;
2427     break;
2428     default:
2429     return FALSE;
2430     }
2431 maya 2476 }
2432 doda 2484 else {
2433     return FALSE;
2434 maya 2476 }
2435 doda 2484
2436 doda 2486 if (r > 255 || g > 255 || b > 255) {
2437 doda 2484 return FALSE;
2438 maya 2476 }
2439    
2440 doda 2484 *color = RGB(r, g, b);
2441     return TRUE;
2442 maya 2476 }
2443    
2444     #define ModeXsFirst 1
2445     #define ModeXsString 2
2446     #define ModeXsColorNum 3
2447     #define ModeXsColorSpec 4
2448     #define ModeXsEsc 5
2449     void XSequence(BYTE b)
2450     {
2451 doda 2484 static BYTE XsParseMode = ModeXsFirst, PrevMode;
2452     static char StrBuff[sizeof(ts.Title)];
2453 doda 2486 static unsigned int ColorNumber, StrLen;
2454 doda 2484 COLORREF color;
2455 maya 2476
2456 doda 2484 switch (XsParseMode) {
2457     case ModeXsFirst:
2458     if (isdigit(b)) {
2459     if (Param[1] < 1000) {
2460     Param[1] = Param[1]*10 + b - '0';
2461     }
2462     }
2463     else if (b == ';') {
2464     StrBuff[0] = '\0';
2465     StrLen = 0;
2466     if (Param[1] == 4) {
2467     ColorNumber = 0;
2468     XsParseMode = ModeXsColorNum;
2469     }
2470     else {
2471     XsParseMode = ModeXsString;
2472     }
2473     }
2474     else {
2475     ParseMode = ModeFirst;
2476     }
2477     break;
2478     case ModeXsString:
2479 doda 2528 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2480 doda 2484 StrBuff[StrLen] = '\0';
2481     switch (Param[1]) {
2482     case 0: /* Change window title and icon name */
2483     case 1: /* Change icon name */
2484     case 2: /* Change window title */
2485 maya 2588 if (ts.AcceptTitleChangeRequest) {
2486 doda 2558 strncpy_s(ts.Title, sizeof(ts.Title), StrBuff, _TRUNCATE);
2487     // (2006.6.15 maya) �^�C�g�����n����������SJIS������
2488     ConvertToCP932(ts.Title, sizeof(ts.Title));
2489     ChangeTitle();
2490     }
2491 doda 2484 break;
2492     default:
2493     /* nothing to do */;
2494     }
2495     ParseMode = ModeFirst;
2496     XsParseMode = ModeXsFirst;
2497     }
2498     else if (b == ESC) { /* Escape */
2499     PrevMode = ModeXsString;
2500     XsParseMode = ModeXsEsc;
2501     }
2502     else if (b <= US) { /* Other control character -- invalid sequence */
2503     ParseMode = ModeFirst;
2504     XsParseMode = ModeXsFirst;
2505     }
2506     else if (StrLen < sizeof(StrBuff) - 1) {
2507     StrBuff[StrLen++] = b;
2508     }
2509     break;
2510     case ModeXsColorNum:
2511     if (isdigit(b)) {
2512     ColorNumber = ColorNumber*10 + b - '0';
2513     }
2514     else if (b == ';') {
2515     XsParseMode = ModeXsColorSpec;
2516     StrBuff[0] = '\0';
2517     StrLen = 0;
2518     }
2519     else {
2520     ParseMode = ModeFirst;
2521     XsParseMode = ModeXsFirst;
2522     }
2523     break;
2524     case ModeXsColorSpec:
2525 doda 2528 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2526 doda 2484 StrBuff[StrLen] = '\0';
2527 doda 2486 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2528 doda 2484 if (strcmp(StrBuff, "?") == 0) {
2529     color = DispGetANSIColor(ColorNumber);
2530     if (Send8BitMode) {
2531     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2532     "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2533     GetRValue(color), GetGValue(color), GetBValue(color));
2534     }
2535     else {
2536     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2537     "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2538     GetRValue(color), GetGValue(color), GetBValue(color));
2539     }
2540     ParseMode = ModeFirst;
2541     XsParseMode = ModeXsFirst;
2542     CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2543     break;
2544     }
2545     else if (XsParseColor(StrBuff, &color)) {
2546     DispSetANSIColor(ColorNumber, color);
2547     }
2548     }
2549     ParseMode = ModeFirst;
2550     XsParseMode = ModeXsFirst;
2551     }
2552     else if (b == ESC) {
2553     PrevMode = ModeXsColorSpec;
2554     XsParseMode = ModeXsEsc;
2555     }
2556     else if (b <= US) { /* Other control character -- invalid sequence */
2557     ParseMode = ModeFirst;
2558     XsParseMode = ModeXsFirst;
2559     }
2560     else if (b == ';') {
2561 doda 2486 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2562 doda 2484 if (strcmp(StrBuff, "?") == 0) {
2563     color = DispGetANSIColor(ColorNumber);
2564     if (Send8BitMode) {
2565     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2566     "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2567     GetRValue(color), GetGValue(color), GetBValue(color));
2568     }
2569     else {
2570     _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2571     "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2572     GetRValue(color), GetGValue(color), GetBValue(color));
2573     }
2574     XsParseMode = ModeXsColorNum;
2575     CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2576     }
2577     else if (XsParseColor(StrBuff, &color)) {
2578     DispSetANSIColor(ColorNumber, color);
2579     }
2580     }
2581     ColorNumber = 0;
2582     StrBuff[0] = '\0';
2583     StrLen = 0;
2584     XsParseMode = ModeXsColorNum;
2585     }
2586     else if (StrLen < sizeof(StrBuff) - 1) {
2587     StrBuff[StrLen++] = b;
2588     }
2589     break;
2590     case ModeXsEsc:
2591     if (b == '\\') { /* String Terminator */
2592     XsParseMode = PrevMode;
2593 doda 2528 // XSequence(ST);
2594     XSequence(BEL);
2595 doda 2484 }
2596     else { /* Other character -- invalid sequence */
2597     ParseMode = ModeFirst;
2598     XsParseMode = ModeXsFirst;
2599     }
2600     break;
2601     // default:
2602     // ParseMode = ModeFirst;
2603     // XsParseMode = ModeXsFirst;
2604 maya 2476 }
2605     }
2606    
2607     void DLESeen(BYTE b)
2608     {
2609     ParseMode = ModeFirst;
2610     if (((ts.FTFlag & FT_BPAUTO)!=0) && (b=='B'))
2611     BPStart(IdBPAuto); /* Auto B-Plus activation */
2612     ChangeEmu = -1;
2613     }
2614    
2615     void CANSeen(BYTE b)
2616     {