-
Notifications
You must be signed in to change notification settings - Fork 4
/
termcapparser.cc
411 lines (343 loc) · 10.7 KB
/
termcapparser.cc
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "puttyparser.hh"
#include "terminal.h"
#include "putty/termcapparser.hh"
#include "state_p.hh"
#include <sstream>
#include <ostream>
using namespace Putty;
namespace
{
static const int CONTROL_SEQUENCE_LENGTH = 8;
static const char ESCAPE_CHAR = '\033';
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
/**
* Determine if a character is a unicode combining character.
*
* For information about combining characters, see:
*
* http://en.wikipedia.org/wiki/Combining_character
*
* For the unicode character sheets, see:
*
* Combining Diacritical Marks: http://www.unicode.org/charts/PDF/U0300.pdf
* Combining Diacritical Marks Supplement: http://www.unicode.org/charts/PDF/U1DC0.pdf
* Combining Diacritical Marks for Symbols: http://www.unicode.org/charts/PDF/U20D0.pdf
* Combining Half Marks: http://www.unicode.org/charts/PDF/UFE20.pdf
*
*/
bool is_combining_character(wchar_t chr)
{
if (likely(chr < 0x0300))
return false;
// Diacritical marks
if (chr >= 0x0300 && chr <= 0x036F)
return true;
// Diacritical marks supplement - range 1
if (chr >= 0x1DC0 && chr <= 0x1DE6)
return true;
// Diacritical marks supplement - range 2
if (chr >= 0x1DFC && chr <= 0x1DFF)
return true;
// Diacritical marks for symbols
if (chr >= 0x20D0 && chr <= 0x20F0)
return true;
// Half marks
if (chr >= 0xFE20 && chr <= 0xFE26)
return true;
return false;
}
/**
* Helper class for changing the value of a variable temporarily (just for the given scope)
* in an exception-safe manner. When the destructor is called (when exiting the scope), the
* value is re-set to its old value.
*/
template < typename _Type >
class TemporalValueChange
{
public:
/**
* Initialize helper object. The reference value is changed to the new value.
*
* @param ref Reference to variable to change.
* @param value New value of the variable.
*/
TemporalValueChange(_Type &ref, const _Type &value)
: ref(ref),
oldval(ref)
{
ref = value;
}
/**
* Destroy the helper object. The referenced variable is set to its old value.
*/
~TemporalValueChange()
{
ref = oldval;
}
private:
_Type &ref; /**< Variable reference */
_Type oldval; /**< Old value of the variable (before change) */
};
inline void move_on(const char *&data, int &size, int offset)
{
data = data + offset;
size -= offset;
}
inline bool is_control_sequence_partial(int data_length)
{
return data_length < CONTROL_SEQUENCE_LENGTH;
}
inline int get_first_control_sequence_pos(const char *data, int len)
{
int check_length = len - 1;
for (int pos = 0; pos < check_length; ++pos)
{
if ((data[pos] == ESCAPE_CHAR) && (data[pos + 1] == 'P'))
{
return pos;
}
}
return -1;
}
}
TermcapParser::TermcapParser(const char *charset, int terminal_buffer_height)
: enable_update_display(true),
terminal_buffer_height(terminal_buffer_height),
log_callback(0)
{
/* Create an instance structure and initialise to zeroes */
inst = snew(struct gui_data);
memset(inst, 0, sizeof(*inst));
/* Set the termcap parser object */
inst->parser = this;
/* initialize unicode config */
init_ucs(&inst->ucsdata, charset, 0, CS_UTF8, VT_UNICODE);
/* Enable ANSI and XTerm 256 colors */
inst->cfg.ansi_colour = 1;
inst->cfg.xterm_256_colour = 1;
/* Configure BiDirectional and Arabic text support, 0 means enable and 1 means disable */
inst->cfg.bidi = 1;
inst->cfg.arabicshaping = 1;
/* DO NOT REMOVE! This config option ensures that an erase will be done with the proper bg color. */
inst->cfg.bce = 1;
/*
* LF implies a CR-LF since in some cases (like ssh command execution) only LF is sent.
* By default, we don't enable this but replace_standalone_linefeeds() can be used to
* change this behaviour.
*/
inst->cfg.lfhascr = 0;
/*
* Set autowrap long lines.
*/
inst->cfg.wrap_mode = 1;
/* initialize terminal structure */
inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
inst->term->ldisc = 0;
/* Set log context */
inst->logctx = NULL;
term_provide_logctx(inst->term, inst->logctx);
/* set terminal visual size: row number, column number, saveline option */
set_terminal_size(80, 24);
}
TermcapParser::~TermcapParser()
{
term_free(inst->term);
sfree(inst);
}
void
TermcapParser::data_input(const char *data, int len)
{
const char *data_to_process = data;
int data_to_process_len = len;
if (buffered_data_input.size() > 0)
{
buffered_data_input.append(data, len);
data_to_process = buffered_data_input.c_str();
data_to_process_len = buffered_data_input.size();
}
while (data_to_process_len > 0)
{
if (data_to_process_len == 1 && data_to_process[0] == ESCAPE_CHAR)
{
buffered_data_input.resize(1);
buffered_data_input[0] = ESCAPE_CHAR;
break;
}
int first_control_sequence_pos = get_first_control_sequence_pos(data_to_process, data_to_process_len);
if (first_control_sequence_pos != -1)
{
data_input_filtered(data_to_process, first_control_sequence_pos);
move_on(data_to_process, data_to_process_len, first_control_sequence_pos);
if (is_control_sequence_partial(data_to_process_len))
{
buffered_data_input = std::string(data_to_process, data_to_process_len);
break;
}
// Skip the control sequence
move_on(data_to_process, data_to_process_len, CONTROL_SEQUENCE_LENGTH);
}
else // Leftover
{
data_input_filtered(data_to_process, data_to_process_len);
data_to_process = 0;
data_to_process_len = 0;
}
if (data_to_process_len <= 0)
buffered_data_input.clear();
}
}
void
TermcapParser::data_input_filtered(const char *data, int len)
{
/* inject input in the terminal */
term_data(inst->term, 0, data, len);
}
void
TermcapParser::set_cell(int row, int col, const std::wstring &characters, uint64_t attr) const
{
bool success = ::set_cell(state, row, col, characters, attr);
if (!success)
{
std::ostringstream stream;
stream << "Invalid position requested to be updated; row='" << row << "', " << "col='" << col << "'";
log_message(stream.str());
}
}
void
TermcapParser::set_buffer_size(int width, int height)
{
inst->width = width;
inst->height = height;
}
void
TermcapParser::set_terminal_size(int width, int height)
{
resize_display(state, width, height);
int max_height = std::max(terminal_buffer_height, height);
term_size(inst->term, height, width, max_height - height);
set_buffer_size(width, max_height);
}
void
TermcapParser::copy_term_content_to_cache(int offset, int row_count) const
{
term_scroll(inst->term, -1, offset);
std::wstring characters;
int absolute_offset = 0;
for (int row = 0; row < row_count; ++row)
{
if (!is_valid_row(state, offset + row))
{
std::ostringstream stream;
stream << "Invalid row requested to be updated; row='" << row << "', offset='" << offset << "'";
log_message(stream.str());
continue;
}
state.rows[offset + row + state.buffer_size].attributes = inst->term->disptext[row]->lattr;
for (auto col = 0; col < inst->term->cols; ++col)
{
int relative_offset;
absolute_offset = col;
// load the character and all its combining characters
do
{
characters.push_back( inst->term->disptext[row]->chars[ absolute_offset ].chr );
relative_offset = inst->term->disptext[row]->chars[ absolute_offset ].cc_next;
absolute_offset += relative_offset;
} while(relative_offset != 0);
set_cell(offset + row, col, characters, inst->term->disptext[row]->chars[col].attr);
characters.clear();
}
}
}
void
TermcapParser::log_message(const std::string &message) const
{
if (log_callback)
log_callback(message);
}
const State &
TermcapParser::get_state() const
{
int buffer_line_count = sblines(inst->term);
if (buffer_line_count < 0)
{
std::ostringstream stream;
stream << "Negative scrollback lines received; number='" << buffer_line_count << "'";
log_message(stream.str());
buffer_line_count = 0;
}
if (buffer_line_count > terminal_buffer_height)
{
std::ostringstream stream;
stream << "Too big scrollback lines received; number='" << buffer_line_count << "'";
log_message(stream.str());
buffer_line_count = terminal_buffer_height;
}
resize(state, inst->term->cols, inst->term->rows, buffer_line_count);
state.palette = palette;
/* update terminal display buffer */
term_update(inst->term);
{
enable_update_display = false;
/* write buffer content */
int offset;
for (offset = -buffer_line_count; offset <= -inst->term->rows; offset += inst->term->rows)
{
copy_term_content_to_cache(offset, inst->term->rows);
}
/* write remainder buffer content which is less than a complete terminal screen */
copy_term_content_to_cache(offset, -offset);
enable_update_display = true;
}
state.cursor = {inst->term->curs.x, inst->term->curs.y, inst->term->cursor_on == 1};
/*
* Scroll to the current content of the display. The content will be updated
* automatically by update_display.
*/
term_scroll(inst->term, -1, 0);
state.alternate_screen = (inst->term->alt_which != 0);
return state;
}
void
TermcapParser::replace_standalone_linefeeds(bool enable)
{
inst->term->cfg.lfhascr = (enable ? 1 : 0);
}
void
TermcapParser::clear_buffer()
{
term_clrsb(inst->term);
}
void
TermcapParser::update_display(int x, int y, const std::wstring &str, unsigned long attr, long lattr)
{
if (!enable_update_display)
return;
std::wstring chr;
if (!is_valid_row(state, y))
{
std::ostringstream stream;
stream << "Invalid row requested to be updated; row='" << y << "'";
log_message(stream.str());
return;
}
state.rows[y + state.buffer_size].attributes = lattr;
for (std::wstring::const_iterator it = str.begin(); it != str.end(); ++it)
{
if (it != str.begin() && !is_combining_character(*it))
{
set_cell(y, x, chr, attr);
++x;
chr.clear();
}
chr.push_back(*it);
}
if (!chr.empty())
set_cell(y, x, chr, attr);
}
void
TermcapParser::set_log_callback(LogCallback log_callback)
{
this->log_callback = log_callback;
}