4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
29 * Stack of paste buffers. Note that paste buffer data is not necessarily a C
33 /* Return each item of the stack in turn. */
35 paste_walk_stack(struct paste_stack
*ps
, u_int
*idx
)
37 struct paste_buffer
*pb
;
39 pb
= paste_get_index(ps
, *idx
);
44 /* Get the top item on the stack. */
46 paste_get_top(struct paste_stack
*ps
)
48 if (ARRAY_LENGTH(ps
) == 0)
50 return (ARRAY_FIRST(ps
));
53 /* Get an item by its index. */
55 paste_get_index(struct paste_stack
*ps
, u_int idx
)
57 if (idx
>= ARRAY_LENGTH(ps
))
59 return (ARRAY_ITEM(ps
, idx
));
62 /* Free the top item on the stack. */
64 paste_free_top(struct paste_stack
*ps
)
66 struct paste_buffer
*pb
;
68 if (ARRAY_LENGTH(ps
) == 0)
80 /* Free an item by index. */
82 paste_free_index(struct paste_stack
*ps
, u_int idx
)
84 struct paste_buffer
*pb
;
86 if (idx
>= ARRAY_LENGTH(ps
))
89 pb
= ARRAY_ITEM(ps
, idx
);
90 ARRAY_REMOVE(ps
, idx
);
99 * Add an item onto the top of the stack, freeing the bottom if at limit. Note
100 * that the caller is responsible for allocating data.
103 paste_add(struct paste_stack
*ps
, char *data
, size_t size
, u_int limit
)
105 struct paste_buffer
*pb
;
110 while (ARRAY_LENGTH(ps
) >= limit
) {
117 pb
= xmalloc(sizeof *pb
);
118 ARRAY_INSERT(ps
, 0, pb
);
126 * Replace an item on the stack. Note that the caller is responsible for
130 paste_replace(struct paste_stack
*ps
, u_int idx
, char *data
, size_t size
)
132 struct paste_buffer
*pb
;
137 if (idx
>= ARRAY_LENGTH(ps
))
140 pb
= ARRAY_ITEM(ps
, idx
);
149 /* Convert a buffer into a visible string. */
151 paste_print(struct paste_buffer
*pb
, size_t width
)
158 buf
= xmalloc(width
* 4 + 1);
164 used
= strvisx(buf
, pb
->data
, len
, VIS_OCTAL
|VIS_TAB
|VIS_NL
);
165 if (pb
->size
> width
|| used
> width
)
166 strlcpy(buf
+ width
- 3, "...", 4);
171 /* Paste into a window pane, filtering '\n' according to separator. */
173 paste_send_pane (struct paste_buffer
*pb
, struct window_pane
*wp
,
174 const char *sep
, int bracket
)
176 const char *data
= pb
->data
, *end
= data
+ pb
->size
, *lf
;
180 bufferevent_write(wp
->event
, "\033[200~", 6);
182 seplen
= strlen(sep
);
183 while ((lf
= memchr(data
, '\n', end
- data
)) != NULL
) {
185 bufferevent_write(wp
->event
, data
, lf
- data
);
186 bufferevent_write(wp
->event
, sep
, seplen
);
191 bufferevent_write(wp
->event
, data
, end
- data
);
194 bufferevent_write(wp
->event
, "\033[201~", 6);