xref: /openbsd/usr.bin/tmux/paste.c (revision 232cb8cb)
1 /* $OpenBSD: paste.c,v 1.47 2024/10/12 08:13:52 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <vis.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Set of paste buffers. Note that paste buffer data is not necessarily a C
30  * string!
31  */
32 
33 struct paste_buffer {
34 	char		*data;
35 	size_t		 size;
36 
37 	char		*name;
38 	time_t		 created;
39 	int		 automatic;
40 	u_int		 order;
41 
42 	RB_ENTRY(paste_buffer) name_entry;
43 	RB_ENTRY(paste_buffer) time_entry;
44 };
45 
46 static u_int	paste_next_index;
47 static u_int	paste_next_order;
48 static u_int	paste_num_automatic;
49 static RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
50 static RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
51 
52 static int	paste_cmp_names(const struct paste_buffer *,
53 		    const struct paste_buffer *);
54 RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
55 
56 static int	paste_cmp_times(const struct paste_buffer *,
57 		    const struct paste_buffer *);
58 RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
59 
60 static int
paste_cmp_names(const struct paste_buffer * a,const struct paste_buffer * b)61 paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
62 {
63 	return (strcmp(a->name, b->name));
64 }
65 
66 static int
paste_cmp_times(const struct paste_buffer * a,const struct paste_buffer * b)67 paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
68 {
69 	if (a->order > b->order)
70 		return (-1);
71 	if (a->order < b->order)
72 		return (1);
73 	return (0);
74 }
75 
76 /* Get paste buffer name. */
77 const char *
paste_buffer_name(struct paste_buffer * pb)78 paste_buffer_name(struct paste_buffer *pb)
79 {
80 	return (pb->name);
81 }
82 
83 /* Get paste buffer order. */
84 u_int
paste_buffer_order(struct paste_buffer * pb)85 paste_buffer_order(struct paste_buffer *pb)
86 {
87 	return (pb->order);
88 }
89 
90 /* Get paste buffer created. */
91 time_t
paste_buffer_created(struct paste_buffer * pb)92 paste_buffer_created(struct paste_buffer *pb)
93 {
94 	return (pb->created);
95 }
96 
97 /* Get paste buffer data. */
98 const char *
paste_buffer_data(struct paste_buffer * pb,size_t * size)99 paste_buffer_data(struct paste_buffer *pb, size_t *size)
100 {
101 	if (size != NULL)
102 		*size = pb->size;
103 	return (pb->data);
104 }
105 
106 /* Walk paste buffers by time. */
107 struct paste_buffer *
paste_walk(struct paste_buffer * pb)108 paste_walk(struct paste_buffer *pb)
109 {
110 	if (pb == NULL)
111 		return (RB_MIN(paste_time_tree, &paste_by_time));
112 	return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
113 }
114 
115 int
paste_is_empty(void)116 paste_is_empty(void)
117 {
118 	return RB_ROOT(&paste_by_time) == NULL;
119 }
120 
121 /* Get the most recent automatic buffer. */
122 struct paste_buffer *
paste_get_top(const char ** name)123 paste_get_top(const char **name)
124 {
125 	struct paste_buffer	*pb;
126 
127 	pb = RB_MIN(paste_time_tree, &paste_by_time);
128 	while (pb != NULL && !pb->automatic)
129 		pb = RB_NEXT(paste_time_tree, &paste_by_time, pb);
130 	if (pb == NULL)
131 		return (NULL);
132 	if (name != NULL)
133 		*name = pb->name;
134 	return (pb);
135 }
136 
137 /* Get a paste buffer by name. */
138 struct paste_buffer *
paste_get_name(const char * name)139 paste_get_name(const char *name)
140 {
141 	struct paste_buffer	pbfind;
142 
143 	if (name == NULL || *name == '\0')
144 		return (NULL);
145 
146 	pbfind.name = (char *)name;
147 	return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
148 }
149 
150 /* Free a paste buffer. */
151 void
paste_free(struct paste_buffer * pb)152 paste_free(struct paste_buffer *pb)
153 {
154 	notify_paste_buffer(pb->name, 1);
155 
156 	RB_REMOVE(paste_name_tree, &paste_by_name, pb);
157 	RB_REMOVE(paste_time_tree, &paste_by_time, pb);
158 	if (pb->automatic)
159 		paste_num_automatic--;
160 
161 	free(pb->data);
162 	free(pb->name);
163 	free(pb);
164 }
165 
166 /*
167  * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
168  * that the caller is responsible for allocating data.
169  */
170 void
paste_add(const char * prefix,char * data,size_t size)171 paste_add(const char *prefix, char *data, size_t size)
172 {
173 	struct paste_buffer	*pb, *pb1;
174 	u_int			 limit;
175 
176 	if (prefix == NULL)
177 		prefix = "buffer";
178 
179 	if (size == 0) {
180 		free(data);
181 		return;
182 	}
183 
184 	limit = options_get_number(global_options, "buffer-limit");
185 	RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
186 		if (paste_num_automatic < limit)
187 			break;
188 		if (pb->automatic)
189 			paste_free(pb);
190 	}
191 
192 	pb = xmalloc(sizeof *pb);
193 
194 	pb->name = NULL;
195 	do {
196 		free(pb->name);
197 		xasprintf(&pb->name, "%s%u", prefix, paste_next_index);
198 		paste_next_index++;
199 	} while (paste_get_name(pb->name) != NULL);
200 
201 	pb->data = data;
202 	pb->size = size;
203 
204 	pb->automatic = 1;
205 	paste_num_automatic++;
206 
207 	pb->created = time(NULL);
208 
209 	pb->order = paste_next_order++;
210 	RB_INSERT(paste_name_tree, &paste_by_name, pb);
211 	RB_INSERT(paste_time_tree, &paste_by_time, pb);
212 
213 	notify_paste_buffer(pb->name, 0);
214 }
215 
216 /* Rename a paste buffer. */
217 int
paste_rename(const char * oldname,const char * newname,char ** cause)218 paste_rename(const char *oldname, const char *newname, char **cause)
219 {
220 	struct paste_buffer	*pb, *pb_new;
221 
222 	if (cause != NULL)
223 		*cause = NULL;
224 
225 	if (oldname == NULL || *oldname == '\0') {
226 		if (cause != NULL)
227 			*cause = xstrdup("no buffer");
228 		return (-1);
229 	}
230 	if (newname == NULL || *newname == '\0') {
231 		if (cause != NULL)
232 			*cause = xstrdup("new name is empty");
233 		return (-1);
234 	}
235 
236 	pb = paste_get_name(oldname);
237 	if (pb == NULL) {
238 		if (cause != NULL)
239 			xasprintf(cause, "no buffer %s", oldname);
240 		return (-1);
241 	}
242 
243 	pb_new = paste_get_name(newname);
244 	if (pb_new == pb)
245 		return (0);
246 	if (pb_new != NULL)
247 		paste_free(pb_new);
248 
249 	RB_REMOVE(paste_name_tree, &paste_by_name, pb);
250 
251 	free(pb->name);
252 	pb->name = xstrdup(newname);
253 
254 	if (pb->automatic)
255 		paste_num_automatic--;
256 	pb->automatic = 0;
257 
258 	RB_INSERT(paste_name_tree, &paste_by_name, pb);
259 
260 	notify_paste_buffer(oldname, 1);
261 	notify_paste_buffer(newname, 0);
262 
263 	return (0);
264 }
265 
266 /*
267  * Add or replace an item in the store. Note that the caller is responsible for
268  * allocating data.
269  */
270 int
paste_set(char * data,size_t size,const char * name,char ** cause)271 paste_set(char *data, size_t size, const char *name, char **cause)
272 {
273 	struct paste_buffer	*pb, *old;
274 
275 	if (cause != NULL)
276 		*cause = NULL;
277 
278 	if (size == 0) {
279 		free(data);
280 		return (0);
281 	}
282 	if (name == NULL) {
283 		paste_add(NULL, data, size);
284 		return (0);
285 	}
286 
287 	if (*name == '\0') {
288 		if (cause != NULL)
289 			*cause = xstrdup("empty buffer name");
290 		return (-1);
291 	}
292 
293 	pb = xmalloc(sizeof *pb);
294 
295 	pb->name = xstrdup(name);
296 
297 	pb->data = data;
298 	pb->size = size;
299 
300 	pb->automatic = 0;
301 	pb->order = paste_next_order++;
302 
303 	pb->created = time(NULL);
304 
305 	if ((old = paste_get_name(name)) != NULL)
306 		paste_free(old);
307 
308 	RB_INSERT(paste_name_tree, &paste_by_name, pb);
309 	RB_INSERT(paste_time_tree, &paste_by_time, pb);
310 
311 	notify_paste_buffer(name, 0);
312 
313 	return (0);
314 }
315 
316 /* Set paste data without otherwise changing it. */
317 void
paste_replace(struct paste_buffer * pb,char * data,size_t size)318 paste_replace(struct paste_buffer *pb, char *data, size_t size)
319 {
320 	free(pb->data);
321 	pb->data = data;
322 	pb->size = size;
323 
324 	notify_paste_buffer(pb->name, 0);
325 }
326 
327 /* Convert start of buffer into a nice string. */
328 char *
paste_make_sample(struct paste_buffer * pb)329 paste_make_sample(struct paste_buffer *pb)
330 {
331 	char		*buf;
332 	size_t		 len, used;
333 	const int	 flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
334 	const size_t	 width = 200;
335 
336 	len = pb->size;
337 	if (len > width)
338 		len = width;
339 	buf = xreallocarray(NULL, len, 4 + 4);
340 
341 	used = utf8_strvis(buf, pb->data, len, flags);
342 	if (pb->size > width || used > width)
343 		strlcpy(buf + width, "...", 4);
344 	return (buf);
345 }
346