2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Config file parser. Pretty quick and simple, each line is parsed into a
|
|
|
|
* argv array and executed as a command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void printflike2 cfg_print(struct cmd_ctx *, const char *, ...);
|
|
|
|
void printflike2 cfg_error(struct cmd_ctx *, const char *, ...);
|
|
|
|
|
2010-02-06 23:22:27 +00:00
|
|
|
char *cfg_cause;
|
|
|
|
int cfg_finished;
|
|
|
|
struct causelist cfg_causes = ARRAY_INITIALIZER;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-06-01 22:58:49 +00:00
|
|
|
void printflike2
|
|
|
|
cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-26 21:37:13 +00:00
|
|
|
/* ARGSUSED */
|
2009-06-01 22:58:49 +00:00
|
|
|
void printflike2
|
|
|
|
cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&cfg_cause, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-02-06 23:22:27 +00:00
|
|
|
void printflike2
|
|
|
|
cfg_add_cause(struct causelist *causes, const char *fmt, ...)
|
2010-02-06 17:15:33 +00:00
|
|
|
{
|
|
|
|
char *cause;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&cause, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2010-02-06 23:22:27 +00:00
|
|
|
ARRAY_ADD(causes, cause);
|
2010-02-06 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load configuration file. Returns -1 for an error with a list of messages in
|
2010-05-25 19:47:30 +00:00
|
|
|
* causes. Note that causes must be initialised by the caller!
|
2010-02-06 17:15:33 +00:00
|
|
|
*/
|
2009-06-01 22:58:49 +00:00
|
|
|
int
|
2010-02-06 23:22:27 +00:00
|
|
|
load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-10-26 21:42:04 +00:00
|
|
|
FILE *f;
|
2009-06-01 22:58:49 +00:00
|
|
|
u_int n;
|
2010-02-06 17:15:33 +00:00
|
|
|
char *buf, *line, *cause;
|
2009-06-01 22:58:49 +00:00
|
|
|
size_t len;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmd_ctx ctx;
|
|
|
|
|
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2010-02-06 23:22:27 +00:00
|
|
|
cfg_add_cause(causes, "%s: %s", path, strerror(errno));
|
2010-02-06 17:15:33 +00:00
|
|
|
return (-1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
line = NULL;
|
|
|
|
while ((buf = fgetln(f, &len))) {
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
else {
|
|
|
|
line = xrealloc(line, 1, len + 1);
|
|
|
|
memcpy(line, buf, len);
|
|
|
|
line[len] = '\0';
|
|
|
|
buf = line;
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
|
2010-02-06 17:15:33 +00:00
|
|
|
if (cmd_string_parse(buf, &cmdlist, &cause) != 0) {
|
|
|
|
if (cause == NULL)
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
2010-02-06 23:22:27 +00:00
|
|
|
cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
|
2010-02-06 17:15:33 +00:00
|
|
|
xfree(cause);
|
|
|
|
continue;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
if (cmdlist == NULL)
|
|
|
|
continue;
|
|
|
|
cfg_cause = NULL;
|
|
|
|
|
2009-08-23 17:29:51 +00:00
|
|
|
if (ctxin == NULL) {
|
|
|
|
ctx.msgdata = NULL;
|
|
|
|
ctx.curclient = NULL;
|
|
|
|
ctx.cmdclient = NULL;
|
|
|
|
} else {
|
|
|
|
ctx.msgdata = ctxin->msgdata;
|
|
|
|
ctx.curclient = ctxin->curclient;
|
|
|
|
ctx.cmdclient = ctxin->cmdclient;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
ctx.error = cfg_error;
|
|
|
|
ctx.print = cfg_print;
|
|
|
|
ctx.info = cfg_print;
|
|
|
|
|
|
|
|
cfg_cause = NULL;
|
|
|
|
cmd_list_exec(cmdlist, &ctx);
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
if (cfg_cause != NULL) {
|
2010-02-06 23:22:27 +00:00
|
|
|
cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
|
2010-02-06 17:15:33 +00:00
|
|
|
xfree(cfg_cause);
|
|
|
|
continue;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (line != NULL)
|
|
|
|
xfree(line);
|
|
|
|
fclose(f);
|
|
|
|
|
2010-02-06 23:22:27 +00:00
|
|
|
if (ARRAY_LENGTH(causes) != 0)
|
2010-02-06 17:15:33 +00:00
|
|
|
return (-1);
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
|
|
|
}
|