Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/ttcomtester/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9313 - (hide annotations) (download) (as text)
Sun Jun 20 01:07:40 2021 UTC (2 years, 9 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 153630 byte(s)
locale設定を削除

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