2009-11-28 14:54:12 +00:00
|
|
|
/* $Id: paste.c,v 1.12 2009-11-28 14:54:12 tcunha Exp $ */
|
2008-06-20 08:36:20 +00: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 17:45:30 +00:00
|
|
|
#include <sys/time.h>
|
2008-06-20 08:36:20 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
/*
|
|
|
|
* Stack of paste buffers. Note that paste buffer data is not necessarily a C
|
|
|
|
* string!
|
|
|
|
*/
|
|
|
|
|
2008-06-20 08:36:20 +00:00
|
|
|
void
|
|
|
|
paste_init_stack(struct paste_stack *ps)
|
|
|
|
{
|
|
|
|
ARRAY_INIT(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
paste_free_stack(struct paste_stack *ps)
|
|
|
|
{
|
|
|
|
while (paste_free_top(ps) == 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
/* Return each item of the stack in turn. */
|
2008-06-20 08:36:20 +00:00
|
|
|
struct paste_buffer *
|
|
|
|
paste_walk_stack(struct paste_stack *ps, uint *idx)
|
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
pb = paste_get_index(ps, *idx);
|
|
|
|
(*idx)++;
|
|
|
|
return (pb);
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
/* Get the top item on the stack. */
|
2008-06-20 08:36:20 +00: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 14:54:12 +00:00
|
|
|
/* Get an item by its index. */
|
2008-06-20 08:36:20 +00: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 14:54:12 +00:00
|
|
|
/* Free the top item on the stack. */
|
2008-06-20 08:36:20 +00:00
|
|
|
int
|
|
|
|
paste_free_top(struct paste_stack *ps)
|
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
if (ARRAY_LENGTH(ps) == 0)
|
2009-01-23 16:19:41 +00:00
|
|
|
return (-1);
|
2008-06-20 08:36:20 +00:00
|
|
|
|
|
|
|
pb = ARRAY_FIRST(ps);
|
|
|
|
ARRAY_REMOVE(ps, 0);
|
|
|
|
|
|
|
|
xfree(pb->data);
|
|
|
|
xfree(pb);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
/* Free an item by index. */
|
2008-06-20 08:36:20 +00:00
|
|
|
int
|
|
|
|
paste_free_index(struct paste_stack *ps, u_int idx)
|
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
|
|
|
if (idx >= ARRAY_LENGTH(ps))
|
2009-01-23 16:19:41 +00:00
|
|
|
return (-1);
|
2008-06-20 08:36:20 +00:00
|
|
|
|
|
|
|
pb = ARRAY_ITEM(ps, idx);
|
|
|
|
ARRAY_REMOVE(ps, idx);
|
|
|
|
|
|
|
|
xfree(pb->data);
|
|
|
|
xfree(pb);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:54:12 +00: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 08:36:20 +00:00
|
|
|
void
|
2009-11-28 14:54:12 +00:00
|
|
|
paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit)
|
2008-06-20 08:36:20 +00:00
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
if (size == 0)
|
2009-07-02 16:23:54 +00:00
|
|
|
return;
|
|
|
|
|
2009-07-31 20:33:49 +00:00
|
|
|
while (ARRAY_LENGTH(ps) >= limit) {
|
|
|
|
pb = ARRAY_LAST(ps);
|
|
|
|
xfree(pb->data);
|
|
|
|
xfree(pb);
|
2008-06-20 18:45:35 +00:00
|
|
|
ARRAY_TRUNC(ps, 1);
|
2009-07-31 20:33:49 +00:00
|
|
|
}
|
2008-06-20 18:45:35 +00:00
|
|
|
|
2008-06-20 08:36:20 +00:00
|
|
|
pb = xmalloc(sizeof *pb);
|
|
|
|
ARRAY_INSERT(ps, 0, pb);
|
|
|
|
|
2009-01-25 18:51:28 +00:00
|
|
|
pb->data = data;
|
2009-09-07 23:48:54 +00:00
|
|
|
pb->size = size;
|
2008-06-20 08:36:20 +00:00
|
|
|
}
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace an item on the stack. Note that the caller is responsible for
|
|
|
|
* allocating data.
|
|
|
|
*/
|
2008-06-20 08:36:20 +00:00
|
|
|
int
|
2009-11-28 14:54:12 +00:00
|
|
|
paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size)
|
2008-06-20 08:36:20 +00:00
|
|
|
{
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
|
2009-11-28 14:54:12 +00:00
|
|
|
if (size == 0)
|
|
|
|
return (0);
|
|
|
|
|
2008-06-20 08:36:20 +00:00
|
|
|
if (idx >= ARRAY_LENGTH(ps))
|
2009-01-23 16:19:41 +00:00
|
|
|
return (-1);
|
2008-06-20 08:36:20 +00:00
|
|
|
|
|
|
|
pb = ARRAY_ITEM(ps, idx);
|
|
|
|
xfree(pb->data);
|
|
|
|
|
2009-01-25 18:51:28 +00:00
|
|
|
pb->data = data;
|
2009-09-07 23:48:54 +00:00
|
|
|
pb->size = size;
|
2008-06-20 08:36:20 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|