1 /* $OpenBSD$ */ 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 27 #include "tmux.h" 28 29 struct client *cfg_client; 30 int cfg_finished; 31 static char **cfg_causes; 32 static u_int cfg_ncauses; 33 static struct cmdq_item *cfg_item; 34 35 int cfg_quiet = 1; 36 char **cfg_files; 37 u_int cfg_nfiles; 38 39 static enum cmd_retval 40 cfg_client_done(__unused struct cmdq_item *item, __unused void *data) 41 { 42 if (!cfg_finished) 43 return (CMD_RETURN_WAIT); 44 return (CMD_RETURN_NORMAL); 45 } 46 47 static enum cmd_retval 48 cfg_done(__unused struct cmdq_item *item, __unused void *data) 49 { 50 if (cfg_finished) 51 return (CMD_RETURN_NORMAL); 52 cfg_finished = 1; 53 54 if (!RB_EMPTY(&sessions)) 55 cfg_show_causes(RB_MIN(sessions, &sessions)); 56 57 if (cfg_item != NULL) 58 cmdq_continue(cfg_item); 59 60 status_prompt_load_history(); 61 62 return (CMD_RETURN_NORMAL); 63 } 64 65 void 66 start_cfg(void) 67 { 68 struct client *c; 69 u_int i; 70 71 /* 72 * Configuration files are loaded without a client, so commands are run 73 * in the global queue with item->client NULL. 74 * 75 * However, we must block the initial client (but just the initial 76 * client) so that its command runs after the configuration is loaded. 77 * Because start_cfg() is called so early, we can be sure the client's 78 * command queue is currently empty and our callback will be at the 79 * front - we need to get in before MSG_COMMAND. 80 */ 81 cfg_client = c = TAILQ_FIRST(&clients); 82 if (c != NULL) { 83 cfg_item = cmdq_get_callback(cfg_client_done, NULL); 84 cmdq_append(c, cfg_item); 85 } 86 87 for (i = 0; i < cfg_nfiles; i++) { 88 if (cfg_quiet) 89 load_cfg(cfg_files[i], c, NULL, CMD_PARSE_QUIET, NULL); 90 else 91 load_cfg(cfg_files[i], c, NULL, 0, NULL); 92 } 93 94 cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL)); 95 } 96 97 int 98 load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags, 99 struct cmdq_item **new_item) 100 { 101 FILE *f; 102 struct cmd_parse_input pi; 103 struct cmd_parse_result *pr; 104 struct cmdq_item *new_item0; 105 struct cmdq_state *state; 106 107 if (new_item != NULL) 108 *new_item = NULL; 109 110 log_debug("loading %s", path); 111 if ((f = fopen(path, "rb")) == NULL) { 112 if (errno == ENOENT && (flags & CMD_PARSE_QUIET)) 113 return (0); 114 cfg_add_cause("%s: %s", path, strerror(errno)); 115 return (-1); 116 } 117 118 memset(&pi, 0, sizeof pi); 119 pi.flags = flags; 120 pi.file = path; 121 pi.line = 1; 122 pi.item = item; 123 pi.c = c; 124 125 pr = cmd_parse_from_file(f, &pi); 126 fclose(f); 127 if (pr->status == CMD_PARSE_EMPTY) 128 return (0); 129 if (pr->status == CMD_PARSE_ERROR) { 130 cfg_add_cause("%s", pr->error); 131 free(pr->error); 132 return (-1); 133 } 134 if (flags & CMD_PARSE_PARSEONLY) { 135 cmd_list_free(pr->cmdlist); 136 return (0); 137 } 138 139 if (item != NULL) 140 state = cmdq_copy_state(cmdq_get_state(item)); 141 else 142 state = cmdq_new_state(NULL, NULL, 0); 143 cmdq_add_format(state, "current_file", "%s", pi.file); 144 145 new_item0 = cmdq_get_command(pr->cmdlist, state); 146 if (item != NULL) 147 new_item0 = cmdq_insert_after(item, new_item0); 148 else 149 new_item0 = cmdq_append(NULL, new_item0); 150 cmd_list_free(pr->cmdlist); 151 cmdq_free_state(state); 152 153 if (new_item != NULL) 154 *new_item = new_item0; 155 return (0); 156 } 157 158 int 159 load_cfg_from_buffer(const void *buf, size_t len, const char *path, 160 struct client *c, struct cmdq_item *item, int flags, 161 struct cmdq_item **new_item) 162 { 163 struct cmd_parse_input pi; 164 struct cmd_parse_result *pr; 165 struct cmdq_item *new_item0; 166 struct cmdq_state *state; 167 168 if (new_item != NULL) 169 *new_item = NULL; 170 171 log_debug("loading %s", path); 172 173 memset(&pi, 0, sizeof pi); 174 pi.flags = flags; 175 pi.file = path; 176 pi.line = 1; 177 pi.item = item; 178 pi.c = c; 179 180 pr = cmd_parse_from_buffer(buf, len, &pi); 181 if (pr->status == CMD_PARSE_EMPTY) 182 return (0); 183 if (pr->status == CMD_PARSE_ERROR) { 184 cfg_add_cause("%s", pr->error); 185 free(pr->error); 186 return (-1); 187 } 188 if (flags & CMD_PARSE_PARSEONLY) { 189 cmd_list_free(pr->cmdlist); 190 return (0); 191 } 192 193 if (item != NULL) 194 state = cmdq_copy_state(cmdq_get_state(item)); 195 else 196 state = cmdq_new_state(NULL, NULL, 0); 197 cmdq_add_format(state, "current_file", "%s", pi.file); 198 199 new_item0 = cmdq_get_command(pr->cmdlist, state); 200 if (item != NULL) 201 new_item0 = cmdq_insert_after(item, new_item0); 202 else 203 new_item0 = cmdq_append(NULL, new_item0); 204 cmd_list_free(pr->cmdlist); 205 cmdq_free_state(state); 206 207 if (new_item != NULL) 208 *new_item = new_item0; 209 return (0); 210 } 211 212 void 213 cfg_add_cause(const char *fmt, ...) 214 { 215 va_list ap; 216 char *msg; 217 218 va_start(ap, fmt); 219 xvasprintf(&msg, fmt, ap); 220 va_end(ap); 221 222 cfg_ncauses++; 223 cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes); 224 cfg_causes[cfg_ncauses - 1] = msg; 225 } 226 227 void 228 cfg_print_causes(struct cmdq_item *item) 229 { 230 u_int i; 231 232 for (i = 0; i < cfg_ncauses; i++) { 233 cmdq_print(item, "%s", cfg_causes[i]); 234 free(cfg_causes[i]); 235 } 236 237 free(cfg_causes); 238 cfg_causes = NULL; 239 cfg_ncauses = 0; 240 } 241 242 void 243 cfg_show_causes(struct session *s) 244 { 245 struct window_pane *wp; 246 struct window_mode_entry *wme; 247 u_int i; 248 249 if (s == NULL || cfg_ncauses == 0) 250 return; 251 wp = s->curw->window->active; 252 253 wme = TAILQ_FIRST(&wp->modes); 254 if (wme == NULL || wme->mode != &window_view_mode) 255 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); 256 for (i = 0; i < cfg_ncauses; i++) { 257 window_copy_add(wp, "%s", cfg_causes[i]); 258 free(cfg_causes[i]); 259 } 260 261 free(cfg_causes); 262 cfg_causes = NULL; 263 cfg_ncauses = 0; 264 } 265