Add -A flag to new-session to make it behave like attach-session if the
[tmux-openbsd.git] / cfg.c
blobe4069d682914030dbd2a8cf1610333e4ac55e140
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 if ((f = fopen(path, "rb")) == NULL) {
45 xasprintf(cause, "%s: %s", path, strerror(errno));
46 return (-1);
49 n = found = 0;
50 line = NULL;
51 while ((buf = fgetln(f, &len))) {
52 /* Trim \n. */
53 if (buf[len - 1] == '\n')
54 len--;
55 log_debug("%s: %.*s", path, (int)len, buf);
57 /* Current line is the continuation of the previous one. */
58 if (line != NULL) {
59 oldlen = strlen(line);
60 line = xrealloc(line, 1, oldlen + len + 1);
61 } else {
62 oldlen = 0;
63 line = xmalloc(len + 1);
66 /* Append current line to the previous. */
67 memcpy(line + oldlen, buf, len);
68 line[oldlen + len] = '\0';
69 n++;
71 /* Continuation: get next line? */
72 len = strlen(line);
73 if (len > 0 && line[len - 1] == '\\') {
74 line[len - 1] = '\0';
76 /* Ignore escaped backslash at EOL. */
77 if (len > 1 && line[len - 2] != '\\')
78 continue;
80 copy = line;
81 line = NULL;
83 /* Skip empty lines. */
84 buf = copy;
85 while (isspace((u_char)*buf))
86 buf++;
87 if (*buf == '\0') {
88 free(copy);
89 continue;
92 /* Parse and run the command. */
93 if (cmd_string_parse(buf, &cmdlist, path, n, &cause1) != 0) {
94 free(copy);
95 if (cause1 == NULL)
96 continue;
97 xasprintf(&msg, "%s:%u: %s", path, n, cause1);
98 ARRAY_ADD(&cfg_causes, msg);
99 free(cause1);
100 continue;
102 free(copy);
104 if (cmdlist == NULL)
105 continue;
106 cmdq_append(cmdq, cmdlist);
107 cmd_list_free(cmdlist);
108 found++;
110 if (line != NULL)
111 free(line);
112 fclose(f);
114 return (found);
117 void
118 cfg_default_done(unused struct cmd_q *cmdq)
120 if (--cfg_references != 0)
121 return;
122 cfg_finished = 1;
124 if (!RB_EMPTY(&sessions))
125 cfg_show_causes(RB_MIN(sessions, &sessions));
127 cmdq_free(cfg_cmd_q);
128 cfg_cmd_q = NULL;
131 void
132 cfg_show_causes(struct session *s)
134 struct window_pane *wp;
135 char *cause;
136 u_int i;
138 if (s == NULL || ARRAY_EMPTY(&cfg_causes))
139 return;
140 wp = s->curw->window->active;
142 window_pane_set_mode(wp, &window_copy_mode);
143 window_copy_init_for_output(wp);
144 for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
145 cause = ARRAY_ITEM(&cfg_causes, i);
146 window_copy_add(wp, "%s", cause);
147 free(cause);
149 ARRAY_FREE(&cfg_causes);