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