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>
28 * Stack of paste buffers. Note that paste buffer data is not necessarily a C
32 /* Return each item of the stack in turn. */
34 paste_walk_stack(struct paste_stack
*ps
, u_int
*idx
)
36 struct paste_buffer
*pb
;
38 pb
= paste_get_index(ps
, *idx
);
43 /* Get the top item on the stack. */
45 paste_get_top(struct paste_stack
*ps
)
47 if (ARRAY_LENGTH(ps
) == 0)
49 return (ARRAY_FIRST(ps
));
52 /* Get an item by its index. */
54 paste_get_index(struct paste_stack
*ps
, u_int idx
)
56 if (idx
>= ARRAY_LENGTH(ps
))
58 return (ARRAY_ITEM(ps
, idx
));
61 /* Free the top item on the stack. */
63 paste_free_top(struct paste_stack
*ps
)
65 struct paste_buffer
*pb
;
67 if (ARRAY_LENGTH(ps
) == 0)
79 /* Free an item by index. */
81 paste_free_index(struct paste_stack
*ps
, u_int idx
)
83 struct paste_buffer
*pb
;
85 if (idx
>= ARRAY_LENGTH(ps
))
88 pb
= ARRAY_ITEM(ps
, idx
);
89 ARRAY_REMOVE(ps
, idx
);
98 * Add an item onto the top of the stack, freeing the bottom if at limit. Note
99 * that the caller is responsible for allocating data.
102 paste_add(struct paste_stack
*ps
, char *data
, size_t size
, u_int limit
)
104 struct paste_buffer
*pb
;
109 while (ARRAY_LENGTH(ps
) >= limit
) {
116 pb
= xmalloc(sizeof *pb
);
117 ARRAY_INSERT(ps
, 0, pb
);
125 * Replace an item on the stack. Note that the caller is responsible for
129 paste_replace(struct paste_stack
*ps
, u_int idx
, char *data
, size_t size
)
131 struct paste_buffer
*pb
;
138 if (idx
>= ARRAY_LENGTH(ps
))
141 pb
= ARRAY_ITEM(ps
, idx
);
150 /* Convert a buffer into a visible string. */
152 paste_print(struct paste_buffer
*pb
, size_t width
)
159 buf
= xmalloc(width
* 4 + 1);
165 used
= strvisx(buf
, pb
->data
, len
, VIS_OCTAL
|VIS_TAB
|VIS_NL
);
166 if (pb
->size
> width
|| used
> width
)
167 strlcpy(buf
+ width
- 3, "...", 4);
172 /* Paste into a window pane, filtering '\n' according to separator. */
174 paste_send_pane (struct paste_buffer
*pb
, struct window_pane
*wp
,
175 const char *sep
, int bracket
)
177 const char *data
= pb
->data
, *end
= data
+ pb
->size
, *lf
;
181 bufferevent_write(wp
->event
, "\033[200~", 6);
183 seplen
= strlen(sep
);
184 while ((lf
= memchr(data
, '\n', end
- data
)) != NULL
) {
186 bufferevent_write(wp
->event
, data
, lf
- data
);
187 bufferevent_write(wp
->event
, sep
, seplen
);
192 bufferevent_write(wp
->event
, data
, end
- data
);
195 bufferevent_write(wp
->event
, "\033[201~", 6);