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>
29 * Set of paste buffers. Note that paste buffer data is not necessarily a C
42 RB_ENTRY(paste_buffer
) name_entry
;
43 RB_ENTRY(paste_buffer
) time_entry
;
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
;
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
);
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
);
61 paste_cmp_names(const struct paste_buffer
*a
, const struct paste_buffer
*b
)
63 return (strcmp(a
->name
, b
->name
));
67 paste_cmp_times(const struct paste_buffer
*a
, const struct paste_buffer
*b
)
69 if (a
->order
> b
->order
)
71 if (a
->order
< b
->order
)
76 /* Get paste buffer name. */
78 paste_buffer_name(struct paste_buffer
*pb
)
83 /* Get paste buffer order. */
85 paste_buffer_order(struct paste_buffer
*pb
)
90 /* Get paste buffer created. */
92 paste_buffer_created(struct paste_buffer
*pb
)
97 /* Get paste buffer data. */
99 paste_buffer_data(struct paste_buffer
*pb
, size_t *size
)
106 /* Walk paste buffers by time. */
107 struct paste_buffer
*
108 paste_walk(struct paste_buffer
*pb
)
111 return (RB_MIN(paste_time_tree
, &paste_by_time
));
112 return (RB_NEXT(paste_time_tree
, &paste_by_time
, pb
));
118 return RB_ROOT(&paste_by_time
) == NULL
;
121 /* Get the most recent automatic buffer. */
122 struct paste_buffer
*
123 paste_get_top(const char **name
)
125 struct paste_buffer
*pb
;
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
);
137 /* Get a paste buffer by name. */
138 struct paste_buffer
*
139 paste_get_name(const char *name
)
141 struct paste_buffer pbfind
;
143 if (name
== NULL
|| *name
== '\0')
146 pbfind
.name
= (char *)name
;
147 return (RB_FIND(paste_name_tree
, &paste_by_name
, &pbfind
));
150 /* Free a paste buffer. */
152 paste_free(struct paste_buffer
*pb
)
154 notify_paste_buffer(pb
->name
, 1);
156 RB_REMOVE(paste_name_tree
, &paste_by_name
, pb
);
157 RB_REMOVE(paste_time_tree
, &paste_by_time
, pb
);
159 paste_num_automatic
--;
167 * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
168 * that the caller is responsible for allocating data.
171 paste_add(const char *prefix
, char *data
, size_t size
)
173 struct paste_buffer
*pb
, *pb1
;
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
)
192 pb
= xmalloc(sizeof *pb
);
197 xasprintf(&pb
->name
, "%s%u", prefix
, paste_next_index
);
199 } while (paste_get_name(pb
->name
) != NULL
);
205 paste_num_automatic
++;
207 pb
->created
= time(NULL
);
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
);
213 notify_paste_buffer(pb
->name
, 0);
216 /* Rename a paste buffer. */
218 paste_rename(const char *oldname
, const char *newname
, char **cause
)
220 struct paste_buffer
*pb
, *pb_new
;
225 if (oldname
== NULL
|| *oldname
== '\0') {
227 *cause
= xstrdup("no buffer");
230 if (newname
== NULL
|| *newname
== '\0') {
232 *cause
= xstrdup("new name is empty");
236 pb
= paste_get_name(oldname
);
239 xasprintf(cause
, "no buffer %s", oldname
);
243 pb_new
= paste_get_name(newname
);
244 if (pb_new
!= NULL
) {
246 xasprintf(cause
, "buffer %s already exists", newname
);
250 RB_REMOVE(paste_name_tree
, &paste_by_name
, pb
);
253 pb
->name
= xstrdup(newname
);
256 paste_num_automatic
--;
259 RB_INSERT(paste_name_tree
, &paste_by_name
, pb
);
261 notify_paste_buffer(oldname
, 1);
262 notify_paste_buffer(newname
, 0);
268 * Add or replace an item in the store. Note that the caller is responsible for
272 paste_set(char *data
, size_t size
, const char *name
, char **cause
)
274 struct paste_buffer
*pb
, *old
;
284 paste_add(NULL
, data
, size
);
290 *cause
= xstrdup("empty buffer name");
294 pb
= xmalloc(sizeof *pb
);
296 pb
->name
= xstrdup(name
);
302 pb
->order
= paste_next_order
++;
304 pb
->created
= time(NULL
);
306 if ((old
= paste_get_name(name
)) != NULL
)
309 RB_INSERT(paste_name_tree
, &paste_by_name
, pb
);
310 RB_INSERT(paste_time_tree
, &paste_by_time
, pb
);
312 notify_paste_buffer(name
, 0);
317 /* Set paste data without otherwise changing it. */
319 paste_replace(struct paste_buffer
*pb
, char *data
, size_t size
)
325 notify_paste_buffer(pb
->name
, 0);
328 /* Convert start of buffer into a nice string. */
330 paste_make_sample(struct paste_buffer
*pb
)
334 const int flags
= VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
;
335 const size_t width
= 200;
340 buf
= xreallocarray(NULL
, len
, 4 + 4);
342 used
= utf8_strvis(buf
, pb
->data
, len
, flags
);
343 if (pb
->size
> width
|| used
> width
)
344 strlcpy(buf
+ width
, "...", 4);