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

1.53    ! nicm        1: /* $OpenBSD: cmd-confirm-before.c,v 1.52 2021/10/28 18:39:15 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:  */
1.22      nicm       18:
                     19: #include <sys/types.h>
1.1       nicm       20:
                     21: #include <ctype.h>
1.13      nicm       22: #include <stdlib.h>
1.1       nicm       23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Asks for confirmation before executing a command.
                     29:  */
                     30:
1.50      nicm       31: static enum args_parse_type    cmd_confirm_before_args_parse(struct args *,
                     32:                                    u_int, char **);
                     33: static enum cmd_retval         cmd_confirm_before_exec(struct cmd *,
                     34:                                    struct cmdq_item *);
1.1       nicm       35:
1.35      nicm       36: static int     cmd_confirm_before_callback(struct client *, void *,
                     37:                    const char *, int);
1.31      nicm       38: static void    cmd_confirm_before_free(void *);
1.1       nicm       39:
                     40: const struct cmd_entry cmd_confirm_before_entry = {
1.27      nicm       41:        .name = "confirm-before",
                     42:        .alias = "confirm",
                     43:
1.53    ! nicm       44:        .args = { "bc:p:t:y", 1, 1, cmd_confirm_before_args_parse },
        !            45:        .usage = "[-by] [-c confirm_key] [-p prompt] " CMD_TARGET_CLIENT_USAGE
        !            46:                 " command",
1.27      nicm       47:
1.41      nicm       48:        .flags = CMD_CLIENT_TFLAG,
1.27      nicm       49:        .exec = cmd_confirm_before_exec
1.1       nicm       50: };
                     51:
1.4       nicm       52: struct cmd_confirm_before_data {
1.44      nicm       53:        struct cmdq_item        *item;
1.49      nicm       54:        struct cmd_list         *cmdlist;
1.53    ! nicm       55:        u_char                   confirm_key;
        !            56:        int                      default_yes;
1.4       nicm       57: };
1.50      nicm       58:
                     59: static enum args_parse_type
                     60: cmd_confirm_before_args_parse(__unused struct args *args, __unused u_int idx,
                     61:     __unused char **cause)
                     62: {
                     63:        return (ARGS_PARSE_COMMANDS_OR_STRING);
                     64: }
1.1       nicm       65:
1.29      nicm       66: static enum cmd_retval
1.31      nicm       67: cmd_confirm_before_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       68: {
1.38      nicm       69:        struct args                     *args = cmd_get_args(self);
1.1       nicm       70:        struct cmd_confirm_before_data  *cdata;
1.41      nicm       71:        struct client                   *tc = cmdq_get_target_client(item);
1.42      nicm       72:        struct cmd_find_state           *target = cmdq_get_target(item);
1.49      nicm       73:        char                            *new_prompt;
1.53    ! nicm       74:        const char                      *confirm_key, *prompt, *cmd;
1.44      nicm       75:        int                              wait = !args_has(args, 'b');
1.34      nicm       76:
1.47      nicm       77:        cdata = xcalloc(1, sizeof *cdata);
1.52      nicm       78:        cdata->cmdlist = args_make_commands_now(self, item, 0, 1);
1.49      nicm       79:        if (cdata->cmdlist == NULL)
                     80:                return (CMD_RETURN_ERROR);
                     81:
                     82:        if (wait)
                     83:                cdata->item = item;
1.47      nicm       84:
1.53    ! nicm       85:        cdata->default_yes = args_has(args, 'y');
        !            86:        if ((confirm_key = args_get(args, 'c')) != NULL) {
        !            87:                if (confirm_key[1] == '\0' &&
        !            88:                    confirm_key[0] > 31 &&
        !            89:                    confirm_key[0] < 127)
        !            90:                        cdata->confirm_key = confirm_key[0];
        !            91:                else {
        !            92:                        cmdq_error(item, "invalid confirm key");
        !            93:                        return (CMD_RETURN_ERROR);
        !            94:                }
        !            95:        }
        !            96:        else
        !            97:                cdata->confirm_key = 'y';
        !            98:
1.12      nicm       99:        if ((prompt = args_get(args, 'p')) != NULL)
                    100:                xasprintf(&new_prompt, "%s ", prompt);
                    101:        else {
1.49      nicm      102:                cmd = cmd_get_entry(cmd_list_first(cdata->cmdlist))->name;
1.53    ! nicm      103:                xasprintf(&new_prompt, "Confirm '%s'? (%c/n) ",
        !           104:                cmd, cdata->confirm_key);
1.12      nicm      105:        }
1.19      nicm      106:
1.42      nicm      107:        status_prompt_set(tc, target, new_prompt, NULL,
                    108:            cmd_confirm_before_callback, cmd_confirm_before_free, cdata,
1.43      nicm      109:            PROMPT_SINGLE, PROMPT_TYPE_COMMAND);
1.45      nicm      110:        free(new_prompt);
1.1       nicm      111:
1.44      nicm      112:        if (!wait)
                    113:                return (CMD_RETURN_NORMAL);
                    114:        return (CMD_RETURN_WAIT);
1.1       nicm      115: }
                    116:
1.29      nicm      117: static int
1.35      nicm      118: cmd_confirm_before_callback(struct client *c, void *data, const char *s,
                    119:     __unused int done)
1.1       nicm      120: {
                    121:        struct cmd_confirm_before_data  *cdata = data;
1.49      nicm      122:        struct cmdq_item                *item = cdata->item, *new_item;
1.45      nicm      123:        int                              retcode = 1;
1.1       nicm      124:
1.19      nicm      125:        if (c->flags & CLIENT_DEAD)
1.45      nicm      126:                goto out;
1.19      nicm      127:
1.53    ! nicm      128:        if (s == NULL)
1.44      nicm      129:                goto out;
1.53    ! nicm      130:        if (s[0] != cdata->confirm_key && (s[0] != '\0' || !cdata->default_yes))
1.44      nicm      131:                goto out;
1.45      nicm      132:        retcode = 0;
1.1       nicm      133:
1.49      nicm      134:        if (item == NULL) {
                    135:                new_item = cmdq_get_command(cdata->cmdlist, NULL);
                    136:                cmdq_append(c, new_item);
                    137:        } else {
                    138:                new_item = cmdq_get_command(cdata->cmdlist,
                    139:                    cmdq_get_state(item));
                    140:                cmdq_insert_after(item, new_item);
1.1       nicm      141:        }
                    142:
1.44      nicm      143: out:
1.53    ! nicm      144:        if (item != NULL) {
        !           145:                if (cmdq_get_client(item) != NULL &&
        !           146:                    cmdq_get_client(item)->session == NULL)
        !           147:                        cmdq_get_client(item)->retval = retcode;
        !           148:                cmdq_continue(item);
        !           149:        }
1.4       nicm      150:        return (0);
                    151: }
                    152:
1.29      nicm      153: static void
1.4       nicm      154: cmd_confirm_before_free(void *data)
                    155: {
                    156:        struct cmd_confirm_before_data  *cdata = data;
                    157:
1.49      nicm      158:        cmd_list_free(cdata->cmdlist);
1.13      nicm      159:        free(cdata);
1.1       nicm      160: }