Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/vtterm.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10128 - (hide annotations) (download) (as text)
Tue Aug 9 01:08:12 2022 UTC (20 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 152334 byte(s)
/sdl オプションが使用できるよう修正

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