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

1.53    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.52 2017/04/20 09:20:22 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 enum cmd_retval cmd_if_shell_error(struct cmdq_item *, void *);
                     35: static void            cmd_if_shell_callback(struct job *);
                     36: static void            cmd_if_shell_free(void *);
1.1       nicm       37:
                     38: const struct cmd_entry cmd_if_shell_entry = {
1.39      nicm       39:        .name = "if-shell",
                     40:        .alias = "if",
                     41:
                     42:        .args = { "bFt:", 2, 3 },
                     43:        .usage = "[-bF] " CMD_TARGET_PANE_USAGE " shell-command command "
                     44:                 "[command]",
                     45:
1.40      nicm       46:        .tflag = CMD_PANE_CANFAIL,
                     47:
                     48:        .flags = 0,
1.39      nicm       49:        .exec = cmd_if_shell_exec
1.1       nicm       50: };
                     51:
1.5       nicm       52: struct cmd_if_shell_data {
1.44      nicm       53:        char                    *file;
                     54:        u_int                    line;
                     55:
1.30      nicm       56:        char                    *cmd_if;
                     57:        char                    *cmd_else;
                     58:
1.47      nicm       59:        struct client           *client;
1.48      nicm       60:        struct cmdq_item        *item;
1.30      nicm       61:        struct mouse_event       mouse;
1.5       nicm       62: };
1.1       nicm       63:
1.45      nicm       64: static enum cmd_retval
1.48      nicm       65: cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       66: {
1.10      nicm       67:        struct args                     *args = self->args;
1.53    ! nicm       68:        struct cmdq_shared              *shared = item->shared;
1.5       nicm       69:        struct cmd_if_shell_data        *cdata;
1.27      nicm       70:        char                            *shellcmd, *cmd, *cause;
                     71:        struct cmd_list                 *cmdlist;
1.48      nicm       72:        struct cmdq_item                *new_item;
1.51      nicm       73:        struct client                   *c = item->state.c;
1.48      nicm       74:        struct session                  *s = item->state.tflag.s;
                     75:        struct winlink                  *wl = item->state.tflag.wl;
                     76:        struct window_pane              *wp = item->state.tflag.wp;
1.34      nicm       77:        const char                      *cwd;
1.19      nicm       78:
1.48      nicm       79:        if (item->client != NULL && item->client->session == NULL)
                     80:                cwd = item->client->cwd;
1.38      nicm       81:        else if (s != NULL)
                     82:                cwd = s->cwd;
                     83:        else
                     84:                cwd = NULL;
1.41      nicm       85:
1.51      nicm       86:        shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
1.27      nicm       87:        if (args_has(args, 'F')) {
                     88:                cmd = NULL;
                     89:                if (*shellcmd != '0' && *shellcmd != '\0')
                     90:                        cmd = args->argv[1];
                     91:                else if (args->argc == 3)
                     92:                        cmd = args->argv[2];
1.35      nicm       93:                free(shellcmd);
1.27      nicm       94:                if (cmd == NULL)
                     95:                        return (CMD_RETURN_NORMAL);
1.49      nicm       96:                cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
                     97:                if (cmdlist == NULL) {
1.27      nicm       98:                        if (cause != NULL) {
1.48      nicm       99:                                cmdq_error(item, "%s", cause);
1.27      nicm      100:                                free(cause);
                    101:                        }
                    102:                        return (CMD_RETURN_ERROR);
                    103:                }
1.53    ! nicm      104:                new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0);
1.48      nicm      105:                cmdq_insert_after(item, new_item);
1.27      nicm      106:                cmd_list_free(cmdlist);
                    107:                return (CMD_RETURN_NORMAL);
                    108:        }
1.5       nicm      109:
1.44      nicm      110:        cdata = xcalloc(1, sizeof *cdata);
                    111:        if (self->file != NULL) {
                    112:                cdata->file = xstrdup(self->file);
                    113:                cdata->line = self->line;
                    114:        }
1.30      nicm      115:
1.13      nicm      116:        cdata->cmd_if = xstrdup(args->argv[1]);
1.14      nicm      117:        if (args->argc == 3)
                    118:                cdata->cmd_else = xstrdup(args->argv[2]);
1.13      nicm      119:        else
                    120:                cdata->cmd_else = NULL;
1.30      nicm      121:
1.48      nicm      122:        cdata->client = item->client;
1.47      nicm      123:        cdata->client->references++;
1.5       nicm      124:
1.47      nicm      125:        if (!args_has(args, 'b'))
1.48      nicm      126:                cdata->item = item;
1.47      nicm      127:        else
1.48      nicm      128:                cdata->item = NULL;
1.53    ! nicm      129:        memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse);
1.5       nicm      130:
1.52      nicm      131:        job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback,
                    132:            cmd_if_shell_free, cdata);
1.19      nicm      133:        free(shellcmd);
1.1       nicm      134:
1.47      nicm      135:        if (args_has(args, 'b'))
1.20      nicm      136:                return (CMD_RETURN_NORMAL);
                    137:        return (CMD_RETURN_WAIT);
1.1       nicm      138: }
                    139:
1.47      nicm      140: static enum cmd_retval
1.48      nicm      141: cmd_if_shell_error(struct cmdq_item *item, void *data)
1.47      nicm      142: {
                    143:        char    *error = data;
                    144:
1.48      nicm      145:        cmdq_error(item, "%s", error);
1.47      nicm      146:        free(error);
                    147:
                    148:        return (CMD_RETURN_NORMAL);
                    149: }
                    150:
1.45      nicm      151: static void
1.5       nicm      152: cmd_if_shell_callback(struct job *job)
1.1       nicm      153: {
1.5       nicm      154:        struct cmd_if_shell_data        *cdata = job->data;
1.47      nicm      155:        struct client                   *c = cdata->client;
1.1       nicm      156:        struct cmd_list                 *cmdlist;
1.48      nicm      157:        struct cmdq_item                *new_item;
1.47      nicm      158:        char                            *cause, *cmd, *file = cdata->file;
                    159:        u_int                            line = cdata->line;
1.20      nicm      160:
                    161:        if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13      nicm      162:                cmd = cdata->cmd_else;
1.20      nicm      163:        else
1.13      nicm      164:                cmd = cdata->cmd_if;
1.20      nicm      165:        if (cmd == NULL)
1.47      nicm      166:                goto out;
1.20      nicm      167:
1.49      nicm      168:        cmdlist = cmd_string_parse(cmd, file, line, &cause);
                    169:        if (cmdlist == NULL) {
1.47      nicm      170:                if (cause != NULL)
1.48      nicm      171:                        new_item = cmdq_get_callback(cmd_if_shell_error, cause);
1.47      nicm      172:                else
1.48      nicm      173:                        new_item = NULL;
1.47      nicm      174:        } else {
1.48      nicm      175:                new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
1.47      nicm      176:                cmd_list_free(cmdlist);
1.1       nicm      177:        }
                    178:
1.48      nicm      179:        if (new_item != NULL) {
                    180:                if (cdata->item == NULL)
                    181:                        cmdq_append(c, new_item);
1.47      nicm      182:                else
1.48      nicm      183:                        cmdq_insert_after(cdata->item, new_item);
1.47      nicm      184:        }
1.25      nicm      185:
1.47      nicm      186: out:
1.48      nicm      187:        if (cdata->item != NULL)
                    188:                cdata->item->flags &= ~CMDQ_WAITING;
1.20      nicm      189: }
                    190:
1.45      nicm      191: static void
1.5       nicm      192: cmd_if_shell_free(void *data)
1.1       nicm      193: {
1.5       nicm      194:        struct cmd_if_shell_data        *cdata = data;
1.1       nicm      195:
1.47      nicm      196:        server_client_unref(cdata->client);
1.1       nicm      197:
1.15      nicm      198:        free(cdata->cmd_else);
                    199:        free(cdata->cmd_if);
1.44      nicm      200:
                    201:        free(cdata->file);
1.15      nicm      202:        free(cdata);
1.1       nicm      203: }