libghostty
Loading...
Searching...
No Matches
Paste Utilities

Detailed Description

Utilities for validating and encoding paste data for terminal input.

Basic Usage

Use ghostty_paste_is_safe() to check if paste data contains potentially dangerous sequences before sending it to the terminal.

Use ghostty_paste_encode() to encode paste data for writing to the pty, including bracketed paste wrapping and unsafe byte stripping.

Examples

Safety Check

void safety_example() {
const char* safe_data = "hello world";
const char* unsafe_data = "rm -rf /\n";
if (ghostty_paste_is_safe(safe_data, strlen(safe_data))) {
printf("Safe to paste\n");
}
if (!ghostty_paste_is_safe(unsafe_data, strlen(unsafe_data))) {
printf("Unsafe! Contains newline\n");
}
}

Encoding

void encode_example() {
// The input buffer is modified in place (unsafe bytes are stripped).
char data[] = "hello\nworld";
char buf[64];
size_t written = 0;
data, strlen(data), true, buf, sizeof(buf), &written);
if (result == GHOSTTY_SUCCESS) {
printf("Encoded %zu bytes: ", written);
fwrite(buf, 1, written, stdout);
printf("\n");
}
}

Functions

GHOSTTY_API bool ghostty_paste_is_safe (const char *data, size_t len)
GHOSTTY_API GhosttyResult ghostty_paste_encode (char *data, size_t data_len, bool bracketed, char *buf, size_t buf_len, size_t *out_written)

Function Documentation

◆ ghostty_paste_encode()

GHOSTTY_API GhosttyResult ghostty_paste_encode ( char * data,
size_t data_len,
bool bracketed,
char * buf,
size_t buf_len,
size_t * out_written )

Encode paste data for writing to the terminal pty.

This function prepares paste data for terminal input by:

  • Stripping unsafe control bytes (NUL, ESC, DEL, etc.) by replacing them with spaces
  • Wrapping the data in bracketed paste sequences if bracketed is true
  • Replacing newlines with carriage returns if bracketed is false

The input data buffer is modified in place during encoding. The encoded result (potentially with bracketed paste prefix/suffix) is written to the output buffer.

If the output buffer is too small, the function returns GHOSTTY_OUT_OF_SPACE and sets the required size in out_written. The caller can then retry with a sufficiently sized buffer.

Parameters
dataThe paste data to encode (modified in place, may be NULL)
data_lenThe length of the input data in bytes
bracketedWhether bracketed paste mode is active
bufOutput buffer to write the encoded result into (may be NULL)
buf_lenSize of the output buffer in bytes
[out]out_writtenOn success, the number of bytes written. On GHOSTTY_OUT_OF_SPACE, the required buffer size.
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if the buffer is too small
Examples
c-vt-paste/src/main.c.

◆ ghostty_paste_is_safe()

GHOSTTY_API bool ghostty_paste_is_safe ( const char * data,
size_t len )

Check if paste data is safe to paste into the terminal.

Data is considered unsafe if it contains:

  • Newlines (\n) which can inject commands
  • The bracketed paste end sequence (\x1b[201~) which can be used to exit bracketed paste mode and inject commands

This check is conservative and considers data unsafe regardless of current terminal state.

Parameters
dataThe paste data to check (must not be NULL)
lenThe length of the data in bytes
Returns
true if the data is safe to paste, false otherwise
Examples
c-vt-paste/src/main.c.