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>
30 struct cmd_q
*cfg_cmd_q
;
33 struct causelist cfg_causes
;
36 load_cfg(const char *path
, struct cmd_q
*cmdq
, char **cause
)
40 char *buf
, *copy
, *line
, *cause1
, *msg
;
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
));
52 while ((buf
= fgetln(f
, &len
))) {
54 if (buf
[len
- 1] == '\n')
56 log_debug("%s: %.*s", path
, (int)len
, buf
);
58 /* Current line is the continuation of the previous one. */
60 oldlen
= strlen(line
);
61 line
= xrealloc(line
, 1, oldlen
+ len
+ 1);
64 line
= xmalloc(len
+ 1);
67 /* Append current line to the previous. */
68 memcpy(line
+ oldlen
, buf
, len
);
69 line
[oldlen
+ len
] = '\0';
72 /* Continuation: get next line? */
74 if (len
> 0 && line
[len
- 1] == '\\') {
77 /* Ignore escaped backslash at EOL. */
78 if (len
> 1 && line
[len
- 2] != '\\')
84 /* Skip empty lines. */
86 while (isspace((u_char
)*buf
))
93 /* Parse and run the command. */
94 if (cmd_string_parse(buf
, &cmdlist
, path
, n
, &cause1
) != 0) {
98 xasprintf(&msg
, "%s:%u: %s", path
, n
, cause1
);
99 ARRAY_ADD(&cfg_causes
, msg
);
107 cmdq_append(cmdq
, cmdlist
);
108 cmd_list_free(cmdlist
);
119 cfg_default_done(unused
struct cmd_q
*cmdq
)
121 if (--cfg_references
!= 0)
125 if (!RB_EMPTY(&sessions
))
126 cfg_show_causes(RB_MIN(sessions
, &sessions
));
128 cmdq_free(cfg_cmd_q
);
133 cfg_show_causes(struct session
*s
)
135 struct window_pane
*wp
;
139 if (s
== NULL
|| ARRAY_EMPTY(&cfg_causes
))
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
);
150 ARRAY_FREE(&cfg_causes
);