Annotation of src/usr.bin/tmux/cfg.c, Revision 1.56
1.56 ! nicm 1: /* $OpenBSD: cfg.c,v 1.55 2017/04/05 10:45:39 nicm Exp $ */
1.1 nicm 2:
3: /*
1.44 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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.40 nicm 26: #include <unistd.h>
1.32 tobias 27: #include <util.h>
1.1 nicm 28:
29: #include "tmux.h"
30:
1.55 nicm 31: static char *cfg_file;
1.51 nicm 32: int cfg_finished;
33: static char **cfg_causes;
34: static u_int cfg_ncauses;
1.40 nicm 35:
1.49 nicm 36: static enum cmd_retval
1.50 nicm 37: cfg_done(__unused struct cmdq_item *item, __unused void *data)
1.49 nicm 38: {
39: if (cfg_finished)
40: return (CMD_RETURN_NORMAL);
41: cfg_finished = 1;
42:
43: if (!RB_EMPTY(&sessions))
44: cfg_show_causes(RB_MIN(sessions, &sessions));
45:
46: return (CMD_RETURN_NORMAL);
47: }
1.40 nicm 48:
49: void
1.41 nicm 50: set_cfg_file(const char *path)
51: {
52: free(cfg_file);
53: cfg_file = xstrdup(path);
54: }
55:
56: void
1.40 nicm 57: start_cfg(void)
58: {
1.41 nicm 59: const char *home;
1.45 tim 60: int quiet = 0;
1.40 nicm 61:
1.56 ! nicm 62: /*
! 63: * Note that the configuration files are loaded without a client, so
! 64: * NULL is passed into load_cfg() which means that commands run in the
! 65: * global queue and item->client is NULL for all commands.
! 66: */
1.40 nicm 67:
1.56 ! nicm 68: load_cfg(TMUX_CONF, NULL, NULL, 1);
1.40 nicm 69:
1.41 nicm 70: if (cfg_file == NULL && (home = find_home()) != NULL) {
71: xasprintf(&cfg_file, "%s/.tmux.conf", home);
1.45 tim 72: quiet = 1;
1.41 nicm 73: }
1.45 tim 74: if (cfg_file != NULL)
1.56 ! nicm 75: load_cfg(cfg_file, NULL, NULL, quiet);
1.40 nicm 76:
1.56 ! nicm 77: cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
1.40 nicm 78: }
1.1 nicm 79:
1.28 nicm 80: int
1.50 nicm 81: load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet)
1.1 nicm 82: {
1.50 nicm 83: FILE *f;
1.52 nicm 84: const char delim[3] = { '\\', '\\', '\0' };
85: u_int found = 0;
1.50 nicm 86: size_t line = 0;
1.52 nicm 87: char *buf, *cause1, *p, *q, *s;
1.50 nicm 88: struct cmd_list *cmdlist;
89: struct cmdq_item *new_item;
1.52 nicm 90: int condition = 0;
91: struct format_tree *ft;
1.1 nicm 92:
1.29 nicm 93: log_debug("loading %s", path);
1.1 nicm 94: if ((f = fopen(path, "rb")) == NULL) {
1.45 tim 95: if (errno == ENOENT && quiet)
96: return (0);
97: cfg_add_cause("%s: %s", path, strerror(errno));
1.28 nicm 98: return (-1);
1.25 nicm 99: }
100:
1.42 nicm 101: while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) {
1.32 tobias 102: log_debug("%s: %s", path, buf);
1.13 nicm 103:
1.32 tobias 104: p = buf;
1.52 nicm 105: while (isspace((u_char)*p))
1.32 tobias 106: p++;
107: if (*p == '\0') {
108: free(buf);
1.21 nicm 109: continue;
1.22 nicm 110: }
1.52 nicm 111: q = p + strlen(p) - 1;
112: while (q != p && isspace((u_char)*q))
113: *q-- = '\0';
114:
115: if (condition != 0 && strcmp(p, "%endif") == 0) {
116: condition = 0;
117: continue;
118: }
119: if (strncmp(p, "%if ", 4) == 0) {
120: if (condition != 0) {
121: cfg_add_cause("%s:%zu: nested %%if", path,
122: line);
123: continue;
124: }
1.54 nicm 125: ft = format_create(NULL, FORMAT_NONE, FORMAT_NOJOBS);
1.52 nicm 126:
127: s = p + 3;
128: while (isspace((u_char)*s))
129: s++;
130: s = format_expand(ft, s);
131: if (*s != '\0' && (s[0] != '0' || s[1] != '\0'))
132: condition = 1;
133: else
134: condition = -1;
135: free(s);
136:
137: format_free(ft);
138: continue;
139: }
140: if (condition == -1)
141: continue;
1.21 nicm 142:
1.53 nicm 143: cmdlist = cmd_string_parse(p, path, line, &cause1);
144: if (cmdlist == NULL) {
1.32 tobias 145: free(buf);
1.28 nicm 146: if (cause1 == NULL)
1.1 nicm 147: continue;
1.33 nicm 148: cfg_add_cause("%s:%zu: %s", path, line, cause1);
1.28 nicm 149: free(cause1);
1.9 nicm 150: continue;
1.20 nicm 151: }
1.32 tobias 152: free(buf);
1.28 nicm 153:
1.1 nicm 154: if (cmdlist == NULL)
155: continue;
1.50 nicm 156: new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
157: if (item != NULL)
158: cmdq_insert_after(item, new_item);
1.49 nicm 159: else
1.50 nicm 160: cmdq_append(c, new_item);
1.1 nicm 161: cmd_list_free(cmdlist);
1.49 nicm 162:
1.28 nicm 163: found++;
1.1 nicm 164: }
165: fclose(f);
1.25 nicm 166:
1.28 nicm 167: return (found);
1.33 nicm 168: }
169:
170: void
1.36 nicm 171: cfg_add_cause(const char *fmt, ...)
1.33 nicm 172: {
1.36 nicm 173: va_list ap;
174: char *msg;
1.33 nicm 175:
176: va_start(ap, fmt);
177: xvasprintf(&msg, fmt, ap);
1.38 nicm 178: va_end(ap);
1.33 nicm 179:
1.35 nicm 180: cfg_ncauses++;
181: cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
182: cfg_causes[cfg_ncauses - 1] = msg;
1.33 nicm 183: }
184:
185: void
1.50 nicm 186: cfg_print_causes(struct cmdq_item *item)
1.33 nicm 187: {
188: u_int i;
189:
1.35 nicm 190: for (i = 0; i < cfg_ncauses; i++) {
1.50 nicm 191: cmdq_print(item, "%s", cfg_causes[i]);
1.35 nicm 192: free(cfg_causes[i]);
1.33 nicm 193: }
1.35 nicm 194:
195: free(cfg_causes);
196: cfg_causes = NULL;
1.37 nicm 197: cfg_ncauses = 0;
1.17 nicm 198: }
199:
200: void
1.28 nicm 201: cfg_show_causes(struct session *s)
1.17 nicm 202: {
203: struct window_pane *wp;
204: u_int i;
205:
1.35 nicm 206: if (s == NULL || cfg_ncauses == 0)
1.17 nicm 207: return;
208: wp = s->curw->window->active;
209:
210: window_pane_set_mode(wp, &window_copy_mode);
211: window_copy_init_for_output(wp);
1.35 nicm 212: for (i = 0; i < cfg_ncauses; i++) {
213: window_copy_add(wp, "%s", cfg_causes[i]);
214: free(cfg_causes[i]);
1.17 nicm 215: }
1.35 nicm 216:
217: free(cfg_causes);
218: cfg_causes = NULL;
1.37 nicm 219: cfg_ncauses = 0;
1.1 nicm 220: }