1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/* $OpenBSD: paste.c,v 1.12 2012/07/10 11:53:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* 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>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <vis.h>
#include "tmux.h"
/*
* Stack of paste buffers. Note that paste buffer data is not necessarily a C
* string!
*/
/* Return each item of the stack in turn. */
struct paste_buffer *
paste_walk_stack(struct paste_stack *ps, u_int *idx)
{
struct paste_buffer *pb;
pb = paste_get_index(ps, *idx);
(*idx)++;
return (pb);
}
/* Get the top item on the stack. */
struct paste_buffer *
paste_get_top(struct paste_stack *ps)
{
if (ARRAY_LENGTH(ps) == 0)
return (NULL);
return (ARRAY_FIRST(ps));
}
/* Get an item by its index. */
struct paste_buffer *
paste_get_index(struct paste_stack *ps, u_int idx)
{
if (idx >= ARRAY_LENGTH(ps))
return (NULL);
return (ARRAY_ITEM(ps, idx));
}
/* Free the top item on the stack. */
int
paste_free_top(struct paste_stack *ps)
{
struct paste_buffer *pb;
if (ARRAY_LENGTH(ps) == 0)
return (-1);
pb = ARRAY_FIRST(ps);
ARRAY_REMOVE(ps, 0);
free(pb->data);
free(pb);
return (0);
}
/* Free an item by index. */
int
paste_free_index(struct paste_stack *ps, u_int idx)
{
struct paste_buffer *pb;
if (idx >= ARRAY_LENGTH(ps))
return (-1);
pb = ARRAY_ITEM(ps, idx);
ARRAY_REMOVE(ps, idx);
free(pb->data);
free(pb);
return (0);
}
/*
* Add an item onto the top of the stack, freeing the bottom if at limit. Note
* that the caller is responsible for allocating data.
*/
void
paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit)
{
struct paste_buffer *pb;
if (size == 0)
return;
while (ARRAY_LENGTH(ps) >= limit) {
pb = ARRAY_LAST(ps);
free(pb->data);
free(pb);
ARRAY_TRUNC(ps, 1);
}
pb = xmalloc(sizeof *pb);
ARRAY_INSERT(ps, 0, pb);
pb->data = data;
pb->size = size;
}
/*
* Replace an item on the stack. Note that the caller is responsible for
* allocating data.
*/
int
paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size)
{
struct paste_buffer *pb;
if (size == 0)
return (0);
if (idx >= ARRAY_LENGTH(ps))
return (-1);
pb = ARRAY_ITEM(ps, idx);
free(pb->data);
pb->data = data;
pb->size = size;
return (0);
}
/* Convert a buffer into a visible string. */
char *
paste_print(struct paste_buffer *pb, size_t width)
{
char *buf;
size_t len, used;
if (width < 3)
width = 3;
buf = xmalloc(width * 4 + 1);
len = pb->size;
if (len > width)
len = width;
used = strvisx(buf, pb->data, len, VIS_OCTAL|VIS_TAB|VIS_NL);
if (pb->size > width || used > width) {
buf[width - 3] = '\0';
strlcat(buf, "...", width);
}
return (buf);
}
|