Annotation of src/usr.bin/tmux/paste.c, Revision 1.8
1.8 ! nicm 1: /* $OpenBSD: paste.c,v 1.7 2009/11/26 22:28:24 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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: #include <sys/time.h>
21:
22: #include <string.h>
23:
24: #include "tmux.h"
25:
1.7 nicm 26: /*
27: * Stack of paste buffers. Note that paste buffer data is not necessarily a C
28: * string!
29: */
30:
1.1 nicm 31: void
32: paste_init_stack(struct paste_stack *ps)
33: {
34: ARRAY_INIT(ps);
35: }
36:
37: void
38: paste_free_stack(struct paste_stack *ps)
39: {
40: while (paste_free_top(ps) == 0)
41: ;
42: }
43:
1.7 nicm 44: /* Return each item of the stack in turn. */
1.1 nicm 45: struct paste_buffer *
46: paste_walk_stack(struct paste_stack *ps, uint *idx)
47: {
48: struct paste_buffer *pb;
49:
50: pb = paste_get_index(ps, *idx);
51: (*idx)++;
52: return (pb);
53: }
54:
1.7 nicm 55: /* Get the top item on the stack. */
1.1 nicm 56: struct paste_buffer *
57: paste_get_top(struct paste_stack *ps)
58: {
59: if (ARRAY_LENGTH(ps) == 0)
60: return (NULL);
61: return (ARRAY_FIRST(ps));
62: }
63:
1.7 nicm 64: /* Get an item by its index. */
1.1 nicm 65: struct paste_buffer *
66: paste_get_index(struct paste_stack *ps, u_int idx)
67: {
68: if (idx >= ARRAY_LENGTH(ps))
69: return (NULL);
70: return (ARRAY_ITEM(ps, idx));
71: }
72:
1.7 nicm 73: /* Free the top item on the stack. */
1.1 nicm 74: int
75: paste_free_top(struct paste_stack *ps)
76: {
77: struct paste_buffer *pb;
78:
79: if (ARRAY_LENGTH(ps) == 0)
80: return (-1);
81:
82: pb = ARRAY_FIRST(ps);
83: ARRAY_REMOVE(ps, 0);
84:
85: xfree(pb->data);
86: xfree(pb);
87:
88: return (0);
89: }
90:
1.7 nicm 91: /* Free an item by index. */
1.1 nicm 92: int
93: paste_free_index(struct paste_stack *ps, u_int idx)
94: {
95: struct paste_buffer *pb;
96:
97: if (idx >= ARRAY_LENGTH(ps))
98: return (-1);
99:
100: pb = ARRAY_ITEM(ps, idx);
101: ARRAY_REMOVE(ps, idx);
102:
103: xfree(pb->data);
104: xfree(pb);
105:
106: return (0);
107: }
108:
1.8 ! nicm 109: /*
1.7 nicm 110: * Add an item onto the top of the stack, freeing the bottom if at limit. Note
111: * that the caller is responsible for allocating data.
112: */
1.1 nicm 113: void
1.7 nicm 114: paste_add(struct paste_stack *ps, char *data, size_t size, u_int limit)
1.1 nicm 115: {
116: struct paste_buffer *pb;
1.2 nicm 117:
1.7 nicm 118: if (size == 0)
1.2 nicm 119: return;
1.1 nicm 120:
1.3 nicm 121: while (ARRAY_LENGTH(ps) >= limit) {
122: pb = ARRAY_LAST(ps);
123: xfree(pb->data);
124: xfree(pb);
1.1 nicm 125: ARRAY_TRUNC(ps, 1);
1.3 nicm 126: }
1.1 nicm 127:
128: pb = xmalloc(sizeof *pb);
129: ARRAY_INSERT(ps, 0, pb);
130:
131: pb->data = data;
1.4 nicm 132: pb->size = size;
1.1 nicm 133: }
134:
1.7 nicm 135:
1.8 ! nicm 136: /*
1.7 nicm 137: * Replace an item on the stack. Note that the caller is responsible for
138: * allocating data.
139: */
1.1 nicm 140: int
1.7 nicm 141: paste_replace(struct paste_stack *ps, u_int idx, char *data, size_t size)
1.1 nicm 142: {
143: struct paste_buffer *pb;
1.7 nicm 144:
145: if (size == 0)
146: return (0);
1.1 nicm 147:
148: if (idx >= ARRAY_LENGTH(ps))
149: return (-1);
150:
151: pb = ARRAY_ITEM(ps, idx);
152: xfree(pb->data);
153:
154: pb->data = data;
1.4 nicm 155: pb->size = size;
1.1 nicm 156:
157: return (0);
158: }