Annotation of src/usr.bin/tmux/cfg.c, Revision 1.32
1.32 ! tobias 1: /* $OpenBSD: cfg.c,v 1.31 2014/04/17 11:38:35 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.28 nicm 30: struct cmd_q *cfg_cmd_q;
1.18 nicm 31: int cfg_finished;
1.28 nicm 32: int cfg_references;
1.18 nicm 33: struct causelist cfg_causes;
1.30 nicm 34: struct client *cfg_client;
1.1 nicm 35:
1.28 nicm 36: int
37: load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
1.1 nicm 38: {
1.7 deraadt 39: FILE *f;
1.32 ! tobias 40: char delim[3] = { '\\', '\\', '\0' };
! 41: u_int found;
! 42: size_t line = 0;
! 43: char *buf, *cause1, *msg, *p;
1.1 nicm 44: struct cmd_list *cmdlist;
45:
1.29 nicm 46: log_debug("loading %s", path);
1.1 nicm 47: if ((f = fopen(path, "rb")) == NULL) {
1.28 nicm 48: xasprintf(cause, "%s: %s", path, strerror(errno));
49: return (-1);
1.25 nicm 50: }
51:
1.32 ! tobias 52: found = 0;
! 53: while ((buf = fparseln(f, NULL, &line, delim, 0))) {
! 54: log_debug("%s: %s", path, buf);
1.13 nicm 55:
1.21 nicm 56: /* Skip empty lines. */
1.32 ! tobias 57: p = buf;
! 58: while (isspace((u_char) *p))
! 59: p++;
! 60: if (*p == '\0') {
! 61: free(buf);
1.21 nicm 62: continue;
1.22 nicm 63: }
1.21 nicm 64:
1.28 nicm 65: /* Parse and run the command. */
1.32 ! tobias 66: if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
! 67: free(buf);
1.28 nicm 68: if (cause1 == NULL)
1.1 nicm 69: continue;
1.32 ! tobias 70: xasprintf(&msg, "%s:%zu: %s", path, line, cause1);
1.28 nicm 71: ARRAY_ADD(&cfg_causes, msg);
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.28 nicm 79: cmdq_append(cmdq, cmdlist);
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.17 nicm 114: }
115:
116: void
1.28 nicm 117: cfg_show_causes(struct session *s)
1.17 nicm 118: {
119: struct window_pane *wp;
120: char *cause;
121: u_int i;
122:
123: if (s == NULL || ARRAY_EMPTY(&cfg_causes))
124: return;
125: wp = s->curw->window->active;
126:
127: window_pane_set_mode(wp, &window_copy_mode);
128: window_copy_init_for_output(wp);
129: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
130: cause = ARRAY_ITEM(&cfg_causes, i);
131: window_copy_add(wp, "%s", cause);
132: free(cause);
133: }
134: ARRAY_FREE(&cfg_causes);
1.1 nicm 135: }