LLDB mainline
ConPTYUtils.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include <cstring>
11
12using namespace lldb_private;
13
14void lldb_private::StripConPTYSequences(void *data, size_t &len,
15 bool strip_init) {
16 auto *buf = static_cast<char *>(data);
17 char *out = buf;
18 const char *in = buf;
19 const char *end = buf + len;
20
21 while (in < end) {
22 if (*in != '\x1b') {
23 *out++ = *in++;
24 continue;
25 }
26
27 size_t remaining = end - in;
28
29 // \x1b[6n - cursor-position query (PSEUDOCONSOLE_INHERIT_CURSOR init)
30 if (remaining >= 4 && memcmp(in, "\x1b[6n", 4) == 0) {
31 in += 4;
32 continue;
33 }
34
35 if (strip_init) {
36 // \x1b[m - SGR reset (ConPTY init)
37 if (remaining >= 3 && memcmp(in, "\x1b[m", 3) == 0) {
38 in += 3;
39 continue;
40 }
41
42 // \x1b[?25h - show cursor (ConPTY init)
43 if (remaining >= 6 && memcmp(in, "\x1b[?25h", 6) == 0) {
44 in += 6;
45 continue;
46 }
47 }
48
49 // \x1b[?9001h / \x1b[?9001l - Win32 Input Mode enable/disable
50 if (remaining >= 8 && memcmp(in, "\x1b[?9001", 7) == 0 &&
51 (in[7] == 'h' || in[7] == 'l')) {
52 in += 8;
53 continue;
54 }
55
56 // \x1b[?1004h / \x1b[?1004l - focus-event reporting enable/disable
57 if (remaining >= 8 && memcmp(in, "\x1b[?1004", 7) == 0 &&
58 (in[7] == 'h' || in[7] == 'l')) {
59 in += 8;
60 continue;
61 }
62
63 // \x1b]0;...\x07 - ConPTY window-title OSC sequence
64 if (remaining >= 4 && in[1] == ']' && in[2] == '0' && in[3] == ';') {
65 const char *bel =
66 static_cast<const char *>(memchr(in + 4, '\x07', end - in - 4));
67 if (bel)
68 in = bel + 1;
69 else
70 in = end;
71 continue;
72 }
73
74 *out++ = *in++;
75 }
76
77 len = static_cast<size_t>(out - buf);
78}
A class that represents a running process on the host machine.
void StripConPTYSequences(void *data, size_t &len, bool strip_init)
Remove ConPTY management sequences from a buffer in-place.