Clear window->flags when clearing winlinks
[tmux-openbsd.git] / cfg.c
blobc625a2fbe4885a2b1957cec648593365f611a0b1
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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>
20 #include <sys/stat.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "tmux.h"
30 struct cmd_q *cfg_cmd_q;
31 int cfg_finished;
32 int cfg_references;
33 struct causelist cfg_causes;
35 int
36 load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
38 FILE *f;
39 u_int n, found;
40 char *buf, *copy, *line, *cause1, *msg;
41 size_t len, oldlen;
42 struct cmd_list *cmdlist;
44 log_debug("loading %s", path);
45 if ((f = fopen(path, "rb")) == NULL) {
46 xasprintf(cause, "%s: %s", path, strerror(errno));
47 return (-1);
50 n = found = 0;
51 line = NULL;
52 while ((buf = fgetln(f, &len))) {
53 /* Trim \n. */
54 if (buf[len - 1] == '\n')
55 len--;
56 log_debug("%s: %.*s", path, (int)len, buf);
58 /* Current line is the continuation of the previous one. */
59 if (line != NULL) {
60 oldlen = strlen(line);
61 line = xrealloc(line, 1, oldlen + len + 1);
62 } else {
63 oldlen = 0;
64 line = xmalloc(len + 1);
67 /* Append current line to the previous. */
68 memcpy(line + oldlen, buf, len);
69 line[oldlen + len] = '\0';
70 n++;
72 /* Continuation: get next line? */
73 len = strlen(line);
74 if (len > 0 && line[len - 1] == '\\') {
75 line[len - 1] = '\0';
77 /* Ignore escaped backslash at EOL. */
78 if (len > 1 && line[len - 2] != '\\')
79 continue;
81 copy = line;
82 line = NULL;
84 /* Skip empty lines. */
85 buf = copy;
86 while (isspace((u_char)*buf))
87 buf++;
88 if (*buf == '\0') {
89 free(copy);
90 continue;
93 /* Parse and run the command. */
94 if (cmd_string_parse(buf, &cmdlist, path, n, &cause1) != 0) {
95 free(copy);
96 if (cause1 == NULL)
97 continue;
98 xasprintf(&msg, "%s:%u: %s", path, n, cause1);
99 ARRAY_ADD(&cfg_causes, msg);
100 free(cause1);
101 continue;
103 free(copy);
105 if (cmdlist == NULL)
106 continue;
107 cmdq_append(cmdq, cmdlist);
108 cmd_list_free(cmdlist);
109 found++;
111 if (line != NULL)
112 free(line);
113 fclose(f);
115 return (found);
118 void
119 cfg_default_done(unused struct cmd_q *cmdq)
121 if (--cfg_references != 0)
122 return;
123 cfg_finished = 1;
125 if (!RB_EMPTY(&sessions))
126 cfg_show_causes(RB_MIN(sessions, &sessions));
128 cmdq_free(cfg_cmd_q);
129 cfg_cmd_q = NULL;
132 void
133 cfg_show_causes(struct session *s)
135 struct window_pane *wp;
136 char *cause;
137 u_int i;
139 if (s == NULL || ARRAY_EMPTY(&cfg_causes))
140 return;
141 wp = s->curw->window->active;
143 window_pane_set_mode(wp, &window_copy_mode);
144 window_copy_init_for_output(wp);
145 for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
146 cause = ARRAY_ITEM(&cfg_causes, i);
147 window_copy_add(wp, "%s", cause);
148 free(cause);
150 ARRAY_FREE(&cfg_causes);