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

Line Branch Exec Source
1
/* $OpenBSD: window-buffer.c,v 1.9 2017/08/09 11:43:45 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 struct screen *
200
window_buffer_draw(__unused void *modedata, void *itemdata, u_int sx, u_int sy)
201
{
202
	struct window_buffer_itemdata	*item = itemdata;
203
	struct paste_buffer		*pb;
204
	static struct screen		 s;
205
	struct screen_write_ctx		 ctx;
206
	char				 line[1024];
207
	const char			*pdata, *end, *cp;
208
	size_t				 psize, at;
209
	u_int				 i;
210
211
	pb = paste_get_name(item->name);
212
	if (pb == NULL)
213
		return (NULL);
214
215
	screen_init(&s, sx, sy, 0);
216
217
	screen_write_start(&ctx, NULL, &s);
218
	screen_write_clearscreen(&ctx, 8);
219
220
	pdata = end = paste_buffer_data(pb, &psize);
221
	for (i = 0; i < sy; i++) {
222
		at = 0;
223
		while (end != pdata + psize && *end != '\n') {
224
			if ((sizeof line) - at > 5) {
225
				cp = vis(line + at, *end, VIS_TAB|VIS_OCTAL, 0);
226
				at = cp - line;
227
			}
228
			end++;
229
		}
230
		if (at > sx)
231
			at = sx;
232
		line[at] = '\0';
233
234
		if (*line != '\0') {
235
			screen_write_cursormove(&ctx, 0, i);
236
			screen_write_puts(&ctx, &grid_default_cell, "%s", line);
237
		}
238
239
		if (end == pdata + psize)
240
			break;
241
		end++;
242
	}
243
244
	screen_write_stop(&ctx);
245
	return (&s);
246
}
247
248
static int
249
window_buffer_search(__unused void *modedata, void *itemdata, const char *ss)
250
{
251
	struct window_buffer_itemdata	*item = itemdata;
252
	struct paste_buffer		*pb;
253
	const char			*bufdata;
254
	size_t				 bufsize;
255
256
	if ((pb = paste_get_name(item->name)) == NULL)
257
		return (0);
258
	if (strstr(item->name, ss) != NULL)
259
		return (1);
260
	bufdata = paste_buffer_data(pb, &bufsize);
261
	return (memmem(bufdata, bufsize, ss, strlen(ss)) != NULL);
262
}
263
264
static struct screen *
265
window_buffer_init(struct window_pane *wp, __unused struct cmd_find_state *fs,
266
    struct args *args)
267
{
268
	struct window_buffer_modedata	*data;
269
	struct screen			*s;
270
271
	wp->modedata = data = xcalloc(1, sizeof *data);
272
273
	if (args == NULL || !args_has(args, 'F'))
274
		data->format = xstrdup(WINDOW_BUFFER_DEFAULT_FORMAT);
275
	else
276
		data->format = xstrdup(args_get(args, 'F'));
277
	if (args == NULL || args->argc == 0)
278
		data->command = xstrdup(WINDOW_BUFFER_DEFAULT_COMMAND);
279
	else
280
		data->command = xstrdup(args->argv[0]);
281
282
	data->data = mode_tree_start(wp, args, window_buffer_build,
283
	    window_buffer_draw, window_buffer_search, data,
284
	    window_buffer_sort_list, nitems(window_buffer_sort_list), &s);
285
286
	mode_tree_build(data->data);
287
	mode_tree_draw(data->data);
288
289
	return (s);
290
}
291
292
static void
293
window_buffer_free(struct window_pane *wp)
294
{
295
	struct window_buffer_modedata	*data = wp->modedata;
296
	u_int				 i;
297
298
	if (data == NULL)
299
		return;
300
301
	mode_tree_free(data->data);
302
303
	for (i = 0; i < data->item_size; i++)
304
		window_buffer_free_item(data->item_list[i]);
305
	free(data->item_list);
306
307
	free(data->format);
308
	free(data->command);
309
310
	free(data);
311
}
312
313
static void
314
window_buffer_resize(struct window_pane *wp, u_int sx, u_int sy)
315
{
316
	struct window_buffer_modedata	*data = wp->modedata;
317
318
	mode_tree_resize(data->data, sx, sy);
319
}
320
321
static void
322
window_buffer_do_delete(void* modedata, void *itemdata, __unused key_code key)
323
{
324
	struct window_buffer_modedata	*data = modedata;
325
	struct window_buffer_itemdata	*item = itemdata;
326
	struct paste_buffer		*pb;
327
328
	if (item == mode_tree_get_current(data->data))
329
		mode_tree_down(data->data, 0);
330
	if ((pb = paste_get_name(item->name)) != NULL)
331
		paste_free(pb);
332
}
333
334
static void
335
window_buffer_key(struct window_pane *wp, struct client *c,
336
    __unused struct session *s, key_code key, struct mouse_event *m)
337
{
338
	struct window_buffer_modedata	*data = wp->modedata;
339
	struct window_buffer_itemdata	*item;
340
	char				*command, *name;
341
	int				 finished;
342
343
	/*
344
	 * t = toggle tag
345
	 * T = tag none
346
	 * C-t = tag all
347
	 * q = exit
348
	 * O = change sort order
349
	 *
350
	 * d = delete buffer
351
	 * D = delete tagged buffers
352
	 * Enter = paste buffer
353
	 */
354
355
	finished = mode_tree_key(data->data, c, &key, m);
356
	switch (key) {
357
	case 'd':
358
		item = mode_tree_get_current(data->data);
359
		window_buffer_do_delete(data, item, key);
360
		mode_tree_build(data->data);
361
		break;
362
	case 'D':
363
		mode_tree_each_tagged(data->data, window_buffer_do_delete, key,
364
		    0);
365
		mode_tree_build(data->data);
366
		break;
367
	case '\r':
368
		item = mode_tree_get_current(data->data);
369
		command = xstrdup(data->command);
370
		name = xstrdup(item->name);
371
		window_pane_reset_mode(wp);
372
		mode_tree_run_command(c, NULL, command, name);
373
		free(name);
374
		free(command);
375
		return;
376
	}
377
	if (finished || paste_get_top(NULL) == NULL)
378
		window_pane_reset_mode(wp);
379
	else {
380
		mode_tree_draw(data->data);
381
		wp->flags |= PANE_REDRAW;
382
	}
383
}