1 |
|
|
/* $OpenBSD: cmd-set-buffer.c,v 1.28 2016/10/16 19:04:05 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
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 |
|
|
|
21 |
|
|
#include <stdlib.h> |
22 |
|
|
#include <string.h> |
23 |
|
|
|
24 |
|
|
#include "tmux.h" |
25 |
|
|
|
26 |
|
|
/* |
27 |
|
|
* Add, set, append to or delete a paste buffer. |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
static enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmdq_item *); |
31 |
|
|
|
32 |
|
|
const struct cmd_entry cmd_set_buffer_entry = { |
33 |
|
|
.name = "set-buffer", |
34 |
|
|
.alias = "setb", |
35 |
|
|
|
36 |
|
|
.args = { "ab:n:", 0, 1 }, |
37 |
|
|
.usage = "[-a] " CMD_BUFFER_USAGE " [-n new-buffer-name] data", |
38 |
|
|
|
39 |
|
|
.flags = CMD_AFTERHOOK, |
40 |
|
|
.exec = cmd_set_buffer_exec |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
const struct cmd_entry cmd_delete_buffer_entry = { |
44 |
|
|
.name = "delete-buffer", |
45 |
|
|
.alias = "deleteb", |
46 |
|
|
|
47 |
|
|
.args = { "b:", 0, 0 }, |
48 |
|
|
.usage = CMD_BUFFER_USAGE, |
49 |
|
|
|
50 |
|
|
.flags = CMD_AFTERHOOK, |
51 |
|
|
.exec = cmd_set_buffer_exec |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
static enum cmd_retval |
55 |
|
|
cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item) |
56 |
|
|
{ |
57 |
|
|
struct args *args = self->args; |
58 |
|
|
struct paste_buffer *pb; |
59 |
|
|
char *bufdata, *cause; |
60 |
|
|
const char *bufname, *olddata; |
61 |
|
|
size_t bufsize, newsize; |
62 |
|
|
|
63 |
|
|
bufname = args_get(args, 'b'); |
64 |
|
|
if (bufname == NULL) |
65 |
|
|
pb = NULL; |
66 |
|
|
else |
67 |
|
|
pb = paste_get_name(bufname); |
68 |
|
|
|
69 |
|
|
if (self->entry == &cmd_delete_buffer_entry) { |
70 |
|
|
if (pb == NULL) |
71 |
|
|
pb = paste_get_top(&bufname); |
72 |
|
|
if (pb == NULL) { |
73 |
|
|
cmdq_error(item, "no buffer"); |
74 |
|
|
return (CMD_RETURN_ERROR); |
75 |
|
|
} |
76 |
|
|
paste_free(pb); |
77 |
|
|
return (CMD_RETURN_NORMAL); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if (args_has(args, 'n')) { |
81 |
|
|
if (pb == NULL) |
82 |
|
|
pb = paste_get_top(&bufname); |
83 |
|
|
if (pb == NULL) { |
84 |
|
|
cmdq_error(item, "no buffer"); |
85 |
|
|
return (CMD_RETURN_ERROR); |
86 |
|
|
} |
87 |
|
|
if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) { |
88 |
|
|
cmdq_error(item, "%s", cause); |
89 |
|
|
free(cause); |
90 |
|
|
return (CMD_RETURN_ERROR); |
91 |
|
|
} |
92 |
|
|
return (CMD_RETURN_NORMAL); |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
if (args->argc != 1) { |
96 |
|
|
cmdq_error(item, "no data specified"); |
97 |
|
|
return (CMD_RETURN_ERROR); |
98 |
|
|
} |
99 |
|
|
if ((newsize = strlen(args->argv[0])) == 0) |
100 |
|
|
return (CMD_RETURN_NORMAL); |
101 |
|
|
|
102 |
|
|
bufsize = 0; |
103 |
|
|
bufdata = NULL; |
104 |
|
|
|
105 |
|
|
if (args_has(args, 'a') && pb != NULL) { |
106 |
|
|
olddata = paste_buffer_data(pb, &bufsize); |
107 |
|
|
bufdata = xmalloc(bufsize); |
108 |
|
|
memcpy(bufdata, olddata, bufsize); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
bufdata = xrealloc(bufdata, bufsize + newsize); |
112 |
|
|
memcpy(bufdata + bufsize, args->argv[0], newsize); |
113 |
|
|
bufsize += newsize; |
114 |
|
|
|
115 |
|
|
if (paste_set(bufdata, bufsize, bufname, &cause) != 0) { |
116 |
|
|
cmdq_error(item, "%s", cause); |
117 |
|
|
free(bufdata); |
118 |
|
|
free(cause); |
119 |
|
|
return (CMD_RETURN_ERROR); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
return (CMD_RETURN_NORMAL); |
123 |
|
|
} |