Merge branch 'obsd-master'
[tmux.git] / cfg.c
blob5109c8c9d5172cf1ae0502eced99c11c8429450f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "tmux.h"
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;
35 int cfg_quiet = 1;
36 char **cfg_files;
37 u_int cfg_nfiles;
39 static enum cmd_retval
40 cfg_client_done(__unused struct cmdq_item *item, __unused void *data)
42 if (!cfg_finished)
43 return (CMD_RETURN_WAIT);
44 return (CMD_RETURN_NORMAL);
47 static enum cmd_retval
48 cfg_done(__unused struct cmdq_item *item, __unused void *data)
50 if (cfg_finished)
51 return (CMD_RETURN_NORMAL);
52 cfg_finished = 1;
54 cfg_show_causes(NULL);
56 if (cfg_item != NULL)
57 cmdq_continue(cfg_item);
59 status_prompt_load_history();
61 return (CMD_RETURN_NORMAL);
64 void
65 start_cfg(void)
67 struct client *c;
68 u_int i;
71 * Configuration files are loaded without a client, so commands are run
72 * in the global queue with item->client NULL.
74 * However, we must block the initial client (but just the initial
75 * client) so that its command runs after the configuration is loaded.
76 * Because start_cfg() is called so early, we can be sure the client's
77 * command queue is currently empty and our callback will be at the
78 * front - we need to get in before MSG_COMMAND.
80 cfg_client = c = TAILQ_FIRST(&clients);
81 if (c != NULL) {
82 cfg_item = cmdq_get_callback(cfg_client_done, NULL);
83 cmdq_append(c, cfg_item);
86 for (i = 0; i < cfg_nfiles; i++) {
87 if (cfg_quiet)
88 load_cfg(cfg_files[i], c, NULL, CMD_PARSE_QUIET, NULL);
89 else
90 load_cfg(cfg_files[i], c, NULL, 0, NULL);
93 cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
96 int
97 load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags,
98 struct cmdq_item **new_item)
100 FILE *f;
101 struct cmd_parse_input pi;
102 struct cmd_parse_result *pr;
103 struct cmdq_item *new_item0;
104 struct cmdq_state *state;
106 if (new_item != NULL)
107 *new_item = NULL;
109 log_debug("loading %s", path);
110 if ((f = fopen(path, "rb")) == NULL) {
111 if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
112 return (0);
113 cfg_add_cause("%s: %s", path, strerror(errno));
114 return (-1);
117 memset(&pi, 0, sizeof pi);
118 pi.flags = flags;
119 pi.file = path;
120 pi.line = 1;
121 pi.item = item;
122 pi.c = c;
124 pr = cmd_parse_from_file(f, &pi);
125 fclose(f);
126 if (pr->status == CMD_PARSE_ERROR) {
127 cfg_add_cause("%s", pr->error);
128 free(pr->error);
129 return (-1);
131 if (flags & CMD_PARSE_PARSEONLY) {
132 cmd_list_free(pr->cmdlist);
133 return (0);
136 if (item != NULL)
137 state = cmdq_copy_state(cmdq_get_state(item));
138 else
139 state = cmdq_new_state(NULL, NULL, 0);
140 cmdq_add_format(state, "current_file", "%s", pi.file);
142 new_item0 = cmdq_get_command(pr->cmdlist, state);
143 if (item != NULL)
144 new_item0 = cmdq_insert_after(item, new_item0);
145 else
146 new_item0 = cmdq_append(NULL, new_item0);
147 cmd_list_free(pr->cmdlist);
148 cmdq_free_state(state);
150 if (new_item != NULL)
151 *new_item = new_item0;
152 return (0);
156 load_cfg_from_buffer(const void *buf, size_t len, const char *path,
157 struct client *c, struct cmdq_item *item, int flags,
158 struct cmdq_item **new_item)
160 struct cmd_parse_input pi;
161 struct cmd_parse_result *pr;
162 struct cmdq_item *new_item0;
163 struct cmdq_state *state;
165 if (new_item != NULL)
166 *new_item = NULL;
168 log_debug("loading %s", path);
170 memset(&pi, 0, sizeof pi);
171 pi.flags = flags;
172 pi.file = path;
173 pi.line = 1;
174 pi.item = item;
175 pi.c = c;
177 pr = cmd_parse_from_buffer(buf, len, &pi);
178 if (pr->status == CMD_PARSE_ERROR) {
179 cfg_add_cause("%s", pr->error);
180 free(pr->error);
181 return (-1);
183 if (flags & CMD_PARSE_PARSEONLY) {
184 cmd_list_free(pr->cmdlist);
185 return (0);
188 if (item != NULL)
189 state = cmdq_copy_state(cmdq_get_state(item));
190 else
191 state = cmdq_new_state(NULL, NULL, 0);
192 cmdq_add_format(state, "current_file", "%s", pi.file);
194 new_item0 = cmdq_get_command(pr->cmdlist, state);
195 if (item != NULL)
196 new_item0 = cmdq_insert_after(item, new_item0);
197 else
198 new_item0 = cmdq_append(NULL, new_item0);
199 cmd_list_free(pr->cmdlist);
200 cmdq_free_state(state);
202 if (new_item != NULL)
203 *new_item = new_item0;
204 return (0);
207 void
208 cfg_add_cause(const char *fmt, ...)
210 va_list ap;
211 char *msg;
213 va_start(ap, fmt);
214 xvasprintf(&msg, fmt, ap);
215 va_end(ap);
217 cfg_ncauses++;
218 cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
219 cfg_causes[cfg_ncauses - 1] = msg;
222 void
223 cfg_print_causes(struct cmdq_item *item)
225 u_int i;
227 for (i = 0; i < cfg_ncauses; i++) {
228 cmdq_print(item, "%s", cfg_causes[i]);
229 free(cfg_causes[i]);
232 free(cfg_causes);
233 cfg_causes = NULL;
234 cfg_ncauses = 0;
237 void
238 cfg_show_causes(struct session *s)
240 struct client *c = TAILQ_FIRST(&clients);
241 struct window_pane *wp;
242 struct window_mode_entry *wme;
243 u_int i;
245 if (cfg_ncauses == 0)
246 return;
248 if (c != NULL && (c->flags & CLIENT_CONTROL)) {
249 for (i = 0; i < cfg_ncauses; i++) {
250 control_write(c, "%%config-error %s", cfg_causes[i]);
251 free(cfg_causes[i]);
253 goto out;
256 if (s == NULL) {
257 if (c != NULL && c->session != NULL)
258 s = c->session;
259 else
260 s = RB_MIN(sessions, &sessions);
262 if (s == NULL || s->attached == 0) /* wait for an attached session */
263 return;
264 wp = s->curw->window->active;
266 wme = TAILQ_FIRST(&wp->modes);
267 if (wme == NULL || wme->mode != &window_view_mode)
268 window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
269 for (i = 0; i < cfg_ncauses; i++) {
270 window_copy_add(wp, 0, "%s", cfg_causes[i]);
271 free(cfg_causes[i]);
274 out:
275 free(cfg_causes);
276 cfg_causes = NULL;
277 cfg_ncauses = 0;