1 |
|
|
/* $OpenBSD: cmd-save-buffer.c,v 1.37 2016/03/05 07:47:52 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org> |
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 |
|
|
#include <sys/stat.h> |
21 |
|
|
|
22 |
|
|
#include <errno.h> |
23 |
|
|
#include <fcntl.h> |
24 |
|
|
#include <stdlib.h> |
25 |
|
|
#include <string.h> |
26 |
|
|
#include <unistd.h> |
27 |
|
|
#include <vis.h> |
28 |
|
|
|
29 |
|
|
#include "tmux.h" |
30 |
|
|
|
31 |
|
|
/* |
32 |
|
|
* Saves a paste buffer to a file. |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *); |
36 |
|
|
|
37 |
|
|
const struct cmd_entry cmd_save_buffer_entry = { |
38 |
|
|
.name = "save-buffer", |
39 |
|
|
.alias = "saveb", |
40 |
|
|
|
41 |
|
|
.args = { "ab:", 1, 1 }, |
42 |
|
|
.usage = "[-a] " CMD_BUFFER_USAGE " path", |
43 |
|
|
|
44 |
|
|
.flags = 0, |
45 |
|
|
.exec = cmd_save_buffer_exec |
46 |
|
|
}; |
47 |
|
|
|
48 |
|
|
const struct cmd_entry cmd_show_buffer_entry = { |
49 |
|
|
.name = "show-buffer", |
50 |
|
|
.alias = "showb", |
51 |
|
|
|
52 |
|
|
.args = { "b:", 0, 0 }, |
53 |
|
|
.usage = CMD_BUFFER_USAGE, |
54 |
|
|
|
55 |
|
|
.flags = 0, |
56 |
|
|
.exec = cmd_save_buffer_exec |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
enum cmd_retval |
60 |
|
|
cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq) |
61 |
|
|
{ |
62 |
|
|
struct args *args = self->args; |
63 |
|
|
struct client *c = cmdq->client; |
64 |
|
|
struct session *s; |
65 |
|
|
struct paste_buffer *pb; |
66 |
|
|
const char *path, *bufname, *bufdata, *start, *end, *cwd; |
67 |
|
|
const char *flags; |
68 |
|
|
char *msg, *file, resolved[PATH_MAX]; |
69 |
|
|
size_t size, used, msglen, bufsize; |
70 |
|
|
FILE *f; |
71 |
|
|
|
72 |
|
|
if (!args_has(args, 'b')) { |
73 |
|
|
if ((pb = paste_get_top(NULL)) == NULL) { |
74 |
|
|
cmdq_error(cmdq, "no buffers"); |
75 |
|
|
return (CMD_RETURN_ERROR); |
76 |
|
|
} |
77 |
|
|
} else { |
78 |
|
|
bufname = args_get(args, 'b'); |
79 |
|
|
pb = paste_get_name(bufname); |
80 |
|
|
if (pb == NULL) { |
81 |
|
|
cmdq_error(cmdq, "no buffer %s", bufname); |
82 |
|
|
return (CMD_RETURN_ERROR); |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
bufdata = paste_buffer_data(pb, &bufsize); |
86 |
|
|
|
87 |
|
|
if (self->entry == &cmd_show_buffer_entry) |
88 |
|
|
path = "-"; |
89 |
|
|
else |
90 |
|
|
path = args->argv[0]; |
91 |
|
|
if (strcmp(path, "-") == 0) { |
92 |
|
|
if (c == NULL) { |
93 |
|
|
cmdq_error(cmdq, "can't write to stdout"); |
94 |
|
|
return (CMD_RETURN_ERROR); |
95 |
|
|
} |
96 |
|
|
if (c->session == NULL || (c->flags & CLIENT_CONTROL)) |
97 |
|
|
goto do_stdout; |
98 |
|
|
goto do_print; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
if (c != NULL && c->session == NULL && c->cwd != NULL) |
102 |
|
|
cwd = c->cwd; |
103 |
|
|
else if ((s = c->session) != NULL && s->cwd != NULL) |
104 |
|
|
cwd = s->cwd; |
105 |
|
|
else |
106 |
|
|
cwd = "."; |
107 |
|
|
|
108 |
|
|
flags = "wb"; |
109 |
|
|
if (args_has(self->args, 'a')) |
110 |
|
|
flags = "ab"; |
111 |
|
|
|
112 |
|
|
if (*path == '/') |
113 |
|
|
file = xstrdup(path); |
114 |
|
|
else |
115 |
|
|
xasprintf(&file, "%s/%s", cwd, path); |
116 |
|
|
if (realpath(file, resolved) == NULL && |
117 |
|
|
strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) { |
118 |
|
|
cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG)); |
119 |
|
|
return (CMD_RETURN_ERROR); |
120 |
|
|
} |
121 |
|
|
f = fopen(resolved, flags); |
122 |
|
|
free(file); |
123 |
|
|
if (f == NULL) { |
124 |
|
|
cmdq_error(cmdq, "%s: %s", resolved, strerror(errno)); |
125 |
|
|
return (CMD_RETURN_ERROR); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
if (fwrite(bufdata, 1, bufsize, f) != bufsize) { |
129 |
|
|
cmdq_error(cmdq, "%s: write error", resolved); |
130 |
|
|
fclose(f); |
131 |
|
|
return (CMD_RETURN_ERROR); |
132 |
|
|
} |
133 |
|
|
fclose(f); |
134 |
|
|
|
135 |
|
|
return (CMD_RETURN_NORMAL); |
136 |
|
|
|
137 |
|
|
do_stdout: |
138 |
|
|
evbuffer_add(c->stdout_data, bufdata, bufsize); |
139 |
|
|
server_client_push_stdout(c); |
140 |
|
|
return (CMD_RETURN_NORMAL); |
141 |
|
|
|
142 |
|
|
do_print: |
143 |
|
|
if (bufsize > (INT_MAX / 4) - 1) { |
144 |
|
|
cmdq_error(cmdq, "buffer too big"); |
145 |
|
|
return (CMD_RETURN_ERROR); |
146 |
|
|
} |
147 |
|
|
msg = NULL; |
148 |
|
|
|
149 |
|
|
used = 0; |
150 |
|
|
while (used != bufsize) { |
151 |
|
|
start = bufdata + used; |
152 |
|
|
end = memchr(start, '\n', bufsize - used); |
153 |
|
|
if (end != NULL) |
154 |
|
|
size = end - start; |
155 |
|
|
else |
156 |
|
|
size = bufsize - used; |
157 |
|
|
|
158 |
|
|
msglen = size * 4 + 1; |
159 |
|
|
msg = xrealloc(msg, msglen); |
160 |
|
|
|
161 |
|
|
strvisx(msg, start, size, VIS_OCTAL|VIS_TAB); |
162 |
|
|
cmdq_print(cmdq, "%s", msg); |
163 |
|
|
|
164 |
|
|
used += size + (end != NULL); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
free(msg); |
168 |
|
|
return (CMD_RETURN_NORMAL); |
169 |
|
|
} |