Annotation of src/usr.bin/tmux/cfg.c, Revision 1.36
1.36 ! nicm 1: /* $OpenBSD: cfg.c,v 1.35 2015/04/25 18:47:01 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.32 tobias 26: #include <util.h>
1.1 nicm 27:
28: #include "tmux.h"
29:
1.36 ! nicm 30: struct cmd_q *cfg_cmd_q;
! 31: int cfg_finished;
! 32: int cfg_references;
! 33: char **cfg_causes;
! 34: u_int cfg_ncauses;
! 35: struct client *cfg_client;
1.1 nicm 36:
1.28 nicm 37: int
38: load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
1.1 nicm 39: {
1.7 deraadt 40: FILE *f;
1.32 tobias 41: char delim[3] = { '\\', '\\', '\0' };
42: u_int found;
43: size_t line = 0;
1.33 nicm 44: char *buf, *cause1, *p;
1.1 nicm 45: struct cmd_list *cmdlist;
46:
1.29 nicm 47: log_debug("loading %s", path);
1.1 nicm 48: if ((f = fopen(path, "rb")) == NULL) {
1.28 nicm 49: xasprintf(cause, "%s: %s", path, strerror(errno));
50: return (-1);
1.25 nicm 51: }
52:
1.32 tobias 53: found = 0;
54: while ((buf = fparseln(f, NULL, &line, delim, 0))) {
55: log_debug("%s: %s", path, buf);
1.13 nicm 56:
1.21 nicm 57: /* Skip empty lines. */
1.32 tobias 58: p = buf;
59: while (isspace((u_char) *p))
60: p++;
61: if (*p == '\0') {
62: free(buf);
1.21 nicm 63: continue;
1.22 nicm 64: }
1.21 nicm 65:
1.28 nicm 66: /* Parse and run the command. */
1.32 tobias 67: if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
68: free(buf);
1.28 nicm 69: if (cause1 == NULL)
1.1 nicm 70: continue;
1.33 nicm 71: cfg_add_cause("%s:%zu: %s", path, line, cause1);
1.28 nicm 72: free(cause1);
1.9 nicm 73: continue;
1.20 nicm 74: }
1.32 tobias 75: free(buf);
1.28 nicm 76:
1.1 nicm 77: if (cmdlist == NULL)
78: continue;
1.34 nicm 79: cmdq_append(cmdq, cmdlist, NULL);
1.1 nicm 80: cmd_list_free(cmdlist);
1.28 nicm 81: found++;
1.1 nicm 82: }
83: fclose(f);
1.25 nicm 84:
1.28 nicm 85: return (found);
86: }
87:
88: void
89: cfg_default_done(unused struct cmd_q *cmdq)
90: {
91: if (--cfg_references != 0)
92: return;
93: cfg_finished = 1;
1.18 nicm 94:
1.28 nicm 95: if (!RB_EMPTY(&sessions))
96: cfg_show_causes(RB_MIN(sessions, &sessions));
1.1 nicm 97:
1.28 nicm 98: cmdq_free(cfg_cmd_q);
99: cfg_cmd_q = NULL;
1.30 nicm 100:
101: if (cfg_client != NULL) {
102: /*
103: * The client command queue starts with client_exit set to 1 so
104: * only continue if not empty (that is, we have been delayed
105: * during configuration parsing for long enough that the
106: * MSG_COMMAND has arrived), else the client will exit before
107: * the MSG_COMMAND which might tell it not to.
108: */
109: if (!TAILQ_EMPTY(&cfg_client->cmdq->queue))
110: cmdq_continue(cfg_client->cmdq);
111: cfg_client->references--;
112: cfg_client = NULL;
113: }
1.33 nicm 114: }
115:
116: void
1.36 ! nicm 117: cfg_add_cause(const char *fmt, ...)
1.33 nicm 118: {
1.36 ! nicm 119: va_list ap;
! 120: char *msg;
1.33 nicm 121:
122: va_start(ap, fmt);
123: xvasprintf(&msg, fmt, ap);
124: va_end (ap);
125:
1.35 nicm 126: cfg_ncauses++;
127: cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
128: cfg_causes[cfg_ncauses - 1] = msg;
1.33 nicm 129: }
130:
131: void
132: cfg_print_causes(struct cmd_q *cmdq)
133: {
134: u_int i;
135:
1.35 nicm 136: for (i = 0; i < cfg_ncauses; i++) {
137: cmdq_print(cmdq, "%s", cfg_causes[i]);
138: free(cfg_causes[i]);
1.33 nicm 139: }
1.35 nicm 140:
141: free(cfg_causes);
142: cfg_causes = NULL;
1.17 nicm 143: }
144:
145: void
1.28 nicm 146: cfg_show_causes(struct session *s)
1.17 nicm 147: {
148: struct window_pane *wp;
149: u_int i;
150:
1.35 nicm 151: if (s == NULL || cfg_ncauses == 0)
1.17 nicm 152: return;
153: wp = s->curw->window->active;
154:
155: window_pane_set_mode(wp, &window_copy_mode);
156: window_copy_init_for_output(wp);
1.35 nicm 157: for (i = 0; i < cfg_ncauses; i++) {
158: window_copy_add(wp, "%s", cfg_causes[i]);
159: free(cfg_causes[i]);
1.17 nicm 160: }
1.35 nicm 161:
162: free(cfg_causes);
163: cfg_causes = NULL;
1.1 nicm 164: }