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

1.81    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.80 2021/08/23 12:33:55 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.81    ! nicm       32: static enum args_parse_type    cmd_if_shell_args_parse(struct args *, u_int,
        !            33:                                    char **);
        !            34: static enum cmd_retval         cmd_if_shell_exec(struct cmd *,
        !            35:                                    struct cmdq_item *);
1.1       nicm       36:
1.80      nicm       37: static void    cmd_if_shell_callback(struct job *);
                     38: static void    cmd_if_shell_free(void *);
1.1       nicm       39:
                     40: const struct cmd_entry cmd_if_shell_entry = {
1.39      nicm       41:        .name = "if-shell",
                     42:        .alias = "if",
                     43:
1.81    ! nicm       44:        .args = { "bFt:", 2, 3, cmd_if_shell_args_parse },
1.39      nicm       45:        .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
                     46:                 "[command]",
                     47:
1.55      nicm       48:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.40      nicm       49:
                     50:        .flags = 0,
1.39      nicm       51:        .exec = cmd_if_shell_exec
1.1       nicm       52: };
                     53:
1.5       nicm       54: struct cmd_if_shell_data {
1.80      nicm       55:        struct cmd_list         *cmd_if;
                     56:        struct cmd_list         *cmd_else;
1.30      nicm       57:
1.47      nicm       58:        struct client           *client;
1.48      nicm       59:        struct cmdq_item        *item;
1.5       nicm       60: };
1.81    ! nicm       61:
        !            62: static enum args_parse_type
        !            63: cmd_if_shell_args_parse(__unused struct args *args, u_int idx,
        !            64:     __unused char **cause)
        !            65: {
        !            66:        if (idx == 1 || idx == 2)
        !            67:                return (ARGS_PARSE_COMMANDS_OR_STRING);
        !            68:        return (ARGS_PARSE_STRING);
        !            69: }
1.1       nicm       70:
1.45      nicm       71: static enum cmd_retval
1.48      nicm       72: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       73: {
1.67      nicm       74:        struct args                     *args = cmd_get_args(self);
1.68      nicm       75:        struct cmd_find_state           *target = cmdq_get_target(item);
1.5       nicm       76:        struct cmd_if_shell_data        *cdata;
1.80      nicm       77:        struct cmdq_item                *new_item;
                     78:        char                            *shellcmd;
1.74      nicm       79:        struct client                   *tc = cmdq_get_target_client(item);
1.68      nicm       80:        struct session                  *s = target->s;
1.80      nicm       81:        struct cmd_list                 *cmdlist = NULL;
1.77      nicm       82:        u_int                            count = args_count(args);
1.41      nicm       83:
1.77      nicm       84:        shellcmd = format_single_from_target(item, args_string(args, 0));
1.27      nicm       85:        if (args_has(args, 'F')) {
                     86:                if (*shellcmd != '0' && *shellcmd != '\0')
1.80      nicm       87:                        cmdlist = args_make_commands_now(self, item, 1);
1.77      nicm       88:                else if (count == 3)
1.80      nicm       89:                        cmdlist = args_make_commands_now(self, item, 2);
                     90:                else {
                     91:                        free(shellcmd);
                     92:                        return (CMD_RETURN_NORMAL);
                     93:                }
1.35      nicm       94:                free(shellcmd);
1.80      nicm       95:                if (cmdlist == NULL)
1.27      nicm       96:                        return (CMD_RETURN_ERROR);
1.80      nicm       97:                new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
                     98:                cmdq_insert_after(item, new_item);
1.27      nicm       99:                return (CMD_RETURN_NORMAL);
                    100:        }
1.5       nicm      101:
1.44      nicm      102:        cdata = xcalloc(1, sizeof *cdata);
1.30      nicm      103:
1.80      nicm      104:        cdata->cmd_if = args_make_commands_now(self, item, 1);
                    105:        if (cdata->cmd_if == NULL)
                    106:            return (CMD_RETURN_ERROR);
                    107:        if (count == 3) {
                    108:                cdata->cmd_else = args_make_commands_now(self, item, 2);
                    109:                if (cdata->cmd_else == NULL)
                    110:                    return (CMD_RETURN_ERROR);
                    111:        }
1.30      nicm      112:
1.65      nicm      113:        if (!args_has(args, 'b'))
1.68      nicm      114:                cdata->client = cmdq_get_client(item);
1.65      nicm      115:        else
1.74      nicm      116:                cdata->client = tc;
1.54      nicm      117:        if (cdata->client != NULL)
                    118:                cdata->client->references++;
1.5       nicm      119:
1.47      nicm      120:        if (!args_has(args, 'b'))
1.48      nicm      121:                cdata->item = item;
1.61      nicm      122:
1.75      nicm      123:        if (job_run(shellcmd, 0, NULL, s,
1.68      nicm      124:            server_client_get_cwd(cmdq_get_client(item), s), NULL,
1.66      nicm      125:            cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
                    126:            -1) == NULL) {
1.60      nicm      127:                cmdq_error(item, "failed to run command: %s", shellcmd);
                    128:                free(shellcmd);
                    129:                free(cdata);
                    130:                return (CMD_RETURN_ERROR);
                    131:        }
1.19      nicm      132:        free(shellcmd);
1.1       nicm      133:
1.47      nicm      134:        if (args_has(args, 'b'))
1.20      nicm      135:                return (CMD_RETURN_NORMAL);
                    136:        return (CMD_RETURN_WAIT);
1.1       nicm      137: }
                    138:
1.45      nicm      139: static void
1.5       nicm      140: cmd_if_shell_callback(struct job *job)
1.1       nicm      141: {
1.59      nicm      142:        struct cmd_if_shell_data        *cdata = job_get_data(job);
1.47      nicm      143:        struct client                   *c = cdata->client;
1.80      nicm      144:        struct cmdq_item                *item = cdata->item, *new_item;
                    145:        struct cmd_list                 *cmdlist;
1.59      nicm      146:        int                              status;
1.20      nicm      147:
1.59      nicm      148:        status = job_get_status(job);
                    149:        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1.80      nicm      150:                cmdlist = cdata->cmd_else;
1.20      nicm      151:        else
1.80      nicm      152:                cmdlist = cdata->cmd_if;
                    153:        if (cmdlist == NULL)
1.47      nicm      154:                goto out;
1.20      nicm      155:
1.80      nicm      156:        if (item == NULL) {
                    157:                new_item = cmdq_get_command(cmdlist, NULL);
                    158:                cmdq_append(c, new_item);
                    159:        } else {
                    160:                new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
                    161:                cmdq_insert_after(item, new_item);
1.47      nicm      162:        }
1.25      nicm      163:
1.47      nicm      164: out:
1.48      nicm      165:        if (cdata->item != NULL)
1.64      nicm      166:                cmdq_continue(cdata->item);
1.20      nicm      167: }
                    168:
1.45      nicm      169: static void
1.5       nicm      170: cmd_if_shell_free(void *data)
1.1       nicm      171: {
1.5       nicm      172:        struct cmd_if_shell_data        *cdata = data;
1.1       nicm      173:
1.54      nicm      174:        if (cdata->client != NULL)
                    175:                server_client_unref(cdata->client);
1.1       nicm      176:
1.80      nicm      177:        if (cdata->cmd_else != NULL)
                    178:                cmd_list_free(cdata->cmd_else);
                    179:        cmd_list_free(cdata->cmd_if);
1.61      nicm      180:
1.15      nicm      181:        free(cdata);
1.1       nicm      182: }