[BACK]Return to cmd-set-hook.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-set-hook.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: cmd-set-hook.c,v 1.2 2015/12/11 15:46:57 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]",
1.3     ! nicm       36:        CMD_SESSION_T,
1.1       nicm       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,
1.3     ! nicm       44:        CMD_SESSION_T,
1.1       nicm       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 cmd_list *cmdlist;
                     53:        struct hooks    *hooks;
                     54:        struct hook     *hook;
                     55:        char            *cause, *tmp;
                     56:        const char      *name, *cmd;
                     57:
                     58:        if (args_has(args, 'g'))
                     59:                hooks = global_hooks;
1.3     ! nicm       60:        else
        !            61:                hooks = cmdq->state.tflag.s->hooks;
1.1       nicm       62:
                     63:        if (self->entry == &cmd_show_hooks_entry) {
                     64:                hook = hooks_first(hooks);
                     65:                while (hook != NULL) {
                     66:                        tmp = cmd_list_print(hook->cmdlist);
                     67:                        cmdq_print(cmdq, "%s -> %s", hook->name, tmp);
                     68:                        free(tmp);
                     69:
                     70:                        hook = hooks_next(hook);
                     71:                }
                     72:                return (CMD_RETURN_NORMAL);
                     73:        }
                     74:
                     75:        name = args->argv[0];
                     76:        if (*name == '\0') {
                     77:                cmdq_error(cmdq, "invalid hook name");
                     78:                return (CMD_RETURN_ERROR);
                     79:        }
                     80:        if (args->argc < 2)
                     81:                cmd = NULL;
                     82:        else
                     83:                cmd = args->argv[1];
                     84:
                     85:        if (args_has(args, 'u')) {
                     86:                if (cmd != NULL) {
                     87:                        cmdq_error(cmdq, "command passed to unset hook: %s",
                     88:                            name);
                     89:                        return (CMD_RETURN_ERROR);
                     90:                }
1.2       nicm       91:                hooks_remove(hooks, name);
1.1       nicm       92:                return (CMD_RETURN_NORMAL);
                     93:        }
                     94:
                     95:        if (cmd == NULL) {
                     96:                cmdq_error(cmdq, "no command to set hook: %s", name);
                     97:                return (CMD_RETURN_ERROR);
                     98:        }
                     99:        if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
                    100:                if (cause != NULL) {
                    101:                        cmdq_error(cmdq, "%s", cause);
                    102:                        free(cause);
                    103:                }
                    104:                return (CMD_RETURN_ERROR);
                    105:        }
                    106:        hooks_add(hooks, name, cmdlist);
                    107:        cmd_list_free(cmdlist);
                    108:
                    109:        return (CMD_RETURN_NORMAL);
                    110: }