2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2008-06-02 20:08:17 +02: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 21:38:51 +02:00
|
|
|
#include <sys/stat.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
|
2012-12-13 16:36:16 +01:00
|
|
|
#include <ctype.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2008-06-02 20:08:17 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cfg_cmd_q;
|
2012-11-27 17:12:29 +01:00
|
|
|
int cfg_finished;
|
2013-02-23 23:25:58 +01:00
|
|
|
int cfg_references;
|
2012-11-27 17:12:29 +01:00
|
|
|
struct causelist cfg_causes;
|
2008-06-02 20:55:53 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
int
|
|
|
|
load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
|
2008-06-02 20:08:17 +02:00
|
|
|
{
|
2009-10-29 00:12:38 +01:00
|
|
|
FILE *f;
|
2013-02-23 23:25:58 +01:00
|
|
|
u_int n, found;
|
|
|
|
char *buf, *copy, *line, *cause1, *msg;
|
2012-12-13 16:36:16 +01:00
|
|
|
size_t len, oldlen;
|
2009-01-18 15:40:48 +01:00
|
|
|
struct cmd_list *cmdlist;
|
2008-06-02 20:08:17 +02:00
|
|
|
|
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
xasprintf(cause, "%s: %s", path, strerror(errno));
|
|
|
|
return (-1);
|
2013-02-19 00:20:21 +01:00
|
|
|
}
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
n = found = 0;
|
2008-06-19 23:13:56 +02:00
|
|
|
line = NULL;
|
|
|
|
while ((buf = fgetln(f, &len))) {
|
2012-12-13 16:36:16 +01:00
|
|
|
/* Trim \n. */
|
2008-06-19 23:13:56 +02:00
|
|
|
if (buf[len - 1] == '\n')
|
2011-08-25 23:14:23 +02:00
|
|
|
len--;
|
2013-02-19 18:28:21 +01:00
|
|
|
log_debug("%s: %.*s", path, (int)len, buf);
|
2011-08-25 23:14:23 +02:00
|
|
|
|
2012-12-06 14:06:05 +01:00
|
|
|
/* Current line is the continuation of the previous one. */
|
|
|
|
if (line != NULL) {
|
2012-12-13 16:36:16 +01:00
|
|
|
oldlen = strlen(line);
|
|
|
|
line = xrealloc(line, 1, oldlen + len + 1);
|
2012-12-06 14:06:05 +01:00
|
|
|
} else {
|
2012-12-13 16:36:16 +01:00
|
|
|
oldlen = 0;
|
|
|
|
line = xmalloc(len + 1);
|
2008-06-02 20:08:17 +02:00
|
|
|
}
|
2011-08-25 23:14:23 +02:00
|
|
|
|
2012-12-06 14:06:05 +01:00
|
|
|
/* Append current line to the previous. */
|
2012-12-13 16:36:16 +01:00
|
|
|
memcpy(line + oldlen, buf, len);
|
|
|
|
line[oldlen + len] = '\0';
|
2008-06-19 23:13:56 +02:00
|
|
|
n++;
|
|
|
|
|
2011-08-25 23:14:23 +02:00
|
|
|
/* Continuation: get next line? */
|
|
|
|
len = strlen(line);
|
|
|
|
if (len > 0 && line[len - 1] == '\\') {
|
|
|
|
line[len - 1] = '\0';
|
2012-12-06 14:06:05 +01:00
|
|
|
|
2012-05-22 22:50:51 +02:00
|
|
|
/* Ignore escaped backslash at EOL. */
|
|
|
|
if (len > 1 && line[len - 2] != '\\')
|
|
|
|
continue;
|
2011-08-25 23:14:23 +02:00
|
|
|
}
|
2012-12-13 16:36:16 +01:00
|
|
|
copy = line;
|
2011-08-25 23:14:23 +02:00
|
|
|
line = NULL;
|
|
|
|
|
2012-12-13 16:36:16 +01:00
|
|
|
/* Skip empty lines. */
|
|
|
|
buf = copy;
|
|
|
|
while (isspace((u_char)*buf))
|
|
|
|
buf++;
|
2013-02-16 20:35:49 +01:00
|
|
|
if (*buf == '\0') {
|
|
|
|
free(copy);
|
2012-12-13 16:36:16 +01:00
|
|
|
continue;
|
2013-02-16 20:35:49 +01:00
|
|
|
}
|
2012-12-13 16:36:16 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
/* Parse and run the command. */
|
|
|
|
if (cmd_string_parse(buf, &cmdlist, path, n, &cause1) != 0) {
|
2012-12-13 16:36:16 +01:00
|
|
|
free(copy);
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cause1 == NULL)
|
2008-06-19 23:20:27 +02:00
|
|
|
continue;
|
2013-02-23 23:25:58 +01:00
|
|
|
xasprintf(&msg, "%s:%u: %s", path, n, cause1);
|
|
|
|
ARRAY_ADD(&cfg_causes, msg);
|
|
|
|
free(cause1);
|
2010-02-08 19:10:07 +01:00
|
|
|
continue;
|
2012-12-06 14:06:05 +01:00
|
|
|
}
|
2012-12-13 16:36:16 +01:00
|
|
|
free(copy);
|
2013-02-23 23:25:58 +01:00
|
|
|
|
2009-01-18 15:40:48 +01:00
|
|
|
if (cmdlist == NULL)
|
2008-07-25 19:20:40 +02:00
|
|
|
continue;
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_append(cmdq, cmdlist);
|
2009-01-18 15:40:48 +01:00
|
|
|
cmd_list_free(cmdlist);
|
2013-02-23 23:25:58 +01:00
|
|
|
found++;
|
2008-06-19 23:13:56 +02:00
|
|
|
}
|
2013-02-23 23:25:58 +01:00
|
|
|
if (line != NULL)
|
2012-07-11 21:34:16 +02:00
|
|
|
free(line);
|
2009-04-01 00:23:43 +02:00
|
|
|
fclose(f);
|
2008-06-02 20:08:17 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cfg_default_done(unused struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
if (--cfg_references != 0)
|
|
|
|
return;
|
|
|
|
cfg_finished = 1;
|
2013-02-19 00:20:21 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
cfg_show_causes(RB_MIN(sessions, &sessions));
|
2012-11-27 17:12:29 +01:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_free(cfg_cmd_q);
|
|
|
|
cfg_cmd_q = NULL;
|
2008-06-02 20:08:17 +02:00
|
|
|
}
|
2012-11-19 11:38:06 +01:00
|
|
|
|
|
|
|
void
|
2013-02-23 23:25:58 +01:00
|
|
|
cfg_show_causes(struct session *s)
|
2012-11-19 11:38:06 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|