Annotation of src/usr.bin/tmux/cfg.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: cfg.c,v 1.27 2013/03/22 15:54:29 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: #include <sys/stat.h>
21:
1.21 nicm 22: #include <ctype.h>
1.1 nicm 23: #include <errno.h>
24: #include <stdio.h>
1.15 nicm 25: #include <stdlib.h>
1.1 nicm 26: #include <string.h>
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.1 nicm 34:
1.28 ! nicm 35: int
! 36: load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
1.1 nicm 37: {
1.7 deraadt 38: FILE *f;
1.28 ! nicm 39: u_int n, found;
! 40: char *buf, *copy, *line, *cause1, *msg;
1.21 nicm 41: size_t len, oldlen;
1.1 nicm 42: struct cmd_list *cmdlist;
43:
44: if ((f = fopen(path, "rb")) == NULL) {
1.28 ! nicm 45: xasprintf(cause, "%s: %s", path, strerror(errno));
! 46: return (-1);
1.25 nicm 47: }
48:
1.28 ! nicm 49: n = found = 0;
1.1 nicm 50: line = NULL;
51: while ((buf = fgetln(f, &len))) {
1.21 nicm 52: /* Trim \n. */
1.1 nicm 53: if (buf[len - 1] == '\n')
1.13 nicm 54: len--;
1.24 nicm 55: log_debug("%s: %.*s", path, (int)len, buf);
1.13 nicm 56:
1.20 nicm 57: /* Current line is the continuation of the previous one. */
58: if (line != NULL) {
1.21 nicm 59: oldlen = strlen(line);
60: line = xrealloc(line, 1, oldlen + len + 1);
1.20 nicm 61: } else {
1.21 nicm 62: oldlen = 0;
63: line = xmalloc(len + 1);
1.1 nicm 64: }
1.13 nicm 65:
1.20 nicm 66: /* Append current line to the previous. */
1.21 nicm 67: memcpy(line + oldlen, buf, len);
68: line[oldlen + len] = '\0';
1.1 nicm 69: n++;
70:
1.13 nicm 71: /* Continuation: get next line? */
72: len = strlen(line);
73: if (len > 0 && line[len - 1] == '\\') {
74: line[len - 1] = '\0';
1.20 nicm 75:
1.14 nicm 76: /* Ignore escaped backslash at EOL. */
77: if (len > 1 && line[len - 2] != '\\')
78: continue;
1.13 nicm 79: }
1.21 nicm 80: copy = line;
1.13 nicm 81: line = NULL;
82:
1.21 nicm 83: /* Skip empty lines. */
84: buf = copy;
85: while (isspace((u_char)*buf))
86: buf++;
1.22 nicm 87: if (*buf == '\0') {
88: free(copy);
1.21 nicm 89: continue;
1.22 nicm 90: }
1.21 nicm 91:
1.28 ! nicm 92: /* Parse and run the command. */
! 93: if (cmd_string_parse(buf, &cmdlist, path, n, &cause1) != 0) {
1.21 nicm 94: free(copy);
1.28 ! nicm 95: if (cause1 == NULL)
1.1 nicm 96: continue;
1.28 ! nicm 97: xasprintf(&msg, "%s:%u: %s", path, n, cause1);
! 98: ARRAY_ADD(&cfg_causes, msg);
! 99: free(cause1);
1.9 nicm 100: continue;
1.20 nicm 101: }
1.21 nicm 102: free(copy);
1.28 ! nicm 103:
1.1 nicm 104: if (cmdlist == NULL)
105: continue;
1.28 ! nicm 106: cmdq_append(cmdq, cmdlist);
1.1 nicm 107: cmd_list_free(cmdlist);
1.28 ! nicm 108: found++;
1.1 nicm 109: }
1.28 ! nicm 110: if (line != NULL)
1.15 nicm 111: free(line);
1.1 nicm 112: fclose(f);
1.25 nicm 113:
1.28 ! nicm 114: return (found);
! 115: }
! 116:
! 117: void
! 118: cfg_default_done(unused struct cmd_q *cmdq)
! 119: {
! 120: if (--cfg_references != 0)
! 121: return;
! 122: cfg_finished = 1;
1.18 nicm 123:
1.28 ! nicm 124: if (!RB_EMPTY(&sessions))
! 125: cfg_show_causes(RB_MIN(sessions, &sessions));
1.1 nicm 126:
1.28 ! nicm 127: cmdq_free(cfg_cmd_q);
! 128: cfg_cmd_q = NULL;
1.17 nicm 129: }
130:
131: void
1.28 ! nicm 132: cfg_show_causes(struct session *s)
1.17 nicm 133: {
134: struct window_pane *wp;
135: char *cause;
136: u_int i;
137:
138: if (s == NULL || ARRAY_EMPTY(&cfg_causes))
139: return;
140: wp = s->curw->window->active;
141:
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);
147: free(cause);
148: }
149: ARRAY_FREE(&cfg_causes);
1.1 nicm 150: }