Annotation of src/usr.bin/tmux/cfg.c, Revision 1.8
1.8 ! nicm 1: /* $OpenBSD: cfg.c,v 1.7 2009/10/26 21:42:04 deraadt 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:
22: #include <errno.h>
23: #include <stdio.h>
24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: /*
29: * Config file parser. Pretty quick and simple, each line is parsed into a
30: * argv array and executed as a command.
31: */
32:
33: void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
34: void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
35:
36: char *cfg_cause;
37:
1.8 ! nicm 38: /* ARGSUSED */
1.1 nicm 39: void printflike2
40: cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
41: {
42: }
43:
1.8 ! nicm 44: /* ARGSUSED */
1.1 nicm 45: void printflike2
46: cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
47: {
48: va_list ap;
49:
50: va_start(ap, fmt);
51: xvasprintf(&cfg_cause, fmt, ap);
52: va_end(ap);
53: }
54:
55: int
1.6 nicm 56: load_cfg(const char *path, struct cmd_ctx *ctxin, char **cause)
1.1 nicm 57: {
1.7 deraadt 58: FILE *f;
1.1 nicm 59: u_int n;
1.7 deraadt 60: char *buf, *line, *ptr;
1.1 nicm 61: size_t len;
62: struct cmd_list *cmdlist;
63: struct cmd_ctx ctx;
64:
65: if ((f = fopen(path, "rb")) == NULL) {
66: xasprintf(cause, "%s: %s", path, strerror(errno));
67: return (1);
68: }
69: n = 0;
70:
71: line = NULL;
72: while ((buf = fgetln(f, &len))) {
73: if (buf[len - 1] == '\n')
74: buf[len - 1] = '\0';
75: else {
76: line = xrealloc(line, 1, len + 1);
77: memcpy(line, buf, len);
78: line[len] = '\0';
79: buf = line;
80: }
81: n++;
82:
83: if (cmd_string_parse(buf, &cmdlist, cause) != 0) {
84: if (*cause == NULL)
85: continue;
86: goto error;
87: }
88: if (cmdlist == NULL)
89: continue;
90: cfg_cause = NULL;
91:
1.6 nicm 92: if (ctxin == NULL) {
93: ctx.msgdata = NULL;
94: ctx.curclient = NULL;
95: ctx.cmdclient = NULL;
96: } else {
97: ctx.msgdata = ctxin->msgdata;
98: ctx.curclient = ctxin->curclient;
99: ctx.cmdclient = ctxin->cmdclient;
100: }
1.1 nicm 101:
102: ctx.error = cfg_error;
103: ctx.print = cfg_print;
104: ctx.info = cfg_print;
105:
106: cfg_cause = NULL;
107: cmd_list_exec(cmdlist, &ctx);
108: cmd_list_free(cmdlist);
109: if (cfg_cause != NULL) {
110: *cause = cfg_cause;
111: goto error;
112: }
113: }
114: if (line != NULL)
115: xfree(line);
116: fclose(f);
117:
118: return (0);
119:
120: error:
1.4 nicm 121: if (line != NULL)
122: xfree(line);
1.1 nicm 123: fclose(f);
124:
125: xasprintf(&ptr, "%s: %s at line %u", path, *cause, n);
126: xfree(*cause);
127: *cause = ptr;
128: return (1);
129: }