Merge branch 'obsd-master'
[tmux.git] / paste.c
blob608ac9c678bad6cd5a7744cf6ea5f80d4aaa4615
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
25 #include "tmux.h"
28 * Set of paste buffers. Note that paste buffer data is not necessarily a C
29 * string!
32 struct paste_buffer {
33 char *data;
34 size_t size;
36 char *name;
37 time_t created;
38 int automatic;
39 u_int order;
41 RB_ENTRY(paste_buffer) name_entry;
42 RB_ENTRY(paste_buffer) time_entry;
45 static u_int paste_next_index;
46 static u_int paste_next_order;
47 static u_int paste_num_automatic;
48 static RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
49 static RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
51 static int paste_cmp_names(const struct paste_buffer *,
52 const struct paste_buffer *);
53 RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
55 static int paste_cmp_times(const struct paste_buffer *,
56 const struct paste_buffer *);
57 RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
59 static int
60 paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
62 return (strcmp(a->name, b->name));
65 static int
66 paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
68 if (a->order > b->order)
69 return (-1);
70 if (a->order < b->order)
71 return (1);
72 return (0);
75 /* Get paste buffer name. */
76 const char *
77 paste_buffer_name(struct paste_buffer *pb)
79 return (pb->name);
82 /* Get paste buffer order. */
83 u_int
84 paste_buffer_order(struct paste_buffer *pb)
86 return (pb->order);
89 /* Get paste buffer created. */
90 time_t
91 paste_buffer_created(struct paste_buffer *pb)
93 return (pb->created);
96 /* Get paste buffer data. */
97 const char *
98 paste_buffer_data(struct paste_buffer *pb, size_t *size)
100 if (size != NULL)
101 *size = pb->size;
102 return (pb->data);
105 /* Walk paste buffers by time. */
106 struct paste_buffer *
107 paste_walk(struct paste_buffer *pb)
109 if (pb == NULL)
110 return (RB_MIN(paste_time_tree, &paste_by_time));
111 return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
115 paste_is_empty(void)
117 return RB_ROOT(&paste_by_time) == NULL;
120 /* Get the most recent automatic buffer. */
121 struct paste_buffer *
122 paste_get_top(const char **name)
124 struct paste_buffer *pb;
126 pb = RB_MIN(paste_time_tree, &paste_by_time);
127 while (pb != NULL && !pb->automatic)
128 pb = RB_NEXT(paste_time_tree, &paste_by_time, pb);
129 if (pb == NULL)
130 return (NULL);
131 if (name != NULL)
132 *name = pb->name;
133 return (pb);
136 /* Get a paste buffer by name. */
137 struct paste_buffer *
138 paste_get_name(const char *name)
140 struct paste_buffer pbfind;
142 if (name == NULL || *name == '\0')
143 return (NULL);
145 pbfind.name = (char *)name;
146 return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
149 /* Free a paste buffer. */
150 void
151 paste_free(struct paste_buffer *pb)
153 notify_paste_buffer(pb->name, 1);
155 RB_REMOVE(paste_name_tree, &paste_by_name, pb);
156 RB_REMOVE(paste_time_tree, &paste_by_time, pb);
157 if (pb->automatic)
158 paste_num_automatic--;
160 free(pb->data);
161 free(pb->name);
162 free(pb);
166 * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
167 * that the caller is responsible for allocating data.
169 void
170 paste_add(const char *prefix, char *data, size_t size)
172 struct paste_buffer *pb, *pb1;
173 u_int limit;
175 if (prefix == NULL)
176 prefix = "buffer";
178 if (size == 0) {
179 free(data);
180 return;
183 limit = options_get_number(global_options, "buffer-limit");
184 RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
185 if (paste_num_automatic < limit)
186 break;
187 if (pb->automatic)
188 paste_free(pb);
191 pb = xmalloc(sizeof *pb);
193 pb->name = NULL;
194 do {
195 free(pb->name);
196 xasprintf(&pb->name, "%s%u", prefix, paste_next_index);
197 paste_next_index++;
198 } while (paste_get_name(pb->name) != NULL);
200 pb->data = data;
201 pb->size = size;
203 pb->automatic = 1;
204 paste_num_automatic++;
206 pb->created = time(NULL);
208 pb->order = paste_next_order++;
209 RB_INSERT(paste_name_tree, &paste_by_name, pb);
210 RB_INSERT(paste_time_tree, &paste_by_time, pb);
212 notify_paste_buffer(pb->name, 0);
215 /* Rename a paste buffer. */
217 paste_rename(const char *oldname, const char *newname, char **cause)
219 struct paste_buffer *pb, *pb_new;
221 if (cause != NULL)
222 *cause = NULL;
224 if (oldname == NULL || *oldname == '\0') {
225 if (cause != NULL)
226 *cause = xstrdup("no buffer");
227 return (-1);
229 if (newname == NULL || *newname == '\0') {
230 if (cause != NULL)
231 *cause = xstrdup("new name is empty");
232 return (-1);
235 pb = paste_get_name(oldname);
236 if (pb == NULL) {
237 if (cause != NULL)
238 xasprintf(cause, "no buffer %s", oldname);
239 return (-1);
242 pb_new = paste_get_name(newname);
243 if (pb_new != NULL)
244 paste_free(pb_new);
246 RB_REMOVE(paste_name_tree, &paste_by_name, pb);
248 free(pb->name);
249 pb->name = xstrdup(newname);
251 if (pb->automatic)
252 paste_num_automatic--;
253 pb->automatic = 0;
255 RB_INSERT(paste_name_tree, &paste_by_name, pb);
257 notify_paste_buffer(oldname, 1);
258 notify_paste_buffer(newname, 0);
260 return (0);
264 * Add or replace an item in the store. Note that the caller is responsible for
265 * allocating data.
268 paste_set(char *data, size_t size, const char *name, char **cause)
270 struct paste_buffer *pb, *old;
272 if (cause != NULL)
273 *cause = NULL;
275 if (size == 0) {
276 free(data);
277 return (0);
279 if (name == NULL) {
280 paste_add(NULL, data, size);
281 return (0);
284 if (*name == '\0') {
285 if (cause != NULL)
286 *cause = xstrdup("empty buffer name");
287 return (-1);
290 pb = xmalloc(sizeof *pb);
292 pb->name = xstrdup(name);
294 pb->data = data;
295 pb->size = size;
297 pb->automatic = 0;
298 pb->order = paste_next_order++;
300 pb->created = time(NULL);
302 if ((old = paste_get_name(name)) != NULL)
303 paste_free(old);
305 RB_INSERT(paste_name_tree, &paste_by_name, pb);
306 RB_INSERT(paste_time_tree, &paste_by_time, pb);
308 notify_paste_buffer(name, 0);
310 return (0);
313 /* Set paste data without otherwise changing it. */
314 void
315 paste_replace(struct paste_buffer *pb, char *data, size_t size)
317 free(pb->data);
318 pb->data = data;
319 pb->size = size;
321 notify_paste_buffer(pb->name, 0);
324 /* Convert start of buffer into a nice string. */
325 char *
326 paste_make_sample(struct paste_buffer *pb)
328 char *buf;
329 size_t len, used;
330 const int flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
331 const size_t width = 200;
333 len = pb->size;
334 if (len > width)
335 len = width;
336 buf = xreallocarray(NULL, len, 4 + 4);
338 used = utf8_strvis(buf, pb->data, len, flags);
339 if (pb->size > width || used > width)
340 strlcpy(buf + width, "...", 4);
341 return (buf);