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