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

1.51    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.50 2017/02/03 11:57:27 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.5       nicm       68:        struct cmd_if_shell_data        *cdata;
1.27      nicm       69:        char                            *shellcmd, *cmd, *cause;
                     70:        struct cmd_list                 *cmdlist;
1.48      nicm       71:        struct cmdq_item                *new_item;
1.51    ! nicm       72:        struct client                   *c = item->state.c;
1.48      nicm       73:        struct session                  *s = item->state.tflag.s;
                     74:        struct winlink                  *wl = item->state.tflag.wl;
                     75:        struct window_pane              *wp = item->state.tflag.wp;
1.34      nicm       76:        const char                      *cwd;
1.19      nicm       77:
1.48      nicm       78:        if (item->client != NULL && item->client->session == NULL)
                     79:                cwd = item->client->cwd;
1.38      nicm       80:        else if (s != NULL)
                     81:                cwd = s->cwd;
                     82:        else
                     83:                cwd = NULL;
1.41      nicm       84:
1.51    ! nicm       85:        shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
1.27      nicm       86:        if (args_has(args, 'F')) {
                     87:                cmd = NULL;
                     88:                if (*shellcmd != '0' && *shellcmd != '\0')
                     89:                        cmd = args->argv[1];
                     90:                else if (args->argc == 3)
                     91:                        cmd = args->argv[2];
1.35      nicm       92:                free(shellcmd);
1.27      nicm       93:                if (cmd == NULL)
                     94:                        return (CMD_RETURN_NORMAL);
1.49      nicm       95:                cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
                     96:                if (cmdlist == NULL) {
1.27      nicm       97:                        if (cause != NULL) {
1.48      nicm       98:                                cmdq_error(item, "%s", cause);
1.27      nicm       99:                                free(cause);
                    100:                        }
                    101:                        return (CMD_RETURN_ERROR);
                    102:                }
1.48      nicm      103:                new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0);
                    104:                cmdq_insert_after(item, new_item);
1.27      nicm      105:                cmd_list_free(cmdlist);
                    106:                return (CMD_RETURN_NORMAL);
                    107:        }
1.5       nicm      108:
1.44      nicm      109:        cdata = xcalloc(1, sizeof *cdata);
                    110:        if (self->file != NULL) {
                    111:                cdata->file = xstrdup(self->file);
                    112:                cdata->line = self->line;
                    113:        }
1.30      nicm      114:
1.13      nicm      115:        cdata->cmd_if = xstrdup(args->argv[1]);
1.14      nicm      116:        if (args->argc == 3)
                    117:                cdata->cmd_else = xstrdup(args->argv[2]);
1.13      nicm      118:        else
                    119:                cdata->cmd_else = NULL;
1.30      nicm      120:
1.48      nicm      121:        cdata->client = item->client;
1.47      nicm      122:        cdata->client->references++;
1.5       nicm      123:
1.47      nicm      124:        if (!args_has(args, 'b'))
1.48      nicm      125:                cdata->item = item;
1.47      nicm      126:        else
1.48      nicm      127:                cdata->item = NULL;
                    128:        memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse);
1.5       nicm      129:
1.32      nicm      130:        job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
                    131:            cdata);
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.47      nicm      139: static enum cmd_retval
1.48      nicm      140: cmd_if_shell_error(struct cmdq_item *item, void *data)
1.47      nicm      141: {
                    142:        char    *error = data;
                    143:
1.48      nicm      144:        cmdq_error(item, "%s", error);
1.47      nicm      145:        free(error);
                    146:
                    147:        return (CMD_RETURN_NORMAL);
                    148: }
                    149:
1.45      nicm      150: static void
1.5       nicm      151: cmd_if_shell_callback(struct job *job)
1.1       nicm      152: {
1.5       nicm      153:        struct cmd_if_shell_data        *cdata = job->data;
1.47      nicm      154:        struct client                   *c = cdata->client;
1.1       nicm      155:        struct cmd_list                 *cmdlist;
1.48      nicm      156:        struct cmdq_item                *new_item;
1.47      nicm      157:        char                            *cause, *cmd, *file = cdata->file;
                    158:        u_int                            line = cdata->line;
1.20      nicm      159:
                    160:        if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13      nicm      161:                cmd = cdata->cmd_else;
1.20      nicm      162:        else
1.13      nicm      163:                cmd = cdata->cmd_if;
1.20      nicm      164:        if (cmd == NULL)
1.47      nicm      165:                goto out;
1.20      nicm      166:
1.49      nicm      167:        cmdlist = cmd_string_parse(cmd, file, line, &cause);
                    168:        if (cmdlist == NULL) {
1.47      nicm      169:                if (cause != NULL)
1.48      nicm      170:                        new_item = cmdq_get_callback(cmd_if_shell_error, cause);
1.47      nicm      171:                else
1.48      nicm      172:                        new_item = NULL;
1.47      nicm      173:        } else {
1.48      nicm      174:                new_item = cmdq_get_command(cmdlist, NULL, &cdata->mouse, 0);
1.47      nicm      175:                cmd_list_free(cmdlist);
1.1       nicm      176:        }
                    177:
1.48      nicm      178:        if (new_item != NULL) {
                    179:                if (cdata->item == NULL)
                    180:                        cmdq_append(c, new_item);
1.47      nicm      181:                else
1.48      nicm      182:                        cmdq_insert_after(cdata->item, new_item);
1.47      nicm      183:        }
1.25      nicm      184:
1.47      nicm      185: out:
1.48      nicm      186:        if (cdata->item != NULL)
                    187:                cdata->item->flags &= ~CMDQ_WAITING;
1.20      nicm      188: }
                    189:
1.45      nicm      190: static void
1.5       nicm      191: cmd_if_shell_free(void *data)
1.1       nicm      192: {
1.5       nicm      193:        struct cmd_if_shell_data        *cdata = data;
1.1       nicm      194:
1.47      nicm      195:        server_client_unref(cdata->client);
1.1       nicm      196:
1.15      nicm      197:        free(cdata->cmd_else);
                    198:        free(cdata->cmd_if);
1.44      nicm      199:
                    200:        free(cdata->file);
1.15      nicm      201:        free(cdata);
1.1       nicm      202: }