Use C-e and C-y for scrolling in vi mode, from Micah Cowan.
[tmux-openbsd.git] / cfg.c
blob05588336a4e891e0405f86a6efff22bfac43ef9d
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "tmux.h"
29 * Config file parser. Pretty quick and simple, each line is parsed into a
30 * argv array and executed as a command.
33 void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
34 void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
36 char *cfg_cause;
38 /* ARGSUSED */
39 void printflike2
40 cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
44 /* ARGSUSED */
45 void printflike2
46 cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
48 va_list ap;
50 va_start(ap, fmt);
51 xvasprintf(&cfg_cause, fmt, ap);
52 va_end(ap);
55 int
56 load_cfg(const char *path, struct cmd_ctx *ctxin, char **cause)
58 FILE *f;
59 u_int n;
60 char *buf, *line, *ptr;
61 size_t len;
62 struct cmd_list *cmdlist;
63 struct cmd_ctx ctx;
65 if ((f = fopen(path, "rb")) == NULL) {
66 xasprintf(cause, "%s: %s", path, strerror(errno));
67 return (1);
69 n = 0;
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;
81 n++;
83 if (cmd_string_parse(buf, &cmdlist, cause) != 0) {
84 if (*cause == NULL)
85 continue;
86 goto error;
88 if (cmdlist == NULL)
89 continue;
90 cfg_cause = NULL;
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;
102 ctx.error = cfg_error;
103 ctx.print = cfg_print;
104 ctx.info = cfg_print;
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;
114 if (line != NULL)
115 xfree(line);
116 fclose(f);
118 return (0);
120 error:
121 if (line != NULL)
122 xfree(line);
123 fclose(f);
125 xasprintf(&ptr, "%s: %s at line %u", path, *cause, n);
126 xfree(*cause);
127 *cause = ptr;
128 return (1);