Annotation of src/usr.bin/tmux/cfg.c, Revision 1.23
1.23 ! nicm 1: /* $OpenBSD: cfg.c,v 1.22 2013/03/21 16:54:37 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:
30: /*
31: * Config file parser. Pretty quick and simple, each line is parsed into a
32: * argv array and executed as a command.
33: */
34:
35: void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
36: void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
37:
1.18 nicm 38: char *cfg_cause;
39: int cfg_finished;
40: int cfg_references;
41: struct causelist cfg_causes;
1.1 nicm 42:
43: void printflike2
44: cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
45: {
46: }
47:
48: void printflike2
49: cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
50: {
51: va_list ap;
52:
53: va_start(ap, fmt);
54: xvasprintf(&cfg_cause, fmt, ap);
55: va_end(ap);
56: }
57:
1.10 nicm 58: void printflike2
59: cfg_add_cause(struct causelist *causes, const char *fmt, ...)
1.9 nicm 60: {
61: char *cause;
62: va_list ap;
63:
64: va_start(ap, fmt);
65: xvasprintf(&cause, fmt, ap);
66: va_end(ap);
67:
1.10 nicm 68: ARRAY_ADD(causes, cause);
1.9 nicm 69: }
70:
71: /*
72: * Load configuration file. Returns -1 for an error with a list of messages in
1.11 nicm 73: * causes. Note that causes must be initialised by the caller!
1.9 nicm 74: */
1.19 nicm 75: enum cmd_retval
1.10 nicm 76: load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
1.1 nicm 77: {
1.7 deraadt 78: FILE *f;
1.1 nicm 79: u_int n;
1.21 nicm 80: char *buf, *copy, *line, *cause;
81: size_t len, oldlen;
1.1 nicm 82: struct cmd_list *cmdlist;
83: struct cmd_ctx ctx;
1.16 nicm 84: enum cmd_retval retval;
1.1 nicm 85:
86: if ((f = fopen(path, "rb")) == NULL) {
1.10 nicm 87: cfg_add_cause(causes, "%s: %s", path, strerror(errno));
1.19 nicm 88: return (CMD_RETURN_ERROR);
1.1 nicm 89: }
90:
1.18 nicm 91: cfg_references++;
92:
1.20 nicm 93: n = 0;
1.1 nicm 94: line = NULL;
1.16 nicm 95: retval = CMD_RETURN_NORMAL;
1.1 nicm 96: while ((buf = fgetln(f, &len))) {
1.21 nicm 97: /* Trim \n. */
1.1 nicm 98: if (buf[len - 1] == '\n')
1.13 nicm 99: len--;
1.21 nicm 100: log_debug ("%s: %s", path, buf);
1.13 nicm 101:
1.20 nicm 102: /* Current line is the continuation of the previous one. */
103: if (line != NULL) {
1.21 nicm 104: oldlen = strlen(line);
105: line = xrealloc(line, 1, oldlen + len + 1);
1.20 nicm 106: } else {
1.21 nicm 107: oldlen = 0;
108: line = xmalloc(len + 1);
1.1 nicm 109: }
1.13 nicm 110:
1.20 nicm 111: /* Append current line to the previous. */
1.21 nicm 112: memcpy(line + oldlen, buf, len);
113: line[oldlen + len] = '\0';
1.1 nicm 114: n++;
115:
1.13 nicm 116: /* Continuation: get next line? */
117: len = strlen(line);
118: if (len > 0 && line[len - 1] == '\\') {
119: line[len - 1] = '\0';
1.20 nicm 120:
1.14 nicm 121: /* Ignore escaped backslash at EOL. */
122: if (len > 1 && line[len - 2] != '\\')
123: continue;
1.13 nicm 124: }
1.21 nicm 125: copy = line;
1.13 nicm 126: line = NULL;
127:
1.21 nicm 128: /* Skip empty lines. */
129: buf = copy;
130: while (isspace((u_char)*buf))
131: buf++;
1.22 nicm 132: if (*buf == '\0') {
133: free(copy);
1.21 nicm 134: continue;
1.22 nicm 135: }
1.21 nicm 136:
1.9 nicm 137: if (cmd_string_parse(buf, &cmdlist, &cause) != 0) {
1.21 nicm 138: free(copy);
1.9 nicm 139: if (cause == NULL)
1.1 nicm 140: continue;
1.10 nicm 141: cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
1.15 nicm 142: free(cause);
1.9 nicm 143: continue;
1.20 nicm 144: }
1.21 nicm 145: free(copy);
1.1 nicm 146: if (cmdlist == NULL)
147: continue;
148:
1.6 nicm 149: if (ctxin == NULL) {
150: ctx.msgdata = NULL;
151: ctx.curclient = NULL;
152: ctx.cmdclient = NULL;
153: } else {
154: ctx.msgdata = ctxin->msgdata;
155: ctx.curclient = ctxin->curclient;
156: ctx.cmdclient = ctxin->cmdclient;
157: }
1.1 nicm 158:
159: ctx.error = cfg_error;
160: ctx.print = cfg_print;
161: ctx.info = cfg_print;
162:
163: cfg_cause = NULL;
1.16 nicm 164: switch (cmd_list_exec(cmdlist, &ctx)) {
165: case CMD_RETURN_YIELD:
166: if (retval != CMD_RETURN_ATTACH)
167: retval = CMD_RETURN_YIELD;
168: break;
169: case CMD_RETURN_ATTACH:
170: retval = CMD_RETURN_ATTACH;
171: break;
172: case CMD_RETURN_ERROR:
173: case CMD_RETURN_NORMAL:
174: break;
175: }
1.1 nicm 176: cmd_list_free(cmdlist);
177: if (cfg_cause != NULL) {
1.20 nicm 178: cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
1.15 nicm 179: free(cfg_cause);
1.1 nicm 180: }
181: }
1.13 nicm 182: if (line != NULL) {
183: cfg_add_cause(causes,
184: "%s: %d: line continuation at end of file", path, n);
1.15 nicm 185: free(line);
1.13 nicm 186: }
1.1 nicm 187: fclose(f);
1.18 nicm 188:
189: cfg_references--;
1.1 nicm 190:
1.12 nicm 191: return (retval);
1.17 nicm 192: }
193:
194: void
195: show_cfg_causes(struct session *s)
196: {
197: struct window_pane *wp;
198: char *cause;
199: u_int i;
200:
201: if (s == NULL || ARRAY_EMPTY(&cfg_causes))
202: return;
203:
204: wp = s->curw->window->active;
205:
206: window_pane_set_mode(wp, &window_copy_mode);
207: window_copy_init_for_output(wp);
208: for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
209: cause = ARRAY_ITEM(&cfg_causes, i);
210: window_copy_add(wp, "%s", cause);
211: free(cause);
212: }
213: ARRAY_FREE(&cfg_causes);
1.1 nicm 214: }