1 /* $OpenBSD: cfg.c,v 1.73 2019/06/05 20:00:53 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com> 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 <ctype.h> 22 #include <errno.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <util.h> 27 28 #include "tmux.h" 29 30 struct client *cfg_client; 31 static char *cfg_file; 32 int cfg_finished; 33 static char **cfg_causes; 34 static u_int cfg_ncauses; 35 static struct cmdq_item *cfg_item; 36 37 static enum cmd_retval 38 cfg_client_done(__unused struct cmdq_item *item, __unused void *data) 39 { 40 if (!cfg_finished) 41 return (CMD_RETURN_WAIT); 42 return (CMD_RETURN_NORMAL); 43 } 44 45 static enum cmd_retval 46 cfg_done(__unused struct cmdq_item *item, __unused void *data) 47 { 48 if (cfg_finished) 49 return (CMD_RETURN_NORMAL); 50 cfg_finished = 1; 51 52 if (!RB_EMPTY(&sessions)) 53 cfg_show_causes(RB_MIN(sessions, &sessions)); 54 55 if (cfg_item != NULL) 56 cfg_item->flags &= ~CMDQ_WAITING; 57 58 status_prompt_load_history(); 59 60 return (CMD_RETURN_NORMAL); 61 } 62 63 void 64 set_cfg_file(const char *path) 65 { 66 free(cfg_file); 67 cfg_file = xstrdup(path); 68 } 69 70 void 71 start_cfg(void) 72 { 73 const char *home; 74 int flags = 0; 75 struct client *c; 76 77 /* 78 * Configuration files are loaded without a client, so commands are run 79 * in the global queue with item->client NULL. 80 * 81 * However, we must block the initial client (but just the initial 82 * client) so that its command runs after the configuration is loaded. 83 * Because start_cfg() is called so early, we can be sure the client's 84 * command queue is currently empty and our callback will be at the 85 * front - we need to get in before MSG_COMMAND. 86 */ 87 cfg_client = c = TAILQ_FIRST(&clients); 88 if (c != NULL) { 89 cfg_item = cmdq_get_callback(cfg_client_done, NULL); 90 cmdq_append(c, cfg_item); 91 } 92 93 if (cfg_file == NULL) 94 load_cfg(TMUX_CONF, NULL, NULL, CMD_PARSE_QUIET, NULL); 95 96 if (cfg_file == NULL && (home = find_home()) != NULL) { 97 xasprintf(&cfg_file, "%s/.tmux.conf", home); 98 flags = CMD_PARSE_QUIET; 99 } 100 if (cfg_file != NULL) 101 load_cfg(cfg_file, NULL, NULL, flags, NULL); 102 103 cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL)); 104 } 105 106 int 107 load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags, 108 struct cmdq_item **new_item) 109 { 110 FILE *f; 111 struct cmd_parse_input pi; 112 struct cmd_parse_result *pr; 113 struct cmdq_item *new_item0; 114 115 if (new_item != NULL) 116 *new_item = NULL; 117 118 log_debug("loading %s", path); 119 if ((f = fopen(path, "rb")) == NULL) { 120 if (errno == ENOENT && (flags & CMD_PARSE_QUIET)) 121 return (0); 122 cfg_add_cause("%s: %s", path, strerror(errno)); 123 return (-1); 124 } 125 126 memset(&pi, 0, sizeof pi); 127 pi.flags = flags; 128 pi.file = path; 129 pi.line = 1; 130 pi.item = item; 131 132 pr = cmd_parse_from_file(f, &pi); 133 fclose(f); 134 if (pr->status == CMD_PARSE_EMPTY) 135 return (0); 136 if (pr->status == CMD_PARSE_ERROR) { 137 cfg_add_cause("%s", pr->error); 138 free(pr->error); 139 return (-1); 140 } 141 if (flags & CMD_PARSE_PARSEONLY) { 142 cmd_list_free(pr->cmdlist); 143 return (0); 144 } 145 146 new_item0 = cmdq_get_command(pr->cmdlist, NULL, NULL, 0); 147 if (item != NULL) 148 cmdq_insert_after(item, new_item0); 149 else 150 cmdq_append(c, new_item0); 151 cmd_list_free(pr->cmdlist); 152 153 if (new_item != NULL) 154 *new_item = new_item0; 155 return (0); 156 } 157 158 void 159 cfg_add_cause(const char *fmt, ...) 160 { 161 va_list ap; 162 char *msg; 163 164 va_start(ap, fmt); 165 xvasprintf(&msg, fmt, ap); 166 va_end(ap); 167 168 cfg_ncauses++; 169 cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes); 170 cfg_causes[cfg_ncauses - 1] = msg; 171 } 172 173 void 174 cfg_print_causes(struct cmdq_item *item) 175 { 176 u_int i; 177 178 for (i = 0; i < cfg_ncauses; i++) { 179 cmdq_print(item, "%s", cfg_causes[i]); 180 free(cfg_causes[i]); 181 } 182 183 free(cfg_causes); 184 cfg_causes = NULL; 185 cfg_ncauses = 0; 186 } 187 188 void 189 cfg_show_causes(struct session *s) 190 { 191 struct window_pane *wp; 192 struct window_mode_entry *wme; 193 u_int i; 194 195 if (s == NULL || cfg_ncauses == 0) 196 return; 197 wp = s->curw->window->active; 198 199 wme = TAILQ_FIRST(&wp->modes); 200 if (wme == NULL || wme->mode != &window_view_mode) 201 window_pane_set_mode(wp, &window_view_mode, NULL, NULL); 202 for (i = 0; i < cfg_ncauses; i++) { 203 window_copy_add(wp, "%s", cfg_causes[i]); 204 free(cfg_causes[i]); 205 } 206 207 free(cfg_causes); 208 cfg_causes = NULL; 209 cfg_ncauses = 0; 210 } 211