rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
Documentation
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use std::io;

use rmux_core::TerminalPaletteIndex;

use super::terminal_response::{
    decode_attached_terminal_control_after_append, TerminalResponseDecode,
};
use super::{
    io_other, prepare_pane_input_write, write_attached_bytes_to_target_io, ActiveAttachIdentity,
    PaneInputLiveness, RequestHandler,
};

const PALETTE_RESPONSE_PREFIX: &[u8] = b"\x1b]4;";
const BRACKETED_PASTE_START: &[u8] = b"\x1b[200~";
const BRACKETED_PASTE_END: &[u8] = b"\x1b[201~";

enum OpaqueInputSpan {
    ModalInputComplete(usize),
    ModalInputPartial,
    PaneBoundComplete(usize),
    PaneBoundPartial,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::handler) enum PaneBoundTerminalStringDecode {
    Matched { size: usize },
    Partial,
    NotString,
}

pub(super) enum ModalPaletteInputSegment {
    Input(Vec<u8>),
    PaneBound(Vec<u8>),
    Response {
        index: TerminalPaletteIndex,
        bytes: Vec<u8>,
    },
    ClipboardResponse {
        selection: Option<u8>,
        content: Vec<u8>,
        bytes: Vec<u8>,
    },
}

impl ModalPaletteInputSegment {
    pub(super) fn into_bytes(self) -> Vec<u8> {
        match self {
            Self::Input(bytes)
            | Self::PaneBound(bytes)
            | Self::Response { bytes, .. }
            | Self::ClipboardResponse { bytes, .. } => bytes,
        }
    }
}

pub(super) struct ModalPaletteInput {
    pub(super) segments: Vec<ModalPaletteInputSegment>,
    pub(super) retained: Vec<u8>,
}

impl RequestHandler {
    /// Separates terminal-protocol strings from input about to enter an
    /// attached modal surface. Correlated OSC 4 responses and opaque
    /// pane-bound strings bypass modal key decoding; consumed outer-terminal
    /// responses are removed, while ordinary keys and bracketed-paste bodies
    /// retain their modal behavior. All segments preserve wire order.
    pub(super) fn split_attached_modal_palette_input(
        &self,
        pending_input: &[u8],
        bytes: &[u8],
    ) -> Option<ModalPaletteInput> {
        if !contains_terminal_string_leader(pending_input)
            && !contains_terminal_string_leader(bytes)
        {
            return None;
        }
        let mut input = Vec::with_capacity(pending_input.len().saturating_add(bytes.len()));
        input.extend_from_slice(pending_input);
        input.extend_from_slice(bytes);

        let mut segments = Vec::new();
        let mut retained = Vec::new();
        let mut copy_from = 0;
        let mut offset = 0;
        let mut changed = false;

        while offset < input.len() {
            let candidate = &input[offset..];
            if candidate.len() < PALETTE_RESPONSE_PREFIX.len()
                && PALETTE_RESPONSE_PREFIX.starts_with(candidate)
            {
                if copy_from < offset {
                    segments.push(ModalPaletteInputSegment::Input(
                        input[copy_from..offset].to_vec(),
                    ));
                }
                retained.extend_from_slice(candidate);
                copy_from = input.len();
                changed = true;
                break;
            }
            if candidate.starts_with(b"\x1b]") {
                match decode_attached_terminal_control_after_append(candidate, false, 0) {
                    TerminalResponseDecode::PaletteResponse { size, index } => {
                        if copy_from < offset {
                            segments.push(ModalPaletteInputSegment::Input(
                                input[copy_from..offset].to_vec(),
                            ));
                        }
                        segments.push(ModalPaletteInputSegment::Response {
                            index,
                            bytes: candidate[..size].to_vec(),
                        });
                        offset += size;
                        copy_from = offset;
                        changed = true;
                        continue;
                    }
                    TerminalResponseDecode::ClipboardResponse {
                        size,
                        selection,
                        content,
                    } => {
                        if copy_from < offset {
                            segments.push(ModalPaletteInputSegment::Input(
                                input[copy_from..offset].to_vec(),
                            ));
                        }
                        segments.push(ModalPaletteInputSegment::ClipboardResponse {
                            selection,
                            content,
                            bytes: candidate[..size].to_vec(),
                        });
                        offset += size;
                        copy_from = offset;
                        changed = true;
                        continue;
                    }
                    TerminalResponseDecode::Partial => {
                        if copy_from < offset {
                            segments.push(ModalPaletteInputSegment::Input(
                                input[copy_from..offset].to_vec(),
                            ));
                        }
                        retained.extend_from_slice(candidate);
                        copy_from = input.len();
                        changed = true;
                        break;
                    }
                    TerminalResponseDecode::Matched { size, .. } => {
                        if copy_from < offset {
                            segments.push(ModalPaletteInputSegment::Input(
                                input[copy_from..offset].to_vec(),
                            ));
                        }
                        // Outer-terminal OSC responses (including malformed
                        // OSC 4 and OSC 10/11/12/52) are consumed on the
                        // ordinary live-input path. Preserve that behavior
                        // before a modal key decoder can interpret their ESC.
                        offset += size;
                        copy_from = offset;
                        changed = true;
                        continue;
                    }
                    TerminalResponseDecode::PaneBound { size } => {
                        if copy_from < offset {
                            segments.push(ModalPaletteInputSegment::Input(
                                input[copy_from..offset].to_vec(),
                            ));
                        }
                        segments.push(ModalPaletteInputSegment::PaneBound(
                            candidate[..size].to_vec(),
                        ));
                        offset += size;
                        copy_from = offset;
                        changed = true;
                        continue;
                    }
                    TerminalResponseDecode::NotResponse => {}
                }
            }

            match opaque_input_span(candidate) {
                Some(OpaqueInputSpan::ModalInputComplete(size)) => {
                    offset += size;
                    continue;
                }
                Some(OpaqueInputSpan::ModalInputPartial) => break,
                Some(OpaqueInputSpan::PaneBoundComplete(size)) => {
                    if copy_from < offset {
                        segments.push(ModalPaletteInputSegment::Input(
                            input[copy_from..offset].to_vec(),
                        ));
                    }
                    segments.push(ModalPaletteInputSegment::PaneBound(
                        candidate[..size].to_vec(),
                    ));
                    offset += size;
                    copy_from = offset;
                    changed = true;
                }
                Some(OpaqueInputSpan::PaneBoundPartial) => {
                    if copy_from < offset {
                        segments.push(ModalPaletteInputSegment::Input(
                            input[copy_from..offset].to_vec(),
                        ));
                    }
                    retained.extend_from_slice(candidate);
                    copy_from = input.len();
                    changed = true;
                    break;
                }
                None => offset += 1,
            }
        }

        if !changed {
            return None;
        }
        if copy_from < input.len() {
            segments.push(ModalPaletteInputSegment::Input(input[copy_from..].to_vec()));
        }
        Some(ModalPaletteInput { segments, retained })
    }

    pub(super) async fn write_attached_palette_response_for_identity(
        &self,
        identity: ActiveAttachIdentity,
        index: TerminalPaletteIndex,
        bytes: &[u8],
    ) -> io::Result<bool> {
        if self
            .attached_client_input_is_read_only_for_identity(identity)
            .await?
        {
            return Ok(false);
        }
        let (target, session_id) = self
            .attached_input_target_identity(identity)
            .await
            .map_err(io_other)?;
        let Some(write) = self
            .with_live_input_state(identity, &target, session_id, |state| {
                let transcript = state.transcript_handle(&target).map_err(io_other)?;
                let mut transcript = transcript
                    .lock()
                    .expect("pane transcript mutex must not be poisoned");
                if !transcript.consume_palette_query_response(index) {
                    return Ok(None);
                }
                drop(transcript);
                let write = prepare_pane_input_write(
                    state,
                    &target,
                    bytes,
                    PaneInputLiveness::TolerateDead,
                )
                .map_err(io_other)?;
                Ok(Some(write))
            })
            .await?
            .flatten()
        else {
            return Ok(false);
        };
        write_attached_bytes_to_target_io(write, bytes.to_vec())
            .await
            .map_err(io_other)?;
        Ok(true)
    }
}

fn contains_terminal_string_leader(input: &[u8]) -> bool {
    input
        .iter()
        .any(|byte| matches!(*byte, b'\x1b' | 0x90 | 0x98 | 0x9d | 0x9e | 0x9f))
}

fn opaque_input_span(input: &[u8]) -> Option<OpaqueInputSpan> {
    if input.starts_with(BRACKETED_PASTE_START) {
        return find_subslice(&input[BRACKETED_PASTE_START.len()..], BRACKETED_PASTE_END)
            .map(|end| {
                OpaqueInputSpan::ModalInputComplete(
                    BRACKETED_PASTE_START.len() + end + BRACKETED_PASTE_END.len(),
                )
            })
            .or(Some(OpaqueInputSpan::ModalInputPartial));
    }
    if input.len() >= 3
        && input.len() < BRACKETED_PASTE_START.len()
        && BRACKETED_PASTE_START.starts_with(input)
    {
        return Some(OpaqueInputSpan::ModalInputPartial);
    }

    match decode_pane_bound_terminal_string(input) {
        PaneBoundTerminalStringDecode::Matched { size } => {
            Some(OpaqueInputSpan::PaneBoundComplete(size))
        }
        PaneBoundTerminalStringDecode::Partial => Some(OpaqueInputSpan::PaneBoundPartial),
        PaneBoundTerminalStringDecode::NotString => None,
    }
}

pub(in crate::handler) fn decode_pane_bound_terminal_string(
    input: &[u8],
) -> PaneBoundTerminalStringDecode {
    let (body_start, bell_terminated) = if input.starts_with(b"\x1b]") {
        (2, true)
    } else if input.starts_with(b"\x1bP")
        || input.starts_with(b"\x1b_")
        || input.starts_with(b"\x1b^")
        || input.starts_with(b"\x1bX")
    {
        (2, false)
    } else {
        match input.first().copied() {
            Some(0x9d) => (1, true),
            Some(0x90 | 0x98 | 0x9e | 0x9f) => (1, false),
            _ => return PaneBoundTerminalStringDecode::NotString,
        }
    };
    find_opaque_terminator(input, body_start, bell_terminated)
        .map(|size| PaneBoundTerminalStringDecode::Matched { size })
        .unwrap_or(PaneBoundTerminalStringDecode::Partial)
}

fn find_opaque_terminator(input: &[u8], mut offset: usize, bell_terminated: bool) -> Option<usize> {
    while offset < input.len() {
        match input[offset] {
            b'\x07' if bell_terminated => return Some(offset + 1),
            0x9c => return Some(offset + 1),
            b'\x1b' if input.get(offset + 1) == Some(&b'\\') => return Some(offset + 2),
            _ => offset += 1,
        }
    }
    None
}

fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

#[cfg(test)]
mod tests {
    use super::{ModalPaletteInputSegment, RequestHandler};

    fn assert_single_pane_bound(input: &[u8]) {
        let handler = RequestHandler::new();
        let split = handler
            .split_attached_modal_palette_input(&[], input)
            .expect("terminal string is separated from modal keys");
        assert!(split.retained.is_empty());
        assert_eq!(split.segments.len(), 1);
        match &split.segments[0] {
            ModalPaletteInputSegment::PaneBound(bytes) => assert_eq!(bytes, input),
            _ => panic!("terminal string must remain one pane-bound segment"),
        }
    }

    #[test]
    fn modal_palette_splitter_keeps_pane_bound_strings_indivisible() {
        for opaque in [
            b"\x1bPtmux;\x1b\x1b]4;1;rgb:1/2/3\x07payload\x1b\\".as_slice(),
            b"\x1b_Gi=1;payload\x1b]4;1;rgb:1/2/3\x07more\x1b\\".as_slice(),
            b"\x1b^private\x1b]4;1;rgb:1/2/3\x07message\x1b\\".as_slice(),
            b"\x90dcs\x1b]4;1;rgb:1/2/3\x07body\x9c".as_slice(),
        ] {
            assert_single_pane_bound(opaque);
        }
    }

    #[test]
    fn modal_palette_splitter_leaves_bracketed_paste_for_the_modal() {
        let handler = RequestHandler::new();
        for paste in [
            b"\x1b[200~paste\x1b]4;1;rgb:1/2/3\x07body\x1b[201~".as_slice(),
            b"\x1b[200~partial\x1b]4;1;rgb:1/2/3\x07".as_slice(),
        ] {
            assert!(
                handler
                    .split_attached_modal_palette_input(&[], paste)
                    .is_none(),
                "paste remains ordinary modal input: {paste:?}"
            );
        }
    }

    #[test]
    fn modal_palette_splitter_retains_partial_pane_bound_strings() {
        let handler = RequestHandler::new();
        for opaque in [
            b"\x1bPtmux;\x1b\x1b]4;1;rgb:1/2/3\x07".as_slice(),
            b"\x1bXpartial-sos".as_slice(),
            b"\x1b_Gi=1;payload\x1b]4;1;rgb:1/2/3\x07".as_slice(),
            b"\x1b^partial-pm".as_slice(),
            b"\x1b_Qpartial-apc".as_slice(),
            b"\x1b]777;payload".as_slice(),
            b"\x90partial-dcs".as_slice(),
            b"\x98partial-sos".as_slice(),
            b"\x9dpartial-osc".as_slice(),
            b"\x9epartial-pm".as_slice(),
            b"\x9fpartial-apc".as_slice(),
        ] {
            let split = handler
                .split_attached_modal_palette_input(&[], opaque)
                .expect("partial terminal string is retained before modal decoding");
            assert!(split.segments.is_empty());
            assert_eq!(split.retained, opaque);
        }
    }

    #[test]
    fn modal_palette_splitter_consumes_outer_terminal_osc_responses() {
        let handler = RequestHandler::new();
        let response = b"\x1b]52;c;payload\x1b]4;1;rgb:1/2/3\x07";
        let split = handler
            .split_attached_modal_palette_input(&[], response)
            .expect("consumed outer-terminal response is removed before modal decoding");
        assert!(split.segments.is_empty());
        assert!(split.retained.is_empty());
    }

    #[test]
    fn modal_palette_splitter_types_clipboard_responses_before_key_decoding() {
        let handler = RequestHandler::new();
        let response = b"\x1b]52;p;bW9kYWw=\x1b\\";
        let split = handler
            .split_attached_modal_palette_input(&[], response)
            .expect("clipboard response is separated from modal keys");
        assert!(split.retained.is_empty());
        assert_eq!(split.segments.len(), 1);
        match &split.segments[0] {
            ModalPaletteInputSegment::ClipboardResponse {
                selection,
                content,
                bytes,
            } => {
                assert_eq!(*selection, Some(b'p'));
                assert_eq!(content, b"modal");
                assert_eq!(bytes, response);
            }
            _ => panic!("clipboard response must remain one typed segment"),
        }
    }
}