Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

paste.h

237 lines8.5 KB

1/**
2 * @file paste.h
3 *
4 * Paste - paste into a terminal, and validate and encode paste data.
5 */
6​
7#ifndef GHOSTTY_VT_PASTE_H
8#define GHOSTTY_VT_PASTE_H
9​
10/** @addtogroup paste
11 *
12 * Paste into a terminal with ghostty_terminal_paste(), or validate and encode
13 * paste data without one.
14 *
15 * What a paste writes depends on the terminal's modes, and
16 * ghostty_terminal_paste() reads them itself. If you have a terminal, that's
17 * the function you want.
18 *
19 * ## Example
20 *
21 * @snippet c-vt-paste/src/main.c terminal-paste
22 *
23 * Full program: @ref c-vt-paste/src/main.c
24 *
25 * The @ref guide-input "Input guide" covers what a paste writes and how unsafe
26 * pastes are refused.
27 *
28 * @{
29 */
30​
31#include <stdbool.h>
32#include <stddef.h>
33#include <ghostty/vt/types.h>
34#include <ghostty/vt/io.h>
35#include <ghostty/vt/terminal.h>
36​
37#ifdef __cplusplus
38extern "C" {
39#endif
40​
41/**
42 * Why a paste happened.
43 */
44typedef enum GHOSTTY_ENUM_TYPED {
45 /** The user pasted from a clipboard: keybind, menu, middle click. */
46 GHOSTTY_PASTE_SOURCE_CLIPBOARD = 0,
47​
48 /**
49 * Text inserted some other way: IME commit, drag and drop, scripted
50 * input. Always written as text, never as a paste event, matching
51 * kitty. This is not a way to opt out of paste events; an embedder
52 * that doesn't want them doesn't install a clipboard_read callback.
53 */
54 GHOSTTY_PASTE_SOURCE_TEXT = 1,
55 GHOSTTY_PASTE_SOURCE_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE,
56} GhosttyPasteSource;
57​
58/**
59 * A paste of clipboard contents into the terminal.
60 *
61 * This is a sized struct. Initialize it with GHOSTTY_INIT_SIZED(), then set
62 * the fields you need. See @ref sized-structs.
63 *
64 * The MIME type array and the strings it points to are borrowed only for
65 * the duration of the ghostty_terminal_paste() call, as is everything
66 * the reader produces.
67 */
68typedef struct {
69 /** Size of this struct in bytes. */
70 size_t size;
71​
72 /**
73 * The clipboard the contents came from. Reported to the program on a
74 * paste event (the selection and primary locations are both reported
75 * as the primary selection, the protocol knows only two); no effect
76 * on a text paste.
77 */
78 GhosttyClipboardLocation location;
79​
80 /** Why this paste happened. */
81 GhosttyPasteSource source;
82​
83 /**
84 * Borrowed array of the MIME types of the representations available,
85 * in preferred order. A text paste reads and writes the first entry
86 * with a text MIME type such as "text/plain" and ignores the rest. A
87 * paste event lists every entry and reads none. May be NULL when
88 * mimes_len is zero, which is nothing to paste.
89 */
90 const GhosttyString* mimes;
91​
92 /** Number of entries in mimes. */
93 size_t mimes_len;
94​
95 /**
96 * Produces the data of a representation on demand. Required when
97 * mimes_len is nonzero.
98 *
99 * Called at most once per ghostty_terminal_paste() call: for the
100 * text representation being pasted, never for anything else and
101 * never for a paste event. The MIME type requested is always an
102 * entry of `mimes`, passed through exactly as given there (the same
103 * pointer and length), so the callback may identify the
104 * representation by pointer or by content. A false return fails the
105 * paste with GHOSTTY_IO_ERROR.
106 */
107 GhosttyMimeReader reader;
108​
109 /**
110 * Write text that could inject commands. Call with false, confirm
111 * with the person using your terminal on GHOSTTY_REJECTED, and call again with true.
112 */
113 bool allow_unsafe;
114} GhosttyPaste;
115​
116/**
117 * Paste into the terminal according to its current state: a Kitty
118 * clipboard protocol paste event if mode 5522 is enabled and a
119 * clipboard_read callback is installed, otherwise the text framed per
120 * mode 2004. See @ref guide-input-paste for the full behavior. Output
121 * streams through the write_pty callback in chunks. The viewport is not
122 * scrolled; that is up to you, as for key input.
123 *
124 * A paste event records a session grant for its one-time password only
125 * once the event is written; a failed call never leaves a grant for an
126 * event that was never sent.
127 *
128 * @param terminal The terminal handle
129 * @param paste The paste request, borrowed for the duration of the call
130 * @param[out] out_pasted On success, whether anything was written to
131 * the pty (the encoded text or a paste event). False means
132 * there was nothing to paste: no non-empty text
133 * representation. May be NULL.
134 * @return GHOSTTY_SUCCESS on success (see @p out_pasted).
135 * GHOSTTY_REJECTED if the text could inject commands and
136 * GhosttyPaste::allow_unsafe is false.
137 * GHOSTTY_INVALID_VALUE for a NULL terminal or paste, an unknown
138 * GhosttyPaste::source, MIME types without a reader, or when no
139 * write_pty callback is installed.
140 * GHOSTTY_OUT_OF_MEMORY if an allocation fails.
141 * GHOSTTY_IO_ERROR if the reader failed or there is no secure
142 * entropy source to create a paste event password
143 * (wasm32-freestanding without GHOSTTY_SYS_OPT_RANDOM_SECURE set).
144 * Errors write nothing.
145 */
146GHOSTTY_API GhosttyResult ghostty_terminal_paste(
147 GhosttyTerminal terminal,
148 const GhosttyPaste* paste,
149 bool* out_pasted);
150​
151/**
152 * Check if paste data is safe to paste into the terminal.
153 *
154 * Data is considered unsafe if it contains:
155 * - Line terminators (`\n` or `\r`) which can inject commands. A
156 * carriage return is what the Enter key sends, so it is as dangerous
157 * as a newline on an unbracketed paste.
158 * - The bracketed paste end sequence (`\x1b[201~`) which can be used
159 * to exit bracketed paste mode and inject commands
160 *
161 * This check is conservative and considers data unsafe regardless of
162 * current terminal state. ghostty_terminal_paste() applies the
163 * terminal-state-aware rule itself (line terminators are safe inside a
164 * bracketed paste); use this to apply the stricter rule on top.
165 *
166 * @param data The paste data to check (NULL is the same as empty data)
167 * @param len The length of the data in bytes
168 * @return true if the data is safe to paste, false otherwise
169 */
170GHOSTTY_API bool ghostty_paste_is_safe(const char* data, size_t len);
171​
172/**
173 * Encode paste data for writing to the terminal pty.
174 *
175 * This function prepares paste data for terminal input by:
176 * - Stripping unsafe control bytes (NUL, ESC, DEL, etc.) by replacing
177 * them with spaces
178 * - Wrapping the data in bracketed paste sequences if @p bracketed is true
179 * - Replacing newlines with carriage returns if @p bracketed is false
180 *
181 * The input @p data buffer is modified in place during encoding. The
182 * encoded result (potentially with bracketed paste prefix/suffix) is
183 * written to the output buffer.
184 *
185 * If @p buf is too small, this returns GHOSTTY_OUT_OF_SPACE, sets
186 * @p out_written to the size needed, and leaves @p buf unchanged. To get
187 * the size first, pass NULL for @p buf and 0 for @p buf_len. An empty
188 * result needs no space, so empty input with @p bracketed false returns
189 * GHOSTTY_SUCCESS with @p out_written set to 0.
190 *
191 * The function modifies @p data even when it returns GHOSTTY_OUT_OF_SPACE.
192 * Encoding the modified data again gives the same result, so you can retry
193 * with the same @p data and a larger buffer:
194 *
195 * @code{.c}
196 * size_t needed = 0;
197 * GhosttyResult result = ghostty_paste_encode(data, data_len, bracketed,
198 * NULL, 0, &needed);
199 * if (result == GHOSTTY_OUT_OF_SPACE) {
200 * uint8_t *buf = malloc(needed);
201 * size_t written = 0;
202 * result = ghostty_paste_encode(data, data_len, bracketed, buf, needed,
203 * &written);
204 * if (result == GHOSTTY_SUCCESS) write(pty_fd, buf, written);
205 * free(buf);
206 * }
207 * @endcode
208 *
209 * This is the encoder ghostty_terminal_paste() uses for a text paste;
210 * use it directly when there is no terminal to paste into.
211 *
212 * @param data The paste data to encode (modified in place, may be NULL)
213 * @param data_len The length of the input data in bytes
214 * @param bracketed Whether bracketed paste mode is active
215 * @param buf Buffer to write the encoded result into. May be NULL if
216 * @p buf_len is 0.
217 * @param buf_len Size of @p buf in bytes
218 * @param[out] out_written The number of bytes written, or the size needed
219 * if the result is GHOSTTY_OUT_OF_SPACE
220 * @return GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if the buffer
221 * is too small, or GHOSTTY_INVALID_VALUE if @p out_written is NULL
222 */
223GHOSTTY_API GhosttyResult ghostty_paste_encode(
224 char* data,
225 size_t data_len,
226 bool bracketed,
227 uint8_t* buf,
228 size_t buf_len,
229 size_t* out_written);
230​
231#ifdef __cplusplus
233#endif
234​
235/** @} */
236​
237#endif /* GHOSTTY_VT_PASTE_H */