Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


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