Annotation of src/usr.bin/tmux/cmd-set-hook.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: cmd-set-hook.c,v 1.1 2015/12/08 01:10:31 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Set or show global or session hooks.
28: */
29:
30: enum cmd_retval cmd_set_hook_exec(struct cmd *, struct cmd_q *);
31:
32: const struct cmd_entry cmd_set_hook_entry = {
33: "set-hook", NULL,
34: "gt:u", 1, 2,
35: "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]",
36: 0,
37: cmd_set_hook_exec
38: };
39:
40: const struct cmd_entry cmd_show_hooks_entry = {
41: "show-hooks", NULL,
42: "gt:", 0, 1,
43: "[-g] " CMD_TARGET_SESSION_USAGE,
44: 0,
45: cmd_set_hook_exec
46: };
47:
48: enum cmd_retval
49: cmd_set_hook_exec(struct cmd *self, struct cmd_q *cmdq)
50: {
51: struct args *args = self->args;
52: struct session *s;
53: struct cmd_list *cmdlist;
54: struct hooks *hooks;
55: struct hook *hook;
56: char *cause, *tmp;
57: const char *name, *cmd;
58:
59: if (args_has(args, 'g'))
60: hooks = global_hooks;
61: else {
62: s = cmd_find_session(cmdq, args_get(args, 't'), 0);
63: if (s == NULL)
64: return (CMD_RETURN_ERROR);
65: hooks = s->hooks;
66: }
67:
68: if (self->entry == &cmd_show_hooks_entry) {
69: hook = hooks_first(hooks);
70: while (hook != NULL) {
71: tmp = cmd_list_print(hook->cmdlist);
72: cmdq_print(cmdq, "%s -> %s", hook->name, tmp);
73: free(tmp);
74:
75: hook = hooks_next(hook);
76: }
77: return (CMD_RETURN_NORMAL);
78: }
79:
80: name = args->argv[0];
81: if (*name == '\0') {
82: cmdq_error(cmdq, "invalid hook name");
83: return (CMD_RETURN_ERROR);
84: }
85: if (args->argc < 2)
86: cmd = NULL;
87: else
88: cmd = args->argv[1];
89:
90: if (args_has(args, 'u')) {
91: if (cmd != NULL) {
92: cmdq_error(cmdq, "command passed to unset hook: %s",
93: name);
94: return (CMD_RETURN_ERROR);
95: }
1.2 ! nicm 96: hooks_remove(hooks, name);
1.1 nicm 97: return (CMD_RETURN_NORMAL);
98: }
99:
100: if (cmd == NULL) {
101: cmdq_error(cmdq, "no command to set hook: %s", name);
102: return (CMD_RETURN_ERROR);
103: }
104: if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
105: if (cause != NULL) {
106: cmdq_error(cmdq, "%s", cause);
107: free(cause);
108: }
109: return (CMD_RETURN_ERROR);
110: }
111: hooks_add(hooks, name, cmdlist);
112: cmd_list_free(cmdlist);
113:
114: return (CMD_RETURN_NORMAL);
115: }