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 2492 - (hide annotations) (download) (as text)
Sun Apr 27 10:18:52 2008 UTC (15 years, 11 months ago) by doda
Original Path: teraterm/trunk/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 68287 byte(s)
Mouse Tracking Modeで不正な位置を報告する事があるのを修正した。

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