Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / tmux / dist / cmd-save-buffer.c
blob57e5f36051cebea4fb58e2e214cde8a81d061253
1 /* Id */
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>
28 #include "tmux.h"
31 * Saves a paste buffer to a file.
34 enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
36 const struct cmd_entry cmd_save_buffer_entry = {
37 "save-buffer", "saveb",
38 "ab:", 1, 1,
39 "[-a] " CMD_BUFFER_USAGE " path",
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 cmd_save_buffer_exec
54 enum cmd_retval
55 cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
57 struct args *args = self->args;
58 struct client *c = cmdq->client;
59 struct session *s;
60 struct paste_buffer *pb;
61 const char *path;
62 char *cause, *start, *end, *msg;
63 size_t size, used, msglen;
64 int cwd, fd, buffer;
65 FILE *f;
67 if (!args_has(args, 'b')) {
68 if ((pb = paste_get_top(&global_buffers)) == NULL) {
69 cmdq_error(cmdq, "no buffers");
70 return (CMD_RETURN_ERROR);
72 } else {
73 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
74 if (cause != NULL) {
75 cmdq_error(cmdq, "buffer %s", cause);
76 free(cause);
77 return (CMD_RETURN_ERROR);
80 pb = paste_get_index(&global_buffers, buffer);
81 if (pb == NULL) {
82 cmdq_error(cmdq, "no buffer %d", buffer);
83 return (CMD_RETURN_ERROR);
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);
96 if (c->session == NULL || (c->flags & CLIENT_CONTROL))
97 goto do_stdout;
98 goto do_print;
101 if (c != NULL && c->session == NULL)
102 cwd = c->cwd;
103 else if ((s = cmd_current_session(cmdq, 0)) != NULL)
104 cwd = s->cwd;
105 else
106 cwd = AT_FDCWD;
108 f = NULL;
109 if (args_has(self->args, 'a')) {
110 fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600);
111 if (fd != -1)
112 f = fdopen(fd, "ab");
113 } else {
114 fd = openat(cwd, path, O_CREAT|O_RDWR, 0600);
115 if (fd != -1)
116 f = fdopen(fd, "wb");
118 if (f == NULL) {
119 if (fd != -1)
120 close(fd);
121 cmdq_error(cmdq, "%s: %s", path, strerror(errno));
122 return (CMD_RETURN_ERROR);
124 if (fwrite(pb->data, 1, pb->size, f) != pb->size) {
125 cmdq_error(cmdq, "%s: fwrite error", path);
126 fclose(f);
127 return (CMD_RETURN_ERROR);
129 fclose(f);
131 return (CMD_RETURN_NORMAL);
133 do_stdout:
134 evbuffer_add(c->stdout_data, pb->data, pb->size);
135 server_push_stdout(c);
136 return (CMD_RETURN_NORMAL);
138 do_print:
139 if (pb->size > (INT_MAX / 4) - 1) {
140 cmdq_error(cmdq, "buffer too big");
141 return (CMD_RETURN_ERROR);
143 msg = NULL;
144 msglen = 0;
146 used = 0;
147 while (used != pb->size) {
148 start = pb->data + used;
149 end = memchr(start, '\n', pb->size - used);
150 if (end != NULL)
151 size = end - start;
152 else
153 size = pb->size - used;
155 msglen = size * 4 + 1;
156 msg = xrealloc(msg, 1, msglen);
158 strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
159 cmdq_print(cmdq, "%s", msg);
161 used += size + (end != NULL);
164 free(msg);
165 return (CMD_RETURN_NORMAL);