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 9118 - (hide annotations) (download) (as text)
Mon Dec 28 15:38:09 2020 UTC (3 years, 3 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/vtterm.c
File MIME type: text/x-csrc
File size: 153851 byte(s)
mbstring.h を削除

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