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

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