2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
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>
|
|
|
|
|
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"
|
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
struct cmd_q *cfg_cmd_q;
|
2012-11-27 16:12:29 +00:00
|
|
|
int cfg_finished;
|
2013-02-23 22:25:58 +00:00
|
|
|
int cfg_references;
|
2014-10-27 22:23:47 +00:00
|
|
|
ARRAY_DECL (, char *) cfg_causes = ARRAY_INITIALIZER;
|
2013-10-20 17:28:43 +00:00
|
|
|
struct client *cfg_client;
|
2008-06-02 18:55:53 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
int
|
|
|
|
load_cfg(const char *path, struct cmd_q *cmdq, char **cause)
|
2008-06-02 18:08:17 +00:00
|
|
|
{
|
2009-10-28 23:12:38 +00:00
|
|
|
FILE *f;
|
2014-06-25 19:17:27 +00:00
|
|
|
char delim[3] = { '\\', '\\', '\0' };
|
|
|
|
u_int found;
|
|
|
|
size_t line = 0;
|
2014-10-27 22:23:47 +00:00
|
|
|
char *buf, *cause1, *p;
|
2009-01-18 14:40:48 +00:00
|
|
|
struct cmd_list *cmdlist;
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2013-03-05 17:17:59 +00:00
|
|
|
log_debug("loading %s", path);
|
2008-06-02 18:08:17 +00:00
|
|
|
if ((f = fopen(path, "rb")) == NULL) {
|
2013-02-23 22:25:58 +00:00
|
|
|
xasprintf(cause, "%s: %s", path, strerror(errno));
|
|
|
|
return (-1);
|
2013-02-18 23:20:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 19:17:27 +00:00
|
|
|
found = 0;
|
|
|
|
while ((buf = fparseln(f, NULL, &line, delim, 0))) {
|
|
|
|
log_debug("%s: %s", path, buf);
|
2011-08-25 21:14:23 +00:00
|
|
|
|
2012-12-13 15:36:16 +00:00
|
|
|
/* Skip empty lines. */
|
2014-06-25 19:17:27 +00:00
|
|
|
p = buf;
|
|
|
|
while (isspace((u_char) *p))
|
|
|
|
p++;
|
|
|
|
if (*p == '\0') {
|
|
|
|
free(buf);
|
2012-12-13 15:36:16 +00:00
|
|
|
continue;
|
2013-02-16 19:35:49 +00:00
|
|
|
}
|
2012-12-13 15:36:16 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
/* Parse and run the command. */
|
2014-06-25 19:17:27 +00:00
|
|
|
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) {
|
|
|
|
free(buf);
|
2013-02-23 22:25:58 +00:00
|
|
|
if (cause1 == NULL)
|
2008-06-19 21:20:27 +00:00
|
|
|
continue;
|
2014-10-27 22:23:47 +00:00
|
|
|
cfg_add_cause("%s:%zu: %s", path, line, cause1);
|
2013-02-23 22:25:58 +00:00
|
|
|
free(cause1);
|
2010-02-08 18:10:07 +00:00
|
|
|
continue;
|
2012-12-06 13:06:05 +00:00
|
|
|
}
|
2014-06-25 19:17:27 +00:00
|
|
|
free(buf);
|
2013-02-23 22:25:58 +00:00
|
|
|
|
2009-01-18 14:40:48 +00:00
|
|
|
if (cmdlist == NULL)
|
2008-07-25 17:20:40 +00:00
|
|
|
continue;
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_append(cmdq, cmdlist);
|
2009-01-18 14:40:48 +00:00
|
|
|
cmd_list_free(cmdlist);
|
2013-02-23 22:25:58 +00:00
|
|
|
found++;
|
2008-06-19 21:13:56 +00:00
|
|
|
}
|
2009-03-31 22:23:43 +00:00
|
|
|
fclose(f);
|
2008-06-02 18:08:17 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
return (found);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cfg_default_done(unused struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
if (--cfg_references != 0)
|
|
|
|
return;
|
|
|
|
cfg_finished = 1;
|
2013-02-18 23:20:21 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
if (!RB_EMPTY(&sessions))
|
|
|
|
cfg_show_causes(RB_MIN(sessions, &sessions));
|
2012-11-27 16:12:29 +00:00
|
|
|
|
2013-02-23 22:25:58 +00:00
|
|
|
cmdq_free(cfg_cmd_q);
|
|
|
|
cfg_cmd_q = NULL;
|
2013-10-20 17:28:43 +00:00
|
|
|
|
|
|
|
if (cfg_client != NULL) {
|
|
|
|
/*
|
|
|
|
* The client command queue starts with client_exit set to 1 so
|
|
|
|
* only continue if not empty (that is, we have been delayed
|
|
|
|
* during configuration parsing for long enough that the
|
|
|
|
* MSG_COMMAND has arrived), else the client will exit before
|
|
|
|
* the MSG_COMMAND which might tell it not to.
|
|
|
|
*/
|
|
|
|
if (!TAILQ_EMPTY(&cfg_client->cmdq->queue))
|
|
|
|
cmdq_continue(cfg_client->cmdq);
|
|
|
|
cfg_client->references--;
|
|
|
|
cfg_client = NULL;
|
|
|
|
}
|
2008-06-02 18:08:17 +00:00
|
|
|
}
|
2012-11-19 10:38:06 +00:00
|
|
|
|
2014-10-27 22:23:47 +00:00
|
|
|
void
|
|
|
|
cfg_add_cause(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char* msg;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&msg, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
ARRAY_ADD(&cfg_causes, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cfg_print_causes(struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
char *cause;
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
|
|
|
|
cause = ARRAY_ITEM(&cfg_causes, i);
|
|
|
|
cmdq_print(cmdq, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
}
|
|
|
|
ARRAY_FREE(&cfg_causes);
|
|
|
|
}
|
|
|
|
|
2012-11-19 10:38:06 +00:00
|
|
|
void
|
2013-02-23 22:25:58 +00:00
|
|
|
cfg_show_causes(struct session *s)
|
2012-11-19 10:38:06 +00: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);
|
|
|
|
}
|