2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2008-08-28 19:45:30 +02:00
|
|
|
#include <sys/time.h>
|
2008-06-20 10:36:20 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2008-06-20 10:36:20 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
/*
|
|
|
|
* Stack of paste buffers. Note that paste buffer data is not necessarily a C
|
|
|
|
* string!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Return each item of the stack in turn. */
|
2008-06-20 10:36:20 +02:00
|
|
|
struct paste_buffer *
|
2011-04-07 00:18:56 +02:00
|
|
|
paste_walk_stack(struct paste_stack *ps, u_int *idx)
|
2008-06-20 10:36:20 +02:00
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
pb = paste_get_index(ps, *idx);
|
|
|
|
(*idx)++;
|
|
|
|
return (pb);
|
|
|
|
}
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
/* Get the top item on the stack. */
|
2008-06-20 10:36:20 +02:00
|
|
|
struct paste_buffer *
|
|
|
|
paste_get_top(struct paste_stack *ps)
|
|
|
|
{
|
|
|
|
if (ARRAY_LENGTH(ps) == 0)
|
|
|
|
return (NULL);
|
|
|
|
return (ARRAY_FIRST(ps));
|
|
|
|
}
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
/* Get an item by its index. */
|
2008-06-20 10:36:20 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
/* Free the top item on the stack. */
|
2008-06-20 10:36:20 +02:00
|
|
|
int
|
|
|
|
paste_free_top(struct paste_stack *ps)
|
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
if (ARRAY_LENGTH(ps) == 0)
|
2009-01-23 17:19:41 +01:00
|
|
|
return (-1);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
pb = ARRAY_FIRST(ps);
|
|
|
|
ARRAY_REMOVE(ps, 0);
|
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(pb->data);
|
|
|
|
free(pb);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
/* Free an item by index. */
|
2008-06-20 10:36:20 +02:00
|
|
|
int
|
|
|
|
paste_free_index(struct paste_stack *ps, u_int idx)
|
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
if (idx >= ARRAY_LENGTH(ps))
|
2009-01-23 17:19:41 +01:00
|
|
|
return (-1);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
pb = ARRAY_ITEM(ps, idx);
|
|
|
|
ARRAY_REMOVE(ps, idx);
|
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(pb->data);
|
|
|
|
free(pb);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-12-04 23:14:47 +01:00
|
|
|
/*
|
2009-11-28 15:54:12 +01:00
|
|
|
* Add an item onto the top of the stack, freeing the bottom if at limit. Note
|
|
|
|
* that the caller is responsible for allocating data.
|
|
|
|
*/
|
2008-06-20 10:36:20 +02:00
|
|
|
void
|
2009-11-28 15:54:12 +01:00
|
|
|
paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit)
|
2008-06-20 10:36:20 +02:00
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
if (size == 0)
|
2009-07-02 18:23:54 +02:00
|
|
|
return;
|
|
|
|
|
2009-07-31 22:33:49 +02:00
|
|
|
while (ARRAY_LENGTH(ps) >= limit) {
|
|
|
|
pb = ARRAY_LAST(ps);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(pb->data);
|
|
|
|
free(pb);
|
2008-06-20 20:45:35 +02:00
|
|
|
ARRAY_TRUNC(ps, 1);
|
2009-07-31 22:33:49 +02:00
|
|
|
}
|
2008-06-20 20:45:35 +02:00
|
|
|
|
2008-06-20 10:36:20 +02:00
|
|
|
pb = xmalloc(sizeof *pb);
|
|
|
|
ARRAY_INSERT(ps, 0, pb);
|
|
|
|
|
2009-01-25 19:51:28 +01:00
|
|
|
pb->data = data;
|
2009-09-08 01:48:54 +02:00
|
|
|
pb->size = size;
|
2008-06-20 10:36:20 +02:00
|
|
|
}
|
|
|
|
|
2009-11-28 15:54:12 +01:00
|
|
|
|
2009-12-04 23:14:47 +01:00
|
|
|
/*
|
2009-11-28 15:54:12 +01:00
|
|
|
* Replace an item on the stack. Note that the caller is responsible for
|
|
|
|
* allocating data.
|
|
|
|
*/
|
2008-06-20 10:36:20 +02:00
|
|
|
int
|
2009-11-28 15:54:12 +01:00
|
|
|
paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size)
|
2008-06-20 10:36:20 +02:00
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
2014-02-18 00:07:03 +01:00
|
|
|
if (size == 0) {
|
|
|
|
free(data);
|
2009-11-28 15:54:12 +01:00
|
|
|
return (0);
|
2014-02-18 00:07:03 +01:00
|
|
|
}
|
2009-11-28 15:54:12 +01:00
|
|
|
|
2008-06-20 10:36:20 +02:00
|
|
|
if (idx >= ARRAY_LENGTH(ps))
|
2009-01-23 17:19:41 +01:00
|
|
|
return (-1);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
pb = ARRAY_ITEM(ps, idx);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(pb->data);
|
2008-06-20 10:36:20 +02:00
|
|
|
|
2009-01-25 19:51:28 +01:00
|
|
|
pb->data = data;
|
2009-09-08 01:48:54 +02:00
|
|
|
pb->size = size;
|
2008-06-20 10:36:20 +02:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2010-06-23 01:35:20 +02:00
|
|
|
|
2014-04-02 20:12:18 +02:00
|
|
|
/* Convert start of buffer into a nice string. */
|
2010-06-23 01:35:20 +02:00
|
|
|
char *
|
2014-04-02 20:12:18 +02:00
|
|
|
paste_make_sample(struct paste_buffer *pb, int utf8flag)
|
2010-06-23 01:35:20 +02:00
|
|
|
{
|
2014-04-02 20:12:18 +02:00
|
|
|
char *buf;
|
|
|
|
size_t len, used;
|
|
|
|
const int flags = VIS_OCTAL|VIS_TAB|VIS_NL;
|
|
|
|
const size_t width = 200;
|
2010-06-23 01:35:20 +02:00
|
|
|
|
|
|
|
len = pb->size;
|
|
|
|
if (len > width)
|
|
|
|
len = width;
|
2014-04-02 20:12:18 +02:00
|
|
|
buf = xmalloc(len * 4 + 4);
|
2010-06-23 01:35:20 +02:00
|
|
|
|
2014-04-02 20:12:18 +02:00
|
|
|
if (utf8flag)
|
|
|
|
used = utf8_strvis(buf, pb->data, len, flags);
|
|
|
|
else
|
|
|
|
used = strvisx(buf, pb->data, len, flags);
|
2012-09-04 15:24:50 +02:00
|
|
|
if (pb->size > width || used > width)
|
2014-04-02 20:12:18 +02:00
|
|
|
strlcpy(buf + width, "...", 4);
|
2010-06-23 01:35:20 +02:00
|
|
|
return (buf);
|
|
|
|
}
|
2012-11-27 21:22:12 +01:00
|
|
|
|
|
|
|
/* Paste into a window pane, filtering '\n' according to separator. */
|
|
|
|
void
|
2014-03-06 12:50:07 +01:00
|
|
|
paste_send_pane(struct paste_buffer *pb, struct window_pane *wp,
|
2012-11-27 21:22:12 +01:00
|
|
|
const char *sep, int bracket)
|
|
|
|
{
|
|
|
|
const char *data = pb->data, *end = data + pb->size, *lf;
|
|
|
|
size_t seplen;
|
|
|
|
|
|
|
|
if (bracket)
|
|
|
|
bufferevent_write(wp->event, "\033[200~", 6);
|
|
|
|
|
|
|
|
seplen = strlen(sep);
|
|
|
|
while ((lf = memchr(data, '\n', end - data)) != NULL) {
|
|
|
|
if (lf != data)
|
|
|
|
bufferevent_write(wp->event, data, lf - data);
|
|
|
|
bufferevent_write(wp->event, sep, seplen);
|
|
|
|
data = lf + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end != data)
|
|
|
|
bufferevent_write(wp->event, data, end - data);
|
|
|
|
|
|
|
|
if (bracket)
|
|
|
|
bufferevent_write(wp->event, "\033[201~", 6);
|
|
|
|
}
|