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

Annotation of src/usr.bin/tmux/cmd-if-shell.c, Revision 1.80

1.80    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.79 2021/08/21 17:25:32 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
1.5       nicm        5:  * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
1.1       nicm        6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     16:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     17:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
                     20: #include <sys/types.h>
1.5       nicm       21: #include <sys/wait.h>
1.1       nicm       22:
1.15      nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: /*
1.14      nicm       29:  * Executes a tmux command if a shell command returns true or false.
1.1       nicm       30:  */
                     31:
1.48      nicm       32: static enum cmd_retval cmd_if_shell_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       33:
1.80    ! nicm       34: static void    cmd_if_shell_callback(struct job *);
        !            35: static void    cmd_if_shell_free(void *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_if_shell_entry = {
1.39      nicm       38:        .name = "if-shell",
                     39:        .alias = "if",
                     40:
1.78      nicm       41:        .args = { "bFt:", 2, 3, NULL },
1.39      nicm       42:        .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
                     43:                 "[command]",
                     44:
1.55      nicm       45:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.40      nicm       46:
                     47:        .flags = 0,
1.39      nicm       48:        .exec = cmd_if_shell_exec
1.1       nicm       49: };
                     50:
1.5       nicm       51: struct cmd_if_shell_data {
1.80    ! nicm       52:        struct cmd_list         *cmd_if;
        !            53:        struct cmd_list         *cmd_else;
1.30      nicm       54:
1.47      nicm       55:        struct client           *client;
1.48      nicm       56:        struct cmdq_item        *item;
1.5       nicm       57: };
1.1       nicm       58:
1.45      nicm       59: static enum cmd_retval
1.48      nicm       60: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       61: {
1.67      nicm       62:        struct args                     *args = cmd_get_args(self);
1.68      nicm       63:        struct cmd_find_state           *target = cmdq_get_target(item);
1.5       nicm       64:        struct cmd_if_shell_data        *cdata;
1.80    ! nicm       65:        struct cmdq_item                *new_item;
        !            66:        char                            *shellcmd;
1.74      nicm       67:        struct client                   *tc = cmdq_get_target_client(item);
1.68      nicm       68:        struct session                  *s = target->s;
1.80    ! nicm       69:        struct cmd_list                 *cmdlist = NULL;
1.77      nicm       70:        u_int                            count = args_count(args);
1.41      nicm       71:
1.77      nicm       72:        shellcmd = format_single_from_target(item, args_string(args, 0));
1.27      nicm       73:        if (args_has(args, 'F')) {
                     74:                if (*shellcmd != '0' && *shellcmd != '\0')
1.80    ! nicm       75:                        cmdlist = args_make_commands_now(self, item, 1);
1.77      nicm       76:                else if (count == 3)
1.80    ! nicm       77:                        cmdlist = args_make_commands_now(self, item, 2);
        !            78:                else {
        !            79:                        free(shellcmd);
        !            80:                        return (CMD_RETURN_NORMAL);
        !            81:                }
1.35      nicm       82:                free(shellcmd);
1.80    ! nicm       83:                if (cmdlist == NULL)
1.27      nicm       84:                        return (CMD_RETURN_ERROR);
1.80    ! nicm       85:                new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
        !            86:                cmdq_insert_after(item, new_item);
1.27      nicm       87:                return (CMD_RETURN_NORMAL);
                     88:        }
1.5       nicm       89:
1.44      nicm       90:        cdata = xcalloc(1, sizeof *cdata);
1.30      nicm       91:
1.80    ! nicm       92:        cdata->cmd_if = args_make_commands_now(self, item, 1);
        !            93:        if (cdata->cmd_if == NULL)
        !            94:            return (CMD_RETURN_ERROR);
        !            95:        if (count == 3) {
        !            96:                cdata->cmd_else = args_make_commands_now(self, item, 2);
        !            97:                if (cdata->cmd_else == NULL)
        !            98:                    return (CMD_RETURN_ERROR);
        !            99:        }
1.30      nicm      100:
1.65      nicm      101:        if (!args_has(args, 'b'))
1.68      nicm      102:                cdata->client = cmdq_get_client(item);
1.65      nicm      103:        else
1.74      nicm      104:                cdata->client = tc;
1.54      nicm      105:        if (cdata->client != NULL)
                    106:                cdata->client->references++;
1.5       nicm      107:
1.47      nicm      108:        if (!args_has(args, 'b'))
1.48      nicm      109:                cdata->item = item;
1.61      nicm      110:
1.75      nicm      111:        if (job_run(shellcmd, 0, NULL, s,
1.68      nicm      112:            server_client_get_cwd(cmdq_get_client(item), s), NULL,
1.66      nicm      113:            cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
                    114:            -1) == NULL) {
1.60      nicm      115:                cmdq_error(item, "failed to run command: %s", shellcmd);
                    116:                free(shellcmd);
                    117:                free(cdata);
                    118:                return (CMD_RETURN_ERROR);
                    119:        }
1.19      nicm      120:        free(shellcmd);
1.1       nicm      121:
1.47      nicm      122:        if (args_has(args, 'b'))
1.20      nicm      123:                return (CMD_RETURN_NORMAL);
                    124:        return (CMD_RETURN_WAIT);
1.1       nicm      125: }
                    126:
1.45      nicm      127: static void
1.5       nicm      128: cmd_if_shell_callback(struct job *job)
1.1       nicm      129: {
1.59      nicm      130:        struct cmd_if_shell_data        *cdata = job_get_data(job);
1.47      nicm      131:        struct client                   *c = cdata->client;
1.80    ! nicm      132:        struct cmdq_item                *item = cdata->item, *new_item;
        !           133:        struct cmd_list                 *cmdlist;
1.59      nicm      134:        int                              status;
1.20      nicm      135:
1.59      nicm      136:        status = job_get_status(job);
                    137:        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1.80    ! nicm      138:                cmdlist = cdata->cmd_else;
1.20      nicm      139:        else
1.80    ! nicm      140:                cmdlist = cdata->cmd_if;
        !           141:        if (cmdlist == NULL)
1.47      nicm      142:                goto out;
1.20      nicm      143:
1.80    ! nicm      144:        if (item == NULL) {
        !           145:                new_item = cmdq_get_command(cmdlist, NULL);
        !           146:                cmdq_append(c, new_item);
        !           147:        } else {
        !           148:                new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
        !           149:                cmdq_insert_after(item, new_item);
1.47      nicm      150:        }
1.25      nicm      151:
1.47      nicm      152: out:
1.48      nicm      153:        if (cdata->item != NULL)
1.64      nicm      154:                cmdq_continue(cdata->item);
1.20      nicm      155: }
                    156:
1.45      nicm      157: static void
1.5       nicm      158: cmd_if_shell_free(void *data)
1.1       nicm      159: {
1.5       nicm      160:        struct cmd_if_shell_data        *cdata = data;
1.1       nicm      161:
1.54      nicm      162:        if (cdata->client != NULL)
                    163:                server_client_unref(cdata->client);
1.1       nicm      164:
1.80    ! nicm      165:        if (cdata->cmd_else != NULL)
        !           166:                cmd_list_free(cdata->cmd_else);
        !           167:        cmd_list_free(cdata->cmd_if);
1.61      nicm      168:
1.15      nicm      169:        free(cdata);
1.1       nicm      170: }