1 /* $OpenBSD: cmd-save-buffer.c,v 1.18 2013/03/24 09:30:41 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 <stdlib.h> 24 #include <string.h> 25 #include <vis.h> 26 27 #include "tmux.h" 28 29 /* 30 * Saves a paste buffer to a file. 31 */ 32 33 enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_ctx *); 34 35 const struct cmd_entry cmd_save_buffer_entry = { 36 "save-buffer", "saveb", 37 "ab:", 1, 1, 38 "[-a] " CMD_BUFFER_USAGE " path", 39 0, 40 NULL, 41 NULL, 42 cmd_save_buffer_exec 43 }; 44 45 const struct cmd_entry cmd_show_buffer_entry = { 46 "show-buffer", "showb", 47 "b:", 0, 0, 48 CMD_BUFFER_USAGE, 49 0, 50 NULL, 51 NULL, 52 cmd_save_buffer_exec 53 }; 54 55 enum cmd_retval 56 cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) 57 { 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; 70 71 if (!args_has(args, 'b')) { 72 if ((pb = paste_get_top(&global_buffers)) == NULL) { 73 ctx->error(ctx, "no buffers"); 74 return (CMD_RETURN_ERROR); 75 } 76 } else { 77 buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); 78 if (cause != NULL) { 79 ctx->error(ctx, "buffer %s", cause); 80 free(cause); 81 return (CMD_RETURN_ERROR); 82 } 83 84 pb = paste_get_index(&global_buffers, buffer); 85 if (pb == NULL) { 86 ctx->error(ctx, "no buffer %d", buffer); 87 return (CMD_RETURN_ERROR); 88 } 89 } 90 91 if (self->entry == &cmd_show_buffer_entry) 92 path = "-"; 93 else 94 path = args->argv[0]; 95 if (strcmp(path, "-") == 0) { 96 c = ctx->cmdclient; 97 if (c != NULL) 98 goto do_stdout; 99 c = ctx->curclient; 100 if (c->flags & CLIENT_CONTROL) 101 goto do_stdout; 102 if (c != NULL) 103 goto do_print; 104 ctx->error(ctx, "can't write to stdout"); 105 return (CMD_RETURN_ERROR); 106 } 107 108 c = ctx->cmdclient; 109 if (c != NULL) 110 wd = c->cwd; 111 else if ((s = cmd_current_session(ctx, 0)) != NULL) { 112 wd = options_get_string(&s->options, "default-path"); 113 if (*wd == '\0') 114 wd = s->cwd; 115 } else 116 wd = NULL; 117 if (wd != NULL && *wd != '\0') { 118 newpath = get_full_path(wd, path); 119 if (newpath != NULL) 120 path = newpath; 121 } 122 123 mask = umask(S_IRWXG | S_IRWXO); 124 if (args_has(self->args, 'a')) 125 f = fopen(path, "ab"); 126 else 127 f = fopen(path, "wb"); 128 umask(mask); 129 if (f == NULL) { 130 ctx->error(ctx, "%s: %s", path, strerror(errno)); 131 return (CMD_RETURN_ERROR); 132 } 133 if (fwrite(pb->data, 1, pb->size, f) != pb->size) { 134 ctx->error(ctx, "%s: fwrite error", path); 135 fclose(f); 136 return (CMD_RETURN_ERROR); 137 } 138 fclose(f); 139 140 return (CMD_RETURN_NORMAL); 141 142 do_stdout: 143 evbuffer_add(c->stdout_data, pb->data, pb->size); 144 server_push_stdout(c); 145 return (CMD_RETURN_NORMAL); 146 147 do_print: 148 if (pb->size > (INT_MAX / 4) - 1) { 149 ctx->error(ctx, "buffer too big"); 150 return (CMD_RETURN_ERROR); 151 } 152 msg = NULL; 153 msglen = 0; 154 155 used = 0; 156 while (used != pb->size) { 157 start = pb->data + used; 158 end = memchr(start, '\n', pb->size - used); 159 if (end != NULL) 160 size = end - start; 161 else 162 size = pb->size - used; 163 164 msglen = size * 4 + 1; 165 msg = xrealloc(msg, 1, msglen); 166 167 strvisx(msg, start, size, VIS_OCTAL|VIS_TAB); 168 ctx->print(ctx, "%s", msg); 169 170 used += size + (end != NULL); 171 } 172 173 free(msg); 174 return (CMD_RETURN_NORMAL); 175 } 176