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 #include <unistd.h>
27
28 #include "tmux.h"
29
30 char *cfg_file;
31 struct cmd_q *cfg_cmd_q;
32 int cfg_finished;
33 int cfg_references;
34 char **cfg_causes;
35 u_int cfg_ncauses;
36 struct client *cfg_client;
37
38 void cfg_default_done(struct cmd_q *);
39
40 void
set_cfg_file(const char * path)41 set_cfg_file(const char *path)
42 {
43 free(cfg_file);
44 cfg_file = xstrdup(path);
45 }
46
47 void
start_cfg(void)48 start_cfg(void)
49 {
50 char *cause = NULL;
51 const char *home;
52
53 cfg_cmd_q = cmdq_new(NULL);
54 cfg_cmd_q->emptyfn = cfg_default_done;
55
56 cfg_finished = 0;
57 cfg_references = 1;
58
59 cfg_client = TAILQ_FIRST(&clients);
60 if (cfg_client != NULL)
61 cfg_client->references++;
62
63 if (access(TMUX_CONF, R_OK) == 0) {
64 if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1)
65 cfg_add_cause("%s: %s", TMUX_CONF, cause);
66 } else if (errno != ENOENT)
67 cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno));
68
69 if (cfg_file == NULL && (home = find_home()) != NULL) {
70 xasprintf(&cfg_file, "%s/.tmux.conf", home);
71 if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
72 free(cfg_file);
73 cfg_file = NULL;
74 }
75 }
76 if (cfg_file != NULL && load_cfg(cfg_file, cfg_cmd_q, &cause) == -1)
77 cfg_add_cause("%s: %s", cfg_file, cause);
78 free(cause);
79
80 cmdq_continue(cfg_cmd_q);
81 }
82
83 int
load_cfg(const char * path,struct cmd_q * cmdq,char ** cause)84 load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
85 {
86 FILE *f;
87 char delim[3] = { '\\', '\\', '\0' };
88 u_int found;
89 size_t line = 0;
90 char *buf, *cause1, *p;
91 struct cmd_list *cmdlist;
92
93 log_debug("loading %s", path);
94 if ((f = fopen(path, "rb")) == NULL) {
95 xasprintf(cause, "%s: %s", path, strerror(errno));
96 return (-1);
97 }
98
99 found = 0;
100 while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
101 log_debug("%s: %s", path, buf);
102
103 /* Skip empty lines. */
104 p = buf;
105 while (isspace((u_char) *p))
106 p++;
107 if (*p == '\0') {
108 free(buf);
109 continue;
110 }
111
112 /* Parse and run the command. */
113 if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
114 free(buf);
115 if (cause1 == NULL)
116 continue;
117 cfg_add_cause("%s:%zu: %s", path, line, cause1);
118 free(cause1);
119 continue;
120 }
121 free(buf);
122
123 if (cmdlist == NULL)
124 continue;
125 cmdq_append(cmdq, cmdlist, NULL);
126 cmd_list_free(cmdlist);
127 found++;
128 }
129 fclose(f);
130
131 return (found);
132 }
133
134 void
cfg_default_done(__unused struct cmd_q * cmdq)135 cfg_default_done(__unused struct cmd_q *cmdq)
136 {
137 if (--cfg_references != 0)
138 return;
139 cfg_finished = 1;
140
141 if (!RB_EMPTY(&sessions))
142 cfg_show_causes(RB_MIN(sessions, &sessions));
143
144 cmdq_free(cfg_cmd_q);
145 cfg_cmd_q = NULL;
146
147 if (cfg_client != NULL) {
148 /*
149 * The client command queue starts with client_exit set to 1 so
150 * only continue if not empty (that is, we have been delayed
151 * during configuration parsing for long enough that the
152 * MSG_COMMAND has arrived), else the client will exit before
153 * the MSG_COMMAND which might tell it not to.
154 */
155 if (!TAILQ_EMPTY(&cfg_client->cmdq->queue))
156 cmdq_continue(cfg_client->cmdq);
157 server_client_unref(cfg_client);
158 cfg_client = NULL;
159 }
160 }
161
162 void
cfg_add_cause(const char * fmt,...)163 cfg_add_cause(const char *fmt, ...)
164 {
165 va_list ap;
166 char *msg;
167
168 va_start(ap, fmt);
169 xvasprintf(&msg, fmt, ap);
170 va_end(ap);
171
172 cfg_ncauses++;
173 cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
174 cfg_causes[cfg_ncauses - 1] = msg;
175 }
176
177 void
cfg_print_causes(struct cmd_q * cmdq)178 cfg_print_causes(struct cmd_q *cmdq)
179 {
180 u_int i;
181
182 for (i = 0; i < cfg_ncauses; i++) {
183 cmdq_print(cmdq, "%s", cfg_causes[i]);
184 free(cfg_causes[i]);
185 }
186
187 free(cfg_causes);
188 cfg_causes = NULL;
189 cfg_ncauses = 0;
190 }
191
192 void
cfg_show_causes(struct session * s)193 cfg_show_causes(struct session *s)
194 {
195 struct window_pane *wp;
196 u_int i;
197
198 if (s == NULL || cfg_ncauses == 0)
199 return;
200 wp = s->curw->window->active;
201
202 window_pane_set_mode(wp, &window_copy_mode);
203 window_copy_init_for_output(wp);
204 for (i = 0; i < cfg_ncauses; i++) {
205 window_copy_add(wp, "%s", cfg_causes[i]);
206 free(cfg_causes[i]);
207 }
208
209 free(cfg_causes);
210 cfg_causes = NULL;
211 cfg_ncauses = 0;
212 }
213