1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2008 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 <glob.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "tmux.h" 27 28 /* 29 * Sources a configuration file. 30 */ 31 32 static enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmdq_item *); 33 34 const struct cmd_entry cmd_source_file_entry = { 35 .name = "source-file", 36 .alias = "source", 37 38 .args = { "Fnqv", 1, -1 }, 39 .usage = "[-Fnqv] path ...", 40 41 .flags = 0, 42 .exec = cmd_source_file_exec 43 }; 44 45 struct cmd_source_file_data { 46 struct cmdq_item *item; 47 int flags; 48 49 struct cmdq_item *after; 50 enum cmd_retval retval; 51 52 u_int current; 53 char **files; 54 u_int nfiles; 55 }; 56 57 static enum cmd_retval 58 cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data) 59 { 60 cfg_print_causes(item); 61 return (CMD_RETURN_NORMAL); 62 } 63 64 static void 65 cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata) 66 { 67 struct cmdq_item *new_item; 68 69 if (cfg_finished) { 70 if (cdata->retval == CMD_RETURN_ERROR && c->session == NULL) 71 c->retval = 1; 72 new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL); 73 cmdq_insert_after(cdata->after, new_item); 74 } 75 76 free(cdata->files); 77 free(cdata); 78 } 79 80 static void 81 cmd_source_file_done(struct client *c, const char *path, int error, 82 int closed, struct evbuffer *buffer, void *data) 83 { 84 struct cmd_source_file_data *cdata = data; 85 struct cmdq_item *item = cdata->item; 86 void *bdata = EVBUFFER_DATA(buffer); 87 size_t bsize = EVBUFFER_LENGTH(buffer); 88 u_int n; 89 struct cmdq_item *new_item; 90 91 if (!closed) 92 return; 93 94 if (error != 0) 95 cmdq_error(item, "%s: %s", path, strerror(error)); 96 else if (bsize != 0) { 97 if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after, 98 cdata->flags, &new_item) < 0) 99 cdata->retval = CMD_RETURN_ERROR; 100 else if (new_item != NULL) 101 cdata->after = new_item; 102 } 103 104 n = ++cdata->current; 105 if (n < cdata->nfiles) 106 file_read(c, cdata->files[n], cmd_source_file_done, cdata); 107 else { 108 cmd_source_file_complete(c, cdata); 109 cmdq_continue(item); 110 } 111 } 112 113 static void 114 cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path) 115 { 116 log_debug("%s: %s", __func__, path); 117 cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1, 118 sizeof *cdata->files); 119 cdata->files[cdata->nfiles++] = xstrdup(path); 120 } 121 122 static enum cmd_retval 123 cmd_source_file_exec(struct cmd *self, struct cmdq_item *item) 124 { 125 struct args *args = cmd_get_args(self); 126 struct cmd_source_file_data *cdata; 127 struct client *c = cmdq_get_client(item); 128 enum cmd_retval retval = CMD_RETURN_NORMAL; 129 char *pattern, *cwd, *expand = NULL; 130 const char *path, *error; 131 glob_t g; 132 int i, result; 133 u_int j; 134 135 cdata = xcalloc(1, sizeof *cdata); 136 cdata->item = item; 137 138 if (args_has(args, 'q')) 139 cdata->flags |= CMD_PARSE_QUIET; 140 if (args_has(args, 'n')) 141 cdata->flags |= CMD_PARSE_PARSEONLY; 142 if (args_has(args, 'v')) 143 cdata->flags |= CMD_PARSE_VERBOSE; 144 145 utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB); 146 147 for (i = 0; i < args->argc; i++) { 148 if (args_has(args, 'F')) { 149 free(expand); 150 expand = format_single_from_target(item, args->argv[i]); 151 path = expand; 152 } else 153 path = args->argv[i]; 154 if (strcmp(path, "-") == 0) { 155 cmd_source_file_add(cdata, "-"); 156 continue; 157 } 158 159 if (*path == '/') 160 pattern = xstrdup(path); 161 else 162 xasprintf(&pattern, "%s/%s", cwd, path); 163 log_debug("%s: %s", __func__, pattern); 164 165 if ((result = glob(pattern, 0, NULL, &g)) != 0) { 166 if (result != GLOB_NOMATCH || 167 (~cdata->flags & CMD_PARSE_QUIET)) { 168 if (result == GLOB_NOMATCH) 169 error = strerror(ENOENT); 170 else if (result == GLOB_NOSPACE) 171 error = strerror(ENOMEM); 172 else 173 error = strerror(EINVAL); 174 cmdq_error(item, "%s: %s", path, error); 175 retval = CMD_RETURN_ERROR; 176 } 177 free(pattern); 178 continue; 179 } 180 free(expand); 181 free(pattern); 182 183 for (j = 0; j < g.gl_pathc; j++) 184 cmd_source_file_add(cdata, g.gl_pathv[j]); 185 } 186 187 cdata->after = item; 188 cdata->retval = retval; 189 190 if (cdata->nfiles != 0) { 191 file_read(c, cdata->files[0], cmd_source_file_done, cdata); 192 retval = CMD_RETURN_WAIT; 193 } else 194 cmd_source_file_complete(c, cdata); 195 196 free(cwd); 197 return (retval); 198 } 199