Coverage Report

Created: 2026-04-12 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/paste.c
Line
Count
Source
1
/* $OpenBSD$ */
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
25
#include "tmux.h"
26
27
/*
28
 * Set of paste buffers. Note that paste buffer data is not necessarily a C
29
 * string!
30
 */
31
32
static u_int  paste_next_index;
33
static u_int  paste_next_order;
34
static u_int  paste_num_automatic;
35
static RB_HEAD(paste_name_tree, paste_buffer) paste_by_name;
36
static RB_HEAD(paste_time_tree, paste_buffer) paste_by_time;
37
38
static int  paste_cmp_names(const struct paste_buffer *,
39
        const struct paste_buffer *);
40
0
RB_GENERATE_STATIC(paste_name_tree, paste_buffer, name_entry, paste_cmp_names);
Unexecuted instantiation: paste.c:paste_name_tree_RB_FIND
Unexecuted instantiation: paste.c:paste_name_tree_RB_REMOVE
Unexecuted instantiation: paste.c:paste_name_tree_RB_REMOVE_COLOR
Unexecuted instantiation: paste.c:paste_name_tree_RB_INSERT
41
0
42
0
static int  paste_cmp_times(const struct paste_buffer *,
43
0
        const struct paste_buffer *);
44
0
RB_GENERATE_STATIC(paste_time_tree, paste_buffer, time_entry, paste_cmp_times);
Unexecuted instantiation: paste.c:paste_time_tree_RB_MINMAX
Unexecuted instantiation: paste.c:paste_time_tree_RB_REMOVE
Unexecuted instantiation: paste.c:paste_time_tree_RB_REMOVE_COLOR
Unexecuted instantiation: paste.c:paste_time_tree_RB_INSERT
45
0
46
0
static int
47
0
paste_cmp_names(const struct paste_buffer *a, const struct paste_buffer *b)
48
0
{
49
0
  return (strcmp(a->name, b->name));
50
0
}
51
52
static int
53
paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
54
0
{
55
0
  if (a->order > b->order)
56
0
    return (-1);
57
0
  if (a->order < b->order)
58
0
    return (1);
59
0
  return (0);
60
0
}
61
62
/* Get paste buffer name. */
63
const char *
64
paste_buffer_name(struct paste_buffer *pb)
65
0
{
66
0
  return (pb->name);
67
0
}
68
69
/* Get paste buffer order. */
70
u_int
71
paste_buffer_order(struct paste_buffer *pb)
72
0
{
73
0
  return (pb->order);
74
0
}
75
76
/* Get paste buffer created. */
77
time_t
78
paste_buffer_created(struct paste_buffer *pb)
79
0
{
80
0
  return (pb->created);
81
0
}
82
83
/* Get paste buffer data. */
84
const char *
85
paste_buffer_data(struct paste_buffer *pb, size_t *size)
86
0
{
87
0
  if (size != NULL)
88
0
    *size = pb->size;
89
0
  return (pb->data);
90
0
}
91
92
/* Walk paste buffers by time. */
93
struct paste_buffer *
94
paste_walk(struct paste_buffer *pb)
95
0
{
96
0
  if (pb == NULL)
97
0
    return (RB_MIN(paste_time_tree, &paste_by_time));
98
0
  return (RB_NEXT(paste_time_tree, &paste_by_time, pb));
99
0
}
100
101
int
102
paste_is_empty(void)
103
0
{
104
0
  return RB_ROOT(&paste_by_time) == NULL;
105
0
}
106
107
/* Get the most recent automatic buffer. */
108
struct paste_buffer *
109
paste_get_top(char **name)
110
0
{
111
0
  struct paste_buffer *pb;
112
113
0
  pb = RB_MIN(paste_time_tree, &paste_by_time);
114
0
  while (pb != NULL && !pb->automatic)
115
0
    pb = RB_NEXT(paste_time_tree, &paste_by_time, pb);
116
0
  if (pb == NULL)
117
0
    return (NULL);
118
0
  if (name != NULL)
119
0
    *name = xstrdup(pb->name);
120
0
  return (pb);
121
0
}
122
123
/* Get a paste buffer by name. */
124
struct paste_buffer *
125
paste_get_name(const char *name)
126
0
{
127
0
  struct paste_buffer pbfind;
128
129
0
  if (name == NULL || *name == '\0')
130
0
    return (NULL);
131
132
0
  pbfind.name = (char *)name;
133
0
  return (RB_FIND(paste_name_tree, &paste_by_name, &pbfind));
134
0
}
135
136
/* Free a paste buffer. */
137
void
138
paste_free(struct paste_buffer *pb)
139
0
{
140
0
  notify_paste_buffer(pb->name, 1);
141
142
0
  RB_REMOVE(paste_name_tree, &paste_by_name, pb);
143
0
  RB_REMOVE(paste_time_tree, &paste_by_time, pb);
144
0
  if (pb->automatic)
145
0
    paste_num_automatic--;
146
147
0
  free(pb->data);
148
0
  free(pb->name);
149
0
  free(pb);
150
0
}
151
152
/*
153
 * Add an automatic buffer, freeing the oldest automatic item if at limit. Note
154
 * that the caller is responsible for allocating data.
155
 */
156
void
157
paste_add(const char *prefix, char *data, size_t size)
158
0
{
159
0
  struct paste_buffer *pb, *pb1;
160
0
  u_int      limit;
161
162
0
  if (prefix == NULL)
163
0
    prefix = "buffer";
164
165
0
  if (size == 0) {
166
0
    free(data);
167
0
    return;
168
0
  }
169
170
0
  limit = options_get_number(global_options, "buffer-limit");
171
0
  RB_FOREACH_REVERSE_SAFE(pb, paste_time_tree, &paste_by_time, pb1) {
172
0
    if (paste_num_automatic < limit)
173
0
      break;
174
0
    if (pb->automatic)
175
0
      paste_free(pb);
176
0
  }
177
178
0
  pb = xmalloc(sizeof *pb);
179
180
0
  pb->name = NULL;
181
0
  do {
182
0
    free(pb->name);
183
0
    xasprintf(&pb->name, "%s%u", prefix, paste_next_index);
184
0
    paste_next_index++;
185
0
  } while (paste_get_name(pb->name) != NULL);
186
187
0
  pb->data = data;
188
0
  pb->size = size;
189
190
0
  pb->automatic = 1;
191
0
  paste_num_automatic++;
192
193
0
  pb->created = time(NULL);
194
195
0
  pb->order = paste_next_order++;
196
0
  RB_INSERT(paste_name_tree, &paste_by_name, pb);
197
0
  RB_INSERT(paste_time_tree, &paste_by_time, pb);
198
199
0
  notify_paste_buffer(pb->name, 0);
200
0
}
201
202
/* Rename a paste buffer. */
203
int
204
paste_rename(const char *oldname, const char *newname, char **cause)
205
0
{
206
0
  struct paste_buffer *pb, *pb_new;
207
208
0
  if (cause != NULL)
209
0
    *cause = NULL;
210
211
0
  if (oldname == NULL || *oldname == '\0') {
212
0
    if (cause != NULL)
213
0
      *cause = xstrdup("no buffer");
214
0
    return (-1);
215
0
  }
216
0
  if (newname == NULL || *newname == '\0') {
217
0
    if (cause != NULL)
218
0
      *cause = xstrdup("new name is empty");
219
0
    return (-1);
220
0
  }
221
222
0
  pb = paste_get_name(oldname);
223
0
  if (pb == NULL) {
224
0
    if (cause != NULL)
225
0
      xasprintf(cause, "no buffer %s", oldname);
226
0
    return (-1);
227
0
  }
228
229
0
  pb_new = paste_get_name(newname);
230
0
  if (pb_new == pb)
231
0
    return (0);
232
0
  if (pb_new != NULL)
233
0
    paste_free(pb_new);
234
235
0
  RB_REMOVE(paste_name_tree, &paste_by_name, pb);
236
237
0
  free(pb->name);
238
0
  pb->name = xstrdup(newname);
239
240
0
  if (pb->automatic)
241
0
    paste_num_automatic--;
242
0
  pb->automatic = 0;
243
244
0
  RB_INSERT(paste_name_tree, &paste_by_name, pb);
245
246
0
  notify_paste_buffer(oldname, 1);
247
0
  notify_paste_buffer(newname, 0);
248
249
0
  return (0);
250
0
}
251
252
/*
253
 * Add or replace an item in the store. Note that the caller is responsible for
254
 * allocating data.
255
 */
256
int
257
paste_set(char *data, size_t size, const char *name, char **cause)
258
0
{
259
0
  struct paste_buffer *pb, *old;
260
261
0
  if (cause != NULL)
262
0
    *cause = NULL;
263
264
0
  if (size == 0) {
265
0
    free(data);
266
0
    return (0);
267
0
  }
268
0
  if (name == NULL) {
269
0
    paste_add(NULL, data, size);
270
0
    return (0);
271
0
  }
272
273
0
  if (*name == '\0') {
274
0
    if (cause != NULL)
275
0
      *cause = xstrdup("empty buffer name");
276
0
    return (-1);
277
0
  }
278
279
0
  pb = xmalloc(sizeof *pb);
280
281
0
  pb->name = xstrdup(name);
282
283
0
  pb->data = data;
284
0
  pb->size = size;
285
286
0
  pb->automatic = 0;
287
0
  pb->order = paste_next_order++;
288
289
0
  pb->created = time(NULL);
290
291
0
  if ((old = paste_get_name(name)) != NULL)
292
0
    paste_free(old);
293
294
0
  RB_INSERT(paste_name_tree, &paste_by_name, pb);
295
0
  RB_INSERT(paste_time_tree, &paste_by_time, pb);
296
297
0
  notify_paste_buffer(name, 0);
298
299
0
  return (0);
300
0
}
301
302
/* Set paste data without otherwise changing it. */
303
void
304
paste_replace(struct paste_buffer *pb, char *data, size_t size)
305
0
{
306
0
  free(pb->data);
307
0
  pb->data = data;
308
0
  pb->size = size;
309
310
0
  notify_paste_buffer(pb->name, 0);
311
0
}
312
313
/* Convert start of buffer into a nice string. */
314
char *
315
paste_make_sample(struct paste_buffer *pb)
316
0
{
317
0
  char    *buf;
318
0
  size_t     len, used;
319
0
  const int  flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
320
0
  const size_t   width = 200;
321
322
0
  len = pb->size;
323
0
  if (len > width)
324
0
    len = width;
325
0
  buf = xreallocarray(NULL, len, 4 + 4);
326
327
0
  used = utf8_strvis(buf, pb->data, len, flags);
328
0
  if (pb->size > width || used > width)
329
0
    strlcpy(buf + width, "...", 4);
330
0
  return (buf);
331
0
}