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 if ((f
= fopen(path
, "rb")) == NULL
) {
45 xasprintf(cause
, "%s: %s", path
, strerror(errno
));
51 while ((buf
= fgetln(f
, &len
))) {
53 if (buf
[len
- 1] == '\n')
55 log_debug("%s: %.*s", path
, (int)len
, buf
);
57 /* Current line is the continuation of the previous one. */
59 oldlen
= strlen(line
);
60 line
= xrealloc(line
, 1, oldlen
+ len
+ 1);
63 line
= xmalloc(len
+ 1);
66 /* Append current line to the previous. */
67 memcpy(line
+ oldlen
, buf
, len
);
68 line
[oldlen
+ len
] = '\0';
71 /* Continuation: get next line? */
73 if (len
> 0 && line
[len
- 1] == '\\') {
76 /* Ignore escaped backslash at EOL. */
77 if (len
> 1 && line
[len
- 2] != '\\')
83 /* Skip empty lines. */
85 while (isspace((u_char
)*buf
))
92 /* Parse and run the command. */
93 if (cmd_string_parse(buf
, &cmdlist
, path
, n
, &cause1
) != 0) {
97 xasprintf(&msg
, "%s:%u: %s", path
, n
, cause1
);
98 ARRAY_ADD(&cfg_causes
, msg
);
106 cmdq_append(cmdq
, cmdlist
);
107 cmd_list_free(cmdlist
);
118 cfg_default_done(unused
struct cmd_q
*cmdq
)
120 if (--cfg_references
!= 0)
124 if (!RB_EMPTY(&sessions
))
125 cfg_show_causes(RB_MIN(sessions
, &sessions
));
127 cmdq_free(cfg_cmd_q
);
132 cfg_show_causes(struct session
*s
)
134 struct window_pane
*wp
;
138 if (s
== NULL
|| ARRAY_EMPTY(&cfg_causes
))
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
);
149 ARRAY_FREE(&cfg_causes
);