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 9311 - (hide annotations) (download) (as text)
Fri Jun 18 16:05:10 2021 UTC (2 years, 9 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 153898 byte(s)
ANSI送信関数を削除

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