-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathparser.cpp
418 lines (371 loc) · 14.3 KB
/
parser.cpp
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
412
413
414
415
416
417
418
/*
Copyright (C) 2020 Crimson AS <info@crimson.no>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to
do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// This is the start of a rewrite of a gradual, in-place rewrite of Terminal.cpp.
// The idea is to move as much of the parsing as possible out of there, and into stateless
// functions that can (and should!) be easily unit tested.
// Whether or not such a rewrite will ever be finished, who knows...
#include "parser.h"
#include "catch.hpp"
#include <QDebug>
template<typename T>
struct Appender
{
Appender(T& container)
: m_size(0)
, m_container(container)
{
}
int size() { return m_size; }
void append(const QColor& color)
{
m_container[m_size++] = color.rgb();
}
private:
int m_size;
T& m_container;
};
struct ColourTable
{
public:
ColourTable()
{
auto colours = Appender<decltype(m_colourData)>(m_colourData);
//normal
colours.append(QColor(0, 0, 0).rgb());
colours.append(QColor(210, 0, 0).rgb());
colours.append(QColor(0, 210, 0).rgb());
colours.append(QColor(210, 210, 0).rgb());
colours.append(QColor(0, 0, 240).rgb());
colours.append(QColor(210, 0, 210).rgb());
colours.append(QColor(0, 210, 210).rgb());
colours.append(QColor(235, 235, 235).rgb());
//bright
colours.append(QColor(127, 127, 127).rgb());
colours.append(QColor(255, 0, 0).rgb());
colours.append(QColor(0, 255, 0).rgb());
colours.append(QColor(255, 255, 0).rgb());
colours.append(QColor(92, 92, 255).rgb());
colours.append(QColor(255, 0, 255).rgb());
colours.append(QColor(0, 255, 255).rgb());
colours.append(QColor(255, 255, 255).rgb());
//colour cube
for (int r = 0x00; r < 0x100;) {
for (int g = 0x00; g < 0x100;) {
for (int b = 0x00; b < 0x100;) {
colours.append(QColor(r, g, b).rgb());
b += b ? 0x28 : 0x5f;
}
g += g ? 0x28 : 0x5f;
}
r += r ? 0x28 : 0x5f;
}
//greyscale ramp
for (int i = 0, g = 8; i < 24; i++, g += 10)
colours.append(QColor(g, g, g).rgb());
assert(colours.size() == 256);
}
QRgb at(uint8_t pos)
{
return m_colourData[pos];
}
private:
std::array<QRgb, 256> m_colourData;
};
Q_GLOBAL_STATIC(ColourTable, colourTable);
QRgb Parser::fetchDefaultFgColor()
{
return colourTable()->at(7);
}
QRgb Parser::fetchDefaultBgColor()
{
return colourTable()->at(0);
}
bool Parser::handleSGR(Parser::SGRParserState& state, const QList<int>& params, QString& errorString)
{
int pidx = 0;
// NOTE: Previously, this code would try to deal with invalid input, but I don't think that's wise.
// It will now discard anything that is malformed, and not try to look for subsequent correct messages.
// If this is too strict, then it may need to be revisited, but I think this is saner.
while (pidx < params.count()) {
int p = params.at(pidx++);
switch (p) {
case 0:
state.colours.fg = state.colours.defaultFg;
state.colours.bg = state.colours.defaultBg;
state.currentAttributes = Parser::NoAttributes;
break;
case 1:
state.currentAttributes |= Parser::BoldAttribute;
break;
case 2:
// TODO: Faint ("half bright")
break;
case 3:
state.currentAttributes |= Parser::ItalicAttribute;
break;
case 4:
state.currentAttributes |= Parser::UnderlineAttribute;
break;
case 5:
state.currentAttributes |= Parser::BlinkAttribute;
break;
case 6:
// This is supposed to be a "fast blink"? what is that?
state.currentAttributes |= Parser::BlinkAttribute;
break;
case 7:
state.currentAttributes |= Parser::NegativeAttribute;
break;
case 8:
// TODO: Invisible..?
break;
case 9:
// TODO: strikethrough
break;
case 21:
// TODO: double underline..?
break;
case 22:
state.currentAttributes &= ~Parser::BoldAttribute;
break;
case 23:
state.currentAttributes &= ~Parser::ItalicAttribute;
break;
case 24:
state.currentAttributes &= ~Parser::UnderlineAttribute;
break;
case 25:
state.currentAttributes &= ~Parser::BlinkAttribute;
break;
case 26:
// "fast blink" off...
state.currentAttributes &= ~Parser::BlinkAttribute;
break;
case 27:
state.currentAttributes &= ~Parser::NegativeAttribute;
break;
case 28:
// TODO: visible..?
break;
case 29:
// TODO: !strikethrough
break;
case 30: // fg black
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
if (state.colours.fg & Parser::BoldAttribute)
p += 8;
state.colours.fg = colourTable()->at(p - 30);
break;
case 39: // fg default
state.colours.fg = state.colours.defaultFg;
break;
case 40: // bg black
case 41:
case 42:
case 43:
case 44:
case 45:
case 46:
case 47:
state.colours.bg = colourTable()->at(p - 40);
break;
case 49: // bg default
state.colours.bg = state.colours.defaultBg;
break;
case 90: // fg black, bold/bright, nonstandard
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
state.colours.fg = colourTable()->at(p - 90 + 8);
break;
case 100: // fg black, bold/bright, nonstandard
case 101:
case 102:
case 103:
case 104:
case 105:
case 106:
case 107:
state.colours.fg = colourTable()->at(p - 100 + 8);
break;
case 38:
case 48: {
if (pidx >= params.count()) {
errorString = "got invalid extended SGR (no type)";
return false;
}
bool isForeground = p == 38;
int ctype = params.at(pidx++);
switch (ctype) {
case 5: {
// 5: 256 colours (xterm)
if (pidx >= params.count()) {
errorString = "got invalid 256color SGR (no color)";
return false;
}
int colorIndex = params.at(pidx++);
if (colorIndex < 0 || colorIndex >= 256) {
errorString = QString::fromLatin1("got invalid 256color SGR with out-of-range color: %1").arg(colorIndex);
return false;
}
if (isForeground) {
// Only apply bold attribute for standard 16-color; 256-color doesn't have bold
if (colorIndex < 9 && state.currentAttributes & Parser::BoldAttribute)
colorIndex += 8;
state.colours.fg = colourTable()->at(colorIndex);
} else {
state.colours.bg = colourTable()->at(colorIndex);
}
break;
}
case 2: {
// 2: 16-bit colours
// r;g;b
if (params.count() - pidx < 3) {
errorString = QString::fromLatin1("got invalid 16bit SGR with too few parameters: %1").arg(params.count() - pidx);
return false;
}
int r = params.at(pidx++);
int g = params.at(pidx++);
int b = params.at(pidx++);
// Ignore any invalid component.
if (r < 0 || r >= 256) {
errorString = QString::fromLatin1("got invalid 16bit SGR with out-of-range r: %1").arg(r);
return false;
}
if (g < 0 || g >= 256) {
errorString = QString::fromLatin1("got invalid 16bit SGR with out-of-range g: %1").arg(g);
return false;
}
if (b < 0 || b >= 256) {
errorString = QString::fromLatin1("got invalid 16bit SGR with out-of-range b: %1").arg(b);
return false;
}
if (isForeground)
state.colours.fg = QColor(r, g, b).rgb();
else
state.colours.bg = QColor(r, g, b).rgb();
break;
}
default:
errorString = QString::fromLatin1("got unknown extended SGR: %1").arg(ctype);
return false;
}
break;
}
default:
errorString = QString::fromLatin1("got unknown SGR: %1").arg(p);
return false;
}
}
return true;
}
static void requireParseFailure(const QList<int>& params, const char* expectedError)
{
Parser::TextAttributes attribs = Parser::TextAttribute::NoAttributes;
QRgb fg = Qt::red;
QRgb bg = Qt::red;
QRgb dfg = Qt::red;
QRgb dbg = Qt::red;
Parser::SGRParserState state(fg, bg, dfg, dbg, attribs);
QString errorString;
REQUIRE(!Parser::handleSGR(state, params, errorString));
//qDebug() << errorString << expectedError;
REQUIRE(errorString == expectedError);
REQUIRE(state.colours.fg == Qt::red);
REQUIRE(state.colours.bg == Qt::red);
REQUIRE(attribs == Parser::TextAttribute::NoAttributes);
}
static void requireParseSuccess(const QList<int>& params, const QRgb& expectedFg, const QRgb& expectedBg, const char* expectedWarning)
{
Parser::TextAttributes attribs = Parser::TextAttribute::NoAttributes;
QRgb fg = Qt::red;
QRgb bg = Qt::red;
QRgb dfg = Qt::red;
QRgb dbg = Qt::red;
Parser::SGRParserState state(fg, bg, dfg, dbg, attribs);
QString errorString;
REQUIRE(Parser::handleSGR(state, params, errorString));
//qDebug() << errorString << expectedWarning;
REQUIRE(errorString == expectedWarning);
//qDebug() << QColor(state.colours.fg) << QColor(expectedFg);
REQUIRE(state.colours.fg == expectedFg);
REQUIRE(state.colours.bg == expectedBg);
REQUIRE(attribs == Parser::TextAttribute::NoAttributes);
}
TEST_CASE("SGR: Invalid", "[terminal] [sgr]")
{
requireParseFailure({ 1024, 3 }, "got unknown SGR: 1024");
requireParseFailure({ 48, 3 }, "got unknown extended SGR: 3");
}
TEST_CASE("SGR: 16bit: invalid", "[terminal] [sgr] [16bit]")
{
// Missing parameters
requireParseFailure({ 48, 2, 0, 0 }, "got invalid 16bit SGR with too few parameters: 2");
requireParseFailure({ 48, 2, 0 }, "got invalid 16bit SGR with too few parameters: 1");
requireParseFailure({ 48, 2 }, "got invalid 16bit SGR with too few parameters: 0");
// All invalid => parse failure.
requireParseFailure({ 48, 2, -1, -1, -1 }, "got invalid 16bit SGR with out-of-range r: -1");
requireParseFailure({ 48, 2, 256, 256, 256 }, "got invalid 16bit SGR with out-of-range r: 256");
// Any one component valid => parse failure.
requireParseFailure({ 48, 2, 256, 0, 0 }, "got invalid 16bit SGR with out-of-range r: 256");
requireParseFailure({ 48, 2, 0, 256, 0 }, "got invalid 16bit SGR with out-of-range g: 256");
requireParseFailure({ 48, 2, 0, 0, 256 }, "got invalid 16bit SGR with out-of-range b: 256");
}
TEST_CASE("SGR: 16bit: Foreground", "[terminal] [sgr] [16bit]")
{
requireParseSuccess({ 38, 2, 0, 0, 0 }, QColor(Qt::black).rgb(), Qt::red, "");
requireParseSuccess({ 38, 2, 255, 0, 0 }, QColor(Qt::red).rgb(), Qt::red, "");
requireParseSuccess({ 38, 2, 0, 255, 0 }, QColor(Qt::green).rgb(), Qt::red, "");
requireParseSuccess({ 38, 2, 0, 0, 255 }, QColor(Qt::blue).rgb(), Qt::red, "");
}
TEST_CASE("SGR: 16bit: Background", "[terminal] [sgr]")
{
requireParseSuccess({ 48, 2, 0, 0, 0 }, Qt::red, QColor(Qt::black).rgb(), "");
requireParseSuccess({ 48, 2, 255, 0, 0 }, Qt::red, QColor(Qt::red).rgb(), "");
requireParseSuccess({ 48, 2, 0, 255, 0 }, Qt::red, QColor(Qt::green).rgb(), "");
requireParseSuccess({ 48, 2, 0, 0, 255 }, Qt::red, QColor(Qt::blue).rgb(), "");
}
TEST_CASE("SGR: 256color: invalid", "[terminal] [sgr] [256color]")
{
requireParseFailure({ 38, 5 }, "got invalid 256color SGR (no color)");
requireParseFailure({ 38, 5, -1 }, "got invalid 256color SGR with out-of-range color: -1");
requireParseFailure({ 38, 5, 256 }, "got invalid 256color SGR with out-of-range color: 256");
}
TEST_CASE("SGR: 256color: Foreground", "[terminal] [sgr] [256color]")
{
requireParseSuccess({ 38, 5, 0 }, QColor(Qt::black).rgb(), Qt::red, "");
requireParseSuccess({ 38, 5, 9 }, QColor(Qt::red).rgb(), Qt::red, "");
requireParseSuccess({ 38, 5, 10 }, QColor(Qt::green).rgb(), Qt::red, "");
}
TEST_CASE("SGR: 256color: Background", "[terminal] [256color]")
{
requireParseSuccess({ 48, 5, 0 }, Qt::red, QColor(Qt::black).rgb(), "");
requireParseSuccess({ 48, 5, 9 }, Qt::red, QColor(Qt::red).rgb(), "");
requireParseSuccess({ 48, 5, 10 }, Qt::red, QColor(Qt::green).rgb(), "");
}