[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.4

1.4     ! nicm        1: /* $OpenBSD: cmd-set-hook.c,v 1.3 2015/12/13 14:32:38 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 = {
1.4     ! nicm       33:        .name = "set-hook",
        !            34:        .alias = NULL,
        !            35:
        !            36:        .args = { "gt:u", 1, 2 },
        !            37:        .usage = "[-gu] " CMD_TARGET_SESSION_USAGE " hook-name [command]",
        !            38:
        !            39:        .flags = CMD_SESSION_T,
        !            40:        .exec = cmd_set_hook_exec
1.1       nicm       41: };
                     42:
                     43: const struct cmd_entry cmd_show_hooks_entry = {
1.4     ! nicm       44:        .name = "show-hooks",
        !            45:        .alias = NULL,
        !            46:
        !            47:        .args = { "gt:", 0, 1 },
        !            48:        .usage = "[-g] " CMD_TARGET_SESSION_USAGE,
        !            49:
        !            50:        .flags = CMD_SESSION_T,
        !            51:        .exec = cmd_set_hook_exec
1.1       nicm       52: };
                     53:
                     54: enum cmd_retval
                     55: cmd_set_hook_exec(struct cmd *self, struct cmd_q *cmdq)
                     56: {
                     57:        struct args     *args = self->args;
                     58:        struct cmd_list *cmdlist;
                     59:        struct hooks    *hooks;
                     60:        struct hook     *hook;
                     61:        char            *cause, *tmp;
                     62:        const char      *name, *cmd;
                     63:
                     64:        if (args_has(args, 'g'))
                     65:                hooks = global_hooks;
1.3       nicm       66:        else
                     67:                hooks = cmdq->state.tflag.s->hooks;
1.1       nicm       68:
                     69:        if (self->entry == &cmd_show_hooks_entry) {
                     70:                hook = hooks_first(hooks);
                     71:                while (hook != NULL) {
                     72:                        tmp = cmd_list_print(hook->cmdlist);
                     73:                        cmdq_print(cmdq, "%s -> %s", hook->name, tmp);
                     74:                        free(tmp);
                     75:
                     76:                        hook = hooks_next(hook);
                     77:                }
                     78:                return (CMD_RETURN_NORMAL);
                     79:        }
                     80:
                     81:        name = args->argv[0];
                     82:        if (*name == '\0') {
                     83:                cmdq_error(cmdq, "invalid hook name");
                     84:                return (CMD_RETURN_ERROR);
                     85:        }
                     86:        if (args->argc < 2)
                     87:                cmd = NULL;
                     88:        else
                     89:                cmd = args->argv[1];
                     90:
                     91:        if (args_has(args, 'u')) {
                     92:                if (cmd != NULL) {
                     93:                        cmdq_error(cmdq, "command passed to unset hook: %s",
                     94:                            name);
                     95:                        return (CMD_RETURN_ERROR);
                     96:                }
1.2       nicm       97:                hooks_remove(hooks, name);
1.1       nicm       98:                return (CMD_RETURN_NORMAL);
                     99:        }
                    100:
                    101:        if (cmd == NULL) {
                    102:                cmdq_error(cmdq, "no command to set hook: %s", name);
                    103:                return (CMD_RETURN_ERROR);
                    104:        }
                    105:        if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
                    106:                if (cause != NULL) {
                    107:                        cmdq_error(cmdq, "%s", cause);
                    108:                        free(cause);
                    109:                }
                    110:                return (CMD_RETURN_ERROR);
                    111:        }
                    112:        hooks_add(hooks, name, cmdlist);
                    113:        cmd_list_free(cmdlist);
                    114:
                    115:        return (CMD_RETURN_NORMAL);
                    116: }