aboutsummaryrefslogtreecommitdiff
path: root/src/pane/xtgettcap.rs
blob: ea6f3a0909bb92b455861dc6ce4d80d7d89d1c7e (plain)
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
// Compatibility for legacy raw-C1 XTGETTCAP only. Ordinary ESC P ... ESC \
// requests are answered exclusively by libghostty. Keep bytes untouched: C1
// normalization across the stream could corrupt UTF-8 or binary string payloads.
use bytes::Bytes;

#[derive(Debug, Default)]
pub(super) struct C1XtgettcapQueryTracker {
    state: C1XtgettcapTrackerState,
    raw_c1_intro: bool,
    native_dcs_pending: bool,
    body: Vec<u8>,
    pending: Vec<C1XtgettcapResponse>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct C1XtgettcapResponse {
    pub(super) end_offset: usize,
    pub(super) bytes: Bytes,
    pub(super) suppress_native: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum C1XtgettcapTrackerState {
    #[default]
    Ground,
    Escape,
    DcsIntro,
    DcsIntroPlus,
    DcsBody,
    DcsEscape,
    IgnoreOsc,
    IgnoreOscEscape,
    IgnoreString,
    IgnoreStringEscape,
    OversizedDcs,
    OversizedDcsEscape,
}

impl C1XtgettcapQueryTracker {
    pub(super) fn observe(&mut self, bytes: &[u8]) {
        let mut index = 0;
        while index < bytes.len() {
            if self.state == C1XtgettcapTrackerState::IgnoreString {
                // Payload cannot affect this state. CAN/SUB still need the
                // global native-DCS bookkeeping when a dispatch is pending.
                let next = bytes[index..].iter().position(|&byte| {
                    matches!(byte, 0x1b | 0x9c)
                        || (self.native_dcs_pending && matches!(byte, 0x18 | 0x1a))
                });
                match next {
                    Some(offset) => index += offset,
                    None => break,
                }
            }
            let byte = bytes[index];
            // With a 7-bit intro and raw ST, the native parser still holds the
            // DCS open. At its eventual unhook it can answer earlier keys in a
            // multi-key request again. Discard only that dispatch's XTGETTCAP
            // replies, not subsequent native requests or other response types.
            if self.native_dcs_pending && matches!(byte, 0x1b | 0x18 | 0x1a) {
                self.pending.push(C1XtgettcapResponse {
                    end_offset: index + 1,
                    bytes: Bytes::new(),
                    suppress_native: true,
                });
                self.native_dcs_pending = false;
            }
            match self.state {
                C1XtgettcapTrackerState::Ground => {
                    if byte == 0x1b {
                        self.state = C1XtgettcapTrackerState::Escape;
                    } else if byte == 0x90 {
                        self.body.clear();
                        self.raw_c1_intro = true;
                        self.state = C1XtgettcapTrackerState::DcsIntro;
                    } else if byte == 0x9d {
                        self.state = C1XtgettcapTrackerState::IgnoreOsc;
                    } else if matches!(byte, 0x98 | 0x9e | 0x9f) {
                        self.state = C1XtgettcapTrackerState::IgnoreString;
                    }
                }
                C1XtgettcapTrackerState::Escape => match byte {
                    b'P' => {
                        self.raw_c1_intro = false;
                        self.body.clear();
                        self.state = C1XtgettcapTrackerState::DcsIntro;
                    }
                    b']' => {
                        self.body.clear();
                        self.state = C1XtgettcapTrackerState::IgnoreOsc;
                    }
                    b'_' | b'^' | b'X' => {
                        self.body.clear();
                        self.state = C1XtgettcapTrackerState::IgnoreString;
                    }
                    0x1b => self.state = C1XtgettcapTrackerState::Escape,
                    _ => self.state = C1XtgettcapTrackerState::Ground,
                },
                C1XtgettcapTrackerState::DcsIntro => match byte {
                    b'+' => self.state = C1XtgettcapTrackerState::DcsIntroPlus,
                    0x1b => self.state = C1XtgettcapTrackerState::IgnoreStringEscape,
                    0x9c => self.state = C1XtgettcapTrackerState::Ground,
                    _ => self.state = C1XtgettcapTrackerState::IgnoreString,
                },
                C1XtgettcapTrackerState::DcsIntroPlus => match byte {
                    b'q' => self.state = C1XtgettcapTrackerState::DcsBody,
                    0x1b => self.state = C1XtgettcapTrackerState::IgnoreStringEscape,
                    0x9c => self.state = C1XtgettcapTrackerState::Ground,
                    _ => self.state = C1XtgettcapTrackerState::IgnoreString,
                },
                C1XtgettcapTrackerState::DcsBody => match byte {
                    0x1b => self.state = C1XtgettcapTrackerState::DcsEscape,
                    0x9c => {
                        self.native_dcs_pending |= !self.raw_c1_intro;
                        self.finalize(index + 1);
                        self.state = C1XtgettcapTrackerState::Ground;
                    }
                    _ => self.body.push(byte),
                },
                C1XtgettcapTrackerState::DcsEscape => {
                    if byte == b'\\' {
                        if self.raw_c1_intro {
                            self.finalize(index + 1);
                        } else {
                            self.body.clear();
                        }
                        self.state = C1XtgettcapTrackerState::Ground;
                    } else if byte != 0x1b {
                        self.body.clear();
                        self.state = C1XtgettcapTrackerState::IgnoreString;
                    }
                }
                C1XtgettcapTrackerState::IgnoreOsc => {
                    if byte == 0x1b {
                        self.state = C1XtgettcapTrackerState::IgnoreOscEscape;
                    } else if matches!(byte, 0x07 | 0x9c) {
                        self.state = C1XtgettcapTrackerState::Ground;
                    }
                }
                C1XtgettcapTrackerState::IgnoreOscEscape => {
                    if byte == b'\\' {
                        self.state = C1XtgettcapTrackerState::Ground;
                    } else if byte != 0x1b {
                        self.state = C1XtgettcapTrackerState::IgnoreOsc;
                    }
                }
                C1XtgettcapTrackerState::IgnoreString => {
                    if byte == 0x1b {
                        self.state = C1XtgettcapTrackerState::IgnoreStringEscape;
                    } else if byte == 0x9c {
                        self.state = C1XtgettcapTrackerState::Ground;
                    }
                }
                C1XtgettcapTrackerState::IgnoreStringEscape => {
                    if byte == b'\\' {
                        self.state = C1XtgettcapTrackerState::Ground;
                    } else if byte != 0x1b {
                        self.state = C1XtgettcapTrackerState::IgnoreString;
                    }
                }
                C1XtgettcapTrackerState::OversizedDcs => {
                    if byte == 0x1b {
                        self.state = C1XtgettcapTrackerState::OversizedDcsEscape;
                    } else if byte == 0x9c {
                        self.state = C1XtgettcapTrackerState::Ground;
                    }
                }
                C1XtgettcapTrackerState::OversizedDcsEscape => {
                    if byte == b'\\' {
                        self.state = C1XtgettcapTrackerState::Ground;
                    } else if byte != 0x1b {
                        self.state = C1XtgettcapTrackerState::OversizedDcs;
                    }
                }
            }

            if self.body.len() > 1024 {
                self.body.clear();
                self.state = C1XtgettcapTrackerState::OversizedDcs;
            }
            index += 1;
        }
    }

    fn finalize(&mut self, end_offset: usize) {
        for cap_hex in self.body.split(|byte| *byte == b';') {
            if let Some(bytes) = xtgettcap_response(cap_hex) {
                self.pending.push(C1XtgettcapResponse {
                    end_offset,
                    bytes,
                    suppress_native: false,
                });
            }
        }
        self.body.clear();
    }

    pub(super) fn drain_pending(&mut self) -> Vec<C1XtgettcapResponse> {
        std::mem::take(&mut self.pending)
    }
}

fn xtgettcap_response(cap_hex: &[u8]) -> Option<Bytes> {
    if cap_hex.is_empty() || !cap_hex.len().is_multiple_of(2) {
        return None;
    }

    let mut normalized_cap_hex = Vec::with_capacity(cap_hex.len());
    for &byte in cap_hex {
        if !byte.is_ascii_hexdigit() {
            return None;
        }
        normalized_cap_hex.push(byte.to_ascii_uppercase());
    }

    let value = xtgettcap_value(&normalized_cap_hex)?;
    Some(build_xtgettcap_response(&normalized_cap_hex, value))
}

fn xtgettcap_value(cap_hex: &[u8]) -> Option<Option<&'static [u8]>> {
    // Mirror only the Ghostty terminfo capabilities that this pane path can stand behind.
    match cap_hex {
        b"5463" => Some(None),
        b"524742" => Some(Some(b"8")),
        b"73657472676266" => Some(Some(b"\\E[38:2:%p1%d:%p2%d:%p3%dm")),
        b"73657472676262" => Some(Some(b"\\E[48:2:%p1%d:%p2%d:%p3%dm")),
        b"4D73" => Some(Some(b"\\E]52;%p1%s;%p2%s\\007")),
        b"5375" => Some(None),
        b"536D756C78" => Some(Some(b"\\E[4:%p1%dm")),
        b"536574756C63" => Some(Some(
            b"\\E[58:2::%p1%{65536}%/%d:%p1%{256}%/%{255}%&%d:%p1%{255}%&%d%;m",
        )),
        _ => None,
    }
}

fn build_xtgettcap_response(cap_hex: &[u8], value: Option<&[u8]>) -> Bytes {
    let mut response =
        Vec::with_capacity(8 + cap_hex.len() + value.map_or(0, |bytes| bytes.len() * 2));
    response.extend_from_slice(b"\x1bP1+r");
    response.extend_from_slice(cap_hex);
    if let Some(value) = value {
        response.push(b'=');
        append_upper_hex(value, &mut response);
    }
    response.extend_from_slice(b"\x1b\\");
    Bytes::from(response)
}

fn append_upper_hex(bytes: &[u8], output: &mut Vec<u8>) {
    const HEX: &[u8; 16] = b"0123456789ABCDEF";
    for &byte in bytes {
        output.push(HEX[usize::from(byte >> 4)]);
        output.push(HEX[usize::from(byte & 0x0f)]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn observe_chunks(chunks: &[&[u8]]) -> (C1XtgettcapQueryTracker, Vec<C1XtgettcapResponse>) {
        let mut tracker = C1XtgettcapQueryTracker::default();
        let mut pending = Vec::new();
        let mut offset = 0;
        for chunk in chunks {
            tracker.observe(chunk);
            pending.extend(tracker.drain_pending().into_iter().map(|mut response| {
                response.end_offset += offset;
                response
            }));
            offset += chunk.len();
        }
        (tracker, pending)
    }

    fn assert_chunk_equivalence(bytes: &[u8]) {
        let one_byte_chunks: Vec<_> = bytes.chunks(1).collect();
        let (expected, expected_pending) = observe_chunks(&one_byte_chunks);
        // Includes a single bulk call and every split, especially ESC / ST.
        for split in 0..=bytes.len() {
            let (actual, actual_pending) = observe_chunks(&[&bytes[..split], &bytes[split..]]);
            assert_eq!(actual_pending, expected_pending, "split {split}: {bytes:?}");
            assert_eq!(actual.state, expected.state);
            assert_eq!(actual.raw_c1_intro, expected.raw_c1_intro);
            assert_eq!(actual.native_dcs_pending, expected.native_dcs_pending);
            assert_eq!(actual.body, expected.body);
            assert_eq!(actual.pending, expected.pending);
        }
    }

    #[test]
    fn ignored_string_bulk_matches_single_bytes() {
        let mut all_bytes = b"\x1b_Gordinary kitty payload".to_vec();
        all_bytes.extend(0..=255u8);
        all_bytes.extend_from_slice(b"\x1b\\\x90+q5463\x9c\x90+q5247");
        assert_chunk_equivalence(&all_bytes);

        // Exercise every byte while actually inside IgnoreString, both with
        // and without a native dispatch waiting for ESC, CAN, or SUB.
        for native_pending in [false, true] {
            for intro in [b"\x1b_".as_slice(), b"\x98", b"\x9e", b"\x9f"] {
                for byte in 0..=255u8 {
                    let mut bytes = Vec::new();
                    if native_pending {
                        bytes.extend_from_slice(b"\x1bP+q5463;524742\x9c");
                    }
                    bytes.extend_from_slice(intro);
                    bytes.extend_from_slice(b"Gordinary payload");
                    bytes.push(byte);
                    assert_chunk_equivalence(&bytes);
                    bytes.extend_from_slice(b"more payload\x1b\\\x90+q5463\x9c\x90+q5247");
                    assert_chunk_equivalence(&bytes);
                }
            }
        }
    }

    #[test]
    fn ignored_string_preserves_native_dispatch_offsets() {
        for cancel in [0x18, 0x1a, 0x1b] {
            let mut bytes = b"\x1bP+q5463\x9c\x9fignored payload".to_vec();
            bytes.push(cancel);
            let end_offset = bytes.len();
            bytes.extend_from_slice(b"\x1b\\\x90+q524742\x9c");
            let (_, pending) = observe_chunks(&[&bytes]);
            assert_eq!(pending.len(), 3);
            assert!(pending[1].suppress_native);
            assert!(pending[1].bytes.is_empty());
            assert_eq!(pending[1].end_offset, end_offset);
            assert_eq!(pending[2].end_offset, bytes.len());
            assert_chunk_equivalence(&bytes);
        }
    }
}