1 /* $OpenBSD: cmd-source-file.c,v 1.35 2017/04/19 16:59:54 nicm Exp $ */ 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 #include <vis.h> 26 27 #include "tmux.h" 28 29 /* 30 * Sources a configuration file. 31 */ 32 33 static enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmdq_item *); 34 35 static enum cmd_retval cmd_source_file_done(struct cmdq_item *, void *); 36 37 const struct cmd_entry cmd_source_file_entry = { 38 .name = "source-file", 39 .alias = "source", 40 41 .args = { "q", 1, 1 }, 42 .usage = "[-q] path", 43 44 .flags = 0, 45 .exec = cmd_source_file_exec 46 }; 47 48 static enum cmd_retval 49 cmd_source_file_exec(struct cmd *self, struct cmdq_item *item) 50 { 51 struct args *args = self->args; 52 int quiet = args_has(args, 'q'); 53 struct client *c = item->client; 54 struct cmdq_item *new_item; 55 enum cmd_retval retval; 56 char *pattern, *tmp; 57 const char *path = args->argv[0]; 58 glob_t g; 59 u_int i; 60 61 if (*path == '/') 62 pattern = xstrdup(path); 63 else { 64 utf8_stravis(&tmp, server_client_get_cwd(c), VIS_GLOB); 65 xasprintf(&pattern, "%s/%s", tmp, path); 66 free(tmp); 67 } 68 log_debug("%s: %s", __func__, pattern); 69 70 retval = CMD_RETURN_NORMAL; 71 if (glob(pattern, 0, NULL, &g) != 0) { 72 if (!quiet || errno != ENOENT) { 73 cmdq_error(item, "%s: %s", path, strerror(errno)); 74 retval = CMD_RETURN_ERROR; 75 } 76 free(pattern); 77 return (retval); 78 } 79 free(pattern); 80 81 for (i = 0; i < (u_int)g.gl_pathc; i++) { 82 if (load_cfg(g.gl_pathv[i], c, item, quiet) < 0) 83 retval = CMD_RETURN_ERROR; 84 } 85 if (cfg_finished) { 86 new_item = cmdq_get_callback(cmd_source_file_done, NULL); 87 cmdq_insert_after(item, new_item); 88 } 89 90 globfree(&g); 91 return (retval); 92 } 93 94 static enum cmd_retval 95 cmd_source_file_done(struct cmdq_item *item, __unused void *data) 96 { 97 cfg_print_causes(item); 98 return (CMD_RETURN_NORMAL); 99 } 100