summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/cmd-save-buffer.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2019-12-12 11:39:56 +0000
committernicm <nicm@openbsd.org>2019-12-12 11:39:56 +0000
commitf4bc7c7a7c1323682a58cd1a89887499be5f4894 (patch)
tree23b9fedb2445ca85f4982dc2cea859311ce9c32f /usr.bin/tmux/cmd-save-buffer.c
parentHeader cleanup. (diff)
downloadwireguard-openbsd-f4bc7c7a7c1323682a58cd1a89887499be5f4894.tar.xz
wireguard-openbsd-f4bc7c7a7c1323682a58cd1a89887499be5f4894.zip
Rewrite the code for reading and writing files. Now, if the client is
not attached, the server process asks it to open the file, similar to how works for stdin, stdout, stderr. This makes special files like /dev/fd/X work (used by some shells). stdin, stdout and stderr and control mode are now just special cases of the same mechanism. This will also make it easier to use for other commands that read files such as source-file.
Diffstat (limited to 'usr.bin/tmux/cmd-save-buffer.c')
-rw-r--r--usr.bin/tmux/cmd-save-buffer.c100
1 files changed, 26 insertions, 74 deletions
diff --git a/usr.bin/tmux/cmd-save-buffer.c b/usr.bin/tmux/cmd-save-buffer.c
index 6c3b6b3a42e..453558ca9db 100644
--- a/usr.bin/tmux/cmd-save-buffer.c
+++ b/usr.bin/tmux/cmd-save-buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-save-buffer.c,v 1.46 2019/06/13 21:44:13 nicm Exp $ */
+/* $OpenBSD: cmd-save-buffer.c,v 1.47 2019/12/12 11:39:56 nicm Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
@@ -56,6 +56,20 @@ const struct cmd_entry cmd_show_buffer_entry = {
.exec = cmd_save_buffer_exec
};
+static void
+cmd_save_buffer_done(__unused struct client *c, const char *path, int error,
+ __unused int closed, __unused struct evbuffer *buffer, void *data)
+{
+ struct cmdq_item *item = data;
+
+ if (!closed)
+ return;
+
+ if (error != 0)
+ cmdq_error(item, "%s: %s", path, strerror(error));
+ cmdq_continue(item);
+}
+
static enum cmd_retval
cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
{
@@ -65,18 +79,17 @@ cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
struct winlink *wl = item->target.wl;
struct window_pane *wp = item->target.wp;
struct paste_buffer *pb;
- const char *bufname, *bufdata, *start, *end, *flags;
- char *msg, *path, *file;
- size_t size, used, msglen, bufsize;
- FILE *f;
+ int flags;
+ const char *bufname = args_get(args, 'b'), *bufdata;
+ size_t bufsize;
+ char *path;
- if (!args_has(args, 'b')) {
+ if (bufname == NULL) {
if ((pb = paste_get_top(NULL)) == NULL) {
cmdq_error(item, "no buffers");
return (CMD_RETURN_ERROR);
}
} else {
- bufname = args_get(args, 'b');
pb = paste_get_name(bufname);
if (pb == NULL) {
cmdq_error(item, "no buffer %s", bufname);
@@ -89,74 +102,13 @@ cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
path = xstrdup("-");
else
path = format_single(item, args->argv[0], c, s, wl, wp);
- if (strcmp(path, "-") == 0) {
- free(path);
- c = item->client;
- if (c == NULL) {
- cmdq_error(item, "can't write to stdout");
- return (CMD_RETURN_ERROR);
- }
- if (c->session == NULL || (c->flags & CLIENT_CONTROL))
- goto do_stdout;
- goto do_print;
- }
-
- flags = "wb";
if (args_has(self->args, 'a'))
- flags = "ab";
-
- file = server_client_get_path(item->client, path);
+ flags = O_APPEND;
+ else
+ flags = 0;
+ file_write(item->client, path, flags, bufdata, bufsize,
+ cmd_save_buffer_done, item);
free(path);
- f = fopen(file, flags);
- if (f == NULL) {
- cmdq_error(item, "%s: %s", file, strerror(errno));
- free(file);
- return (CMD_RETURN_ERROR);
- }
-
- if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
- cmdq_error(item, "%s: write error", file);
- fclose(f);
- free(file);
- return (CMD_RETURN_ERROR);
- }
-
- fclose(f);
- free(file);
-
- return (CMD_RETURN_NORMAL);
-
-do_stdout:
- evbuffer_add(c->stdout_data, bufdata, bufsize);
- server_client_push_stdout(c);
- return (CMD_RETURN_NORMAL);
-
-do_print:
- if (bufsize > (INT_MAX / 4) - 1) {
- cmdq_error(item, "buffer too big");
- return (CMD_RETURN_ERROR);
- }
- msg = NULL;
-
- used = 0;
- while (used != bufsize) {
- start = bufdata + used;
- end = memchr(start, '\n', bufsize - used);
- if (end != NULL)
- size = end - start;
- else
- size = bufsize - used;
-
- msglen = size * 4 + 1;
- msg = xrealloc(msg, msglen);
-
- strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
- cmdq_print(item, "%s", msg);
-
- used += size + (end != NULL);
- }
-
- free(msg);
- return (CMD_RETURN_NORMAL);
+ return (CMD_RETURN_WAIT);
}