2015-12-08 01:10:31 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
|
|
|
|
*
|
|
|
|
* 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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set or show global or session hooks.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmdq_item *);
|
2015-12-08 01:10:31 +00:00
|
|
|
|
|
|
|
const struct cmd_entry cmd_set_hook_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "set-hook",
|
|
|
|
.alias = NULL,
|
|
|
|
|
|
|
|
.args = { "gt:u", 1, 2 },
|
|
|
|
.usage = "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_set_hook_exec
|
2015-12-08 01:10:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct cmd_entry cmd_show_hooks_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "show-hooks",
|
|
|
|
.alias = NULL,
|
|
|
|
|
|
|
|
.args = { "gt:", 0, 1 },
|
|
|
|
.usage = "[-g] " CMD_TARGET_SESSION_USAGE,
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_SESSION, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_set_hook_exec
|
2015-12-08 01:10:31 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_set_hook_exec(struct cmd *self, struct cmdq_item *item)
|
2015-12-08 01:10:31 +00:00
|
|
|
{
|
|
|
|
struct args *args = self->args;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
struct hooks *hooks;
|
|
|
|
struct hook *hook;
|
|
|
|
char *cause, *tmp;
|
2016-05-12 13:21:56 +00:00
|
|
|
const char *name, *cmd, *target;
|
2015-12-08 01:10:31 +00:00
|
|
|
|
|
|
|
if (args_has(args, 'g'))
|
|
|
|
hooks = global_hooks;
|
2016-05-12 13:21:56 +00:00
|
|
|
else {
|
2017-04-22 10:22:39 +00:00
|
|
|
if (item->target.s == NULL) {
|
2016-05-12 13:21:56 +00:00
|
|
|
target = args_get(args, 't');
|
|
|
|
if (target != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no such session: %s", target);
|
2016-05-12 13:21:56 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no current session");
|
2016-05-12 13:21:56 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2017-04-22 10:22:39 +00:00
|
|
|
hooks = item->target.s->hooks;
|
2016-05-12 13:21:56 +00:00
|
|
|
}
|
2015-12-08 01:10:31 +00:00
|
|
|
|
|
|
|
if (self->entry == &cmd_show_hooks_entry) {
|
|
|
|
hook = hooks_first(hooks);
|
|
|
|
while (hook != NULL) {
|
|
|
|
tmp = cmd_list_print(hook->cmdlist);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(item, "%s -> %s", hook->name, tmp);
|
2015-12-08 01:10:31 +00:00
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
hook = hooks_next(hook);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
name = args->argv[0];
|
|
|
|
if (*name == '\0') {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "invalid hook name");
|
2015-12-08 01:10:31 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
if (args->argc < 2)
|
|
|
|
cmd = NULL;
|
|
|
|
else
|
|
|
|
cmd = args->argv[1];
|
|
|
|
|
|
|
|
if (args_has(args, 'u')) {
|
|
|
|
if (cmd != NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "command passed to unset hook: %s",
|
2015-12-08 01:10:31 +00:00
|
|
|
name);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2015-12-11 15:46:57 +00:00
|
|
|
hooks_remove(hooks, name);
|
2015-12-08 01:10:31 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no command to set hook: %s", name);
|
2015-12-08 01:10:31 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2017-01-15 22:00:56 +00:00
|
|
|
cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
|
|
|
|
if (cmdlist == NULL) {
|
2015-12-08 01:10:31 +00:00
|
|
|
if (cause != NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
2015-12-08 01:10:31 +00:00
|
|
|
free(cause);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
hooks_add(hooks, name, cmdlist);
|
|
|
|
cmd_list_free(cmdlist);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|