2011-07-09 09:42:33 +00:00
|
|
|
/* $Id$ */
|
2008-06-02 18:08:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2009-05-21 19:38:51 +00:00
|
|
|
#include <sys/stat.h>
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2012-12-13 15:36:16 +00:00
|
|
|
#include <ctype.h>
|
2008-06-02 18:08:17 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2012-07-11 19:34:16 +00:00
|
|
|
#include <stdlib.h>
|
2008-06-02 18:08:17 +00:00
|
|
|
#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 *, ...);
|
|
|
|
|
2012-11-27 16:12:29 +00:00
|
|
|
char *cfg_cause;
|
|
|
|
int cfg_finished;
|
|
|
|
int cfg_references;
|
|
|
|
struct causelist cfg_causes;
|
2008-06-02 18:55:53 +00:00
|
|
|
|
2009-11-28 14:50:37 +00:00
|
|
|
/* ARGSUSED */
|
2008-06-02 18:08:17 +00:00
|
|
|
void printflike2
|
|
|
|
cfg_print(unused struct cmd_ctx *ctx, unused const char *fmt, ...)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:50:37 +00:00
|
|
|
/* ARGSUSED */
|
2008-06-02 18:08:17 +00:00
|
|
|
void printflike2
|
2008-06-02 18:55:53 +00:00
|
|
|
cfg_error(unused struct cmd_ctx *ctx, const char *fmt, ...)
|
2008-06-02 18:08:17 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2008-06-02 18:55:53 +00:00
|
|
|
xvasprintf(&cfg_cause, fmt, ap);
|
2008-06-02 18:08:17 +00:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2010-02-08 18:29:32 +00:00
|
|
|
void printflike2
|
|
|
|
cfg_add_cause(struct causelist *causes, const char *fmt, ...)
|
2010-02-08 18:10:07 +00:00
|
|
|
{
|
|
|
|
char *cause;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&cause, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2010-02-08 18:29:32 +00:00
|
|
|
ARRAY_ADD(causes, cause);
|
2010-02-08 18:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load configuration file. Returns -1 for an error with a list of messages in
|
2010-06-06 00:04:18 +00:00
|
|
|
* causes. Note that causes must be initialised by the caller!
|
2010-02-08 18:10:07 +00:00
|
|
|
*/
|
2012-11-27 22:59:34 +00:00
|
|
|
enum cmd_retval
|
2010-02-08 18:29:32 +00:00
|
|
|
load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes)
|
2008-06-02 18:08:17 +00:00
|
|
|
{
|
2009-10-28 23:12:38 +00:00
|
|
|
FILE *f;
|
2009-01-18 14:40:48 +00:00
|
|
|
u_int n;
|
2012-12-13 15:36:16 +00:00
|
|
|
char *buf, *copy, *line, *cause;
|
|
|
|
size_t len, oldlen;
|
2009-01-18 14:40:48 +00:00
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct cmd_ctx ctx;
|
2012-07-11 19:37:32 +00:00
|
|
|
enum cmd_retval retval;
|
2008-06-02 18:08:17 +00:00
|
|
|
|
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2010-02-08 18:29:32 +00:00
|
|
|
cfg_add_cause(causes, "%s: %s", path, strerror(errno));
|
2012-11-27 22:59:34 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
2008-06-19 21:13:56 +00:00
|
|
|
|
2012-11-27 16:12:29 +00:00
|
|
|
cfg_references++;
|
|
|
|
|
2012-12-06 13:06:05 +00:00
|
|
|
n = 0;
|
2008-06-19 21:13:56 +00:00
|
|
|
line = NULL;
|
2012-07-11 19:37:32 +00:00
|
|
|
retval = CMD_RETURN_NORMAL;
|
2008-06-19 21:13:56 +00:00
|
|
|
while ((buf = fgetln(f, &len))) {
|
2012-12-13 15:36:16 +00:00
|
|
|
/* Trim \n. */
|
2008-06-19 21:13:56 +00:00
|
|
|
if (buf[len - 1] == '\n')
|
2011-08-25 21:14:23 +00:00
|
|
|
len--;
|
2012-12-13 15:36:16 +00:00
|
|
|
log_debug ("%s: %s", path, buf);
|
2011-08-25 21:14:23 +00:00
|
|
|
|
2012-12-06 13:06:05 +00:00
|
|
|
/* Current line is the continuation of the previous one. */
|
|
|
|
if (line != NULL) {
|
2012-12-13 15:36:16 +00:00
|
|
|
oldlen = strlen(line);
|
|
|
|
line = xrealloc(line, 1, oldlen + len + 1);
|
2012-12-06 13:06:05 +00:00
|
|
|
} else {
|
2012-12-13 15:36:16 +00:00
|
|
|
oldlen = 0;
|
|
|
|
line = xmalloc(len + 1);
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
2011-08-25 21:14:23 +00:00
|
|
|
|
2012-12-06 13:06:05 +00:00
|
|
|
/* Append current line to the previous. */
|
2012-12-13 15:36:16 +00:00
|
|
|
memcpy(line + oldlen, buf, len);
|
|
|
|
line[oldlen + len] = '\0';
|
2008-06-19 21:13:56 +00:00
|
|
|
n++;
|
|
|
|
|
2011-08-25 21:14:23 +00:00
|
|
|
/* Continuation: get next line? */
|
|
|
|
len = strlen(line);
|
|
|
|
if (len > 0 && line[len - 1] == '\\') {
|
|
|
|
line[len - 1] = '\0';
|
2012-12-06 13:06:05 +00:00
|
|
|
|
2012-05-22 20:50:51 +00:00
|
|
|
/* Ignore escaped backslash at EOL. */
|
|
|
|
if (len > 1 && line[len - 2] != '\\')
|
|
|
|
continue;
|
2011-08-25 21:14:23 +00:00
|
|
|
}
|
2012-12-13 15:36:16 +00:00
|
|
|
copy = line;
|
2011-08-25 21:14:23 +00:00
|
|
|
line = NULL;
|
|
|
|
|
2012-12-13 15:36:16 +00:00
|
|
|
/* Skip empty lines. */
|
|
|
|
buf = copy;
|
|
|
|
while (isspace((u_char)*buf))
|
|
|
|
buf++;
|
|
|
|
if (*buf == '\0')
|
|
|
|
continue;
|
|
|
|
|
2010-02-08 18:10:07 +00:00
|
|
|
if (cmd_string_parse(buf, &cmdlist, &cause) != 0) {
|
2012-12-13 15:36:16 +00:00
|
|
|
free(copy);
|
2010-02-08 18:10:07 +00:00
|
|
|
if (cause == NULL)
|
2008-06-19 21:20:27 +00:00
|
|
|
continue;
|
2010-02-08 18:29:32 +00:00
|
|
|
cfg_add_cause(causes, "%s: %u: %s", path, n, cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cause);
|
2010-02-08 18:10:07 +00:00
|
|
|
continue;
|
2012-12-06 13:06:05 +00:00
|
|
|
}
|
2012-12-13 15:36:16 +00:00
|
|
|
free(copy);
|
2009-01-18 14:40:48 +00:00
|
|
|
if (cmdlist == NULL)
|
2008-07-25 17:20:40 +00:00
|
|
|
continue;
|
2008-06-02 18:55:53 +00:00
|
|
|
|
2009-08-24 16:27:03 +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;
|
|
|
|
}
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2008-06-19 21:13:56 +00:00
|
|
|
ctx.error = cfg_error;
|
|
|
|
ctx.print = cfg_print;
|
|
|
|
ctx.info = cfg_print;
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2008-06-19 21:13:56 +00:00
|
|
|
cfg_cause = NULL;
|
2012-07-11 19:37:32 +00:00
|
|
|
switch (cmd_list_exec(cmdlist, &ctx)) {
|
|
|
|
case CMD_RETURN_YIELD:
|
|
|
|
if (retval != CMD_RETURN_ATTACH)
|
|
|
|
retval = CMD_RETURN_YIELD;
|
|
|
|
break;
|
|
|
|
case CMD_RETURN_ATTACH:
|
|
|
|
retval = CMD_RETURN_ATTACH;
|
|
|
|
break;
|
|
|
|
case CMD_RETURN_ERROR:
|
|
|
|
case CMD_RETURN_NORMAL:
|
|
|
|
break;
|
|
|
|
}
|
2009-01-18 14:40:48 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2008-06-19 21:13:56 +00:00
|
|
|
if (cfg_cause != NULL) {
|
2012-12-06 13:06:05 +00:00
|
|
|
cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(cfg_cause);
|
2008-06-19 21:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-25 21:14:23 +00:00
|
|
|
if (line != NULL) {
|
|
|
|
cfg_add_cause(causes,
|
|
|
|
"%s: %d: line continuation at end of file", path, n);
|
2012-07-11 19:34:16 +00:00
|
|
|
free(line);
|
2011-08-25 21:14:23 +00:00
|
|
|
}
|
2009-03-31 22:23:43 +00:00
|
|
|
fclose(f);
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2012-11-27 16:12:29 +00:00
|
|
|
cfg_references--;
|
|
|
|
|
2010-12-30 22:26:07 +00:00
|
|
|
return (retval);
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
2012-11-19 10:38:06 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
show_cfg_causes(struct session *s)
|
|
|
|
{
|
|
|
|
struct window_pane *wp;
|
|
|
|
char *cause;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
if (s == NULL || ARRAY_EMPTY(&cfg_causes))
|
|
|
|
return;
|
|
|
|
|
|
|
|
wp = s->curw->window->active;
|
|
|
|
|
|
|
|
window_pane_set_mode(wp, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(wp);
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
|
|
|
|
cause = ARRAY_ITEM(&cfg_causes, i);
|
|
|
|
window_copy_add(wp, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
}
|
|
|
|
ARRAY_FREE(&cfg_causes);
|
|
|
|
}
|