Alter how tmux handles the working directory to internally use file
[tmux-openbsd.git] / cmd-save-buffer.c
blob1b0a4e7bcc00ed6ead07d734a74f924323e9c8d0
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
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>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <vis.h>
29 #include "tmux.h"
32 * Saves a paste buffer to a file.
35 enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
37 const struct cmd_entry cmd_save_buffer_entry = {
38 "save-buffer", "saveb",
39 "ab:", 1, 1,
40 "[-a] " CMD_BUFFER_USAGE " path",
42 NULL,
43 cmd_save_buffer_exec
46 const struct cmd_entry cmd_show_buffer_entry = {
47 "show-buffer", "showb",
48 "b:", 0, 0,
49 CMD_BUFFER_USAGE,
51 NULL,
52 cmd_save_buffer_exec
55 enum cmd_retval
56 cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
58 struct args *args = self->args;
59 struct client *c = cmdq->client;
60 struct session *s;
61 struct paste_buffer *pb;
62 const char *path;
63 char *cause, *start, *end, *msg;
64 size_t size, used, msglen;
65 int cwd, fd, buffer;
66 FILE *f;
68 if (!args_has(args, 'b')) {
69 if ((pb = paste_get_top(&global_buffers)) == NULL) {
70 cmdq_error(cmdq, "no buffers");
71 return (CMD_RETURN_ERROR);
73 } else {
74 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
75 if (cause != NULL) {
76 cmdq_error(cmdq, "buffer %s", cause);
77 free(cause);
78 return (CMD_RETURN_ERROR);
81 pb = paste_get_index(&global_buffers, buffer);
82 if (pb == NULL) {
83 cmdq_error(cmdq, "no buffer %d", buffer);
84 return (CMD_RETURN_ERROR);
88 if (self->entry == &cmd_show_buffer_entry)
89 path = "-";
90 else
91 path = args->argv[0];
92 if (strcmp(path, "-") == 0) {
93 if (c == NULL) {
94 cmdq_error(cmdq, "can't write to stdout");
95 return (CMD_RETURN_ERROR);
97 if (c->session == NULL || (c->flags & CLIENT_CONTROL))
98 goto do_stdout;
99 goto do_print;
102 if (c != NULL && c->session == NULL)
103 cwd = c->cwd;
104 else if ((s = cmd_current_session(cmdq, 0)) != NULL)
105 cwd = s->cwd;
106 else
107 cwd = AT_FDCWD;
109 f = NULL;
110 if (args_has(self->args, 'a')) {
111 fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600);
112 if (fd != -1)
113 f = fdopen(fd, "ab");
114 } else {
115 fd = openat(cwd, path, O_CREAT|O_RDWR, 0600);
116 if (fd != -1)
117 f = fdopen(fd, "wb");
119 if (f == NULL) {
120 if (fd != -1)
121 close(fd);
122 cmdq_error(cmdq, "%s: %s", path, strerror(errno));
123 return (CMD_RETURN_ERROR);
125 if (fwrite(pb->data, 1, pb->size, f) != pb->size) {
126 cmdq_error(cmdq, "%s: fwrite error", path);
127 fclose(f);
128 return (CMD_RETURN_ERROR);
130 fclose(f);
132 return (CMD_RETURN_NORMAL);
134 do_stdout:
135 evbuffer_add(c->stdout_data, pb->data, pb->size);
136 server_push_stdout(c);
137 return (CMD_RETURN_NORMAL);
139 do_print:
140 if (pb->size > (INT_MAX / 4) - 1) {
141 cmdq_error(cmdq, "buffer too big");
142 return (CMD_RETURN_ERROR);
144 msg = NULL;
145 msglen = 0;
147 used = 0;
148 while (used != pb->size) {
149 start = pb->data + used;
150 end = memchr(start, '\n', pb->size - used);
151 if (end != NULL)
152 size = end - start;
153 else
154 size = pb->size - used;
156 msglen = size * 4 + 1;
157 msg = xrealloc(msg, 1, msglen);
159 strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
160 cmdq_print(cmdq, "%s", msg);
162 used += size + (end != NULL);
165 free(msg);
166 return (CMD_RETURN_NORMAL);