xref: /openbsd-src/usr.bin/tmux/cfg.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
1 /* $OpenBSD: cfg.c,v 1.75 2019/06/20 06:51:36 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 		cmdq_continue(cfg_item);
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, c, 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, c, 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 	pi.c = c;
132 
133 	pr = cmd_parse_from_file(f, &pi);
134 	fclose(f);
135 	if (pr->status == CMD_PARSE_EMPTY)
136 		return (0);
137 	if (pr->status == CMD_PARSE_ERROR) {
138 		cfg_add_cause("%s", pr->error);
139 		free(pr->error);
140 		return (-1);
141 	}
142 	if (flags & CMD_PARSE_PARSEONLY) {
143 		cmd_list_free(pr->cmdlist);
144 		return (0);
145 	}
146 
147 	new_item0 = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
148 	if (item != NULL)
149 		cmdq_insert_after(item, new_item0);
150 	else
151 		cmdq_append(NULL, new_item0);
152 	cmd_list_free(pr->cmdlist);
153 
154 	if (new_item != NULL)
155 		*new_item = new_item0;
156 	return (0);
157 }
158 
159 void
160 cfg_add_cause(const char *fmt, ...)
161 {
162 	va_list	 ap;
163 	char	*msg;
164 
165 	va_start(ap, fmt);
166 	xvasprintf(&msg, fmt, ap);
167 	va_end(ap);
168 
169 	cfg_ncauses++;
170 	cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
171 	cfg_causes[cfg_ncauses - 1] = msg;
172 }
173 
174 void
175 cfg_print_causes(struct cmdq_item *item)
176 {
177 	u_int	 i;
178 
179 	for (i = 0; i < cfg_ncauses; i++) {
180 		cmdq_print(item, "%s", cfg_causes[i]);
181 		free(cfg_causes[i]);
182 	}
183 
184 	free(cfg_causes);
185 	cfg_causes = NULL;
186 	cfg_ncauses = 0;
187 }
188 
189 void
190 cfg_show_causes(struct session *s)
191 {
192 	struct window_pane		*wp;
193 	struct window_mode_entry	*wme;
194 	u_int				 i;
195 
196 	if (s == NULL || cfg_ncauses == 0)
197 		return;
198 	wp = s->curw->window->active;
199 
200 	wme = TAILQ_FIRST(&wp->modes);
201 	if (wme == NULL || wme->mode != &window_view_mode)
202 		window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
203 	for (i = 0; i < cfg_ncauses; i++) {
204 		window_copy_add(wp, "%s", cfg_causes[i]);
205 		free(cfg_causes[i]);
206 	}
207 
208 	free(cfg_causes);
209 	cfg_causes = NULL;
210 	cfg_ncauses = 0;
211 }
212