2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2016-10-12 09:07:58 +00:00
|
|
|
#include <time.h>
|
2010-06-21 21:44:09 +00:00
|
|
|
#include <vis.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-26 22:28:24 +00:00
|
|
|
/*
|
2014-05-13 07:34:35 +00:00
|
|
|
* Set of paste buffers. Note that paste buffer data is not necessarily a C
|
2009-11-26 22:28:24 +00:00
|
|
|
* string!
|
|
|
|
*/
|
|
|
|
|
2015-08-29 09:25:00 +00:00
|
|
|
struct paste_buffer {
|
|
|
|
char *data;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
char *name;
|
2016-10-05 12:34:05 +00:00
|
|
|
time_t created;
|
2015-08-29 09:25:00 +00:00
|
|
|
int automatic;
|
|
|
|
u_int order;
|
|
|
|
|
|
|
|
RB_ENTRY(paste_buffer) name_entry;
|
|
|
|
RB_ENTRY(paste_buffer) time_entry;
|
|
|
|
};
|
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static u_int paste_next_index;
|
|
|
|
static u_int paste_next_order;
|
|
|
|
static u_int paste_num_automatic;
|
2016-10-11 13:45:47 +00:00
|
|
|
static RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
|
|
|
|
static RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
|
2014-04-24 09:14:43 +00:00
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static int paste_cmp_names(const struct paste_buffer *,
|
|
|
|
const struct paste_buffer *);
|
|
|
|
RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
|
2014-05-13 07:34:35 +00:00
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static int paste_cmp_times(const struct paste_buffer *,
|
|
|
|
const struct paste_buffer *);
|
|
|
|
RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
|
2014-05-13 07:34:35 +00:00
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static int
|
2014-05-13 07:34:35 +00:00
|
|
|
paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2014-05-13 07:34:35 +00:00
|
|
|
return (strcmp(a->name, b->name));
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2016-10-10 13:54:47 +00:00
|
|
|
static int
|
2014-05-13 07:34:35 +00:00
|
|
|
paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
|
|
|
|
{
|
|
|
|
if (a->order > b->order)
|
|
|
|
return (-1);
|
|
|
|
if (a->order < b->order)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 09:25:00 +00:00
|
|
|
/* Get paste buffer name. */
|
|
|
|
const char *
|
|
|
|
paste_buffer_name(struct paste_buffer *pb)
|
|
|
|
{
|
|
|
|
return (pb->name);
|
|
|
|
}
|
|
|
|
|
2016-10-05 12:34:05 +00:00
|
|
|
/* Get paste buffer order. */
|
|
|
|
u_int
|
|
|
|
paste_buffer_order(struct paste_buffer *pb)
|
|
|
|
{
|
|
|
|
return (pb->order);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get paste buffer created. */
|
|
|
|
time_t
|
|
|
|
paste_buffer_created(struct paste_buffer *pb)
|
|
|
|
{
|
|
|
|
return (pb->created);
|
|
|
|
}
|
|
|
|
|
2015-08-29 09:25:00 +00:00
|
|
|
/* Get paste buffer data. */
|
|
|
|
const char *
|
|
|
|
paste_buffer_data(struct paste_buffer *pb, size_t *size)
|
|
|
|
{
|
|
|
|
if (size != NULL)
|
|
|
|
*size = pb->size;
|
|
|
|
return (pb->data);
|
|
|
|
}
|
|
|
|
|
2016-10-05 12:34:05 +00:00
|
|
|
/* Walk paste buffers by time. */
|
2009-06-01 22:58:49 +00:00
|
|
|
struct paste_buffer *
|
2014-05-13 07:34:35 +00:00
|
|
|
paste_walk(struct paste_buffer *pb)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2014-05-13 07:34:35 +00:00
|
|
|
if (pb == NULL)
|
|
|
|
return (RB_MIN(paste_time_tree, &paste_by_time));
|
|
|
|
return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 07:42:07 +00:00
|
|
|
int
|
|
|
|
paste_is_empty(void)
|
|
|
|
{
|
|
|
|
return RB_ROOT(&paste_by_time) == NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-20 11:00:19 +00:00
|
|
|
/* Get the most recent automatic buffer. */
|
2009-06-01 22:58:49 +00:00
|
|
|
struct paste_buffer *
|
2015-08-29 09:25:00 +00:00
|
|
|
paste_get_top(const char **name)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2014-05-13 07:34:35 +00:00
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
pb = RB_MIN(paste_time_tree, &paste_by_time);
|
2022-06-04 07:42:07 +00:00
|
|
|
while (pb != NULL && !pb->automatic)
|
|
|
|
pb = RB_NEXT(paste_time_tree, &paste_by_time, pb);
|
2014-05-13 07:34:35 +00:00
|
|
|
if (pb == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (NULL);
|
2015-08-29 09:25:00 +00:00
|
|
|
if (name != NULL)
|
|
|
|
*name = pb->name;
|
2014-05-13 07:34:35 +00:00
|
|
|
return (pb);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
/* Get a paste buffer by name. */
|
|
|
|
struct paste_buffer *
|
|
|
|
paste_get_name(const char *name)
|
|
|
|
{
|
|
|
|
struct paste_buffer pbfind;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
if (name == NULL || *name == '\0')
|
|
|
|
return (NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-09-01 21:50:18 +00:00
|
|
|
pbfind.name = (char *)name;
|
2014-05-13 07:34:35 +00:00
|
|
|
return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 14:41:50 +00:00
|
|
|
/* Free a paste buffer. */
|
|
|
|
void
|
|
|
|
paste_free(struct paste_buffer *pb)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_paste_buffer(pb->name);
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
RB_REMOVE(paste_name_tree, &paste_by_name, pb);
|
|
|
|
RB_REMOVE(paste_time_tree, &paste_by_time, pb);
|
|
|
|
if (pb->automatic)
|
|
|
|
paste_num_automatic--;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(pb->data);
|
2014-05-13 07:34:35 +00:00
|
|
|
free(pb->name);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(pb);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2014-05-13 07:34:35 +00:00
|
|
|
* Add an automatic buffer, freeing the oldest automatic item if at limit. Note
|
2009-11-26 22:28:24 +00:00
|
|
|
* that the caller is responsible for allocating data.
|
|
|
|
*/
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2019-04-02 09:03:39 +00:00
|
|
|
paste_add(const char *prefix, char *data, size_t size)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2014-05-13 07:34:35 +00:00
|
|
|
struct paste_buffer *pb, *pb1;
|
|
|
|
u_int limit;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2019-04-02 09:03:39 +00:00
|
|
|
if (prefix == NULL)
|
|
|
|
prefix = "buffer";
|
|
|
|
|
2017-01-24 13:28:33 +00:00
|
|
|
if (size == 0) {
|
|
|
|
free(data);
|
2009-07-02 16:15:43 +00:00
|
|
|
return;
|
2017-01-24 13:28:33 +00:00
|
|
|
}
|
2009-07-02 16:15:43 +00:00
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
limit = options_get_number(global_options, "buffer-limit");
|
2014-05-13 07:34:35 +00:00
|
|
|
RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
|
|
|
|
if (paste_num_automatic < limit)
|
|
|
|
break;
|
|
|
|
if (pb->automatic)
|
2015-09-11 14:41:50 +00:00
|
|
|
paste_free(pb);
|
2009-07-30 20:50:54 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
pb = xmalloc(sizeof *pb);
|
2014-05-13 07:34:35 +00:00
|
|
|
|
|
|
|
pb->name = NULL;
|
|
|
|
do {
|
|
|
|
free(pb->name);
|
2019-04-02 09:03:39 +00:00
|
|
|
xasprintf(&pb->name, "%s%u", prefix, paste_next_index);
|
2014-05-13 07:34:35 +00:00
|
|
|
paste_next_index++;
|
|
|
|
} while (paste_get_name(pb->name) != NULL);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
pb->data = data;
|
2009-09-07 18:50:45 +00:00
|
|
|
pb->size = size;
|
2014-05-13 07:34:35 +00:00
|
|
|
|
|
|
|
pb->automatic = 1;
|
|
|
|
paste_num_automatic++;
|
|
|
|
|
2016-10-05 12:34:05 +00:00
|
|
|
pb->created = time(NULL);
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
pb->order = paste_next_order++;
|
|
|
|
RB_INSERT(paste_name_tree, &paste_by_name, pb);
|
|
|
|
RB_INSERT(paste_time_tree, &paste_by_time, pb);
|
2022-08-15 09:10:34 +00:00
|
|
|
|
|
|
|
notify_paste_buffer(pb->name);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
/* Rename a paste buffer. */
|
|
|
|
int
|
|
|
|
paste_rename(const char *oldname, const char *newname, char **cause)
|
|
|
|
{
|
2014-05-13 22:54:18 +00:00
|
|
|
struct paste_buffer *pb, *pb_new;
|
2014-05-13 07:34:35 +00:00
|
|
|
|
|
|
|
if (cause != NULL)
|
|
|
|
*cause = NULL;
|
|
|
|
|
|
|
|
if (oldname == NULL || *oldname == '\0') {
|
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("no buffer");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (newname == NULL || *newname == '\0') {
|
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("new name is empty");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pb = paste_get_name(oldname);
|
|
|
|
if (pb == NULL) {
|
|
|
|
if (cause != NULL)
|
2014-05-13 22:54:18 +00:00
|
|
|
xasprintf(cause, "no buffer %s", oldname);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pb_new = paste_get_name(newname);
|
|
|
|
if (pb_new != NULL) {
|
|
|
|
if (cause != NULL)
|
|
|
|
xasprintf(cause, "buffer %s already exists", newname);
|
2014-05-13 07:34:35 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_REMOVE(paste_name_tree, &paste_by_name, pb);
|
|
|
|
|
|
|
|
free(pb->name);
|
|
|
|
pb->name = xstrdup(newname);
|
|
|
|
|
|
|
|
if (pb->automatic)
|
|
|
|
paste_num_automatic--;
|
|
|
|
pb->automatic = 0;
|
|
|
|
|
|
|
|
RB_INSERT(paste_name_tree, &paste_by_name, pb);
|
|
|
|
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_paste_buffer(oldname);
|
|
|
|
notify_paste_buffer(newname);
|
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2009-11-26 22:28:24 +00:00
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2014-05-13 07:34:35 +00:00
|
|
|
* Add or replace an item in the store. Note that the caller is responsible for
|
2009-11-26 22:28:24 +00:00
|
|
|
* allocating data.
|
|
|
|
*/
|
2009-06-01 22:58:49 +00:00
|
|
|
int
|
2014-05-13 07:34:35 +00:00
|
|
|
paste_set(char *data, size_t size, const char *name, char **cause)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2015-09-11 14:41:50 +00:00
|
|
|
struct paste_buffer *pb, *old;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
if (cause != NULL)
|
|
|
|
*cause = NULL;
|
|
|
|
|
2014-02-17 23:07:03 +00:00
|
|
|
if (size == 0) {
|
|
|
|
free(data);
|
2009-11-26 22:28:24 +00:00
|
|
|
return (0);
|
2014-02-17 23:07:03 +00:00
|
|
|
}
|
2014-05-13 07:34:35 +00:00
|
|
|
if (name == NULL) {
|
2019-04-02 09:03:39 +00:00
|
|
|
paste_add(NULL, data, size);
|
2014-05-13 07:34:35 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2009-11-26 22:28:24 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
if (*name == '\0') {
|
|
|
|
if (cause != NULL)
|
|
|
|
*cause = xstrdup("empty buffer name");
|
2009-06-01 22:58:49 +00:00
|
|
|
return (-1);
|
2014-05-13 07:34:35 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
pb = xmalloc(sizeof *pb);
|
|
|
|
|
|
|
|
pb->name = xstrdup(name);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
pb->data = data;
|
2009-09-07 18:50:45 +00:00
|
|
|
pb->size = size;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
pb->automatic = 0;
|
|
|
|
pb->order = paste_next_order++;
|
|
|
|
|
2016-10-05 12:34:05 +00:00
|
|
|
pb->created = time(NULL);
|
|
|
|
|
2015-09-11 14:41:50 +00:00
|
|
|
if ((old = paste_get_name(name)) != NULL)
|
|
|
|
paste_free(old);
|
2015-04-07 13:06:22 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
RB_INSERT(paste_name_tree, &paste_by_name, pb);
|
|
|
|
RB_INSERT(paste_time_tree, &paste_by_time, pb);
|
|
|
|
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_paste_buffer(name);
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2010-06-21 21:44:09 +00:00
|
|
|
|
2020-05-16 15:24:28 +00:00
|
|
|
/* Set paste data without otherwise changing it. */
|
|
|
|
void
|
|
|
|
paste_replace(struct paste_buffer *pb, char *data, size_t size)
|
|
|
|
{
|
|
|
|
free(pb->data);
|
|
|
|
pb->data = data;
|
|
|
|
pb->size = size;
|
2022-08-15 09:10:34 +00:00
|
|
|
|
|
|
|
notify_paste_buffer(pb->name);
|
2020-05-16 15:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 18:12:18 +00:00
|
|
|
/* Convert start of buffer into a nice string. */
|
2010-06-21 21:44:09 +00:00
|
|
|
char *
|
2015-11-12 11:09:11 +00:00
|
|
|
paste_make_sample(struct paste_buffer *pb)
|
2010-06-21 21:44:09 +00:00
|
|
|
{
|
2014-04-02 18:12:18 +00:00
|
|
|
char *buf;
|
|
|
|
size_t len, used;
|
2020-05-16 15:35:19 +00:00
|
|
|
const int flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
|
2014-04-02 18:12:18 +00:00
|
|
|
const size_t width = 200;
|
2010-06-21 21:44:09 +00:00
|
|
|
|
|
|
|
len = pb->size;
|
|
|
|
if (len > width)
|
|
|
|
len = width;
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xreallocarray(NULL, len, 4 + 4);
|
2010-06-21 21:44:09 +00:00
|
|
|
|
2015-11-12 11:09:11 +00:00
|
|
|
used = utf8_strvis(buf, pb->data, len, flags);
|
2012-09-04 13:24:50 +00:00
|
|
|
if (pb->size > width || used > width)
|
2014-04-02 18:12:18 +00:00
|
|
|
strlcpy(buf + width, "...", 4);
|
2010-06-21 21:44:09 +00:00
|
|
|
return (buf);
|
|
|
|
}
|