Skip to content

Commit b428e73

Browse files
authored
core: fix GNU Screen OSC 52 passthrough (#1334)
Screen handles DCS payloads differently from tmux and consumes the inner ST, leaving the outer terminal with an unterminated OSC 52 request. Use BEL and bounded payload framing so Screen forwards complete clipboard sequences, including large values. Reference: anomalyco/opencode#28590
1 parent ffb3c96 commit b428e73

3 files changed

Lines changed: 50 additions & 43 deletions

File tree

‎packages/core/src/zig/terminal.zig‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const NOTIFICATION_QUERY_ID = "opentui-notifications";
7777
pub const SCREEN_PASSTHROUGH_CHUNK_SIZE = 252;
7878
pub const CLIPBOARD_PAYLOAD_SIZE_MAX = std.math.maxInt(u32);
7979
const OSC52_FRAMING_SIZE = "\x1b]52;c;".len + "\x1b\\".len;
80-
const PASSTHROUGH_ESCAPED_OSC52_SIZE = OSC52_FRAMING_SIZE + 2;
80+
const SCREEN_OSC52_FRAMING_SIZE = "\x1b]52;c;".len + "\x07".len;
8181

8282
pub const MouseLevel = enum {
8383
none,
@@ -1562,10 +1562,10 @@ pub fn clipboardSequenceSize(self: *Terminal, payload_len: usize) !usize {
15621562
}
15631563

15641564
if (self.isInScreen()) {
1565-
const escaped_len = try std.math.add(usize, encoded_len, PASSTHROUGH_ESCAPED_OSC52_SIZE);
1566-
const chunk_count = @divFloor(escaped_len - 1, SCREEN_PASSTHROUGH_CHUNK_SIZE) + 1;
1565+
const screen_sequence_len = try std.math.add(usize, encoded_len, SCREEN_OSC52_FRAMING_SIZE);
1566+
const chunk_count = @divFloor(screen_sequence_len - 1, SCREEN_PASSTHROUGH_CHUNK_SIZE) + 1;
15671567
const envelopes_len = try std.math.mul(usize, chunk_count, ansi.ANSI.screenDcsStart.len + ansi.ANSI.screenDcsEnd.len);
1568-
return std.math.add(usize, escaped_len, envelopes_len);
1568+
return std.math.add(usize, screen_sequence_len, envelopes_len);
15691569
}
15701570

15711571
return sequence_len;
@@ -1581,19 +1581,19 @@ pub fn writeClipboard(self: *Terminal, tty: anytype, target: ClipboardTarget, te
15811581

15821582
if (self.isInTmux()) {
15831583
try tty.writeAll(ansi.ANSI.tmuxDcsStart);
1584-
try writeClipboardSequence(tty, target, text_utf8, true);
1584+
try writeClipboardSequence(tty, target, text_utf8, true, "\x1b\\");
15851585
try tty.writeAll(ansi.ANSI.tmuxDcsEnd);
15861586
return;
15871587
}
15881588

15891589
if (self.isInScreen()) {
15901590
var screen_writer = ScreenPassthroughWriter(@TypeOf(tty)).init(tty);
1591-
try writeClipboardSequence(&screen_writer, target, text_utf8, false);
1591+
try writeClipboardSequence(&screen_writer, target, text_utf8, false, "\x07");
15921592
try screen_writer.finish();
15931593
return;
15941594
}
15951595

1596-
try writeClipboardSequence(tty, target, text_utf8, false);
1596+
try writeClipboardSequence(tty, target, text_utf8, false, "\x1b\\");
15971597
}
15981598

15991599
fn ScreenPassthroughWriter(comptime Writer: type) type {
@@ -1613,13 +1613,7 @@ fn ScreenPassthroughWriter(comptime Writer: type) type {
16131613
}
16141614

16151615
pub fn writeByte(self: *Self, byte: u8) !void {
1616-
const encoded_length: usize = if (byte == '\x1b') 2 else 1;
1617-
if (self.length + encoded_length > self.buffer.len) try self.flush();
1618-
1619-
if (byte == '\x1b') {
1620-
self.buffer[self.length] = '\x1b';
1621-
self.length += 1;
1622-
}
1616+
if (self.length == self.buffer.len) try self.flush();
16231617
self.buffer[self.length] = byte;
16241618
self.length += 1;
16251619
}
@@ -1638,12 +1632,12 @@ fn ScreenPassthroughWriter(comptime Writer: type) type {
16381632
};
16391633
}
16401634

1641-
fn writeClipboardSequence(writer: anytype, target: ClipboardTarget, text_utf8: []const u8, escape: bool) !void {
1635+
fn writeClipboardSequence(writer: anytype, target: ClipboardTarget, text_utf8: []const u8, escape: bool, terminator: []const u8) !void {
16421636
try writeClipboardBytes(writer, "\x1b]52;", escape);
16431637
try writer.writeByte(target.toChar());
16441638
try writer.writeByte(';');
16451639
try writeClipboardBase64(writer, text_utf8);
1646-
try writeClipboardBytes(writer, "\x1b\\", escape);
1640+
try writeClipboardBytes(writer, terminator, escape);
16471641
}
16481642

16491643
fn writeClipboardBase64(writer: anytype, source: []const u8) !void {

‎packages/core/src/zig/tests/renderer_test.zig‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,13 +1048,13 @@ test "renderer - clipboard chunks OSC 52 in screen DCS passthrough" {
10481048
const payload = [_]u8{'A'} ** 2048;
10491049
try std.testing.expect(test_renderer.renderer.copyToClipboardOSC52(.clipboard, &payload));
10501050

1051-
// Escaped sequence: base64 (2732) + OSC 52 framing (9) + doubled ESC bytes
1052-
// (+2) = 2743 bytes, split into 11 chunks of at most 252 bytes, each wrapped
1053-
// in "\x1bP" .. "\x1b\\" (+4 per chunk) = 2787 total.
1051+
// Sequence: base64 (2732) + OSC 52 framing with BEL (8) = 2740 bytes,
1052+
// split into 11 chunks of at most 252 bytes, each wrapped in
1053+
// "\x1bP" .. "\x1b\\" (+4 per chunk) = 2784 total.
10541054
const output = test_renderer.lastOutput();
1055-
try std.testing.expectEqual(@as(usize, 2787), output.len);
1056-
try std.testing.expect(std.mem.startsWith(u8, output, "\x1bP\x1b\x1b]52;c;QUFB"));
1057-
try std.testing.expect(std.mem.endsWith(u8, output, "QUE=\x1b\x1b\\\x1b\\"));
1055+
try std.testing.expectEqual(@as(usize, 2784), output.len);
1056+
try std.testing.expect(std.mem.startsWith(u8, output, "\x1bP\x1b]52;c;QUFB"));
1057+
try std.testing.expect(std.mem.endsWith(u8, output, "QUE=\x07\x1b\\"));
10581058

10591059
// ESC only precedes 'P' at envelope starts, so this counts the chunks.
10601060
try std.testing.expectEqual(@as(usize, 11), std.mem.count(u8, output, "\x1bP"));

‎packages/core/src/zig/tests/terminal_test.zig‎

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,16 +1329,16 @@ test "writeClipboard - chunks large payload through GNU Screen passthrough" {
13291329
try term.writeClipboard(&writer, .clipboard, payload);
13301330

13311331
const output = writer.getWritten();
1332-
try testing.expect(countSubstring(output, ansi.ANSI.screenDcsStart) > 1);
1333-
try testing.expect(std.mem.endsWith(u8, output, ansi.ANSI.screenDcsEnd));
1334-
1335-
var frame_start: usize = 0;
1336-
while (std.mem.findPos(u8, output, frame_start, ansi.ANSI.screenDcsStart)) |start| {
1337-
const content_start = start + ansi.ANSI.screenDcsStart.len;
1338-
const next_start = std.mem.findPos(u8, output, content_start, ansi.ANSI.screenDcsStart) orelse output.len;
1339-
try testing.expect(next_start - content_start <= Terminal.SCREEN_PASSTHROUGH_CHUNK_SIZE + ansi.ANSI.screenDcsEnd.len);
1340-
frame_start = next_start;
1341-
}
1332+
const forwarded_buffer = try testing.allocator.alloc(u8, output.len);
1333+
defer testing.allocator.free(forwarded_buffer);
1334+
const forwarded = try forwardScreenDcs(output, forwarded_buffer);
1335+
const encoded_len = std.base64.standard.Encoder.calcSize(payload.len);
1336+
const expected = try testing.allocator.alloc(u8, "\x1b]52;c;".len + encoded_len + 1);
1337+
defer testing.allocator.free(expected);
1338+
@memcpy(expected[0.."\x1b]52;c;".len], "\x1b]52;c;");
1339+
_ = std.base64.standard.Encoder.encode(expected["\x1b]52;c;".len .. expected.len - 1], payload);
1340+
expected[expected.len - 1] = '\x07';
1341+
try testing.expectEqualSlices(u8, expected, forwarded);
13421342
}
13431343

13441344
test "writeClipboard - base64 encodes raw UTF-8 bytes" {
@@ -1431,15 +1431,15 @@ test "writeClipboard - Screen framing crosses the 252-byte boundary" {
14311431
try env.put("STY", "12345.pts-0.hostname");
14321432
var term = Terminal.init(.{ .env_map = &env });
14331433

1434-
const payload_one_chunk = [_]u8{'A'} ** 180;
1434+
const payload_one_chunk = [_]u8{'A'} ** 183;
14351435
var writer = TestWriter.init(testing.allocator);
14361436
defer writer.deinit();
14371437
try term.writeClipboard(&writer, .clipboard, &payload_one_chunk);
14381438
try testing.expectEqual(@as(usize, 1), countSubstring(writer.getWritten(), ansi.ANSI.screenDcsStart));
14391439
try testing.expectEqual(try term.clipboardSequenceSize(payload_one_chunk.len), writer.getWritten().len);
14401440

14411441
writer.reset();
1442-
const payload_two_chunks = [_]u8{'A'} ** 181;
1442+
const payload_two_chunks = [_]u8{'A'} ** 184;
14431443
try term.writeClipboard(&writer, .clipboard, &payload_two_chunks);
14441444
try testing.expectEqual(@as(usize, 2), countSubstring(writer.getWritten(), ansi.ANSI.screenDcsStart));
14451445
try testing.expectEqual(try term.clipboardSequenceSize(payload_two_chunks.len), writer.getWritten().len);
@@ -1477,21 +1477,13 @@ test "writeClipboard - wraps in DCS passthrough for GNU Screen" {
14771477
try env.put("STY", "12345.pts-0.hostname");
14781478

14791479
var term = Terminal.init(.{ .env_map = &env });
1480-
term.caps.osc52 = true;
14811480

14821481
var writer = TestWriter.init(testing.allocator);
14831482
defer writer.deinit();
14841483

14851484
try term.writeClipboard(&writer, .clipboard, "test");
14861485

1487-
const output = writer.getWritten();
1488-
// Should start with DCS (but not tmux prefix)
1489-
try testing.expect(std.mem.startsWith(u8, output, "\x1bP"));
1490-
try testing.expect(!std.mem.startsWith(u8, output, "\x1bPtmux;"));
1491-
// Should end with DCS terminator
1492-
try testing.expect(std.mem.endsWith(u8, output, "\x1b\\"));
1493-
// Should have doubled ESC characters
1494-
try testing.expect(std.mem.find(u8, output, "\x1b\x1b") != null);
1486+
try testing.expectEqualStrings("\x1bP\x1b]52;c;dGVzdA==\x07\x1b\\", writer.getWritten());
14951487
}
14961488

14971489
test "writeClipboard - handles tmux sessions" {
@@ -1542,6 +1534,27 @@ fn countSubstring(haystack: []const u8, needle: []const u8) usize {
15421534
return count;
15431535
}
15441536

1537+
fn forwardScreenDcs(input: []const u8, output: []u8) ![]const u8 {
1538+
var input_offset: usize = 0;
1539+
var output_offset: usize = 0;
1540+
while (input_offset < input.len) {
1541+
if (!std.mem.startsWith(u8, input[input_offset..], ansi.ANSI.screenDcsStart)) {
1542+
return error.InvalidScreenDcsStart;
1543+
}
1544+
const content_start = input_offset + ansi.ANSI.screenDcsStart.len;
1545+
const end_offset = std.mem.find(u8, input[content_start..], ansi.ANSI.screenDcsEnd) orelse {
1546+
return error.MissingScreenDcsEnd;
1547+
};
1548+
const content = input[content_start .. content_start + end_offset];
1549+
if (content.len > Terminal.SCREEN_PASSTHROUGH_CHUNK_SIZE) return error.ScreenDcsPayloadTooLarge;
1550+
if (output_offset + content.len > output.len) return error.ScreenDcsOutputTooSmall;
1551+
@memcpy(output[output_offset .. output_offset + content.len], content);
1552+
output_offset += content.len;
1553+
input_offset = content_start + end_offset + ansi.ANSI.screenDcsEnd.len;
1554+
}
1555+
return output[0..output_offset];
1556+
}
1557+
15451558
test "queryTerminalSend - skips OSC 66 queries when OPENTUI_FORCE_EXPLICIT_WIDTH=false" {
15461559
if (builtin.os.tag == .windows) return error.SkipZigTest;
15471560

0 commit comments

Comments
 (0)