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>
28 * Set of paste buffers. Note that paste buffer data is not necessarily a C
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
);
60 paste_cmp_names(const struct paste_buffer
*a
, const struct paste_buffer
*b
)
62 return (strcmp(a
->name
, b
->name
));
66 paste_cmp_times(const struct paste_buffer
*a
, const struct paste_buffer
*b
)
68 if (a
->order
> b
->order
)
70 if (a
->order
< b
->order
)
75 /* Get paste buffer name. */
77 paste_buffer_name(struct paste_buffer
*pb
)
82 /* Get paste buffer order. */
84 paste_buffer_order(struct paste_buffer
*pb
)
89 /* Get paste buffer created. */
91 paste_buffer_created(struct paste_buffer
*pb
)
96 /* Get paste buffer data. */
98 paste_buffer_data(struct paste_buffer
*pb
, size_t *size
)
105 /* Walk paste buffers by time. */
106 struct paste_buffer
*
107 paste_walk(struct paste_buffer
*pb
)
110 return (RB_MIN(paste_time_tree
, &paste_by_time
));
111 return (RB_NEXT(paste_time_tree
, &paste_by_time
, pb
));
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
);
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')
145 pbfind
.name
= (char *)name
;
146 return (RB_FIND(paste_name_tree
, &paste_by_name
, &pbfind
));
149 /* Free a paste buffer. */
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
);
158 paste_num_automatic
--;
166 * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
167 * that the caller is responsible for allocating data.
170 paste_add(const char *prefix
, char *data
, size_t size
)
172 struct paste_buffer
*pb
, *pb1
;
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
)
191 pb
= xmalloc(sizeof *pb
);
196 xasprintf(&pb
->name
, "%s%u", prefix
, paste_next_index
);
198 } while (paste_get_name(pb
->name
) != NULL
);
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
;
224 if (oldname
== NULL
|| *oldname
== '\0') {
226 *cause
= xstrdup("no buffer");
229 if (newname
== NULL
|| *newname
== '\0') {
231 *cause
= xstrdup("new name is empty");
235 pb
= paste_get_name(oldname
);
238 xasprintf(cause
, "no buffer %s", oldname
);
242 pb_new
= paste_get_name(newname
);
248 RB_REMOVE(paste_name_tree
, &paste_by_name
, pb
);
251 pb
->name
= xstrdup(newname
);
254 paste_num_automatic
--;
257 RB_INSERT(paste_name_tree
, &paste_by_name
, pb
);
259 notify_paste_buffer(oldname
, 1);
260 notify_paste_buffer(newname
, 0);
266 * Add or replace an item in the store. Note that the caller is responsible for
270 paste_set(char *data
, size_t size
, const char *name
, char **cause
)
272 struct paste_buffer
*pb
, *old
;
282 paste_add(NULL
, data
, size
);
288 *cause
= xstrdup("empty buffer name");
292 pb
= xmalloc(sizeof *pb
);
294 pb
->name
= xstrdup(name
);
300 pb
->order
= paste_next_order
++;
302 pb
->created
= time(NULL
);
304 if ((old
= paste_get_name(name
)) != NULL
)
307 RB_INSERT(paste_name_tree
, &paste_by_name
, pb
);
308 RB_INSERT(paste_time_tree
, &paste_by_time
, pb
);
310 notify_paste_buffer(name
, 0);
315 /* Set paste data without otherwise changing it. */
317 paste_replace(struct paste_buffer
*pb
, char *data
, size_t size
)
323 notify_paste_buffer(pb
->name
, 0);
326 /* Convert start of buffer into a nice string. */
328 paste_make_sample(struct paste_buffer
*pb
)
332 const int flags
= VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
;
333 const size_t width
= 200;
338 buf
= xreallocarray(NULL
, len
, 4 + 4);
340 used
= utf8_strvis(buf
, pb
->data
, len
, flags
);
341 if (pb
->size
> width
|| used
> width
)
342 strlcpy(buf
+ width
, "...", 4);