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

1.77    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.76 2021/08/13 06:50:42 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.48      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:
                     41:        .args = { "bFt:", 2, 3 },
                     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.61      nicm       52:        struct cmd_parse_input   input;
1.44      nicm       53:
1.30      nicm       54:        char                    *cmd_if;
                     55:        char                    *cmd_else;
                     56:
1.47      nicm       57:        struct client           *client;
1.48      nicm       58:        struct cmdq_item        *item;
1.5       nicm       59: };
1.1       nicm       60:
1.45      nicm       61: static enum cmd_retval
1.48      nicm       62: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       63: {
1.67      nicm       64:        struct args                     *args = cmd_get_args(self);
1.68      nicm       65:        struct cmd_find_state           *target = cmdq_get_target(item);
1.72      nicm       66:        struct cmdq_state               *state = cmdq_get_state(item);
1.5       nicm       67:        struct cmd_if_shell_data        *cdata;
1.77    ! nicm       68:        char                            *shellcmd, *error;
        !            69:        const char                      *cmd = NULL, *file;
1.74      nicm       70:        struct client                   *tc = cmdq_get_target_client(item);
1.68      nicm       71:        struct session                  *s = target->s;
1.61      nicm       72:        struct cmd_parse_input           pi;
1.73      nicm       73:        enum cmd_parse_status            status;
1.77    ! nicm       74:        u_int                            count = args_count(args);
1.41      nicm       75:
1.77    ! nicm       76:        shellcmd = format_single_from_target(item, args_string(args, 0));
1.27      nicm       77:        if (args_has(args, 'F')) {
                     78:                if (*shellcmd != '0' && *shellcmd != '\0')
1.77    ! nicm       79:                        cmd = args_string(args, 1);
        !            80:                else if (count == 3)
        !            81:                        cmd = args_string(args, 2);
1.35      nicm       82:                free(shellcmd);
1.27      nicm       83:                if (cmd == NULL)
                     84:                        return (CMD_RETURN_NORMAL);
1.61      nicm       85:
                     86:                memset(&pi, 0, sizeof pi);
1.67      nicm       87:                cmd_get_source(self, &pi.file, &pi.line);
1.61      nicm       88:                pi.item = item;
1.74      nicm       89:                pi.c = tc;
1.68      nicm       90:                cmd_find_copy_state(&pi.fs, target);
1.61      nicm       91:
1.73      nicm       92:                status = cmd_parse_and_insert(cmd, &pi, item, state, &error);
                     93:                if (status == CMD_PARSE_ERROR) {
                     94:                        cmdq_error(item, "%s", error);
                     95:                        free(error);
1.27      nicm       96:                        return (CMD_RETURN_ERROR);
                     97:                }
                     98:                return (CMD_RETURN_NORMAL);
                     99:        }
1.5       nicm      100:
1.44      nicm      101:        cdata = xcalloc(1, sizeof *cdata);
1.30      nicm      102:
1.77    ! nicm      103:        cdata->cmd_if = xstrdup(args_string(args, 1));
        !           104:        if (count == 3)
        !           105:                cdata->cmd_else = xstrdup(args_string(args, 2));
1.30      nicm      106:
1.65      nicm      107:        if (!args_has(args, 'b'))
1.68      nicm      108:                cdata->client = cmdq_get_client(item);
1.65      nicm      109:        else
1.74      nicm      110:                cdata->client = tc;
1.54      nicm      111:        if (cdata->client != NULL)
                    112:                cdata->client->references++;
1.5       nicm      113:
1.47      nicm      114:        if (!args_has(args, 'b'))
1.48      nicm      115:                cdata->item = item;
1.61      nicm      116:
1.67      nicm      117:        cmd_get_source(self, &file, &cdata->input.line);
                    118:        if (file != NULL)
                    119:                cdata->input.file = xstrdup(file);
1.74      nicm      120:        cdata->input.c = tc;
1.61      nicm      121:        if (cdata->input.c != NULL)
                    122:                cdata->input.c->references++;
1.68      nicm      123:        cmd_find_copy_state(&cdata->input.fs, target);
1.5       nicm      124:
1.75      nicm      125:        if (job_run(shellcmd, 0, NULL, s,
1.68      nicm      126:            server_client_get_cwd(cmdq_get_client(item), s), NULL,
1.66      nicm      127:            cmd_if_shell_callback, cmd_if_shell_free, cdata, 0, -1,
                    128:            -1) == NULL) {
1.60      nicm      129:                cmdq_error(item, "failed to run command: %s", shellcmd);
                    130:                free(shellcmd);
                    131:                free(cdata);
                    132:                return (CMD_RETURN_ERROR);
                    133:        }
1.19      nicm      134:        free(shellcmd);
1.1       nicm      135:
1.47      nicm      136:        if (args_has(args, 'b'))
1.20      nicm      137:                return (CMD_RETURN_NORMAL);
                    138:        return (CMD_RETURN_WAIT);
1.1       nicm      139: }
                    140:
1.45      nicm      141: static void
1.5       nicm      142: cmd_if_shell_callback(struct job *job)
1.1       nicm      143: {
1.59      nicm      144:        struct cmd_if_shell_data        *cdata = job_get_data(job);
1.47      nicm      145:        struct client                   *c = cdata->client;
1.62      nicm      146:        struct cmdq_item                *new_item = NULL;
1.72      nicm      147:        struct cmdq_state               *new_state = NULL;
1.61      nicm      148:        char                            *cmd;
1.59      nicm      149:        int                              status;
1.61      nicm      150:        struct cmd_parse_result         *pr;
1.20      nicm      151:
1.59      nicm      152:        status = job_get_status(job);
                    153:        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1.13      nicm      154:                cmd = cdata->cmd_else;
1.20      nicm      155:        else
1.13      nicm      156:                cmd = cdata->cmd_if;
1.20      nicm      157:        if (cmd == NULL)
1.47      nicm      158:                goto out;
1.20      nicm      159:
1.61      nicm      160:        pr = cmd_parse_from_string(cmd, &cdata->input);
                    161:        switch (pr->status) {
                    162:        case CMD_PARSE_EMPTY:
                    163:                break;
                    164:        case CMD_PARSE_ERROR:
1.62      nicm      165:                if (cdata->item != NULL)
                    166:                       cmdq_error(cdata->item, "%s", pr->error);
1.61      nicm      167:                free(pr->error);
                    168:                break;
                    169:        case CMD_PARSE_SUCCESS:
1.72      nicm      170:                if (cdata->item == NULL)
                    171:                        new_state = cmdq_new_state(NULL, NULL, 0);
                    172:                else
                    173:                        new_state = cmdq_get_state(cdata->item);
                    174:                new_item = cmdq_get_command(pr->cmdlist, new_state);
                    175:                if (cdata->item == NULL)
                    176:                        cmdq_free_state(new_state);
1.61      nicm      177:                cmd_list_free(pr->cmdlist);
                    178:                break;
1.1       nicm      179:        }
1.48      nicm      180:        if (new_item != NULL) {
                    181:                if (cdata->item == NULL)
                    182:                        cmdq_append(c, new_item);
1.47      nicm      183:                else
1.48      nicm      184:                        cmdq_insert_after(cdata->item, new_item);
1.47      nicm      185:        }
1.25      nicm      186:
1.47      nicm      187: out:
1.48      nicm      188:        if (cdata->item != NULL)
1.64      nicm      189:                cmdq_continue(cdata->item);
1.20      nicm      190: }
                    191:
1.45      nicm      192: static void
1.5       nicm      193: cmd_if_shell_free(void *data)
1.1       nicm      194: {
1.5       nicm      195:        struct cmd_if_shell_data        *cdata = data;
1.1       nicm      196:
1.54      nicm      197:        if (cdata->client != NULL)
                    198:                server_client_unref(cdata->client);
1.1       nicm      199:
1.15      nicm      200:        free(cdata->cmd_else);
                    201:        free(cdata->cmd_if);
1.44      nicm      202:
1.61      nicm      203:        if (cdata->input.c != NULL)
                    204:                server_client_unref(cdata->input.c);
                    205:        free((void *)cdata->input.file);
                    206:
1.15      nicm      207:        free(cdata);
1.1       nicm      208: }