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 pb
= paste_get_top(&bufname
);
75 cmdq_error(item
, "no buffer");
76 return (CMD_RETURN_ERROR
);
79 return (CMD_RETURN_NORMAL
);
82 if (args_has(args
, 'n')) {
84 pb
= paste_get_top(&bufname
);
86 cmdq_error(item
, "no buffer");
87 return (CMD_RETURN_ERROR
);
89 if (paste_rename(bufname
, args_get(args
, 'n'), &cause
) != 0) {
90 cmdq_error(item
, "%s", cause
);
92 return (CMD_RETURN_ERROR
);
94 return (CMD_RETURN_NORMAL
);
97 if (args_count(args
) != 1) {
98 cmdq_error(item
, "no data specified");
99 return (CMD_RETURN_ERROR
);
101 if ((newsize
= strlen(args_string(args
, 0))) == 0)
102 return (CMD_RETURN_NORMAL
);
107 if (args_has(args
, 'a') && pb
!= NULL
) {
108 olddata
= paste_buffer_data(pb
, &bufsize
);
109 bufdata
= xmalloc(bufsize
);
110 memcpy(bufdata
, olddata
, bufsize
);
113 bufdata
= xrealloc(bufdata
, bufsize
+ newsize
);
114 memcpy(bufdata
+ bufsize
, args_string(args
, 0), newsize
);
117 if (paste_set(bufdata
, bufsize
, bufname
, &cause
) != 0) {
118 cmdq_error(item
, "%s", cause
);
121 return (CMD_RETURN_ERROR
);
123 if (args_has(args
, 'w') && tc
!= NULL
)
124 tty_set_selection(&tc
->tty
, bufdata
, bufsize
);
126 return (CMD_RETURN_NORMAL
);