Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3464 - (show annotations) (download) (as text)
Wed Jun 10 14:22:14 2009 UTC (14 years, 10 months ago) by doda
File MIME type: text/x-csrc
File size: 79515 byte(s)
ウィンドウサイズ変更シーケンス(ピクセル単位)に対応。
  <CSI> 4 ; H ; W t
     H: ウィンドウの高さ
     W: ウィンドウの横幅

1 /* 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 #define MAPSIZE(x) (sizeof(x)/sizeof((x)[0]))
29
30 /* Parsing modes */
31 #define ModeFirst 0
32 #define ModeESC 1
33 #define ModeDCS 2
34 #define ModeDCUserKey 3
35 #define ModeSOS 4
36 #define ModeCSI 5
37 #define ModeXS 6
38 #define ModeDLE 7
39 #define ModeCAN 8
40
41 #define NParamMax 16
42 #define IntCharMax 5
43
44 void VisualBell();
45
46 /* character attribute */
47 static TCharAttr CharAttr;
48
49 /* various modes of VT emulation */
50 static BOOL RelativeOrgMode;
51 static BOOL InsertMode;
52 static BOOL LFMode;
53 static BOOL AutoWrapMode;
54 static BOOL FocusReportMode;
55 int MouseReportMode;
56
57 // save/restore cursor
58 typedef struct {
59 int CursorX, CursorY;
60 TCharAttr Attr;
61 int Glr[2], Gn[4]; // G0-G3, GL & GR
62 BOOL AutoWrapMode;
63 BOOL RelativeOrgMode;
64 } TStatusBuff;
65 typedef TStatusBuff *PStatusBuff;
66
67 // status buffer for main screen & status line
68 static TStatusBuff SBuff1, SBuff2;
69
70 static BOOL ESCFlag, JustAfterESC;
71 static BOOL KanjiIn;
72 static BOOL EUCkanaIn, EUCsupIn;
73 static int EUCcount;
74 static BOOL Special;
75
76 static int Param[NParamMax+1];
77 static int NParam;
78 static BOOL FirstPrm;
79 static BYTE IntChar[IntCharMax+1];
80 static int ICount;
81 static BYTE Prv;
82 static int ParseMode, SavedMode;
83 static int ChangeEmu;
84
85 /* user defined keys */
86 static BOOL WaitKeyId, WaitHi;
87
88 /* GL, GR code group */
89 static int Glr[2];
90 /* G0, G1, G2, G3 code group */
91 static int Gn[4];
92 /* GL for single shift 2/3 */
93 static int GLtmp;
94 /* single shift 2/3 flag */
95 static BOOL SSflag;
96 /* JIS -> SJIS conversion flag */
97 static BOOL ConvJIS;
98 static WORD Kanji;
99
100 // variables for status line mode
101 static int StatusX=0;
102 static BOOL StatusWrap=FALSE;
103 static BOOL StatusCursor=TRUE;
104 static int MainX, MainY; //cursor registers
105 static int MainTop, MainBottom; // scroll region registers
106 static BOOL MainWrap;
107 static BOOL MainCursor=TRUE;
108
109 /* status for printer escape sequences */
110 static BOOL PrintEX = TRUE; // printing extent
111 // (TRUE: screen, FALSE: scroll region)
112 static BOOL AutoPrintMode = FALSE;
113 static BOOL PrinterMode = FALSE;
114 static BOOL DirectPrn = FALSE;
115
116 /* User key */
117 static BYTE NewKeyStr[FuncKeyStrMax];
118 static int NewKeyId, NewKeyLen;
119
120 static _locale_t CLocale = NULL;
121
122 void ResetSBuffers()
123 {
124 SBuff1.CursorX = 0;
125 SBuff1.CursorY = 0;
126 SBuff1.Attr = DefCharAttr;
127 if (ts.Language==IdJapanese)
128 {
129 SBuff1.Gn[0] = IdASCII;
130 SBuff1.Gn[1] = IdKatakana;
131 SBuff1.Gn[2] = IdKatakana;
132 SBuff1.Gn[3] = IdKanji;
133 SBuff1.Glr[0] = 0;
134 if ((ts.KanjiCode==IdJIS) &&
135 (ts.JIS7Katakana==0))
136 SBuff1.Glr[1] = 2; // 8-bit katakana
137 else
138 SBuff1.Glr[1] = 3;
139 }
140 else {
141 SBuff1.Gn[0] = IdASCII;
142 SBuff1.Gn[1] = IdSpecial;
143 SBuff1.Gn[2] = IdASCII;
144 SBuff1.Gn[3] = IdASCII;
145 SBuff1.Glr[0] = 0;
146 SBuff1.Glr[1] = 0;
147 }
148 SBuff1.AutoWrapMode = TRUE;
149 SBuff1.RelativeOrgMode = FALSE;
150 // copy SBuff1 to SBuff2
151 SBuff2 = SBuff1;
152 }
153
154 void ResetTerminal() /*reset variables but don't update screen */
155 {
156 DispReset();
157 BuffReset();
158
159 /* Attribute */
160 CharAttr = DefCharAttr;
161 Special = FALSE;
162 BuffSetCurCharAttr(CharAttr);
163
164 /* Various modes */
165 InsertMode = FALSE;
166 LFMode = (ts.CRSend == IdCRLF);
167 AutoWrapMode = TRUE;
168 AppliKeyMode = FALSE;
169 AppliCursorMode = FALSE;
170 RelativeOrgMode = FALSE;
171 ts.ColorFlag &= ~CF_REVERSEVIDEO;
172 AutoRepeatMode = TRUE;
173 Send8BitMode = ts.Send8BitCtrl;
174 FocusReportMode = FALSE;
175 MouseReportMode = IdMouseTrackNone;
176
177 if (CLocale == NULL) {
178 CLocale = _create_locale(LC_ALL, "C");
179 }
180
181 /* Character sets */
182 ResetCharSet();
183
184 /* ESC flag for device control sequence */
185 ESCFlag = FALSE;
186 /* for TEK sequence */
187 JustAfterESC = FALSE;
188
189 /* Parse mode */
190 ParseMode = ModeFirst;
191
192 /* Clear printer mode */
193 PrinterMode = FALSE;
194
195 // status buffers
196 ResetSBuffers();
197 }
198
199 void ResetCharSet()
200 {
201 if (ts.Language==IdJapanese)
202 {
203 Gn[0] = IdASCII;
204 Gn[1] = IdKatakana;
205 Gn[2] = IdKatakana;
206 Gn[3] = IdKanji;
207 Glr[0] = 0;
208 if ((ts.KanjiCode==IdJIS) &&
209 (ts.JIS7Katakana==0))
210 Glr[1] = 2; // 8-bit katakana
211 else
212 Glr[1] = 3;
213 }
214 else {
215 Gn[0] = IdASCII;
216 Gn[1] = IdSpecial;
217 Gn[2] = IdASCII;
218 Gn[3] = IdASCII;
219 Glr[0] = 0;
220 Glr[1] = 0;
221 cv.SendCode = IdASCII;
222 cv.SendKanjiFlag = FALSE;
223 cv.EchoCode = IdASCII;
224 cv.EchoKanjiFlag = FALSE;
225 }
226 /* Kanji flag */
227 KanjiIn = FALSE;
228 EUCkanaIn = FALSE;
229 EUCsupIn = FALSE;
230 SSflag = FALSE;
231
232 cv.Language = ts.Language;
233 cv.CRSend = ts.CRSend;
234 cv.KanjiCodeEcho = ts.KanjiCode;
235 cv.JIS7KatakanaEcho = ts.JIS7Katakana;
236 cv.KanjiCodeSend = ts.KanjiCodeSend;
237 cv.JIS7KatakanaSend = ts.JIS7KatakanaSend;
238 cv.KanjiIn = ts.KanjiIn;
239 cv.KanjiOut = ts.KanjiOut;
240 }
241
242 void ResetKeypadMode(BOOL DisabledModeOnly)
243 {
244 if (!DisabledModeOnly || ts.DisableAppKeypad) AppliKeyMode = FALSE;
245 if (!DisabledModeOnly || ts.DisableAppCursor) AppliCursorMode = FALSE;
246 }
247
248 void MoveToMainScreen()
249 {
250 StatusX = CursorX;
251 StatusWrap = Wrap;
252 StatusCursor = IsCaretEnabled();
253
254 CursorTop = MainTop;
255 CursorBottom = MainBottom;
256 Wrap = MainWrap;
257 DispEnableCaret(MainCursor);
258 MoveCursor(MainX,MainY); // move to main screen
259 }
260
261 void MoveToStatusLine()
262 {
263 MainX = CursorX;
264 MainY = CursorY;
265 MainTop = CursorTop;
266 MainBottom = CursorBottom;
267 MainWrap = Wrap;
268 MainCursor = IsCaretEnabled();
269
270 DispEnableCaret(StatusCursor);
271 MoveCursor(StatusX,NumOfLines-1); // move to status line
272 CursorTop = NumOfLines-1;
273 CursorBottom = CursorTop;
274 Wrap = StatusWrap;
275 }
276
277 void HideStatusLine()
278 {
279 if ((StatusLine>0) &&
280 (CursorY==NumOfLines-1))
281 MoveToMainScreen();
282 StatusX = 0;
283 StatusWrap = FALSE;
284 StatusCursor = TRUE;
285 ShowStatusLine(0); //hide
286 }
287
288 void ChangeTerminalSize(int Nx, int Ny)
289 {
290 BuffChangeTerminalSize(Nx,Ny);
291 StatusX = 0;
292 MainX = 0;
293 MainY = 0;
294 MainTop = 0;
295 MainBottom = NumOfColumns-1;
296 }
297
298 void BackSpace()
299 {
300 if (CursorX == 0)
301 {
302 if ((CursorY>0) &&
303 ((ts.TermFlag & TF_BACKWRAP)!=0))
304 {
305 MoveCursor(NumOfColumns-1,CursorY-1);
306 // if (cv.HLogBuf!=0) Log1Byte(BS);
307 // (2005.2.20 yutaka)
308 if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);
309 }
310 }
311 else if (CursorX > 0)
312 {
313 MoveCursor(CursorX-1,CursorY);
314 // if (cv.HLogBuf!=0) Log1Byte(BS);
315 // (2005.2.20 yutaka)
316 if (cv.HLogBuf!=0 && !ts.LogTypePlainText) Log1Byte(BS);
317 }
318 }
319
320 void CarriageReturn(BOOL logFlag)
321 {
322 #ifndef NO_COPYLINE_FIX
323 if (!ts.EnableContinuedLineCopy || logFlag)
324 #endif /* NO_COPYLINE_FIX */
325 if (cv.HLogBuf!=0) Log1Byte(CR);
326
327 if (CursorX>0)
328 MoveCursor(0,CursorY);
329 }
330
331 void LineFeed(BYTE b, BOOL logFlag)
332 {
333 /* for auto print mode */
334 if ((AutoPrintMode) &&
335 (b>=LF) && (b<=FF))
336 BuffDumpCurrentLine(b);
337
338 #ifndef NO_COPYLINE_FIX
339 if (!ts.EnableContinuedLineCopy || logFlag)
340 #endif /* NO_COPYLINE_FIX */
341 if (cv.HLogBuf!=0) Log1Byte(LF);
342
343 if (CursorY < CursorBottom)
344 MoveCursor(CursorX,CursorY+1);
345 else if (CursorY == CursorBottom) BuffScrollNLines(1);
346 else if (CursorY < NumOfLines-StatusLine-1)
347 MoveCursor(CursorX,CursorY+1);
348
349 #ifndef NO_COPYLINE_FIX
350 ClearLineContinued();
351 #endif /* NO_COPYLINE_FIX */
352
353 if (LFMode) CarriageReturn(logFlag);
354 }
355
356 void Tab()
357 {
358 if (Wrap && !ts.VTCompatTab) {
359 CarriageReturn(FALSE);
360 LineFeed(LF,FALSE);
361 #ifndef NO_COPYLINE_FIX
362 if (ts.EnableContinuedLineCopy) {
363 SetLineContinued();
364 }
365 #endif /* NO_COPYLINE_FIX */
366 Wrap = FALSE;
367 }
368 CursorForwardTab(1, AutoWrapMode);
369 if (cv.HLogBuf!=0) Log1Byte(HT);
370 }
371
372 void PutChar(BYTE b)
373 {
374 BOOL SpecialNew;
375 TCharAttr CharAttrTmp;
376
377 CharAttrTmp = CharAttr;
378
379 if (PrinterMode) { // printer mode
380 WriteToPrnFile(b,TRUE);
381 return;
382 }
383
384 if (Wrap)
385 {
386 CarriageReturn(FALSE);
387 LineFeed(LF,FALSE);
388 #ifndef NO_COPYLINE_FIX
389 CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;
390 #endif /* NO_COPYLINE_FIX */
391 }
392
393 // if (cv.HLogBuf!=0) Log1Byte(b);
394 // (2005.2.20 yutaka)
395 if (ts.LogTypePlainText) {
396 if (__isascii(b) && !isprint(b)) {
397 // ASCII�������A���\�������������O�����������B
398 } else {
399 if (cv.HLogBuf!=0) Log1Byte(b);
400 }
401 } else {
402 if (cv.HLogBuf!=0) Log1Byte(b);
403 }
404
405 Wrap = FALSE;
406
407 SpecialNew = FALSE;
408 if ((b>0x5F) && (b<0x80))
409 {
410 if (SSflag)
411 SpecialNew = (Gn[GLtmp]==IdSpecial);
412 else
413 SpecialNew = (Gn[Glr[0]]==IdSpecial);
414 }
415 else if (b>0xDF)
416 {
417 if (SSflag)
418 SpecialNew = (Gn[GLtmp]==IdSpecial);
419 else
420 SpecialNew = (Gn[Glr[1]]==IdSpecial);
421 }
422
423 if (SpecialNew != Special)
424 {
425 UpdateStr();
426 Special = SpecialNew;
427 }
428
429 if (Special)
430 {
431 b = b & 0x7F;
432 CharAttrTmp.Attr |= AttrSpecial;
433 }
434 else
435 CharAttrTmp.Attr |= CharAttr.Attr;
436
437 BuffPutChar(b, CharAttrTmp, InsertMode);
438
439 if (CursorX < NumOfColumns-1)
440 MoveRight();
441 else {
442 UpdateStr();
443 Wrap = AutoWrapMode;
444 }
445 }
446
447 void PutDecSp(BYTE b)
448 {
449 TCharAttr CharAttrTmp;
450
451 CharAttrTmp = CharAttr;
452
453 if (PrinterMode) { // printer mode
454 WriteToPrnFile(b, TRUE);
455 return;
456 }
457
458 if (Wrap) {
459 CarriageReturn(FALSE);
460 LineFeed(LF, FALSE);
461 #ifndef NO_COPYLINE_FIX
462 CharAttrTmp.Attr |= ts.EnableContinuedLineCopy ? AttrLineContinued : 0;
463 #endif /* NO_COPYLINE_FIX */
464 }
465
466 if (cv.HLogBuf!=0) Log1Byte(b);
467 /*
468 if (ts.LogTypePlainText && __isascii(b) && !isprint(b)) {
469 // ASCII�������A���\�������������O�����������B
470 } else {
471 if (cv.HLogBuf!=0) Log1Byte(b);
472 }
473 */
474
475 Wrap = FALSE;
476
477 if (!Special) {
478 UpdateStr();
479 Special = TRUE;
480 }
481
482 CharAttrTmp.Attr |= AttrSpecial;
483 BuffPutChar(b, CharAttrTmp, InsertMode);
484
485 if (CursorX < NumOfColumns-1)
486 MoveRight();
487 else {
488 UpdateStr();
489 Wrap = AutoWrapMode;
490 }
491 }
492
493 void PutKanji(BYTE b)
494 {
495 #ifndef NO_COPYLINE_FIX
496 TCharAttr CharAttrTmp;
497
498 CharAttrTmp = CharAttr;
499 #endif /* NO_COPYLINE_FIX */
500 Kanji = Kanji + b;
501
502 if (PrinterMode && DirectPrn)
503 {
504 WriteToPrnFile(HIBYTE(Kanji),FALSE);
505 WriteToPrnFile(LOBYTE(Kanji),TRUE);
506 return;
507 }
508
509 if (ConvJIS)
510 Kanji = JIS2SJIS((WORD)(Kanji & 0x7f7f));
511
512 if (PrinterMode) { // printer mode
513 WriteToPrnFile(HIBYTE(Kanji),FALSE);
514 WriteToPrnFile(LOBYTE(Kanji),TRUE);
515 return;
516 }
517
518 if (Wrap)
519 {
520 CarriageReturn(FALSE);
521 LineFeed(LF,FALSE);
522 #ifndef NO_COPYLINE_FIX
523 if (ts.EnableContinuedLineCopy)
524 CharAttrTmp.Attr |= AttrLineContinued;
525 #endif /* NO_COPYLINE_FIX */
526 }
527 else if (CursorX > NumOfColumns-2)
528 if (AutoWrapMode)
529 {
530 #ifndef NO_COPYLINE_FIX
531 if (ts.EnableContinuedLineCopy)
532 {
533 CharAttrTmp.Attr |= AttrLineContinued;
534 if (CursorX == NumOfColumns-1)
535 BuffPutChar(0x20, CharAttr, FALSE);
536 }
537 #endif /* NO_COPYLINE_FIX */
538 CarriageReturn(FALSE);
539 LineFeed(LF,FALSE);
540 }
541 else return;
542
543 Wrap = FALSE;
544
545 if (cv.HLogBuf!=0)
546 {
547 Log1Byte(HIBYTE(Kanji));
548 Log1Byte(LOBYTE(Kanji));
549 }
550
551 if (Special)
552 {
553 UpdateStr();
554 Special = FALSE;
555 }
556
557 #ifndef NO_COPYLINE_FIX
558 BuffPutKanji(Kanji, CharAttrTmp, InsertMode);
559 #else
560 BuffPutKanji(Kanji, CharAttr, InsertMode);
561 #endif /* NO_COPYLINE_FIX */
562
563 if (CursorX < NumOfColumns-2)
564 {
565 MoveRight();
566 MoveRight();
567 }
568 else {
569 UpdateStr();
570 Wrap = AutoWrapMode;
571 }
572 }
573
574 void PutDebugChar(BYTE b)
575 {
576 InsertMode = FALSE;
577 AutoWrapMode = TRUE;
578
579 if ((b & 0x80) == 0x80)
580 {
581 UpdateStr();
582 CharAttr.Attr = AttrReverse;
583 b = b & 0x7f;
584 }
585
586 if (b<=US)
587 {
588 PutChar('^');
589 PutChar((char)(b+0x40));
590 }
591 else if (b==DEL)
592 {
593 PutChar('<');
594 PutChar('D');
595 PutChar('E');
596 PutChar('L');
597 PutChar('>');
598 }
599 else
600 PutChar(b);
601
602 if (CharAttr.Attr != AttrDefault)
603 {
604 UpdateStr();
605 CharAttr.Attr = AttrDefault;
606 }
607 }
608
609 void PrnParseControl(BYTE b) // printer mode
610 {
611 switch (b) {
612 case NUL: return;
613 case SO:
614 if (! DirectPrn)
615 {
616 if ((ts.Language==IdJapanese) &&
617 (ts.KanjiCode==IdJIS) &&
618 (ts.JIS7Katakana==1) &&
619 ((ts.TermFlag & TF_FIXEDJIS)!=0))
620 Gn[1] = IdKatakana;
621 Glr[0] = 1; /* LS1 */
622 return;
623 }
624 break;
625 case SI:
626 if (! DirectPrn)
627 {
628 Glr[0] = 0; /* LS0 */
629 return;
630 }
631 break;
632 case DC1:
633 case DC3: return;
634 case ESC:
635 ICount = 0;
636 JustAfterESC = TRUE;
637 ParseMode = ModeESC;
638 WriteToPrnFile(0,TRUE); // flush prn buff
639 return;
640 case CSI:
641 if ((ts.TerminalID<IdVT220J) ||
642 ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))
643 {
644 PutChar(b); /* Disp C1 char in VT100 mode */
645 return;
646 }
647 ICount = 0;
648 FirstPrm = TRUE;
649 NParam = 1;
650 Param[1] = -1;
651 Prv = 0;
652 ParseMode = ModeCSI;
653 WriteToPrnFile(0,TRUE); // flush prn buff
654 WriteToPrnFile(b,FALSE);
655 return;
656 }
657 /* send the uninterpreted character to printer */
658 WriteToPrnFile(b,TRUE);
659 }
660
661 void ParseControl(BYTE b)
662 {
663 if (PrinterMode) { // printer mode
664 PrnParseControl(b);
665 return;
666 }
667
668 if (b>=0x80) /* C1 char */
669 {
670 /* English mode */
671 if (ts.Language==IdEnglish)
672 {
673 if ((ts.TerminalID<IdVT220J) ||
674 ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0))
675 {
676 PutChar(b); /* Disp C1 char in VT100 mode */
677 return;
678 }
679 }
680 else { /* Japanese mode */
681 if ((ts.TermFlag & TF_ACCEPT8BITCTRL)==0)
682 return; /* ignore C1 char */
683 /* C1 chars are interpreted as C0 chars in VT100 mode */
684 if (ts.TerminalID<IdVT220J)
685 b = b & 0x7F;
686 }
687 }
688 switch (b) {
689 /* C0 group */
690 case ENQ:
691 CommBinaryOut(&cv,&(ts.Answerback[0]),ts.AnswerbackLen);
692 break;
693 case BEL:
694 switch (ts.Beep) {
695 case IdBeepOff:
696 /* nothing to do */
697 break;
698 case IdBeepOn:
699 MessageBeep(0);
700 break;
701 case IdBeepVisual:
702 VisualBell();
703 break;
704 }
705 break;
706 case BS: BackSpace(); break;
707 case HT: Tab(); break;
708
709 case LF:
710 // ���M�������s�R�[�h�� LF ���������A�T�[�o���� LF ���������������������������A
711 // CR+LF���������������������B
712 // cf. http://www.neocom.ca/forum/viewtopic.php?t=216
713 // (2007.1.21 yutaka)
714 if (ts.CRReceive == IdLF) {
715 CarriageReturn(TRUE);
716 LineFeed(b, TRUE);
717 break;
718 }
719
720 case VT: LineFeed(b,TRUE); break;
721
722 case FF:
723 if ((ts.AutoWinSwitch>0) && JustAfterESC)
724 {
725 CommInsert1Byte(&cv,b);
726 CommInsert1Byte(&cv,ESC);
727 ChangeEmu = IdTEK; /* Enter TEK Mode */
728 }
729 else
730 LineFeed(b,TRUE);
731 break;
732 case CR:
733 CarriageReturn(TRUE);
734 if (ts.CRReceive==IdCRLF)
735 CommInsert1Byte(&cv,LF);
736 break;
737 case SO:
738 if ((ts.Language==IdJapanese) &&
739 (ts.KanjiCode==IdJIS) &&
740 (ts.JIS7Katakana==1) &&
741 ((ts.TermFlag & TF_FIXEDJIS)!=0))
742 Gn[1] = IdKatakana;
743
744 Glr[0] = 1; /* LS1 */
745 break;
746 case SI: Glr[0] = 0; break; /* LS0 */
747 case DLE:
748 if ((ts.FTFlag & FT_BPAUTO)!=0)
749 ParseMode = ModeDLE; /* Auto B-Plus activation */
750 break;
751 case CAN:
752 if ((ts.FTFlag & FT_ZAUTO)!=0)
753 ParseMode = ModeCAN; /* Auto ZMODEM activation */
754 // else if (ts.AutoWinSwitch>0)
755 // ChangeEmu = IdTEK; /* Enter TEK Mode */
756 else
757 ParseMode = ModeFirst;
758 break;
759 case SUB: ParseMode = ModeFirst; break;
760 case ESC:
761 ICount = 0;
762 JustAfterESC = TRUE;
763 ParseMode = ModeESC;
764 break;
765 case FS:
766 case GS:
767 case RS:
768 case US:
769 if (ts.AutoWinSwitch>0)
770 {
771 CommInsert1Byte(&cv,b);
772 ChangeEmu = IdTEK; /* Enter TEK Mode */
773 }
774 break;
775
776 /* C1 char */
777 case IND: LineFeed(0,TRUE); break;
778 case NEL:
779 LineFeed(0,TRUE);
780 CarriageReturn(TRUE);
781 break;
782 case HTS: SetTabStop(); break;
783 case RI: CursorUpWithScroll(); break;
784 case SS2:
785 GLtmp = 2;
786 SSflag = TRUE;
787 break;
788 case SS3:
789 GLtmp = 3;
790 SSflag = TRUE;
791 break;
792 case DCS:
793 SavedMode = ParseMode;
794 ESCFlag = FALSE;
795 NParam = 1;
796 Param[1] = -1;
797 ParseMode = ModeDCS;
798 break;
799 case SOS:
800 SavedMode = ParseMode;
801 ESCFlag = FALSE;
802 ParseMode = ModeSOS;
803 break;
804 case CSI:
805 ICount = 0;
806 FirstPrm = TRUE;
807 NParam = 1;
808 Param[1] = -1;
809 Prv = 0;
810 ParseMode = ModeCSI;
811 break;
812 case OSC:
813 Param[1] = 0;
814 ParseMode = ModeXS;
815 break;
816 case PM:
817 case APC:
818 SavedMode = ParseMode;
819 ESCFlag = FALSE;
820 ParseMode = ModeSOS;
821 break;
822 }
823 }
824
825 void SaveCursor()
826 {
827 int i;
828 PStatusBuff Buff;
829
830 if ((StatusLine>0) &&
831 (CursorY==NumOfLines-1))
832 Buff = &SBuff2; // for status line
833 else
834 Buff = &SBuff1; // for main screen
835
836 Buff->CursorX = CursorX;
837 Buff->CursorY = CursorY;
838 Buff->Attr = CharAttr;
839 Buff->Glr[0] = Glr[0];
840 Buff->Glr[1] = Glr[1];
841 for (i=0 ; i<=3; i++)
842 Buff->Gn[i] = Gn[i];
843 Buff->AutoWrapMode = AutoWrapMode;
844 Buff->RelativeOrgMode = RelativeOrgMode;
845 }
846
847 void RestoreCursor()
848 {
849 int i;
850 PStatusBuff Buff;
851 UpdateStr();
852
853 if ((StatusLine>0) &&
854 (CursorY==NumOfLines-1))
855 Buff = &SBuff2; // for status line
856 else
857 Buff = &SBuff1; // for main screen
858
859 if (Buff->CursorX > NumOfColumns-1)
860 Buff->CursorX = NumOfColumns-1;
861 if (Buff->CursorY > NumOfLines-1-StatusLine)
862 Buff->CursorY = NumOfLines-1-StatusLine;
863 MoveCursor(Buff->CursorX,Buff->CursorY);
864 CharAttr = Buff->Attr;
865 Glr[0] = Buff->Glr[0];
866 Glr[1] = Buff->Glr[1];
867 for (i=0 ; i<=3; i++)
868 Gn[i] = Buff->Gn[i];
869 AutoWrapMode = Buff->AutoWrapMode;
870 RelativeOrgMode = Buff->RelativeOrgMode;
871 }
872
873 void AnswerTerminalType()
874 {
875 char Tmp[31];
876
877 if (ts.TerminalID<IdVT320)
878 strncpy_s(Tmp, sizeof(Tmp),"\033[?", _TRUNCATE);
879 else
880 strncpy_s(Tmp, sizeof(Tmp),"\233?", _TRUNCATE);
881
882 switch (ts.TerminalID) {
883 case IdVT100:
884 strncat_s(Tmp,sizeof(Tmp),"1;2",_TRUNCATE);
885 break;
886 case IdVT100J:
887 strncat_s(Tmp,sizeof(Tmp),"5;2",_TRUNCATE);
888 break;
889 case IdVT101:
890 strncat_s(Tmp,sizeof(Tmp),"1;0",_TRUNCATE);
891 break;
892 case IdVT102:
893 strncat_s(Tmp,sizeof(Tmp),"6",_TRUNCATE);
894 break;
895 case IdVT102J:
896 strncat_s(Tmp,sizeof(Tmp),"15",_TRUNCATE);
897 break;
898 case IdVT220J:
899 strncat_s(Tmp,sizeof(Tmp),"62;1;2;5;6;7;8",_TRUNCATE);
900 break;
901 case IdVT282:
902 strncat_s(Tmp,sizeof(Tmp),"62;1;2;4;5;6;7;8;10;11",_TRUNCATE);
903 break;
904 case IdVT320:
905 strncat_s(Tmp,sizeof(Tmp),"63;1;2;6;7;8",_TRUNCATE);
906 break;
907 case IdVT382:
908 strncat_s(Tmp,sizeof(Tmp),"63;1;2;4;5;6;7;8;10;15",_TRUNCATE);
909 break;
910 }
911 strncat_s(Tmp,sizeof(Tmp),"c",_TRUNCATE);
912
913 CommBinaryOut(&cv,Tmp,strlen(Tmp)); /* Report terminal ID */
914 }
915
916 void ESCSpace(BYTE b)
917 {
918 switch (b) {
919 case 'F': Send8BitMode = FALSE; break; // S7C1T
920 case 'G': Send8BitMode = TRUE; break; // S8C1T
921 }
922 }
923
924 void ESCSharp(BYTE b)
925 {
926 switch (b) {
927 case '8': /* Fill screen with "E" */
928 BuffUpdateScroll();
929 BuffFillWithE();
930 MoveCursor(0,0);
931 ParseMode = ModeFirst;
932 break;
933 }
934 }
935
936 /* select double byte code set */
937 void ESCDBCSSelect(BYTE b)
938 {
939 int Dist;
940
941 if (ts.Language!=IdJapanese) return;
942
943 switch (ICount) {
944 case 1:
945 if ((b=='@') || (b=='B'))
946 {
947 Gn[0] = IdKanji; /* Kanji -> G0 */
948 if ((ts.TermFlag & TF_AUTOINVOKE)!=0)
949 Glr[0] = 0; /* G0->GL */
950 }
951 break;
952 case 2:
953 /* Second intermediate char must be
954 '(' or ')' or '*' or '+'. */
955 Dist = (IntChar[2]-'(') & 3; /* G0 - G3 */
956 if ((b=='1') || (b=='3') ||
957 (b=='@') || (b=='B'))
958 {
959 Gn[Dist] = IdKanji; /* Kanji -> G0-3 */
960 if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
961 (Dist==0))
962 Glr[0] = 0; /* G0->GL */
963 }
964 break;
965 }
966 }
967
968 void ESCSelectCode(BYTE b)
969 {
970 switch (b) {
971 case '0':
972 if (ts.AutoWinSwitch>0)
973 ChangeEmu = IdTEK; /* enter TEK mode */
974 break;
975 }
976 }
977
978 /* select single byte code set */
979 void ESCSBCSSelect(BYTE b)
980 {
981 int Dist;
982
983 /* Intermediate char must be
984 '(' or ')' or '*' or '+'. */
985 Dist = (IntChar[1]-'(') & 3; /* G0 - G3 */
986
987 switch (b) {
988 case '0': Gn[Dist] = IdSpecial; break;
989 case '<': Gn[Dist] = IdASCII; break;
990 case '>': Gn[Dist] = IdASCII; break;
991 case 'A': Gn[Dist] = IdASCII; break;
992 case 'B': Gn[Dist] = IdASCII; break;
993 case 'H': Gn[Dist] = IdASCII; break;
994 case 'I':
995 if (ts.Language==IdJapanese)
996 Gn[Dist] = IdKatakana;
997 break;
998 case 'J': Gn[Dist] = IdASCII; break;
999 }
1000
1001 if (((ts.TermFlag & TF_AUTOINVOKE)!=0) &&
1002 (Dist==0))
1003 Glr[0] = 0; /* G0->GL */
1004 }
1005
1006 void PrnParseEscape(BYTE b) // printer mode
1007 {
1008 int i;
1009
1010 ParseMode = ModeFirst;
1011 switch (ICount) {
1012 /* no intermediate char */
1013 case 0:
1014 switch (b) {
1015 case '[': /* CSI */
1016 ICount = 0;
1017 FirstPrm = TRUE;
1018 NParam = 1;
1019 Param[1] = -1;
1020 Prv = 0;
1021 WriteToPrnFile(ESC,FALSE);
1022 WriteToPrnFile('[',FALSE);
1023 ParseMode = ModeCSI;
1024 return;
1025 } /* end of case Icount=0 */
1026 break;
1027 /* one intermediate char */
1028 case 1:
1029 switch (IntChar[1]) {
1030 case '$':
1031 if (! DirectPrn)
1032 {
1033 ESCDBCSSelect(b);
1034 return;
1035 }
1036 break;
1037 case '(':
1038 case ')':
1039 case '*':
1040 case '+':
1041 if (! DirectPrn)
1042 {
1043 ESCSBCSSelect(b);
1044 return;
1045 }
1046 break;
1047 }
1048 break;
1049 /* two intermediate char */
1050 case 2:
1051 if ((! DirectPrn) &&
1052 (IntChar[1]=='$') &&
1053 ('('<=IntChar[2]) &&
1054 (IntChar[2]<='+'))
1055 {
1056 ESCDBCSSelect(b);
1057 return;
1058 }
1059 break;
1060 }
1061 // send the uninterpreted sequence to printer
1062 WriteToPrnFile(ESC,FALSE);
1063 for (i=1; i<=ICount; i++)
1064 WriteToPrnFile(IntChar[i],FALSE);
1065 WriteToPrnFile(b,TRUE);
1066 }
1067
1068 void ParseEscape(BYTE b) /* b is the final char */
1069 {
1070 if (PrinterMode) { // printer mode
1071 PrnParseEscape(b);
1072 return;
1073 }
1074
1075 switch (ICount) {
1076 /* no intermediate char */
1077 case 0:
1078 switch (b) {
1079 case '7': SaveCursor(); break;
1080 case '8': RestoreCursor(); break;
1081 case '=': AppliKeyMode = TRUE; break;
1082 case '>': AppliKeyMode = FALSE; break;
1083 case 'D': /* IND */
1084 LineFeed(0,TRUE);
1085 break;
1086 case 'E': /* NEL */
1087 MoveCursor(0,CursorY);
1088 LineFeed(0,TRUE);
1089 break;
1090 case 'H': /* HTS */
1091 SetTabStop();
1092 break;
1093 case 'M': /* RI */
1094 CursorUpWithScroll();
1095 break;
1096 case 'N': /* SS2 */
1097 GLtmp = 2;
1098 SSflag = TRUE;
1099 break;
1100 case 'O': /* SS3 */
1101 GLtmp = 3;
1102 SSflag = TRUE;
1103 break;
1104 case 'P': /* DCS */
1105 SavedMode = ParseMode;
1106 ESCFlag = FALSE;
1107 NParam = 1;
1108 Param[1] = -1;
1109 ParseMode = ModeDCS;
1110 return;
1111 case 'X': /* SOS */
1112 SavedMode = ParseMode;
1113 ESCFlag = FALSE;
1114 ParseMode = ModeSOS;
1115 return;
1116 case 'Z': AnswerTerminalType(); break;
1117 case '[': /* CSI */
1118 ICount = 0;
1119 FirstPrm = TRUE;
1120 NParam = 1;
1121 Param[1] = -1;
1122 Prv = 0;
1123 ParseMode = ModeCSI;
1124 return;
1125 case '\\': break; /* ST */
1126 case ']': /* XTERM sequence (OSC) */
1127 NParam = 1;
1128 Param[1] = 0;
1129 ParseMode = ModeXS;
1130 return;
1131 case '^':
1132 case '_': /* PM, APC */
1133 SavedMode = ParseMode;
1134 ESCFlag = FALSE;
1135 ParseMode = ModeSOS;
1136 return;
1137 case 'c': /* Hardware reset */
1138 HideStatusLine();
1139 ResetTerminal();
1140 ClearUserKey();
1141 ClearBuffer();
1142 if (ts.PortType==IdSerial) // reset serial port
1143 CommResetSerial(&ts, &cv, TRUE);
1144 break;
1145 case 'g': /* Visual Bell (screen original?) */
1146 VisualBell();
1147 break;
1148 case 'n': Glr[0] = 2; break; /* LS2 */
1149 case 'o': Glr[0] = 3; break; /* LS3 */
1150 case '|': Glr[1] = 3; break; /* LS3R */
1151 case '}': Glr[1] = 2; break; /* LS2R */
1152 case '~': Glr[1] = 1; break; /* LS1R */
1153 } /* end of case Icount=0 */
1154 break;
1155 /* one intermediate char */
1156 case 1:
1157 switch (IntChar[1]) {
1158 case ' ': ESCSpace(b); break;
1159 case '#': ESCSharp(b); break;
1160 case '$': ESCDBCSSelect(b); break;
1161 case '%': break;
1162 case '(':
1163 case ')':
1164 case '*':
1165 case '+':
1166 ESCSBCSSelect(b);
1167 break;
1168 }
1169 break;
1170 /* two intermediate char */
1171 case 2:
1172 if ((IntChar[1]=='$') &&
1173 ('('<=IntChar[2]) &&
1174 (IntChar[2]<='+'))
1175 ESCDBCSSelect(b);
1176 else if ((IntChar[1]=='%') &&
1177 (IntChar[2]=='!'))
1178 ESCSelectCode(b);
1179 break;
1180 }
1181 ParseMode = ModeFirst;
1182 }
1183
1184 void EscapeSequence(BYTE b)
1185 {
1186 if (b<=US)
1187 ParseControl(b);
1188 else if ((b>=0x20) && (b<=0x2F))
1189 {
1190 if (ICount<IntCharMax) ICount++;
1191 IntChar[ICount] = b;
1192 }
1193 else if ((b>=0x30) && (b<=0x7E))
1194 ParseEscape(b);
1195 else if ((b>=0x80) && (b<=0x9F))
1196 ParseControl(b);
1197
1198 JustAfterESC = FALSE;
1199 }
1200
1201 void CSInsertCharacter()
1202 {
1203 // Insert space characters at cursor
1204 int Count;
1205
1206 BuffUpdateScroll();
1207 if (Param[1]<1) Param[1] = 1;
1208 Count = Param[1];
1209 BuffInsertSpace(Count);
1210 }
1211
1212 void CSCursorUp()
1213 {
1214 if (Param[1]<1) Param[1] = 1;
1215
1216 if (CursorY >= CursorTop)
1217 {
1218 if (CursorY-Param[1] > CursorTop)
1219 MoveCursor(CursorX,CursorY-Param[1]);
1220 else
1221 MoveCursor(CursorX,CursorTop);
1222 }
1223 else {
1224 if (CursorY > 0)
1225 MoveCursor(CursorX,CursorY-Param[1]);
1226 else
1227 MoveCursor(CursorX,0);
1228 }
1229 }
1230
1231 void CSCursorUp1()
1232 {
1233 MoveCursor(0,CursorY);
1234 CSCursorUp();
1235 }
1236
1237 void CSCursorDown()
1238 {
1239 if (Param[1]<1) Param[1] = 1;
1240
1241 if (CursorY <= CursorBottom)
1242 {
1243 if (CursorY+Param[1] < CursorBottom)
1244 MoveCursor(CursorX,CursorY+Param[1]);
1245 else
1246 MoveCursor(CursorX,CursorBottom);
1247 }
1248 else {
1249 if (CursorY < NumOfLines-StatusLine-1)
1250 MoveCursor(CursorX,CursorY+Param[1]);
1251 else
1252 MoveCursor(CursorX,NumOfLines-StatusLine);
1253 }
1254 }
1255
1256 void CSCursorDown1()
1257 {
1258 MoveCursor(0,CursorY);
1259 CSCursorDown();
1260 }
1261
1262 void CSScreenErase()
1263 {
1264 if (Param[1] == -1) Param[1] = 0;
1265 BuffUpdateScroll();
1266 switch (Param[1]) {
1267 case 0:
1268 // <ESC>[H(Cursor in left upper corner)�������J�[�\�������������w�������������A
1269 // <ESC>[J��<ESC>[2J�����������������A�����������A���s�o�b�t�@���X�N���[���A�E�g
1270 // �����������������B(2005.5.29 yutaka)
1271 // �R���t�B�O���[�V�������������������������������B(2008.5.3 yutaka)
1272 if (ts.ScrollWindowClearScreen &&
1273 (CursorX == 0 && CursorY == 0)) {
1274 // Erase screen (scroll out)
1275 BuffClearScreen();
1276 UpdateWindow(HVTWin);
1277
1278 } else {
1279 // Erase characters from cursor to the end of screen
1280 BuffEraseCurToEnd();
1281 }
1282 break;
1283
1284 case 1:
1285 // Erase characters from home to cursor
1286 BuffEraseHomeToCur();
1287 break;
1288
1289 case 2:
1290 // Erase screen (scroll out)
1291 BuffClearScreen();
1292 UpdateWindow(HVTWin);
1293 break;
1294 }
1295 }
1296
1297 void CSInsertLine()
1298 {
1299 // Insert lines at current position
1300 int Count, YEnd;
1301
1302 if (CursorY < CursorTop) return;
1303 if (CursorY > CursorBottom) return;
1304 if (Param[1]<1) Param[1] = 1;
1305 Count = Param[1];
1306
1307 YEnd = CursorBottom;
1308 if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1309 if (Count > YEnd+1 - CursorY) Count = YEnd+1 - CursorY;
1310
1311 BuffInsertLines(Count,YEnd);
1312 }
1313
1314 void CSLineErase()
1315 {
1316 if (Param[1] == -1) Param[1] = 0;
1317 BuffUpdateScroll();
1318 switch (Param[1]) {
1319 /* erase char from cursor to end of line */
1320 case 0:
1321 BuffEraseCharsInLine(CursorX,NumOfColumns-CursorX);
1322 break;
1323 /* erase char from start of line to cursor */
1324 case 1:
1325 BuffEraseCharsInLine(0,CursorX+1);
1326 break;
1327 /* erase entire line */
1328 case 2:
1329 BuffEraseCharsInLine(0,NumOfColumns);
1330 break;
1331 }
1332 }
1333
1334 void CSDeleteNLines()
1335 // Delete lines from current line
1336 {
1337 int Count, YEnd;
1338
1339 if (CursorY < CursorTop) return;
1340 if (CursorY > CursorBottom) return;
1341 Count = Param[1];
1342 if (Count<1) Count = 1;
1343
1344 YEnd = CursorBottom;
1345 if (CursorY > YEnd) YEnd = NumOfLines-1-StatusLine;
1346 if (Count > YEnd+1-CursorY) Count = YEnd+1-CursorY;
1347 BuffDeleteLines(Count,YEnd);
1348 }
1349
1350 void CSDeleteCharacter()
1351 {
1352 // Delete characters in current line from cursor
1353
1354 if (Param[1]<1) Param[1] = 1;
1355 BuffUpdateScroll();
1356 BuffDeleteChars(Param[1]);
1357 }
1358
1359 void CSEraseCharacter()
1360 {
1361 if (Param[1]<1) Param[1] = 1;
1362 BuffUpdateScroll();
1363 BuffEraseChars(Param[1]);
1364 }
1365
1366 void CSScrollUP()
1367 {
1368 if (Param[1]<1) Param[1] = 1;
1369 BuffUpdateScroll();
1370 BuffRegionScrollUpNLines(Param[1]);
1371 }
1372
1373 void CSScrollDown()
1374 {
1375 if (Param[1]<1) Param[1] = 1;
1376 BuffUpdateScroll();
1377 BuffRegionScrollDownNLines(Param[1]);
1378 }
1379
1380 void CSForwardTab()
1381 {
1382 if (Param[1]<1) Param[1] = 1;
1383 CursorForwardTab(Param[1], AutoWrapMode);
1384 }
1385
1386 void CSBackwardTab()
1387 {
1388 if (Param[1]<1) Param[1] = 1;
1389 CursorBackwardTab(Param[1]);
1390 }
1391
1392 void CSMoveToColumnN()
1393 {
1394 if (Param[1]<1) Param[1] = 1;
1395 Param[1]--;
1396 if (Param[1] < 0) Param[1] = 0;
1397 if (Param[1] > NumOfColumns-1) Param[1] = NumOfColumns-1;
1398 MoveCursor(Param[1],CursorY);
1399 }
1400
1401 void CSCursorRight()
1402 {
1403 if (Param[1]<1) Param[1] = 1;
1404 if (CursorX + Param[1] > NumOfColumns-1)
1405 MoveCursor(NumOfColumns-1,CursorY);
1406 else
1407 MoveCursor(CursorX+Param[1],CursorY);
1408 }
1409
1410 void CSCursorLeft()
1411 {
1412 if (Param[1]<1) Param[1] = 1;
1413 if (CursorX-Param[1] < 0)
1414 MoveCursor(0,CursorY);
1415 else
1416 MoveCursor(CursorX-Param[1],CursorY);
1417 }
1418
1419 void CSMoveToLineN()
1420 {
1421 if (Param[1]<1) Param[1] = 1;
1422 if (RelativeOrgMode)
1423 {
1424 if (CursorTop+Param[1]-1 > CursorBottom)
1425 MoveCursor(CursorX,CursorBottom);
1426 else
1427 MoveCursor(CursorX,CursorTop+Param[1]-1);
1428 }
1429 else {
1430 if (Param[1] > NumOfLines-StatusLine)
1431 MoveCursor(CursorX,NumOfLines-1-StatusLine);
1432 else
1433 MoveCursor(CursorX,Param[1]-1);
1434 }
1435 }
1436
1437 void CSMoveToXY()
1438 {
1439 int NewX, NewY;
1440
1441 if (Param[1]<1) Param[1] = 1;
1442 if ((NParam < 2) || (Param[2]<1)) Param[2] = 1;
1443 NewX = Param[2] - 1;
1444 if (NewX > NumOfColumns-1) NewX = NumOfColumns-1;
1445
1446 if ((StatusLine>0) && (CursorY==NumOfLines-1))
1447 NewY = CursorY;
1448 else if (RelativeOrgMode)
1449 {
1450 NewY = CursorTop + Param[1] - 1;
1451 if (NewY > CursorBottom) NewY = CursorBottom;
1452 }
1453 else {
1454 NewY = Param[1] - 1;
1455 if (NewY > NumOfLines-1-StatusLine)
1456 NewY = NumOfLines-1-StatusLine;
1457 }
1458 MoveCursor(NewX,NewY);
1459 }
1460
1461 void CSDeleteTabStop()
1462 {
1463 if (Param[1]==-1) Param[1] = 0;
1464 ClearTabStop(Param[1]);
1465 }
1466
1467 void CS_h_Mode()
1468 {
1469 switch (Param[1]) {
1470 case 2: // KAM
1471 KeybEnabled = FALSE; break;
1472 case 4: // IRM
1473 InsertMode = TRUE; break;
1474 case 12: // SRM
1475 ts.LocalEcho = 0;
1476 if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1477 TelChangeEcho();
1478 break;
1479 case 20: // LF/NL
1480 LFMode = TRUE;
1481 ts.CRSend = IdCRLF;
1482 cv.CRSend = IdCRLF;
1483 break;
1484 case 33: // WYSTCURM
1485 ts.NonblinkingCursor = TRUE;
1486 ChangeCaret();
1487 break;
1488 case 34: // WYULCURM
1489 ts.CursorShape = IdHCur;
1490 ChangeCaret();
1491 break;
1492 }
1493 }
1494
1495 void CS_i_Mode()
1496 {
1497 if (Param[1]==-1) Param[1] = 0;
1498 switch (Param[1]) {
1499 /* print screen */
1500 // PrintEX -- TRUE: print screen
1501 // FALSE: scroll region
1502 case 0: BuffPrint(! PrintEX); break;
1503 /* printer controller mode off */
1504 case 4: break; /* See PrnParseCS() */
1505 /* printer controller mode on */
1506 case 5:
1507 if (! AutoPrintMode)
1508 OpenPrnFile();
1509 DirectPrn = (ts.PrnDev[0]!=0);
1510 PrinterMode = TRUE;
1511 break;
1512 }
1513 }
1514
1515 void CS_l_Mode()
1516 {
1517 switch (Param[1]) {
1518 case 2: // KAM
1519 KeybEnabled = TRUE; break;
1520 case 4: // IRM
1521 InsertMode = FALSE; break;
1522 case 12: // SRM
1523 ts.LocalEcho = 1;
1524 if (cv.Ready && cv.TelFlag && (ts.TelEcho>0))
1525 TelChangeEcho();
1526 break;
1527 case 20: // LF/NL
1528 LFMode = FALSE;
1529 ts.CRSend = IdCR;
1530 cv.CRSend = IdCR;
1531 break;
1532 case 33: // WYSTCURM
1533 ts.NonblinkingCursor = FALSE;
1534 ChangeCaret();
1535 break;
1536 case 34: // WYULCURM
1537 ts.CursorShape = IdBlkCur;
1538 ChangeCaret();
1539 break;
1540 }
1541 }
1542
1543 void CS_n_Mode()
1544 {
1545 char Report[16];
1546 int Y;
1547
1548 switch (Param[1]) {
1549 case 5:
1550 /* Device Status Report -> Ready */
1551 if (Send8BitMode)
1552 CommBinaryOut(&cv,"\2330n",3);
1553 else
1554 CommBinaryOut(&cv,"\033[0n",4);
1555 break;
1556 case 6:
1557 /* Cursor Position Report */
1558 Y = CursorY+1;
1559 if ((StatusLine>0) &&
1560 (Y==NumOfLines))
1561 Y = 1;
1562 if (Send8BitMode)
1563 _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\233%u;%uR", CLocale, Y, CursorX+1);
1564 else
1565 _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[%u;%uR", CLocale, Y, CursorX+1);
1566 CommBinaryOut(&cv,Report,strlen(Report));
1567 break;
1568 }
1569 }
1570
1571 void CSSetAttr()
1572 {
1573 int i, P;
1574
1575 UpdateStr();
1576 for (i=1 ; i<=NParam ; i++)
1577 {
1578 P = Param[i];
1579 if (P<0) P = 0;
1580 switch (P) {
1581 case 0: /* Clear all */
1582 CharAttr = DefCharAttr;
1583 BuffSetCurCharAttr(CharAttr);
1584 break;
1585
1586 case 1: /* Bold */
1587 CharAttr.Attr |= AttrBold;
1588 BuffSetCurCharAttr(CharAttr);
1589 break;
1590
1591 case 4: /* Under line */
1592 CharAttr.Attr |= AttrUnder;
1593 BuffSetCurCharAttr(CharAttr);
1594 break;
1595
1596 case 5: /* Blink */
1597 CharAttr.Attr |= AttrBlink;
1598 BuffSetCurCharAttr(CharAttr);
1599 break;
1600
1601 case 7: /* Reverse */
1602 CharAttr.Attr |= AttrReverse;
1603 BuffSetCurCharAttr(CharAttr);
1604 break;
1605
1606 case 22: /* Bold off */
1607 CharAttr.Attr &= ~ AttrBold;
1608 BuffSetCurCharAttr(CharAttr);
1609 break;
1610
1611 case 24: /* Under line off */
1612 CharAttr.Attr &= ~ AttrUnder;
1613 BuffSetCurCharAttr(CharAttr);
1614 break;
1615
1616 case 25: /* Blink off */
1617 CharAttr.Attr &= ~ AttrBlink;
1618 BuffSetCurCharAttr(CharAttr);
1619 break;
1620
1621 case 27: /* Reverse off */
1622 CharAttr.Attr &= ~ AttrReverse;
1623 BuffSetCurCharAttr(CharAttr);
1624 break;
1625
1626 case 30:
1627 case 31:
1628 case 32:
1629 case 33:
1630 case 34:
1631 case 35:
1632 case 36:
1633 case 37: /* text color */
1634 CharAttr.Attr2 |= Attr2Fore;
1635 CharAttr.Fore = P - 30;
1636 BuffSetCurCharAttr(CharAttr);
1637 break;
1638
1639 case 38: /* text color (256color mode) */
1640 if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1641 i++;
1642 if (i < NParam) {
1643 P = Param[++i];
1644 if (P<0) {
1645 P = 0;
1646 }
1647 CharAttr.Attr2 |= Attr2Fore;
1648 CharAttr.Fore = P;
1649 BuffSetCurCharAttr(CharAttr);
1650 }
1651 }
1652 break;
1653
1654 case 39: /* Reset text color */
1655 CharAttr.Attr2 &= ~ Attr2Fore;
1656 CharAttr.Fore = AttrDefaultFG;
1657 BuffSetCurCharAttr(CharAttr);
1658 break;
1659
1660 case 40:
1661 case 41:
1662 case 42:
1663 case 43:
1664 case 44:
1665 case 45:
1666 case 46:
1667 case 47: /* Back color */
1668 CharAttr.Attr2 |= Attr2Back;
1669 CharAttr.Back = P - 40;
1670 BuffSetCurCharAttr(CharAttr);
1671 break;
1672
1673 case 48: /* Back color (256color mode) */
1674 if ((ts.ColorFlag & CF_XTERM256) && i < NParam && Param[i+1] == 5) {
1675 i++;
1676 if (i < NParam) {
1677 P = Param[++i];
1678 if (P<0) {
1679 P = 0;
1680 }
1681 CharAttr.Attr2 |= Attr2Back;
1682 CharAttr.Back = P;
1683 BuffSetCurCharAttr(CharAttr);
1684 }
1685 }
1686 break;
1687
1688 case 49: /* Reset back color */
1689 CharAttr.Attr2 &= ~ Attr2Back;
1690 CharAttr.Back = AttrDefaultBG;
1691 BuffSetCurCharAttr(CharAttr);
1692 break;
1693
1694 case 90:
1695 case 91:
1696 case 92:
1697 case 93:
1698 case 94:
1699 case 95:
1700 case 96:
1701 case 97: /* aixterm style text color */
1702 if (ts.ColorFlag & CF_AIXTERM16) {
1703 CharAttr.Attr2 |= Attr2Fore;
1704 CharAttr.Fore = P - 90 + 8;
1705 BuffSetCurCharAttr(CharAttr);
1706 }
1707 break;
1708
1709 case 100:
1710 if (! (ts.ColorFlag & CF_AIXTERM16)) {
1711 /* Reset text and back color */
1712 CharAttr.Attr2 &= ~ (Attr2Fore | Attr2Back);
1713 CharAttr.Fore = AttrDefaultFG;
1714 CharAttr.Back = AttrDefaultBG;
1715 BuffSetCurCharAttr(CharAttr);
1716 break;
1717 }
1718 /* fall through to aixterm style back color */
1719
1720 case 101:
1721 case 102:
1722 case 103:
1723 case 104:
1724 case 105:
1725 case 106:
1726 case 107: /* aixterm style back color */
1727 if (ts.ColorFlag & CF_AIXTERM16) {
1728 CharAttr.Attr2 |= Attr2Back;
1729 CharAttr.Back = P - 100 + 8;
1730 BuffSetCurCharAttr(CharAttr);
1731 }
1732 break;
1733 }
1734 }
1735 }
1736
1737 void CSSetScrollRegion()
1738 {
1739 if ((StatusLine>0) &&
1740 (CursorY==NumOfLines-1))
1741 {
1742 MoveCursor(0,CursorY);
1743 return;
1744 }
1745 if (Param[1]<1) Param[1] =1;
1746 if ((NParam < 2) | (Param[2]<1))
1747 Param[2] = NumOfLines-StatusLine;
1748 Param[1]--;
1749 Param[2]--;
1750 if (Param[1] > NumOfLines-1-StatusLine)
1751 Param[1] = NumOfLines-1-StatusLine;
1752 if (Param[2] > NumOfLines-1-StatusLine)
1753 Param[2] = NumOfLines-1-StatusLine;
1754 if (Param[1] >= Param[2]) return;
1755 CursorTop = Param[1];
1756 CursorBottom = Param[2];
1757 if (RelativeOrgMode) MoveCursor(0,CursorTop);
1758 else MoveCursor(0,0);
1759 }
1760
1761 void CSSunSequence() /* Sun terminal private sequences */
1762 {
1763 char Report[16];
1764
1765 switch (Param[1]) {
1766 case 1: // De-iconify window
1767 DispShowWindow(WINDOW_RESTORE);
1768 break;
1769 case 2: // Iconify window
1770 DispShowWindow(WINDOW_MINIMIZE);
1771 break;
1772 case 3: // set window position
1773 if (NParam < 2) Param[2] = 0;
1774 if (NParam < 3) Param[3] = 0;
1775 DispMoveWindow(Param[2], Param[3]);
1776 break;
1777 case 4: // set window size by pixel
1778 if (NParam < 2) Param[2] = 0;
1779 if (NParam < 3) Param[3] = 0;
1780 DispResizeWin(Param[3], Param[2]);
1781 break;
1782 case 5: // Raise window
1783 DispShowWindow(WINDOW_RAISE);
1784 break;
1785 case 6: // Lower window
1786 DispShowWindow(WINDOW_LOWER);
1787 break;
1788 case 7: // Refresh window
1789 DispShowWindow(WINDOW_REFRESH);
1790 break;
1791 case 8: /* set terminal size */
1792 if ((Param[2]<=1) || (NParam<2)) Param[2] = 24;
1793 if ((Param[3]<=1) || (NParam<3)) Param[3] = 80;
1794 ChangeTerminalSize(Param[3],Param[2]);
1795 break;
1796 case 9: // Maximize/Restore window
1797 if (NParam < 2 || Param[2] == 0) {
1798 DispShowWindow(WINDOW_RESTORE);
1799 }
1800 else {
1801 DispShowWindow(WINDOW_MAXIMIZE);
1802 }
1803 break;
1804 case 14: /* get window size??? */
1805 /* this is not actual window size */
1806 if (Send8BitMode)
1807 CommBinaryOut(&cv,"\2334;640;480t",11);
1808 else
1809 CommBinaryOut(&cv,"\033[4;640;480t",12);
1810 break;
1811 case 18: /* get terminal size */
1812 if (Send8BitMode)
1813 _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\2338;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1814 else
1815 _snprintf_s_l(Report, sizeof(Report), _TRUNCATE, "\033[8;%u;%u;t", CLocale, NumOfLines-StatusLine, NumOfColumns);
1816 CommBinaryOut(&cv,Report,strlen(Report));
1817 break;
1818 }
1819 }
1820
1821 void CSGT(BYTE b)
1822 {
1823 switch (b) {
1824 case 'c': /* second terminal report (Secondary DA) */
1825 if (Send8BitMode)
1826 CommBinaryOut(&cv,"\233>32;10;2c",11); /* VT382 */
1827 else
1828 CommBinaryOut(&cv,"\033[>32;10;2c",11); /* VT382 */
1829 break;
1830 case 'J':
1831 if (Param[1]==3) // IO-8256 terminal
1832 {
1833 if (Param[2]<1) Param[2]=1;
1834 if (Param[3]<1) Param[3]=1;
1835 if (Param[4]<1) Param[4]=1;
1836 if (Param[5]<1) Param[5]=1;
1837 BuffEraseBox(Param[3]-1,Param[2]-1,
1838 Param[5]-1,Param[4]-1);
1839 }
1840 break;
1841 case 'K':
1842 if ((NParam>=2) && (Param[1]==5))
1843 { // IO-8256 terminal
1844 switch (Param[2]) {
1845 case 3:
1846 case 4:
1847 case 5:
1848 case 6:
1849 BuffDrawLine(CharAttr, Param[2], Param[3]);
1850 break;
1851 case 12:
1852 /* Text color */
1853 if ((Param[3]>=0) && (Param[3]<=7))
1854 {
1855 switch (Param[3]) {
1856 case 3: CharAttr.Fore = IdBlue; break;
1857 case 4: CharAttr.Fore = IdCyan; break;
1858 case 5: CharAttr.Fore = IdYellow; break;
1859 case 6: CharAttr.Fore = IdMagenta; break;
1860 default: CharAttr.Fore = Param[3]; break;
1861 }
1862 CharAttr.Attr2 |= Attr2Fore;
1863 BuffSetCurCharAttr(CharAttr);
1864 }
1865 break;
1866 }
1867 }
1868 else if (Param[1]==3)
1869 {// IO-8256 terminal
1870 if (Param[2]<1) Param[2] = 1;
1871 if (Param[3]<1) Param[2] = 1;
1872 BuffEraseCharsInLine(Param[2]-1,Param[3]-Param[2]+1);
1873 }
1874 break;
1875 }
1876 }
1877
1878 void CSQExchangeColor()
1879 {
1880 COLORREF ColorRef;
1881
1882 BuffUpdateScroll();
1883
1884 if (ts.ColorFlag & CF_REVERSECOLOR) {
1885 ColorRef = ts.VTColor[0];
1886 ts.VTColor[0] = ts.VTReverseColor[0];
1887 ts.VTReverseColor[0] = ColorRef;
1888 ColorRef = ts.VTColor[1];
1889 ts.VTColor[1] = ts.VTReverseColor[1];
1890 ts.VTReverseColor[1] = ColorRef;
1891 }
1892 else {
1893 ColorRef = ts.VTColor[0];
1894 ts.VTColor[0] = ts.VTColor[1];
1895 ts.VTColor[1] = ColorRef;
1896 }
1897
1898 ColorRef = ts.VTBoldColor[0];
1899 ts.VTBoldColor[0] = ts.VTBoldColor[1];
1900 ts.VTBoldColor[1] = ColorRef;
1901
1902 ColorRef = ts.VTBlinkColor[0];
1903 ts.VTBlinkColor[0] = ts.VTBlinkColor[1];
1904 ts.VTBlinkColor[1] = ColorRef;
1905
1906 ColorRef = ts.URLColor[0];
1907 ts.URLColor[0] = ts.URLColor[1];
1908 ts.URLColor[1] = ColorRef;
1909
1910 ts.ColorFlag ^= CF_REVERSEVIDEO;
1911
1912 #ifdef ALPHABLEND_TYPE2
1913 BGInitialize();
1914 #endif
1915 DispChangeBackground();
1916 UpdateWindow(HVTWin);
1917 }
1918
1919 void CSQ_h_Mode()
1920 {
1921 int i;
1922
1923 for (i = 1 ; i<=NParam ; i++)
1924 switch (Param[i]) {
1925 case 1: AppliCursorMode = TRUE; break;
1926 case 3:
1927 ChangeTerminalSize(132,NumOfLines-StatusLine);
1928 break;
1929 case 5: /* Reverse Video */
1930 if (!(ts.ColorFlag & CF_REVERSEVIDEO))
1931 CSQExchangeColor(); /* Exchange text/back color */
1932 break;
1933 case 6:
1934 if ((StatusLine>0) &&
1935 (CursorY==NumOfLines-1))
1936 MoveCursor(0,CursorY);
1937 else {
1938 RelativeOrgMode = TRUE;
1939 MoveCursor(0,CursorTop);
1940 }
1941 break;
1942 case 7: AutoWrapMode = TRUE; break;
1943 case 8: AutoRepeatMode = TRUE; break;
1944 case 9:
1945 if (ts.MouseEventTracking)
1946 MouseReportMode = IdMouseTrackX10;
1947 break;
1948 case 12: ts.NonblinkingCursor = FALSE; ChangeCaret(); break;
1949 case 19: PrintEX = TRUE; break;
1950 case 25: DispEnableCaret(TRUE); break; // cursor on
1951 case 38:
1952 if (ts.AutoWinSwitch>0)
1953 ChangeEmu = IdTEK; /* Enter TEK Mode */
1954 break;
1955 case 59:
1956 if (ts.Language==IdJapanese)
1957 { /* kanji terminal */
1958 Gn[0] = IdASCII;
1959 Gn[1] = IdKatakana;
1960 Gn[2] = IdKatakana;
1961 Gn[3] = IdKanji;
1962 Glr[0] = 0;
1963 if ((ts.KanjiCode==IdJIS) &&
1964 (ts.JIS7Katakana==0))
1965 Glr[1] = 2; // 8-bit katakana
1966 else
1967 Glr[1] = 3;
1968 }
1969 break;
1970 case 66: AppliKeyMode = TRUE; break;
1971 case 67: ts.BSKey = IdBS; break;
1972 case 1000:
1973 if (ts.MouseEventTracking)
1974 MouseReportMode = IdMouseTrackVT200;
1975 break;
1976 case 1001:
1977 if (ts.MouseEventTracking)
1978 MouseReportMode = IdMouseTrackVT200Hl;
1979 break;
1980 case 1002:
1981 if (ts.MouseEventTracking)
1982 MouseReportMode = IdMouseTrackBtnEvent;
1983 break;
1984 case 1003:
1985 if (ts.MouseEventTracking)
1986 MouseReportMode = IdMouseTrackAllEvent;
1987 break;
1988 case 1004:
1989 if (ts.MouseEventTracking)
1990 FocusReportMode = TRUE;
1991 break;
1992 }
1993 }
1994
1995 void CSQ_i_Mode()
1996 {
1997 if (Param[1]==-1) Param[1] = 0;
1998 switch (Param[1]) {
1999 case 1:
2000 OpenPrnFile();
2001 BuffDumpCurrentLine(LF);
2002 if (! AutoPrintMode)
2003 ClosePrnFile();
2004 break;
2005 /* auto print mode off */
2006 case 4:
2007 if (AutoPrintMode)
2008 {
2009 ClosePrnFile();
2010 AutoPrintMode = FALSE;
2011 }
2012 break;
2013 /* auto print mode on */
2014 case 5:
2015 if (! AutoPrintMode)
2016 {
2017 OpenPrnFile();
2018 AutoPrintMode = TRUE;
2019 }
2020 break;
2021 }
2022 }
2023
2024 void CSQ_l_Mode()
2025 {
2026 int i;
2027
2028 for (i = 1 ; i <= NParam ; i++)
2029 switch (Param[i]) {
2030 case 1: AppliCursorMode = FALSE; break;
2031 case 3:
2032 ChangeTerminalSize(80,NumOfLines-StatusLine);
2033 break;
2034 case 5: /* Normal Video */
2035 if (ts.ColorFlag & CF_REVERSEVIDEO)
2036 CSQExchangeColor(); /* Exchange text/back color */
2037 break;
2038 case 6:
2039 if ((StatusLine>0) &&
2040 (CursorY==NumOfLines-1))
2041 MoveCursor(0,CursorY);
2042 else {
2043 RelativeOrgMode = FALSE;
2044 MoveCursor(0,0);
2045 }
2046 break;
2047 case 7: AutoWrapMode = FALSE; break;
2048 case 8: AutoRepeatMode = FALSE; break;
2049 case 9: MouseReportMode = IdMouseTrackNone; break;
2050 case 12: ts.NonblinkingCursor = TRUE; ChangeCaret(); break;
2051 case 19: PrintEX = FALSE; break;
2052 case 25: DispEnableCaret(FALSE); break; // cursor off
2053 case 59:
2054 if (ts.Language==IdJapanese)
2055 { /* katakana terminal */
2056 Gn[0] = IdASCII;
2057 Gn[1] = IdKatakana;
2058 Gn[2] = IdKatakana;
2059 Gn[3] = IdKanji;
2060 Glr[0] = 0;
2061 if ((ts.KanjiCode==IdJIS) &&
2062 (ts.JIS7Katakana==0))
2063 Glr[1] = 2; // 8-bit katakana
2064 else
2065 Glr[1] = 3;
2066 }
2067 break;
2068 case 66: AppliKeyMode = FALSE; break;
2069 case 67: ts.BSKey = IdDEL; break;
2070 case 1000:
2071 case 1001:
2072 case 1002:
2073 case 1003: MouseReportMode = IdMouseTrackNone; break;
2074 case 1004: FocusReportMode = FALSE; break;
2075 }
2076 }
2077
2078 void CSQ_n_Mode()
2079 {
2080 }
2081
2082 void CSQuest(BYTE b)
2083 {
2084 switch (b) {
2085 case 'K': CSLineErase(); break;
2086 case 'h': CSQ_h_Mode(); break;
2087 case 'i': CSQ_i_Mode(); break;
2088 case 'l': CSQ_l_Mode(); break;
2089 case 'n': CSQ_n_Mode(); break;
2090 }
2091 }
2092
2093 void SoftReset()
2094 // called by software-reset escape sequence handler
2095 {
2096 UpdateStr();
2097 AutoRepeatMode = TRUE;
2098 DispEnableCaret(TRUE); // cursor on
2099 InsertMode = FALSE;
2100 RelativeOrgMode = FALSE;
2101 AppliKeyMode = FALSE;
2102 AppliCursorMode = FALSE;
2103 if ((StatusLine>0) &&
2104 (CursorY == NumOfLines-1))
2105 MoveToMainScreen();
2106 CursorTop = 0;
2107 CursorBottom = NumOfLines-1-StatusLine;
2108 ResetCharSet();
2109
2110 Send8BitMode = ts.Send8BitCtrl;
2111
2112 /* Attribute */
2113 CharAttr = DefCharAttr;
2114 Special = FALSE;
2115 BuffSetCurCharAttr(CharAttr);
2116
2117 // status buffers
2118 ResetSBuffers();
2119 }
2120
2121 void CSExc(BYTE b)
2122 {
2123 switch (b) {
2124 case 'p':
2125 /* Software reset */
2126 SoftReset();
2127 break;
2128 }
2129 }
2130
2131 void CSDouble(BYTE b)
2132 {
2133 switch (b) {
2134 case 'p':
2135 /* Select terminal mode (software reset) */
2136 SoftReset();
2137 if (NParam > 0) {
2138 switch (Param[1]) {
2139 case 61: // VT100 Mode
2140 Send8BitMode = FALSE; break;
2141 case 62: // VT200 Mode
2142 case 63: // VT300 Mode
2143 case 64: // VT400 Mode
2144 if (NParam > 1 && Param[2] == 1)
2145 Send8BitMode = FALSE;
2146 else
2147 Send8BitMode = TRUE;
2148 break;
2149 }
2150 }
2151 break;
2152 }
2153 }
2154
2155 void CSDol(BYTE b)
2156 {
2157 switch (b) {
2158 case '}':
2159 if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2160 if (StatusLine==0) return;
2161 if ((Param[1]<1) && (CursorY==NumOfLines-1))
2162 MoveToMainScreen();
2163 else if ((Param[1]==1) && (CursorY<NumOfLines-1))
2164 MoveToStatusLine();
2165 break;
2166 case '~':
2167 if ((ts.TermFlag & TF_ENABLESLINE)==0) return;
2168 if (Param[1]<=1)
2169 HideStatusLine();
2170 else if ((StatusLine==0) && (Param[1]==2))
2171 ShowStatusLine(1); // show
2172 break;
2173 }
2174 }
2175
2176 void CSSpace(BYTE b) {
2177 switch (b) {
2178 case 'q':
2179 if (NParam > 0) {
2180 if (Param[1] < 0) Param[1] = 0;
2181 switch (Param[1]) {
2182 case 0:
2183 case 1:
2184 ts.CursorShape = IdBlkCur;
2185 ts.NonblinkingCursor = FALSE;
2186 break;
2187 case 2:
2188 ts.CursorShape = IdBlkCur;
2189 ts.NonblinkingCursor = TRUE;
2190 break;
2191 case 3:
2192 ts.CursorShape = IdHCur;
2193 ts.NonblinkingCursor = FALSE;
2194 break;
2195 case 4:
2196 ts.CursorShape = IdHCur;
2197 ts.NonblinkingCursor = TRUE;
2198 break;
2199 case 5:
2200 ts.CursorShape = IdVCur;
2201 ts.NonblinkingCursor = FALSE;
2202 break;
2203 case 6:
2204 ts.CursorShape = IdVCur;
2205 ts.NonblinkingCursor = TRUE;
2206 break;
2207 default:
2208 return;
2209 }
2210 ChangeCaret();
2211 }
2212 break;
2213 }
2214 }
2215
2216 void PrnParseCS(BYTE b) // printer mode
2217 {
2218 ParseMode = ModeFirst;
2219 switch (ICount) {
2220 /* no intermediate char */
2221 case 0:
2222 switch (Prv) {
2223 /* no private parameter */
2224 case 0:
2225 switch (b) {
2226 case 'i':
2227 if (Param[1]==4)
2228 {
2229 PrinterMode = FALSE;
2230 // clear prn buff
2231 WriteToPrnFile(0,FALSE);
2232 if (! AutoPrintMode)
2233 ClosePrnFile();
2234 return;
2235 }
2236 break;
2237 } /* of case Prv=0 */
2238 break;
2239 }
2240 break;
2241 /* one intermediate char */
2242 case 1: break;
2243 } /* of case Icount */
2244
2245 WriteToPrnFile(b,TRUE);
2246 }
2247
2248 void ParseCS(BYTE b) /* b is the final char */
2249 {
2250 if (PrinterMode) { // printer mode
2251 PrnParseCS(b);
2252 return;
2253 }
2254
2255 switch (ICount) {
2256 /* no intermediate char */
2257 case 0:
2258 switch (Prv) {
2259 /* no private parameter */
2260 case 0:
2261 switch (b) {
2262 // ISO/IEC 6429 / ECMA-48 Sequence
2263 case '@': CSInsertCharacter(); break; // ICH
2264 case 'A': CSCursorUp(); break; // CUU
2265 case 'B': CSCursorDown(); break; // CUD
2266 case 'C': CSCursorRight(); break; // CUF
2267 case 'D': CSCursorLeft(); break; // CUB
2268 case 'E': CSCursorDown1(); break; // CNL
2269 case 'F': CSCursorUp1(); break; // CPL
2270 case 'G': CSMoveToColumnN(); break; // CHA
2271 case 'H': CSMoveToXY(); break; // CUP
2272 case 'I': CSForwardTab(); break; // CHT
2273 case 'J': CSScreenErase(); break; // ED
2274 case 'K': CSLineErase(); break; // EL
2275 case 'L': CSInsertLine(); break; // IL
2276 case 'M': CSDeleteNLines(); break; // DL
2277 // case 'N': break; // EF -- Not support
2278 // case 'O': break; // EA -- Not support
2279 case 'P': CSDeleteCharacter(); break; // DCH
2280 // case 'Q': break; // SEE -- Not support
2281 // case 'R': break; // CPR -- Not support
2282 case 'S': CSScrollUP(); break; // SU
2283 case 'T': CSScrollDown(); break; // SD
2284 // case 'U': break; // NP -- Not support
2285 // case 'V': break; // PP -- Not support
2286 // case 'W': break; // CTC -- Not support
2287 case 'X': CSEraseCharacter(); break; // ECH
2288 // case 'Y': break; // CVT -- Not support
2289 case 'Z': CSBackwardTab(); break; // CBT
2290 // caes '[': break; // SRS -- Not support
2291 // caes '\\': break; // PTX -- Not support
2292 // caes ']': break; // SDS -- Not support
2293 // caes '^': break; // SIMD -- Not support
2294 case '`': CSMoveToColumnN(); break; // HPA
2295 case 'a': CSCursorRight(); break; // HPR
2296 // caes 'b': break; // REP -- Not support
2297 case 'c': AnswerTerminalType(); break; // DA
2298 case 'd': CSMoveToLineN(); break; // VPA
2299 case 'e': CSCursorUp(); break; // VPR
2300 case 'f': CSMoveToXY(); break; // HVP
2301 case 'g': CSDeleteTabStop(); break; // TBC
2302 case 'h': CS_h_Mode(); break; // SM
2303 case 'i': CS_i_Mode(); break; // MC
2304 // caes 'b': break; // HPB -- Not support
2305 // caes 'b': break; // VPB -- Not support
2306 case 'l': CS_l_Mode(); break; // RM
2307 case 'm': CSSetAttr(); break; // SGR
2308 case 'n': CS_n_Mode(); break; // DSR
2309 // caes 'o': break; // DAQ -- Not support
2310
2311 // Private Sequence
2312 case 'r': CSSetScrollRegion(); break; // DECSTBM
2313 case 's': SaveCursor(); break; // SCP (Save cursor (ANSI.SYS/SCO?))
2314 case 't': CSSunSequence(); break; // DECSLPP / Window manipulation(dtterm?)
2315 case 'u': RestoreCursor(); break; // RCP (Restore cursor (ANSI.SYS/SCO))
2316 } /* of case Prv=0 */
2317 break;
2318 /* private parameter = '>' */
2319 case '>': CSGT(b); break;
2320 /* private parameter = '?' */
2321 case '?': CSQuest(b); break;
2322 } /* end of siwtch (Prv) */
2323 break;
2324 /* one intermediate char */
2325 case 1:
2326 switch (IntChar[1]) {
2327 /* intermediate char = ' ' */
2328 case ' ': CSSpace(b); break;
2329 /* intermediate char = '!' */
2330 case '!': CSExc(b); break;
2331 /* intermediate char = '"' */
2332 case '"': CSDouble(b); break;
2333 /* intermediate char = '$' */
2334 case '$': CSDol(b); break;
2335 }
2336 break;
2337 } /* of case Icount */
2338
2339 ParseMode = ModeFirst;
2340 }
2341
2342 void ControlSequence(BYTE b)
2343 {
2344 if ((b<=US) || (b>=0x80) && (b<=0x9F))
2345 ParseControl(b); /* ctrl char */
2346 else if ((b>=0x40) && (b<=0x7E))
2347 ParseCS(b); /* terminate char */
2348 else {
2349 if (PrinterMode)
2350 WriteToPrnFile(b,FALSE);
2351
2352 if ((b>=0x20) && (b<=0x2F))
2353 { /* intermediate char */
2354 if (ICount<IntCharMax) ICount++;
2355 IntChar[ICount] = b;
2356 }
2357 else if ((b>=0x30) && (b<=0x39))
2358 {
2359 if (Param[NParam] < 0)
2360 Param[NParam] = 0;
2361 if (Param[NParam]<1000)
2362 Param[NParam] = Param[NParam]*10 + b - 0x30;
2363 }
2364 else if (b==0x3B)
2365 {
2366 if (NParam < NParamMax)
2367 {
2368 NParam++;
2369 Param[NParam] = -1;
2370 }
2371 }
2372 else if ((b>=0x3C) && (b<=0x3F))
2373 { /* private char */
2374 if (FirstPrm) Prv = b;
2375 }
2376 }
2377 FirstPrm = FALSE;
2378 }
2379
2380 void DeviceControl(BYTE b)
2381 {
2382 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2383 {
2384 ESCFlag = FALSE;
2385 ParseMode = SavedMode;
2386 return;
2387 }
2388
2389 if (b==ESC)
2390 {
2391 ESCFlag = TRUE;
2392 return;
2393 }
2394 else ESCFlag = FALSE;
2395
2396 if (b<=US)
2397 ParseControl(b);
2398 else if ((b>=0x30) && (b<=0x39))
2399 {
2400 if (Param[NParam] < 0) Param[NParam] = 0;
2401 if (Param[NParam]<1000)
2402 Param[NParam] = Param[NParam]*10 + b - 0x30;
2403 }
2404 else if (b==0x3B)
2405 {
2406 if (NParam < NParamMax)
2407 {
2408 NParam++;
2409 Param[NParam] = -1;
2410 }
2411 }
2412 else if ((b>=0x40) && (b<=0x7E))
2413 {
2414 if (b=='|')
2415 {
2416 ParseMode = ModeDCUserKey;
2417 if (Param[1] < 1) ClearUserKey();
2418 WaitKeyId = TRUE;
2419 NewKeyId = 0;
2420 }
2421 else ParseMode = ModeSOS;
2422 }
2423 }
2424
2425 void DCUserKey(BYTE b)
2426 {
2427 if (ESCFlag && (b=='\\') || (b==ST && ts.KanjiCode!=IdSJIS))
2428 {
2429 if (! WaitKeyId) DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2430 ESCFlag = FALSE;
2431 ParseMode = SavedMode;
2432 return;
2433 }
2434
2435 if (b==ESC)
2436 {
2437 ESCFlag = TRUE;
2438 return;
2439 }
2440 else ESCFlag = FALSE;
2441
2442 if (WaitKeyId)
2443 {
2444 if ((b>=0x30) && (b<=0x39))
2445 {
2446 if (NewKeyId<1000)
2447 NewKeyId = NewKeyId*10 + b - 0x30;
2448 }
2449 else if (b==0x2F)
2450 {
2451 WaitKeyId = FALSE;
2452 WaitHi = TRUE;
2453 NewKeyLen = 0;
2454 }
2455 }
2456 else {
2457 if (b==0x3B)
2458 {
2459 DefineUserKey(NewKeyId,NewKeyStr,NewKeyLen);
2460 WaitKeyId = TRUE;
2461 NewKeyId = 0;
2462 }
2463 else {
2464 if (NewKeyLen < FuncKeyStrMax)
2465 {
2466 if (WaitHi)
2467 {
2468 NewKeyStr[NewKeyLen] = ConvHexChar(b) << 4;
2469 WaitHi = FALSE;
2470 }
2471 else {
2472 NewKeyStr[NewKeyLen] = NewKeyStr[NewKeyLen] +
2473 ConvHexChar(b);
2474 WaitHi = TRUE;
2475 NewKeyLen++;
2476 }
2477 }
2478 }
2479 }
2480 }
2481
2482 void IgnoreString(BYTE b)
2483 {
2484 if ((ESCFlag && (b=='\\')) ||
2485 (b<=US && b!=ESC && b!=HT) ||
2486 (b==ST && ts.KanjiCode!=IdSJIS))
2487 ParseMode = SavedMode;
2488
2489 if (b==ESC) ESCFlag = TRUE;
2490 else ESCFlag = FALSE;
2491 }
2492
2493 BOOL XsParseColor(char *colspec, COLORREF *color)
2494 {
2495 unsigned int r, g, b;
2496 // double dr, dg, db;
2497
2498 r = g = b = 255;
2499
2500 if (colspec == NULL || color == NULL) {
2501 return FALSE;
2502 }
2503
2504 if (_strnicmp(colspec, "rgb:", 4) == 0) {
2505 switch (strlen(colspec)) {
2506 case 9: // rgb:R/G/B
2507 if (sscanf(colspec, "rgb:%1x/%1x/%1x", &r, &g, &b) != 3) {
2508 return FALSE;
2509 }
2510 r *= 17; g *= 17; b *= 17;
2511 break;
2512 case 12: // rgb:RR/GG/BB
2513 if (sscanf(colspec, "rgb:%2x/%2x/%2x", &r, &g, &b) != 3) {
2514 return FALSE;
2515 }
2516 break;
2517 case 15: // rgb:RRR/GGG/BBB
2518 if (sscanf(colspec, "rgb:%3x/%3x/%3x", &r, &g, &b) != 3) {
2519 return FALSE;
2520 }
2521 r >>= 4; g >>= 4; b >>= 4;
2522 break;
2523 case 18: // rgb:RRRR/GGGG/BBBB
2524 if (sscanf(colspec, "rgb:%4x/%4x/%4x", &r, &g, &b) != 3) {
2525 return FALSE;
2526 }
2527 r >>= 8; g >>= 8; b >>= 8;
2528 break;
2529 default:
2530 return FALSE;
2531 }
2532 }
2533 // else if (_strnicmp(colspec, "rgbi:", 5) == 0) {
2534 // ; /* nothing to do */
2535 // }
2536 else if (colspec[0] == '#') {
2537 switch (strlen(colspec)) {
2538 case 4: // #RGB
2539 if (sscanf(colspec, "#%1x%1x%1x", &r, &g, &b) != 3) {
2540 return FALSE;
2541 }
2542 r <<= 4; g <<= 4; b <<= 4;
2543 break;
2544 case 7: // #RRGGBB
2545 if (sscanf(colspec, "#%2x%2x%2x", &r, &g, &b) != 3) {
2546 return FALSE;
2547 }
2548 break;
2549 case 10: // #RRRGGGBBB
2550 if (sscanf(colspec, "#%3x%3x%3x", &r, &g, &b) != 3) {
2551 return FALSE;
2552 }
2553 r >>= 4; g >>= 4; b >>= 4;
2554 break;
2555 case 13: // #RRRRGGGGBBBB
2556 if (sscanf(colspec, "#%4x%4x%4x", &r, &g, &b) != 3) {
2557 return FALSE;
2558 }
2559 r >>= 8; g >>= 8; b >>= 8;
2560 break;
2561 default:
2562 return FALSE;
2563 }
2564 }
2565 else {
2566 return FALSE;
2567 }
2568
2569 if (r > 255 || g > 255 || b > 255) {
2570 return FALSE;
2571 }
2572
2573 *color = RGB(r, g, b);
2574 return TRUE;
2575 }
2576
2577 #define ModeXsFirst 1
2578 #define ModeXsString 2
2579 #define ModeXsColorNum 3
2580 #define ModeXsColorSpec 4
2581 #define ModeXsEsc 5
2582 void XSequence(BYTE b)
2583 {
2584 static BYTE XsParseMode = ModeXsFirst, PrevMode;
2585 static char StrBuff[sizeof(ts.Title)];
2586 static unsigned int ColorNumber, StrLen;
2587 COLORREF color;
2588
2589 switch (XsParseMode) {
2590 case ModeXsFirst:
2591 if (isdigit(b)) {
2592 if (Param[1] < 1000) {
2593 Param[1] = Param[1]*10 + b - '0';
2594 }
2595 }
2596 else if (b == ';') {
2597 StrBuff[0] = '\0';
2598 StrLen = 0;
2599 if (Param[1] == 4) {
2600 ColorNumber = 0;
2601 XsParseMode = ModeXsColorNum;
2602 }
2603 else {
2604 XsParseMode = ModeXsString;
2605 }
2606 }
2607 else {
2608 ParseMode = ModeFirst;
2609 }
2610 break;
2611 case ModeXsString:
2612 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2613 StrBuff[StrLen] = '\0';
2614 switch (Param[1]) {
2615 case 0: /* Change window title and icon name */
2616 case 1: /* Change icon name */
2617 case 2: /* Change window title */
2618 if (ts.AcceptTitleChangeRequest) {
2619 strncpy_s(cv.TitleRemote, sizeof(cv.TitleRemote), StrBuff, _TRUNCATE);
2620 // (2006.6.15 maya) �^�C�g�����n����������SJIS������
2621 ConvertToCP932(cv.TitleRemote, sizeof(cv.TitleRemote));
2622 ChangeTitle();
2623 }
2624 break;
2625 default:
2626 /* nothing to do */;
2627 }
2628 ParseMode = ModeFirst;
2629 XsParseMode = ModeXsFirst;
2630 }
2631 else if (b == ESC) { /* Escape */
2632 PrevMode = ModeXsString;
2633 XsParseMode = ModeXsEsc;
2634 }
2635 else if (b <= US) { /* Other control character -- invalid sequence */
2636 ParseMode = ModeFirst;
2637 XsParseMode = ModeXsFirst;
2638 }
2639 else if (StrLen < sizeof(StrBuff) - 1) {
2640 StrBuff[StrLen++] = b;
2641 }
2642 break;
2643 case ModeXsColorNum:
2644 if (isdigit(b)) {
2645 ColorNumber = ColorNumber*10 + b - '0';
2646 }
2647 else if (b == ';') {
2648 XsParseMode = ModeXsColorSpec;
2649 StrBuff[0] = '\0';
2650 StrLen = 0;
2651 }
2652 else {
2653 ParseMode = ModeFirst;
2654 XsParseMode = ModeXsFirst;
2655 }
2656 break;
2657 case ModeXsColorSpec:
2658 if ((b==ST && ts.KanjiCode!=IdSJIS) || b==BEL) { /* String Terminator */
2659 StrBuff[StrLen] = '\0';
2660 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2661 if (strcmp(StrBuff, "?") == 0) {
2662 color = DispGetANSIColor(ColorNumber);
2663 if (Send8BitMode) {
2664 _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2665 "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2666 GetRValue(color), GetGValue(color), GetBValue(color));
2667 }
2668 else {
2669 _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2670 "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2671 GetRValue(color), GetGValue(color), GetBValue(color));
2672 }
2673 ParseMode = ModeFirst;
2674 XsParseMode = ModeXsFirst;
2675 CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2676 break;
2677 }
2678 else if (XsParseColor(StrBuff, &color)) {
2679 DispSetANSIColor(ColorNumber, color);
2680 }
2681 }
2682 ParseMode = ModeFirst;
2683 XsParseMode = ModeXsFirst;
2684 }
2685 else if (b == ESC) {
2686 PrevMode = ModeXsColorSpec;
2687 XsParseMode = ModeXsEsc;
2688 }
2689 else if (b <= US) { /* Other control character -- invalid sequence */
2690 ParseMode = ModeFirst;
2691 XsParseMode = ModeXsFirst;
2692 }
2693 else if (b == ';') {
2694 if ((ts.ColorFlag & CF_XTERM256) && ColorNumber <= 255) {
2695 if (strcmp(StrBuff, "?") == 0) {
2696 color = DispGetANSIColor(ColorNumber);
2697 if (Send8BitMode) {
2698 _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2699 "\2354;%d;rgb:%02x/%02x/%02x\234", CLocale, ColorNumber,
2700 GetRValue(color), GetGValue(color), GetBValue(color));
2701 }
2702 else {
2703 _snprintf_s_l(StrBuff, sizeof(StrBuff), _TRUNCATE,
2704 "\033]4;%d;rgb:%02x/%02x/%02x\033\\", CLocale, ColorNumber,
2705 GetRValue(color), GetGValue(color), GetBValue(color));
2706 }
2707 XsParseMode = ModeXsColorNum;
2708 CommBinaryOut(&cv, StrBuff, strlen(StrBuff));
2709 }
2710 else if (XsParseColor(StrBuff, &color)) {
2711 DispSetANSIColor(ColorNumber, color);
2712 }
2713 }
2714 ColorNumber = 0;
2715 StrBuff[0] = '\0';
2716 StrLen = 0;
2717 XsParseMode = ModeXsColorNum;
2718 }
2719 else if (StrLen < sizeof(StrBuff) - 1) {
2720 StrBuff[StrLen++] = b;
2721 }
2722 break;
2723 case ModeXsEsc:
2724 if (b == '\\') { /* String Terminator */
2725 XsParseMode = PrevMode;
2726 // XSequence(ST);
2727 XSequence(BEL);
2728 }
2729 else { /* Other character -- invalid sequence */
2730 ParseMode = ModeFirst;
2731 XsParseMode = ModeXsFirst;
2732 }
2733 break;
2734 // default:
2735 // ParseMode = ModeFirst;
2736 // XsParseMode = ModeXsFirst;
2737 }
2738 }
2739
2740 void DLESeen(BYTE b)
2741 {
2742 ParseMode = ModeFirst;
2743 if (((ts.FTFlag & FT_BPAUTO)!=0) && (b=='B'))
2744 BPStart(IdBPAuto); /* Auto B-Plus activation */
2745 ChangeEmu = -1;
2746 }
2747
2748 void CANSeen(BYTE b)
2749 {
2750 ParseMode = ModeFirst;
2751 if (((ts.FTFlag & FT_ZAUTO)!=0) && (b=='B'))
2752 ZMODEMStart(IdZAuto); /* Auto ZMODEM activation */
2753 ChangeEmu = -1;
2754 }
2755
2756 BOOL CheckKanji(BYTE b)
2757 {
2758 BOOL Check;
2759
2760 if (ts.Language!=IdJapanese) return FALSE;
2761
2762 ConvJIS = FALSE;
2763
2764 if (ts.KanjiCode==IdSJIS)
2765 {
2766 if ((0x80<b) && (b<0xa0) || (0xdf<b) && (b<0xfd))
2767 return TRUE; // SJIS kanji
2768 if ((0xa1<=b) && (b<=0xdf))
2769 return FALSE; // SJIS katakana
2770 }
2771
2772 if ((b>=0x21) && (b<=0x7e))
2773 {
2774 Check = (Gn[Glr[0]]==IdKanji);
2775 ConvJIS = Check;
2776 }
2777 else if ((b>=0xA1) && (b<=0xFE))
2778 {
2779 Check = (Gn[Glr[1]]==IdKanji);
2780 if (ts.KanjiCode==IdEUC)
2781 Check = TRUE;
2782 else if (ts.KanjiCode==IdJIS)
2783 {
2784 if (((ts.TermFlag & TF_FIXEDJIS)!=0) &&
2785 (ts.JIS7Katakana==0))
2786 Check = FALSE; // 8-bit katakana
2787 }
2788 ConvJIS = Check;
2789 }
2790 else
2791 Check = FALSE;
2792
2793 return Check;
2794 }
2795
2796 BOOL CheckKorean(BYTE b)
2797 {
2798 BOOL Check;
2799 if (ts.Language!=IdKorean)
2800 return FALSE;
2801
2802 if (ts.KanjiCode == IdSJIS) {
2803 if ((0xA1<=b) && (b<=0xFE)) {
2804 Check = TRUE;
2805 }
2806 else {
2807 Check = FALSE;
2808 }
2809 }
2810
2811 return Check;
2812 }
2813
2814 BOOL ParseFirstJP(BYTE b)
2815 // returns TRUE if b is processed
2816 // (actually allways returns TRUE)
2817 {
2818 if (KanjiIn) {
2819 if ((! ConvJIS) && (0x3F<b) && (b<0xFD) ||
2820 ConvJIS && ( (0x20<b) && (b<0x7f) ||
2821 (0xa0<b) && (b<0xff) ))
2822 {
2823 PutKanji(b);
2824 KanjiIn = FALSE;
2825 return TRUE;
2826 }
2827 else if ((ts.TermFlag & TF_CTRLINKANJI)==0) {
2828 KanjiIn = FALSE;
2829 }
2830 else if ((b==CR) && Wrap) {
2831 CarriageReturn(FALSE);
2832 LineFeed(LF,FALSE);
2833 Wrap = FALSE;
2834 }
2835 }
2836
2837 if (SSflag) {
2838 if (Gn[GLtmp] == IdKanji) {
2839 Kanji = b << 8;
2840 KanjiIn = TRUE;
2841 SSflag = FALSE;
2842 return TRUE;
2843 }
2844 else if (Gn[GLtmp] == IdKatakana) {
2845 b = b | 0x80;
2846 }
2847
2848 PutChar(b);
2849 SSflag = FALSE;
2850 return TRUE;
2851 }
2852
2853 if ((!EUCsupIn) && (!EUCkanaIn) && (!KanjiIn) && CheckKanji(b)) {
2854 Kanji = b << 8;
2855 KanjiIn = TRUE;
2856 return TRUE;
2857 }
2858
2859 if (b<=US) {
2860 ParseControl(b);
2861 }
2862 else if (b==0x20) {
2863 PutChar(b);
2864 }
2865 else if ((b>=0x21) && (b<=0x7E)) {
2866 if (EUCsupIn) {
2867 EUCcount--;
2868 EUCsupIn = (EUCcount==0);
2869 return TRUE;
2870 }
2871
2872 if ((Gn[Glr[0]] == IdKatakana) || EUCkanaIn) {
2873 b = b | 0x80;
2874 EUCkanaIn = FALSE;
2875 }
2876 PutChar(b);
2877 }
2878 else if (b==0x7f) {
2879 return TRUE;
2880 }
2881 else if ((b>=0x80) && (b<=0x8D)) {
2882 ParseControl(b);
2883 }
2884 else if (b==0x8E) { // SS2
2885 if (ts.KanjiCode==IdEUC) {
2886 EUCkanaIn = TRUE;
2887 }
2888 else {
2889 ParseControl(b);
2890 }
2891 }
2892 else if (b==0x8F) { // SS3
2893 if (ts.KanjiCode==IdEUC) {
2894 EUCcount = 2;
2895 EUCsupIn = TRUE;
2896 }
2897 else {
2898 ParseControl(b);
2899 }
2900 }
2901 else if ((b>=0x90) && (b<=0x9F)) {
2902 ParseControl(b);
2903 }
2904 else if (b==0xA0) {
2905 PutChar(0x20);
2906 }
2907 else if ((b>=0xA1) && (b<=0xFE)) {
2908 if (EUCsupIn) {
2909 EUCcount--;
2910 EUCsupIn = (EUCcount==0);
2911 return TRUE;
2912 }
2913
2914 if ((Gn[Glr[1]] != IdASCII) ||
2915 (ts.KanjiCode==IdEUC) && EUCkanaIn ||
2916 (ts.KanjiCode==IdSJIS) ||
2917 (ts.KanjiCode==IdJIS) &&
2918 (ts.JIS7Katakana==0) &&
2919 ((ts.TermFlag & TF_FIXEDJIS)!=0))
2920 PutChar(b); // katakana
2921 else {
2922 if (Gn[Glr[1]] == IdASCII) {
2923 b = b & 0x7f;
2924 }
2925 PutChar(b);
2926 }
2927 EUCkanaIn = FALSE;
2928 }
2929 else {
2930 PutChar(b);
2931 }
2932
2933 return TRUE;
2934 }
2935
2936 BOOL ParseFirstKR(BYTE b)
2937 // returns TRUE if b is processed
2938 // (actually allways returns TRUE)
2939 {
2940 if (KanjiIn) {
2941 if ((0x41<=b) && (b<=0x5A) ||
2942 (0x61<=b) && (b<=0x7A) ||
2943 (0x81<=b) && (b<=0xFE))
2944 {
2945 PutKanji(b);
2946 KanjiIn = FALSE;
2947 return TRUE;
2948 }
2949 else if ((ts.TermFlag & TF_CTRLINKANJI)==0) {
2950 KanjiIn = FALSE;
2951 }
2952 else if ((b==CR) && Wrap) {
2953 CarriageReturn(FALSE);
2954 LineFeed(LF,FALSE);
2955 Wrap = FALSE;
2956 }
2957 }
2958
2959 if ((!KanjiIn) && CheckKorean(b)) {
2960 Kanji = b << 8;
2961 KanjiIn = TRUE;
2962 return TRUE;
2963 }
2964
2965 if (b<=US) {
2966 ParseControl(b);
2967 }
2968 else if (b==0x20) {
2969 PutChar(b);
2970 }
2971 else if ((b>=0x21) && (b<=0x7E)) {
2972 // if (Gn[Glr[0]] == IdKatakana) {
2973 // b = b | 0x80;
2974 // }
2975 PutChar(b);
2976 }
2977 else if (b==0x7f) {
2978 return TRUE;
2979 }
2980 else if ((0x80<=b) && (b<=0x9F)) {
2981 ParseControl(b);
2982 }
2983 else if (b==0xA0) {
2984 PutChar(0x20);
2985 }
2986 else if ((b>=0xA1) && (b<=0xFE)) {
2987 if (Gn[Glr[1]] == IdASCII) {
2988 b = b & 0x7f;
2989 }
2990 PutChar(b);
2991 }
2992 else {
2993 PutChar(b);
2994 }
2995
2996 return TRUE;
2997 }
2998
2999
3000 static void ParseASCII(BYTE b)
3001 {
3002 if (b<=US) {
3003 ParseControl(b);
3004 } else if ((b>=0x20) && (b<=0x7E)) {
3005 //Kanji = 0;
3006 //PutKanji(b);
3007 PutChar(b);
3008 } else if ((b>=0x80) && (b<=0x9F)) {
3009 ParseControl(b);
3010 } else if (b>=0xA0) {
3011 //Kanji = 0;
3012 //PutKanji(b);
3013 PutChar(b);
3014 }
3015 }
3016
3017 //
3018 // UTF-8
3019 //
3020 #include "uni2sjis.map"
3021 #include "unisym2decsp.map"
3022 extern unsigned short ConvertUnicode(unsigned short code, codemap_t *table, int tmax);
3023
3024
3025 //
3026 // UTF-8 for Mac OS X(HFS plus)
3027 //
3028 #include "hfs_plus.map"
3029
3030 unsigned short GetIllegalUnicode(int start_index, unsigned short first_code, unsigned short code,
3031 hfsplus_codemap_t *table, int tmax)
3032 {
3033 unsigned short result = 0;
3034 int i;
3035
3036 for (i = start_index ; i < tmax ; i++) {
3037 if (table[i].first_code != first_code) { // 1�������������������A���~���������������������B
3038 break;
3039 }
3040
3041 if (table[i].second_code == code) {
3042 result = table[i].illegal_code;
3043 break;
3044 }
3045 }
3046
3047 return (result);
3048 }
3049
3050 int GetIndexOfHFSPlusFirstCode(unsigned short code, hfsplus_codemap_t *table, int tmax)
3051 {
3052 int low, mid, high;
3053 int index = -1;
3054
3055 low = 0;
3056 high = tmax - 1;
3057
3058 // binary search
3059 while (low < high) {
3060 mid = (low + high) / 2;
3061 if (table[mid].first_code < code) {
3062 low = mid + 1;
3063 } else {
3064 high = mid;
3065 }
3066 }
3067
3068 if (table[low].first_code == code) {
3069 while (low >= 0 && table[low].first_code == code) {
3070 index = low;
3071 low--;
3072 }
3073 }
3074
3075 return (index);
3076 }
3077
3078
3079 static void UnicodeToCP932(unsigned int code)
3080 {
3081 int ret;
3082 char mbchar[32];
3083 unsigned char wchar[32];
3084 unsigned short cset = 0;
3085
3086 #if 0
3087 Kanji = code & 0xff00;
3088 PutKanji(code & 0x00ff);
3089 return;
3090 #else
3091
3092 wchar[0] = code & 0xff;
3093 wchar[1] = (code >> 8) & 0xff;
3094
3095 if (ts.UnicodeDecSpMapping) {
3096 cset = ConvertUnicode(code, mapUnicodeSymbolToDecSp, MAPSIZE(mapUnicodeSymbolToDecSp));
3097 }
3098 if (((cset >> 8) & ts.UnicodeDecSpMapping) != 0) {
3099 PutDecSp(cset & 0xff);
3100 }
3101 else {
3102 // Unicode -> CP932
3103 ret = wctomb(mbchar, ((wchar_t *)wchar)[0]);
3104 switch (ret) {
3105 case -1:
3106 if (_stricmp(ts.Locale, DEFAULT_LOCALE) == 0) {
3107 // U+301C�������������������BUnicode -> Shift_JIS���������������B
3108 cset = ConvertUnicode(code, mapUnicodeToSJIS, MAPSIZE(mapUnicodeToSJIS));
3109 if (cset != 0) {
3110 Kanji = cset & 0xff00;
3111 PutKanji(cset & 0x00ff);
3112 }
3113 }
3114
3115 if (cset == 0) {
3116 ParseASCII('?');
3117 if (ts.UnknownUnicodeCharaAsWide) {
3118 ParseASCII('?');
3119 }
3120 }
3121 break;
3122 case 1:
3123 ParseASCII(mbchar[0]);
3124 break;
3125 default:
3126 Kanji = mbchar[0] << 8;
3127 PutKanji(mbchar[1]);
3128 break;
3129 }
3130 }
3131 #endif
3132 }
3133
3134 // UTF-8�����M�f�[�^����������
3135 BOOL ParseFirstUTF8(BYTE b, int hfsplus_mode)
3136 // returns TRUE if b is processed
3137 // (actually allways returns TRUE)
3138 {
3139 static BYTE buf[3];
3140 static int count = 0;
3141 static int maybe_hfsplus = 0;
3142 static unsigned int first_code;
3143 static int first_code_index;
3144
3145 unsigned int code;
3146 char mbchar[32];
3147 unsigned short cset;
3148 char *locptr;
3149
3150 locptr = setlocale(LC_ALL, ts.Locale);
3151
3152 if ((b & 0x80) != 0x80 || ((b & 0xe0) == 0x80 && count == 0)) {
3153 // 1�o�C�g��������2�o�C�g����ASCII���������A������ASCII�o���������B
3154 // 1�o�C�g����C1��������(0x80-0x9f)�����������l�B
3155 if (count == 0 || count == 1) {
3156 if (hfsplus_mode == 1 && maybe_hfsplus == 1) {
3157 UnicodeToCP932(first_code);
3158 maybe_hfsplus = 0;
3159 }
3160
3161 if (count == 1) {
3162 ParseASCII(buf[0]);
3163 }
3164 ParseASCII(b);
3165
3166 count = 0; // reset counter
3167 return TRUE;
3168 }
3169 }
3170
3171 buf[count++] = b;
3172 if (count < 2) {
3173 return TRUE;
3174 }
3175
3176 memset(mbchar, 0, sizeof(mbchar));
3177
3178 // 2�o�C�g�R�[�h������
3179 if ((buf[0] & 0xe0) == 0xc0) {
3180 if ((buf[1] & 0xc0) == 0x80) {
3181
3182 if (hfsplus_mode == 1 && maybe_hfsplus == 1) {
3183 UnicodeToCP932(first_code);
3184 maybe_hfsplus = 0;
3185 }
3186
3187 code = ((buf[0] & 0x1f) << 6);
3188 code |= ((buf[1] & 0x3f));
3189
3190 UnicodeToCP932(code);
3191 }
3192 else {
3193 ParseASCII(buf[0]);
3194 ParseASCII(buf[1]);
3195 }
3196 count = 0;
3197 return TRUE;
3198 }
3199
3200 if (count < 3) {
3201 return TRUE;
3202 }
3203
3204 if ((buf[0] & 0xe0) == 0xe0 &&
3205 (buf[1] & 0xc0) == 0x80 &&
3206 (buf[2] & 0xc0) == 0x80) { // 3�o�C�g�R�[�h������
3207
3208 // UTF-8 BOM(Byte Order Mark)
3209 if (buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf) {
3210 goto skip;
3211 }
3212
3213 code = ((buf[0] & 0xf) << 12);
3214 code |= ((buf[1] & 0x3f) << 6);
3215 code |= ((buf[2] & 0x3f));
3216
3217 if (hfsplus_mode == 1) {
3218 if (maybe_hfsplus == 0) {
3219 if ((first_code_index = GetIndexOfHFSPlusFirstCode(
3220 code, mapHFSPlusUnicode, MAPSIZE(mapHFSPlusUnicode)
3221 )) != -1) {
3222 maybe_hfsplus = 1;
3223 first_code = code;
3224 count = 0;
3225 return (TRUE);
3226 }
3227 } else {
3228 maybe_hfsplus = 0;
3229 cset = GetIllegalUnicode(first_code_index, first_code, code, mapHFSPlusUnicode, MAPSIZE(mapHFSPlusUnicode));
3230 if (cset != 0) { // success
3231 code = cset;
3232
3233 } else { // error
3234 // 2�����������������_��1�����������������������A���x�������������B(2005.10.15 yutaka)
3235 if ((first_code_index = GetIndexOfHFSPlusFirstCode(
3236 code, mapHFSPlusUnicode, MAPSIZE(mapHFSPlusUnicode)
3237 )) != -1) {
3238
3239 // 1���������������������o������
3240 UnicodeToCP932(first_code);
3241
3242 maybe_hfsplus = 1;
3243 first_code = code;
3244 count = 0;
3245 return (TRUE);
3246 }
3247
3248 UnicodeToCP932(first_code);
3249 UnicodeToCP932(code);
3250 count = 0;
3251 return (TRUE);
3252 }
3253 }
3254 }
3255
3256 UnicodeToCP932(code);
3257
3258 skip:
3259 count = 0;
3260
3261 } else {
3262 ParseASCII(buf[0]);
3263 ParseASCII(buf[1]);
3264 ParseASCII(buf[2]);
3265 count = 0;
3266
3267 }
3268
3269 return TRUE;
3270 }
3271
3272
3273 BOOL ParseFirstRus(BYTE b)
3274 // returns if b is processed
3275 {
3276 if (b>=128)
3277 {
3278 b = RussConv(ts.RussHost,ts.RussClient,b);
3279 PutChar(b);
3280 return TRUE;
3281 }
3282 return FALSE;
3283 }
3284
3285 void ParseFirst(BYTE b)
3286 {
3287 switch (ts.Language) {
3288 case IdUtf8:
3289 ParseFirstUTF8(b, ts.KanjiCode == IdUTF8m);
3290 return;
3291
3292 case IdJapanese:
3293 switch (ts.KanjiCode) {
3294 case IdUTF8:
3295 if (ParseFirstUTF8(b, 0)) {
3296 return;
3297 }
3298 break;
3299 case IdUTF8m:
3300 if (ParseFirstUTF8(b, 1)) {
3301 return;
3302 }
3303 break;
3304 default:
3305 if (ParseFirstJP(b)) {
3306 return;
3307 }
3308 }
3309 break;
3310
3311 case IdKorean:
3312 switch (ts.KanjiCode) {
3313 case IdUTF8:
3314 if (ParseFirstUTF8(b, 0)) {
3315 return;
3316 }
3317 break;
3318 case IdUTF8m:
3319 if (ParseFirstUTF8(b, 1)) {
3320 return;
3321 }
3322 break;
3323 default:
3324 if (ParseFirstKR(b)) {
3325 return;
3326 }
3327 }
3328 break;
3329
3330
3331 case IdRussian:
3332 if (ParseFirstRus(b)) {
3333 return;
3334 }
3335 break;
3336 }
3337
3338 if (SSflag) {
3339 PutChar(b);
3340 SSflag = FALSE;
3341 return;
3342 }
3343
3344 if (b<=US)
3345 ParseControl(b);
3346 else if ((b>=0x20) && (b<=0x7E))
3347 PutChar(b);
3348 else if ((b>=0x80) && (b<=0x9F))
3349 ParseControl(b);
3350 else if (b>=0xA0)
3351 PutChar(b);
3352 }
3353
3354 int VTParse()
3355 {
3356 BYTE b;
3357 int c;
3358
3359 c = CommRead1Byte(&cv,&b);
3360
3361 if (c==0) return 0;
3362
3363 CaretOff();
3364 UpdateCaretPosition(FALSE); // ���A�N�e�B�u�������������`������
3365
3366 ChangeEmu = 0;
3367
3368 /* Get