choose-tree: Reset top when toggling items
[tmux-openbsd.git] / cmd-save-buffer.c
blob29f71837f1a554b489fe3a142705da3a6af768da
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 <stdlib.h>
24 #include <string.h>
25 #include <vis.h>
27 #include "tmux.h"
30 * Saves a paste buffer to a file.
33 enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
35 const struct cmd_entry cmd_save_buffer_entry = {
36 "save-buffer", "saveb",
37 "ab:", 1, 1,
38 "[-a] " CMD_BUFFER_USAGE " path",
40 NULL,
41 NULL,
42 cmd_save_buffer_exec
45 const struct cmd_entry cmd_show_buffer_entry = {
46 "show-buffer", "showb",
47 "b:", 0, 0,
48 CMD_BUFFER_USAGE,
50 NULL,
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;
60 struct session *s;
61 struct paste_buffer *pb;
62 const char *path, *newpath, *wd;
63 char *cause, *start, *end;
64 size_t size, used;
65 int buffer;
66 mode_t mask;
67 FILE *f;
68 char *msg;
69 size_t msglen;
71 if (!args_has(args, 'b')) {
72 if ((pb = paste_get_top(&global_buffers)) == NULL) {
73 cmdq_error(cmdq, "no buffers");
74 return (CMD_RETURN_ERROR);
76 } else {
77 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
78 if (cause != NULL) {
79 cmdq_error(cmdq, "buffer %s", cause);
80 free(cause);
81 return (CMD_RETURN_ERROR);
84 pb = paste_get_index(&global_buffers, buffer);
85 if (pb == NULL) {
86 cmdq_error(cmdq, "no buffer %d", buffer);
87 return (CMD_RETURN_ERROR);
91 if (self->entry == &cmd_show_buffer_entry)
92 path = "-";
93 else
94 path = args->argv[0];
95 if (strcmp(path, "-") == 0) {
96 c = cmdq->client;
97 if (c == NULL) {
98 cmdq_error(cmdq, "can't write to stdout");
99 return (CMD_RETURN_ERROR);
101 if (c->session == NULL || (c->flags & CLIENT_CONTROL))
102 goto do_stdout;
103 goto do_print;
106 c = cmdq->client;
107 if (c != NULL)
108 wd = c->cwd;
109 else if ((s = cmd_current_session(cmdq, 0)) != NULL) {
110 wd = options_get_string(&s->options, "default-path");
111 if (*wd == '\0')
112 wd = s->cwd;
113 } else
114 wd = NULL;
115 if (wd != NULL && *wd != '\0') {
116 newpath = get_full_path(wd, path);
117 if (newpath != NULL)
118 path = newpath;
121 mask = umask(S_IRWXG | S_IRWXO);
122 if (args_has(self->args, 'a'))
123 f = fopen(path, "ab");
124 else
125 f = fopen(path, "wb");
126 umask(mask);
127 if (f == NULL) {
128 cmdq_error(cmdq, "%s: %s", path, strerror(errno));
129 return (CMD_RETURN_ERROR);
131 if (fwrite(pb->data, 1, pb->size, f) != pb->size) {
132 cmdq_error(cmdq, "%s: fwrite error", path);
133 fclose(f);
134 return (CMD_RETURN_ERROR);
136 fclose(f);
138 return (CMD_RETURN_NORMAL);
140 do_stdout:
141 evbuffer_add(c->stdout_data, pb->data, pb->size);
142 server_push_stdout(c);
143 return (CMD_RETURN_NORMAL);
145 do_print:
146 if (pb->size > (INT_MAX / 4) - 1) {
147 cmdq_error(cmdq, "buffer too big");
148 return (CMD_RETURN_ERROR);
150 msg = NULL;
151 msglen = 0;
153 used = 0;
154 while (used != pb->size) {
155 start = pb->data + used;
156 end = memchr(start, '\n', pb->size - used);
157 if (end != NULL)
158 size = end - start;
159 else
160 size = pb->size - used;
162 msglen = size * 4 + 1;
163 msg = xrealloc(msg, 1, msglen);
165 strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
166 cmdq_print(cmdq, "%s", msg);
168 used += size + (end != NULL);
171 free(msg);
172 return (CMD_RETURN_NORMAL);