4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
27 * Add, set, append to or delete a paste buffer.
30 static enum cmd_retval
cmd_set_buffer_exec(struct cmd
*, struct cmdq_item
*);
32 const struct cmd_entry cmd_set_buffer_entry
= {
36 .args
= { "ab:t:n:w", 0, 1, NULL
},
37 .usage
= "[-aw] " CMD_BUFFER_USAGE
" [-n new-buffer-name] "
38 CMD_TARGET_CLIENT_USAGE
" data",
40 .flags
= CMD_AFTERHOOK
|CMD_CLIENT_TFLAG
|CMD_CLIENT_CANFAIL
,
41 .exec
= cmd_set_buffer_exec
44 const struct cmd_entry cmd_delete_buffer_entry
= {
45 .name
= "delete-buffer",
48 .args
= { "b:", 0, 0, NULL
},
49 .usage
= CMD_BUFFER_USAGE
,
51 .flags
= CMD_AFTERHOOK
,
52 .exec
= cmd_set_buffer_exec
55 static enum cmd_retval
56 cmd_set_buffer_exec(struct cmd
*self
, struct cmdq_item
*item
)
58 struct args
*args
= cmd_get_args(self
);
59 struct client
*tc
= cmdq_get_target_client(item
);
60 struct paste_buffer
*pb
;
61 char *bufdata
, *cause
;
62 const char *bufname
, *olddata
;
63 size_t bufsize
, newsize
;
65 bufname
= args_get(args
, 'b');
69 pb
= paste_get_name(bufname
);
71 if (cmd_get_entry(self
) == &cmd_delete_buffer_entry
) {
73 if (bufname
!= NULL
) {
74 cmdq_error(item
, "unknown buffer: %s", bufname
);
75 return (CMD_RETURN_ERROR
);
77 pb
= paste_get_top(&bufname
);
80 cmdq_error(item
, "no buffer");
81 return (CMD_RETURN_ERROR
);
84 return (CMD_RETURN_NORMAL
);
87 if (args_has(args
, 'n')) {
89 if (bufname
!= NULL
) {
90 cmdq_error(item
, "unknown buffer: %s", bufname
);
91 return (CMD_RETURN_ERROR
);
93 pb
= paste_get_top(&bufname
);
96 cmdq_error(item
, "no buffer");
97 return (CMD_RETURN_ERROR
);
99 if (paste_rename(bufname
, args_get(args
, 'n'), &cause
) != 0) {
100 cmdq_error(item
, "%s", cause
);
102 return (CMD_RETURN_ERROR
);
104 return (CMD_RETURN_NORMAL
);
107 if (args_count(args
) != 1) {
108 cmdq_error(item
, "no data specified");
109 return (CMD_RETURN_ERROR
);
111 if ((newsize
= strlen(args_string(args
, 0))) == 0)
112 return (CMD_RETURN_NORMAL
);
117 if (args_has(args
, 'a') && pb
!= NULL
) {
118 olddata
= paste_buffer_data(pb
, &bufsize
);
119 bufdata
= xmalloc(bufsize
);
120 memcpy(bufdata
, olddata
, bufsize
);
123 bufdata
= xrealloc(bufdata
, bufsize
+ newsize
);
124 memcpy(bufdata
+ bufsize
, args_string(args
, 0), newsize
);
127 if (paste_set(bufdata
, bufsize
, bufname
, &cause
) != 0) {
128 cmdq_error(item
, "%s", cause
);
131 return (CMD_RETURN_ERROR
);
133 if (args_has(args
, 'w') && tc
!= NULL
)
134 tty_set_selection(&tc
->tty
, "", bufdata
, bufsize
);
136 return (CMD_RETURN_NORMAL
);