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

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