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