Files
ghostty-rs/crates/ghostty-vt-capi/src/paste.rs
T
ddidderr 75252a7651 feat(vt-capi): port the libghostty-vt C API
Ports ZIG/src/lib_vt.zig and terminal/c/* as crates/ghostty-vt-capi
(cdylib + staticlib + rlib, soname libghostty-vt.so) implementing the
unchanged include/ghostty/vt.h + vt/*.h ABI: OSC parser, SGR parser,
key events and key encoding, paste safety, colors, result codes - all
38 native symbols, plus the ghostty_wasm_* helpers on wasm32.

Verification:
- tests/abi.rs generates a C probe from the headers, compiled with cc,
  that checks sizeof/alignof/offsetof of every struct/union, every
  enumerator and #define against Rust, and links every declared symbol;
  nm -D exports equal the declared functions.
- Ghostty's example/c-vt* programs compile unchanged against the Rust
  library (shared and static) and produce the expected output.
- The wasm32 build runs Ghostty's wasm-key-encode/wasm-sgr example
  scripts unmodified under node (upstream's wasm-sgr tag table is stale
  vs sgr.h; a regenerated copy checks full output).

Test Plan:
- cargo test -p ghostty-vt-capi (22 unit + 2 ABI + 5 C + 3 wasm tests)
2026-10-12 05:16:41 +02:00

54 lines
1.3 KiB
Rust

//! `ghostty_paste_is_safe` (ZIG/src/terminal/c/paste.zig).
#![allow(unsafe_code)]
/// Whether pasting `data` is safe (no newline, no bracketed-paste end).
///
/// # Safety
/// `data` must be null or point to `len` readable bytes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn ghostty_paste_is_safe(data: *const u8, len: usize) -> bool {
let slice: &[u8] = if data.is_null() {
&[]
} else {
// SAFETY: caller guarantees `data` points to `len` readable bytes.
unsafe { std::slice::from_raw_parts(data, len) }
};
ghostty_input::paste::is_safe(slice)
}
#[cfg(test)]
mod tests {
use super::*;
fn safe(s: &[u8]) -> bool {
// SAFETY: valid slice.
unsafe { ghostty_paste_is_safe(s.as_ptr(), s.len()) }
}
#[test]
fn is_safe_with_safe_data() {
assert!(safe(b"hello world"));
}
#[test]
fn is_safe_with_newline() {
assert!(!safe(b"hello\nworld"));
}
#[test]
fn is_safe_with_bracketed_paste_end() {
assert!(!safe(b"hello\x1b[201~world"));
}
#[test]
fn is_safe_with_empty_data() {
assert!(safe(b""));
}
#[test]
fn is_safe_with_null_empty_data() {
// SAFETY: null with len 0 is explicitly allowed.
assert!(unsafe { ghostty_paste_is_safe(std::ptr::null(), 0) });
}
}