forked from Maximus5/ConEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnsi.h
285 lines (243 loc) · 10.2 KB
/
Ansi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright (c) 2013-2016 Maximus5
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#ifdef _DEBUG
#define DUMP_WRITECONSOLE_LINES
#define DUMP_UNKNOWN_ESCAPES
#endif
extern DWORD AnsiTlsIndex;
//#include "../common/MMap.h"
#include "../common/ConsoleMixAttr.h"
#include "ExtConsole.h"
#include "hkConsoleOutput.h"
#define CEAnsi_MaxPrevPart 512
#define CEAnsi_MaxPrevAnsiPart 256
struct MSectionSimple;
enum WriteProcessedStream
{
wps_None = 0,
wps_Output = 1,
wps_Error = 2,
wps_Input = 4, // Reserved for StdInput
wps_Ansi = 8, // Reserved as a Flag for IsAnsiCapable
};
#if defined(__GNUC__)
extern "C" {
#endif
BOOL WINAPI WriteProcessed(LPCWSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten);
BOOL WINAPI WriteProcessed2(LPCWSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, WriteProcessedStream Stream);
BOOL WINAPI WriteProcessed3(LPCWSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, HANDLE hConsoleOutput);
BOOL WINAPI WriteProcessedA(LPCSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, WriteProcessedStream Stream);
#if defined(__GNUC__)
};
#endif
extern FARPROC CallWriteConsoleW;
// In debug build if SHOW_FIRST_ANSI_CALL is defined,
// MsgBox may be shown on first ANSI sequence printout
#ifdef _DEBUG
extern void FIRST_ANSI_CALL(const BYTE* lpBuf, DWORD nNumberOfBytes); // Entry.cpp
#else
#define FIRST_ANSI_CALL(lpBuf,nNumberOfBytes)
#endif
enum DumpEscapeCodes
{
de_Normal = 0,
de_Unknown = 1,
de_BadUnicode = 2,
de_Ignored = 3,
de_UnkControl = 4,
de_Report = 5,
de_ScrollRegion = 6,
de_Comment = 7,
};
struct CpCvt;
struct CEAnsi
{
//private:
// static MMap<DWORD,CEAnsi*> AnsiTls;
public:
/* ************************************* */
/* Init and release thread local storage */
/* ************************************* */
static CEAnsi* Object(bool bForceCreate = false);
static void Release();
public:
/* ************************************* */
/* STATIC Helper routines */
/* ************************************* */
static void StartVimTerm(bool bFromDllStart);
static void StopVimTerm();
static BOOL OurWriteConsoleW(HANDLE hConsoleOutput, const VOID *lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, LPVOID lpReserved, bool bInternal = true);
static void OnReadConsoleBefore(HANDLE hConOut, const CONSOLE_SCREEN_BUFFER_INFO& csbi);
static void OnReadConsoleAfter(bool bFinal, bool bNoLineFeed);
static void InitAnsiLog(LPCWSTR asFilePath);
static void DoneAnsiLog(bool bFinal);
static void GetFeatures(bool* pbAnsiAllowed, bool* pbSuppressBells);
static SHORT GetDefaultTextAttr();
static HANDLE ghAnsiLogFile /*= NULL*/;
static LONG gnEnterPressed /*= 0*/;
static bool gbAnsiLogNewLine /*= false*/;
static bool gbAnsiWasNewLine /*= false*/;
static MSectionSimple* gcsAnsiLogFile;
static bool gbWasXTermOutput;
protected:
static int NextNumber(LPCWSTR& asMS);
public:
static void ChangeTermMode(TermModeCommand mode, DWORD value, DWORD nPID = 0);
static void StartXTermMode(bool bStart);
static void StorePromptBegin();
public:
struct AnsiEscCode
{
wchar_t First; // ESC (27)
wchar_t Second; // any of 64 to 95 ('@' to '_')
wchar_t Action; // any of 64 to 126 (@ to ~). this is terminator
wchar_t Skip; // Если !=0 - то эту последовательность нужно пропустить
int ArgC;
int ArgV[16];
LPCWSTR ArgSZ; // Reserved for key mapping
size_t cchArgSZ;
#ifdef _DEBUG
LPCWSTR pszEscStart;
size_t nTotalLen;
#endif
int PvtLen;
wchar_t Pvt[16];
};
public:
/* ************************************* */
/* Working methods */
/* ************************************* */
// NON-static, because we need to ‘cache’ parts of non-translated MBCS chars (one UTF-8 symbol may be transmitted by up to *three* parts)
BOOL OurWriteConsoleA(HANDLE hConsoleOutput, const char *lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten);
// Unicode method
BOOL WriteAnsiCodes(OnWriteConsoleW_t _WriteConsoleW, HANDLE hConsoleOutput, LPCWSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten);
protected:
CpCvt* mp_Cvt;
protected:
void WriteAnsiCode_CSI(OnWriteConsoleW_t _WriteConsoleW, HANDLE hConsoleOutput, AnsiEscCode& Code, BOOL& lbApply);
void WriteAnsiCode_OSC(OnWriteConsoleW_t _WriteConsoleW, HANDLE hConsoleOutput, AnsiEscCode& Code, BOOL& lbApply);
void WriteAnsiCode_VIM(OnWriteConsoleW_t _WriteConsoleW, HANDLE hConsoleOutput, AnsiEscCode& Code, BOOL& lbApply);
BOOL ReportString(LPCWSTR asRet);
void ReportConsoleTitle();
void ReportTerminalPixelSize();
static BOOL WriteAnsiLogUtf8(const char* lpBuffer, DWORD nChars);
public:
static UINT GetCodePage();
static void WriteAnsiLogA(LPCSTR lpBuffer, DWORD nChars);
static void WriteAnsiLogW(LPCWSTR lpBuffer, DWORD nChars);
static void WriteAnsiLogFarPrompt();
static void AnsiLogEnterPressed();
protected:
static void XTermSaveRestoreCursor(bool bSaveCursor, HANDLE hConsoleOutput = NULL);
static void XTermAltBuffer(bool bSetAltBuffer);
public:
void ReSetDisplayParm(HANDLE hConsoleOutput, BOOL bReset, BOOL bApply);
static void DumpEscape(LPCWSTR buf, size_t cchLen, DumpEscapeCodes iUnknown);
BOOL WriteText(OnWriteConsoleW_t _WriteConsoleW, HANDLE hConsoleOutput, LPCWSTR lpBuffer, DWORD nNumberOfCharsToWrite, LPDWORD lpNumberOfCharsWritten, BOOL abCommit = FALSE, EXTREADWRITEFLAGS AddFlags = ewtf_None);
BOOL ScrollLine(HANDLE hConsoleOutput, int nDir);
BOOL ScrollScreen(HANDLE hConsoleOutput, int nDir);
BOOL PadAndScroll(HANDLE hConsoleOutput, CONSOLE_SCREEN_BUFFER_INFO& csbi);
BOOL FullReset(HANDLE hConsoleOutput);
BOOL ForwardLF(HANDLE hConsoleOutput, BOOL& bApply);
BOOL ReverseLF(HANDLE hConsoleOutput, BOOL& bApply);
BOOL LinesInsert(HANDLE hConsoleOutput, const int LinesCount);
BOOL LinesDelete(HANDLE hConsoleOutput, const int LinesCount);
void DoSleep(LPCWSTR asMS);
void EscCopyCtrlString(wchar_t* pszDst, LPCWSTR asMsg, INT_PTR cchMaxLen);
void DoMessage(LPCWSTR asMsg, INT_PTR cchLen);
void DoProcess(LPCWSTR asCmd, INT_PTR cchLen);
void DoGuiMacro(LPCWSTR asCmd, INT_PTR cchLen);
void DoPrintEnv(LPCWSTR asCmd, INT_PTR cchLen);
void DoSendCWD(LPCWSTR asCmd, INT_PTR cchLen);
bool IsAnsiExecAllowed(LPCWSTR asCmd);
int NextEscCode(LPCWSTR lpBuffer, LPCWSTR lpEnd, wchar_t (&szPreDump)[CEAnsi_MaxPrevPart], DWORD& cchPrevPart, LPCWSTR& lpStart, LPCWSTR& lpNext, AnsiEscCode& Code, BOOL ReEntrance = FALSE);
protected:
/* ************************************* */
/* Instance variables */
/* ************************************* */
OnWriteConsoleW_t pfnWriteConsoleW;
HANDLE mh_WriteOutput;
struct DisplayParm
{
BOOL WasSet;
UINT BrightOrBold; // 1
BOOL ItalicOrInverse; // 3
UINT BackOrUnderline; // 4
int TextColor; // 30-37,38,39
BOOL Text256; // 38
int BackColor; // 40-47,48,49
BOOL Back256; // 48
// xterm
BOOL Inverse;
}; // gDisplayParm = {};
// Bad thing... Thought, it must be synced between thread, but when?
static DisplayParm gDisplayParm;
friend BOOL WINAPI OnWriteConsoleOutputCharacterA(HANDLE hConsoleOutput, LPCSTR lpCharacter, DWORD nLength, COORD dwWriteCoord, LPDWORD lpNumberOfCharsWritten);
friend BOOL WINAPI OnWriteConsoleOutputCharacterW(HANDLE hConsoleOutput, LPCWSTR lpCharacter, DWORD nLength, COORD dwWriteCoord, LPDWORD lpNumberOfCharsWritten);
struct DisplayCursorPos
{
// Internal
BOOL bCursorPosStored;
COORD StoredCursorPos;
// Esc[?1h Set cursor key to application DECCKM
// Esc[?1l Set cursor key to cursor DECCKM
BOOL CursorKeysApp; // "?1h"
}; // gDisplayCursor = {};
// Bad thing again...
static DisplayCursorPos gDisplayCursor;
struct DisplayOpt
{
BOOL WrapWasSet;
SHORT WrapAt; // Rightmost X coord (1-based)
//
BOOL AutoLfNl; // LF/NL (default off): Automatically follow echo of LF, VT or FF with CR.
//
BOOL ScrollRegion;
SHORT ScrollStart, ScrollEnd; // 0-based absolute line indexes
//
BOOL ShowRawAnsi; // \e[3h display ANSI control characters (TRUE), \e[3l process ANSI (FALSE, normal mode)
}; // gDisplayOpt;
// Bad thing again...
static DisplayOpt gDisplayOpt;
// Store absolute coords by relative ANSI values
void SetScrollRegion(bool bRegion, bool bRelative = true, int nStart = 0, int nEnd = 0, HANDLE hConsoleOutput = NULL);
wchar_t gsPrevAnsiPart[CEAnsi_MaxPrevPart]; // = {};
INT_PTR gnPrevAnsiPart; // = 0;
wchar_t gsPrevAnsiPart2[CEAnsi_MaxPrevPart]; // = {};
INT_PTR gnPrevAnsiPart2; // = 0;
bool mb_SuppressBells;
// In "ReadLine" we can't control scrolling
// thats why we need to mark some rows for identification
struct XtermStoreMarks
{
// [0] - above CursorPos, but if CursorPos was on FIRST line - it can't be...
// [1] - exactly on CursorPos, but if CursorX on the FIRST column - this will be illegal
SHORT SaveRow[2];
WORD RowId[2];
CONSOLE_SCREEN_BUFFER_INFO csbi;
} m_RowMarks;
};