1 /* $OpenBSD: cmd-load-buffer.c,v 1.29 2014/05/13 07:34:35 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 21 #include <errno.h> 22 #include <fcntl.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 28 #include "tmux.h" 29 30 /* 31 * Loads a paste buffer from a file. 32 */ 33 34 enum cmd_retval cmd_load_buffer_exec(struct cmd *, struct cmd_q *); 35 void cmd_load_buffer_callback(struct client *, int, void *); 36 37 const struct cmd_entry cmd_load_buffer_entry = { 38 "load-buffer", "loadb", 39 "b:", 1, 1, 40 CMD_BUFFER_USAGE " path", 41 0, 42 NULL, 43 cmd_load_buffer_exec 44 }; 45 46 enum cmd_retval 47 cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) 48 { 49 struct args *args = self->args; 50 struct client *c = cmdq->client; 51 struct session *s; 52 FILE *f; 53 const char *path, *bufname; 54 char *pdata, *new_pdata, *cause; 55 size_t psize; 56 int ch, error, cwd, fd; 57 58 bufname = NULL; 59 if (args_has(args, 'b')) 60 bufname = args_get(args, 'b'); 61 62 path = args->argv[0]; 63 if (strcmp(path, "-") == 0) { 64 error = server_set_stdin_callback(c, cmd_load_buffer_callback, 65 (void*)bufname, &cause); 66 if (error != 0) { 67 cmdq_error(cmdq, "%s: %s", path, cause); 68 free(cause); 69 return (CMD_RETURN_ERROR); 70 } 71 return (CMD_RETURN_WAIT); 72 } 73 74 if (c != NULL && c->session == NULL) 75 cwd = c->cwd; 76 else if ((s = cmd_current_session(cmdq, 0)) != NULL) 77 cwd = s->cwd; 78 else 79 cwd = AT_FDCWD; 80 81 if ((fd = openat(cwd, path, O_RDONLY)) == -1 || 82 (f = fdopen(fd, "rb")) == NULL) { 83 if (fd != -1) 84 close(fd); 85 cmdq_error(cmdq, "%s: %s", path, strerror(errno)); 86 return (CMD_RETURN_ERROR); 87 } 88 89 pdata = NULL; 90 psize = 0; 91 while ((ch = getc(f)) != EOF) { 92 /* Do not let the server die due to memory exhaustion. */ 93 if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { 94 cmdq_error(cmdq, "realloc error: %s", strerror(errno)); 95 goto error; 96 } 97 pdata = new_pdata; 98 pdata[psize++] = ch; 99 } 100 if (ferror(f)) { 101 cmdq_error(cmdq, "%s: read error", path); 102 goto error; 103 } 104 if (pdata != NULL) 105 pdata[psize] = '\0'; 106 107 fclose(f); 108 109 if (paste_set(pdata, psize, bufname, &cause) != 0) { 110 cmdq_error(cmdq, "%s", cause); 111 free(pdata); 112 free(cause); 113 return (CMD_RETURN_ERROR); 114 } 115 116 return (CMD_RETURN_NORMAL); 117 118 error: 119 free(pdata); 120 if (f != NULL) 121 fclose(f); 122 return (CMD_RETURN_ERROR); 123 } 124 125 void 126 cmd_load_buffer_callback(struct client *c, int closed, void *data) 127 { 128 const char *bufname = data; 129 char *pdata, *cause; 130 size_t psize; 131 132 if (!closed) 133 return; 134 c->stdin_callback = NULL; 135 136 c->references--; 137 if (c->flags & CLIENT_DEAD) 138 return; 139 140 psize = EVBUFFER_LENGTH(c->stdin_data); 141 if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) 142 goto out; 143 144 memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize); 145 pdata[psize] = '\0'; 146 evbuffer_drain(c->stdin_data, psize); 147 148 if (paste_set(pdata, psize, bufname, &cause) != 0) { 149 /* No context so can't use server_client_msg_error. */ 150 evbuffer_add_printf(c->stderr_data, "%s", cause); 151 server_push_stderr(c); 152 free(pdata); 153 free(cause); 154 } 155 156 out: 157 cmdq_continue(c->cmdq); 158 } 159