GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/window-buffer.c Lines: 0 140 0.0 %
Date: 2017-11-13 Branches: 0 72 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: window-buffer.c,v 1.12 2017/11/03 17:02:33 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2017 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
#include <vis.h>
25
26
#include "tmux.h"
27
28
static struct screen	*window_buffer_init(struct window_pane *,
29
			     struct cmd_find_state *, struct args *);
30
static void		 window_buffer_free(struct window_pane *);
31
static void		 window_buffer_resize(struct window_pane *, u_int,
32
			     u_int);
33
static void		 window_buffer_key(struct window_pane *,
34
			     struct client *, struct session *, key_code,
35
			     struct mouse_event *);
36
37
#define WINDOW_BUFFER_DEFAULT_COMMAND "paste-buffer -b '%%'"
38
39
#define WINDOW_BUFFER_DEFAULT_FORMAT \
40
	"#{buffer_size} bytes (#{t:buffer_created})"
41
42
const struct window_mode window_buffer_mode = {
43
	.name = "buffer-mode",
44
45
	.init = window_buffer_init,
46
	.free = window_buffer_free,
47
	.resize = window_buffer_resize,
48
	.key = window_buffer_key,
49
};
50
51
enum window_buffer_sort_type {
52
	WINDOW_BUFFER_BY_TIME,
53
	WINDOW_BUFFER_BY_NAME,
54
	WINDOW_BUFFER_BY_SIZE,
55
};
56
static const char *window_buffer_sort_list[] = {
57
	"time",
58
	"name",
59
	"size"
60
};
61
62
struct window_buffer_itemdata {
63
	const char	*name;
64
	u_int		 order;
65
	size_t		 size;
66
};
67
68
struct window_buffer_modedata {
69
	struct mode_tree_data		 *data;
70
	char				 *command;
71
	char				 *format;
72
73
	struct window_buffer_itemdata	**item_list;
74
	u_int				  item_size;
75
};
76
77
static struct window_buffer_itemdata *
78
window_buffer_add_item(struct window_buffer_modedata *data)
79
{
80
	struct window_buffer_itemdata	*item;
81
82
	data->item_list = xreallocarray(data->item_list, data->item_size + 1,
83
	    sizeof *data->item_list);
84
	item = data->item_list[data->item_size++] = xcalloc(1, sizeof *item);
85
	return (item);
86
}
87
88
static void
89
window_buffer_free_item(struct window_buffer_itemdata *item)
90
{
91
	free((void *)item->name);
92
	free(item);
93
}
94
95
static int
96
window_buffer_cmp_name(const void *a0, const void *b0)
97
{
98
	const struct window_buffer_itemdata *const *a = a0;
99
	const struct window_buffer_itemdata *const *b = b0;
100
101
	return (strcmp((*a)->name, (*b)->name));
102
}
103
104
static int
105
window_buffer_cmp_time(const void *a0, const void *b0)
106
{
107
	const struct window_buffer_itemdata *const *a = a0;
108
	const struct window_buffer_itemdata *const *b = b0;
109
110
	if ((*a)->order > (*b)->order)
111
		return (-1);
112
	if ((*a)->order < (*b)->order)
113
		return (1);
114
	return (strcmp((*a)->name, (*b)->name));
115
}
116
117
static int
118
window_buffer_cmp_size(const void *a0, const void *b0)
119
{
120
	const struct window_buffer_itemdata *const *a = a0;
121
	const struct window_buffer_itemdata *const *b = b0;
122
123
	if ((*a)->size > (*b)->size)
124
		return (-1);
125
	if ((*a)->size < (*b)->size)
126
		return (1);
127
	return (strcmp((*a)->name, (*b)->name));
128
}
129
130
static void
131
window_buffer_build(void *modedata, u_int sort_type, __unused uint64_t *tag,
132
    const char *filter)
133
{
134
	struct window_buffer_modedata	*data = modedata;
135
	struct window_buffer_itemdata	*item;
136
	u_int				 i;
137
	struct paste_buffer		*pb;
138
	char				*text, *cp;
139
	struct format_tree		*ft;
140
141
	for (i = 0; i < data->item_size; i++)
142
		window_buffer_free_item(data->item_list[i]);
143
	free(data->item_list);
144
	data->item_list = NULL;
145
	data->item_size = 0;
146
147
	pb = NULL;
148
	while ((pb = paste_walk(pb)) != NULL) {
149
		item = window_buffer_add_item(data);
150
		item->name = xstrdup(paste_buffer_name(pb));
151
		paste_buffer_data(pb, &item->size);
152
		item->order = paste_buffer_order(pb);
153
	}
154
155
	switch (sort_type) {
156
	case WINDOW_BUFFER_BY_NAME:
157
		qsort(data->item_list, data->item_size, sizeof *data->item_list,
158
		    window_buffer_cmp_name);
159
		break;
160
	case WINDOW_BUFFER_BY_TIME:
161
		qsort(data->item_list, data->item_size, sizeof *data->item_list,
162
		    window_buffer_cmp_time);
163
		break;
164
	case WINDOW_BUFFER_BY_SIZE:
165
		qsort(data->item_list, data->item_size, sizeof *data->item_list,
166
		    window_buffer_cmp_size);
167
		break;
168
	}
169
170
	for (i = 0; i < data->item_size; i++) {
171
		item = data->item_list[i];
172
173
		pb = paste_get_name(item->name);
174
		if (pb == NULL)
175
			continue;
176
		ft = format_create(NULL, NULL, FORMAT_NONE, 0);
177
		format_defaults_paste_buffer(ft, pb);
178
179
		if (filter != NULL) {
180
			cp = format_expand(ft, filter);
181
			if (!format_true(cp)) {
182
				free(cp);
183
				format_free(ft);
184
				continue;
185
			}
186
			free(cp);
187
		}
188
189
		text = format_expand(ft, data->format);
190
		mode_tree_add(data->data, NULL, item, item->order, item->name,
191
		    text, -1);
192
		free(text);
193
194
		format_free(ft);
195
	}
196
197
}
198
199
static void
200
window_buffer_draw(__unused void *modedata, void *itemdata,
201
    struct screen_write_ctx *ctx, u_int sx, u_int sy)
202
{
203
	struct window_buffer_itemdata	*item = itemdata;
204
	struct paste_buffer		*pb;
205
	char				 line[1024];
206
	const char			*pdata, *end, *cp;
207
	size_t				 psize, at;
208
	u_int				 i, cx = ctx->s->cx, cy = ctx->s->cy;
209
210
	pb = paste_get_name(item->name);
211
	if (pb == NULL)
212
		return;
213
214
	pdata = end = paste_buffer_data(pb, &psize);
215
	for (i = 0; i < sy; i++) {
216
		at = 0;
217
		while (end != pdata + psize && *end != '\n') {
218
			if ((sizeof line) - at > 5) {
219
				cp = vis(line + at, *end, VIS_TAB|VIS_OCTAL, 0);
220
				at = cp - line;
221
			}
222
			end++;
223
		}
224
		if (at > sx)
225
			at = sx;
226
		line[at] = '\0';
227
228
		if (*line != '\0') {
229
			screen_write_cursormove(ctx, cx, cy + i);
230
			screen_write_puts(ctx, &grid_default_cell, "%s", line);
231
		}
232
233
		if (end == pdata + psize)
234
			break;
235
		end++;
236
	}
237
}
238
239
static int
240
window_buffer_search(__unused void *modedata, void *itemdata, const char *ss)
241
{
242
	struct window_buffer_itemdata	*item = itemdata;
243
	struct paste_buffer		*pb;
244
	const char			*bufdata;
245
	size_t				 bufsize;
246
247
	if ((pb = paste_get_name(item->name)) == NULL)
248
		return (0);
249
	if (strstr(item->name, ss) != NULL)
250
		return (1);
251
	bufdata = paste_buffer_data(pb, &bufsize);
252
	return (memmem(bufdata, bufsize, ss, strlen(ss)) != NULL);
253
}
254
255
static struct screen *
256
window_buffer_init(struct window_pane *wp, __unused struct cmd_find_state *fs,
257
    struct args *args)
258
{
259
	struct window_buffer_modedata	*data;
260
	struct screen			*s;
261
262
	wp->modedata = data = xcalloc(1, sizeof *data);
263
264
	if (args == NULL || !args_has(args, 'F'))
265
		data->format = xstrdup(WINDOW_BUFFER_DEFAULT_FORMAT);
266
	else
267
		data->format = xstrdup(args_get(args, 'F'));
268
	if (args == NULL || args->argc == 0)
269
		data->command = xstrdup(WINDOW_BUFFER_DEFAULT_COMMAND);
270
	else
271
		data->command = xstrdup(args->argv[0]);
272
273
	data->data = mode_tree_start(wp, args, window_buffer_build,
274
	    window_buffer_draw, window_buffer_search, data,
275
	    window_buffer_sort_list, nitems(window_buffer_sort_list), &s);
276
277
	mode_tree_build(data->data);
278
	mode_tree_draw(data->data);
279
280
	return (s);
281
}
282
283
static void
284
window_buffer_free(struct window_pane *wp)
285
{
286
	struct window_buffer_modedata	*data = wp->modedata;
287
	u_int				 i;
288
289
	if (data == NULL)
290
		return;
291
292
	mode_tree_free(data->data);
293
294
	for (i = 0; i < data->item_size; i++)
295
		window_buffer_free_item(data->item_list[i]);
296
	free(data->item_list);
297
298
	free(data->format);
299
	free(data->command);
300
301
	free(data);
302
}
303
304
static void
305
window_buffer_resize(struct window_pane *wp, u_int sx, u_int sy)
306
{
307
	struct window_buffer_modedata	*data = wp->modedata;
308
309
	mode_tree_resize(data->data, sx, sy);
310
}
311
312
static void
313
window_buffer_do_delete(void* modedata, void *itemdata,
314
    __unused struct client *c, __unused key_code key)
315
{
316
	struct window_buffer_modedata	*data = modedata;
317
	struct window_buffer_itemdata	*item = itemdata;
318
	struct paste_buffer		*pb;
319
320
	if (item == mode_tree_get_current(data->data))
321
		mode_tree_down(data->data, 0);
322
	if ((pb = paste_get_name(item->name)) != NULL)
323
		paste_free(pb);
324
}
325
326
static void
327
window_buffer_do_paste(void* modedata, void *itemdata, struct client *c,
328
    __unused key_code key)
329
{
330
	struct window_buffer_modedata	*data = modedata;
331
	struct window_buffer_itemdata	*item = itemdata;
332
	struct paste_buffer		*pb;
333
334
	if ((pb = paste_get_name(item->name)) != NULL)
335
		mode_tree_run_command(c, NULL, data->command, item->name);
336
}
337
338
static void
339
window_buffer_key(struct window_pane *wp, struct client *c,
340
    __unused struct session *s, key_code key, struct mouse_event *m)
341
{
342
	struct window_buffer_modedata	*data = wp->modedata;
343
	struct mode_tree_data		*mtd = data->data;
344
	struct window_buffer_itemdata	*item;
345
	int				 finished;
346
347
	finished = mode_tree_key(mtd, c, &key, m, NULL, NULL);
348
	switch (key) {
349
	case 'd':
350
		item = mode_tree_get_current(mtd);
351
		window_buffer_do_delete(data, item, c, key);
352
		mode_tree_build(mtd);
353
		break;
354
	case 'D':
355
		mode_tree_each_tagged(mtd, window_buffer_do_delete, c, key, 0);
356
		mode_tree_build(mtd);
357
		break;
358
	case 'P':
359
		mode_tree_each_tagged(mtd, window_buffer_do_paste, c, key, 0);
360
		finished = 1;
361
		break;
362
	case 'p':
363
	case '\r':
364
		item = mode_tree_get_current(mtd);
365
		window_buffer_do_paste(data, item, c, key);
366
		finished = 1;
367
		break;
368
	}
369
	if (finished || paste_get_top(NULL) == NULL)
370
		window_pane_reset_mode(wp);
371
	else {
372
		mode_tree_draw(mtd);
373
		wp->flags |= PANE_REDRAW;
374
	}
375
}