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