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

Annotation of src/usr.bin/tmux/cmd-confirm-before.c, Revision 1.1

1.1     ! nicm        1: /* $OpenBSD$ */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.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 <ctype.h>
        !            20: #include <string.h>
        !            21:
        !            22: #include "tmux.h"
        !            23:
        !            24: /*
        !            25:  * Asks for confirmation before executing a command.
        !            26:  */
        !            27:
        !            28: int    cmd_confirm_before_exec(struct cmd *, struct cmd_ctx *);
        !            29: void   cmd_confirm_before_init(struct cmd *, int);
        !            30:
        !            31: int    cmd_confirm_before_callback(void *, const char *);
        !            32:
        !            33: struct cmd_confirm_before_data {
        !            34:        struct client   *c;
        !            35:        char            *cmd;
        !            36: };
        !            37:
        !            38: const struct cmd_entry cmd_confirm_before_entry = {
        !            39:        "confirm-before", "confirm",
        !            40:        CMD_TARGET_CLIENT_USAGE " command",
        !            41:        CMD_ARG1,
        !            42:        cmd_confirm_before_init,
        !            43:        cmd_target_parse,
        !            44:        cmd_confirm_before_exec,
        !            45:        cmd_target_send,
        !            46:        cmd_target_recv,
        !            47:        cmd_target_free,
        !            48:        cmd_target_print
        !            49: };
        !            50:
        !            51: void
        !            52: cmd_confirm_before_init(struct cmd *self, int key)
        !            53: {
        !            54:        struct cmd_target_data  *data;
        !            55:
        !            56:        cmd_target_init(self, key);
        !            57:        data = self->data;
        !            58:
        !            59:        switch (key) {
        !            60:        case '&':
        !            61:                data->arg = xstrdup("kill-window");
        !            62:                break;
        !            63:        case 'x':
        !            64:                data->arg = xstrdup("kill-pane");
        !            65:                break;
        !            66:        }
        !            67: }
        !            68:
        !            69: int
        !            70: cmd_confirm_before_exec(unused struct cmd *self, struct cmd_ctx *ctx)
        !            71: {
        !            72:        struct cmd_target_data          *data = self->data;
        !            73:        struct cmd_confirm_before_data  *cdata;
        !            74:        struct client                   *c;
        !            75:        char                            *buf, *cmd, *ptr;
        !            76:
        !            77:        if (ctx->curclient == NULL) {
        !            78:                ctx->error(ctx, "must be run interactively");
        !            79:                return (-1);
        !            80:        }
        !            81:
        !            82:        if ((c = cmd_find_client(ctx, data->target)) == NULL)
        !            83:                return (-1);
        !            84:
        !            85:        ptr = xstrdup(data->arg);
        !            86:        if ((cmd = strtok(ptr, " \t")) == NULL)
        !            87:                cmd = ptr;
        !            88:        xasprintf(&buf, "Confirm '%s'? (y/n) ", cmd);
        !            89:        xfree(ptr);
        !            90:
        !            91:        cdata = xmalloc(sizeof *cdata);
        !            92:        cdata->cmd = xstrdup(data->arg);
        !            93:        cdata->c = c;
        !            94:        status_prompt_set(
        !            95:            cdata->c, buf, cmd_confirm_before_callback, cdata, PROMPT_SINGLE);
        !            96:
        !            97:        xfree(buf);
        !            98:        return (1);
        !            99: }
        !           100:
        !           101: int
        !           102: cmd_confirm_before_callback(void *data, const char *s)
        !           103: {
        !           104:        struct cmd_confirm_before_data  *cdata = data;
        !           105:        struct client                   *c = cdata->c;
        !           106:        struct cmd_list                 *cmdlist;
        !           107:        struct cmd_ctx                   ctx;
        !           108:        char                            *cause;
        !           109:
        !           110:        if (s == NULL || tolower((u_char) s[0]) != 'y' || s[1] != '\0')
        !           111:                goto out;
        !           112:
        !           113:        if (cmd_string_parse(cdata->cmd, &cmdlist, &cause) != 0) {
        !           114:                if (cause != NULL) {
        !           115:                        *cause = toupper((u_char) *cause);
        !           116:                        status_message_set(c, cause);
        !           117:                        xfree(cause);
        !           118:                }
        !           119:                goto out;
        !           120:        }
        !           121:
        !           122:        ctx.msgdata = NULL;
        !           123:        ctx.cursession = c->session;
        !           124:        ctx.curclient = c;
        !           125:
        !           126:        ctx.error = key_bindings_error;
        !           127:        ctx.print = key_bindings_print;
        !           128:        ctx.info = key_bindings_info;
        !           129:
        !           130:        ctx.cmdclient = NULL;
        !           131:
        !           132:        cmd_list_exec(cmdlist, &ctx);
        !           133:        cmd_list_free(cmdlist);
        !           134:
        !           135: out:
        !           136:        if (cdata->cmd != NULL)
        !           137:                xfree(cdata->cmd);
        !           138:        xfree(cdata);
        !           139:
        !           140:        return (0);
        !           141: }