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 9104 - (hide annotations) (download) (as text)
Sun Dec 20 12:16:27 2020 UTC (3 years, 3 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 152940 byte(s)
ttftypes.h への依存を減らした

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