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

1.39    ! nicm        1: /* $OpenBSD: cmd-if-shell.c,v 1.38 2015/12/13 14:32:38 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.20      nicm       32: enum cmd_retval         cmd_if_shell_exec(struct cmd *, struct cmd_q *);
1.1       nicm       33:
1.5       nicm       34: void   cmd_if_shell_callback(struct job *);
1.20      nicm       35: void   cmd_if_shell_done(struct cmd_q *);
1.5       nicm       36: 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:
        !            46:        .flags = CMD_PANE_T|CMD_CANFAIL,
        !            47:        .exec = cmd_if_shell_exec
1.1       nicm       48: };
                     49:
1.5       nicm       50: struct cmd_if_shell_data {
1.30      nicm       51:        char                    *cmd_if;
                     52:        char                    *cmd_else;
                     53:
                     54:        struct cmd_q            *cmdq;
                     55:        struct mouse_event       mouse;
                     56:
                     57:        int                      bflag;
1.31      nicm       58:        int                      references;
1.5       nicm       59: };
1.1       nicm       60:
1.16      nicm       61: enum cmd_retval
1.20      nicm       62: cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       63: {
1.10      nicm       64:        struct args                     *args = self->args;
1.5       nicm       65:        struct cmd_if_shell_data        *cdata;
1.27      nicm       66:        char                            *shellcmd, *cmd, *cause;
                     67:        struct cmd_list                 *cmdlist;
1.38      nicm       68:        struct session                  *s = cmdq->state.tflag.s;
                     69:        struct winlink                  *wl = cmdq->state.tflag.wl;
                     70:        struct window_pane              *wp = cmdq->state.tflag.wp;
1.19      nicm       71:        struct format_tree              *ft;
1.34      nicm       72:        const char                      *cwd;
1.19      nicm       73:
1.38      nicm       74:        cwd = wp->cwd;
1.19      nicm       75:
1.38      nicm       76:        if (cmdq->client != NULL && cmdq->client->session == NULL)
                     77:                cwd = cmdq->client->cwd;
                     78:        else if (s != NULL)
                     79:                cwd = s->cwd;
                     80:        else
                     81:                cwd = NULL;
1.37      nicm       82:        ft = format_create(cmdq, 0);
1.28      nicm       83:        format_defaults(ft, NULL, s, wl, wp);
1.19      nicm       84:        shellcmd = format_expand(ft, args->argv[0]);
                     85:        format_free(ft);
1.27      nicm       86:
                     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);
                     96:                if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
                     97:                        if (cause != NULL) {
                     98:                                cmdq_error(cmdq, "%s", cause);
                     99:                                free(cause);
                    100:                        }
                    101:                        return (CMD_RETURN_ERROR);
                    102:                }
1.30      nicm      103:                cmdq_run(cmdq, cmdlist, &cmdq->item->mouse);
1.27      nicm      104:                cmd_list_free(cmdlist);
                    105:                return (CMD_RETURN_NORMAL);
                    106:        }
1.5       nicm      107:
                    108:        cdata = xmalloc(sizeof *cdata);
1.30      nicm      109:
1.13      nicm      110:        cdata->cmd_if = xstrdup(args->argv[1]);
1.14      nicm      111:        if (args->argc == 3)
                    112:                cdata->cmd_else = xstrdup(args->argv[2]);
1.13      nicm      113:        else
                    114:                cdata->cmd_else = NULL;
1.30      nicm      115:
1.20      nicm      116:        cdata->bflag = args_has(args, 'b');
1.5       nicm      117:
1.20      nicm      118:        cdata->cmdq = cmdq;
1.30      nicm      119:        memcpy(&cdata->mouse, &cmdq->item->mouse, sizeof cdata->mouse);
1.20      nicm      120:        cmdq->references++;
1.5       nicm      121:
1.31      nicm      122:        cdata->references = 1;
1.32      nicm      123:        job_run(shellcmd, s, cwd, cmd_if_shell_callback, cmd_if_shell_free,
                    124:            cdata);
1.19      nicm      125:        free(shellcmd);
1.1       nicm      126:
1.20      nicm      127:        if (cdata->bflag)
                    128:                return (CMD_RETURN_NORMAL);
                    129:        return (CMD_RETURN_WAIT);
1.1       nicm      130: }
                    131:
1.5       nicm      132: void
                    133: cmd_if_shell_callback(struct job *job)
1.1       nicm      134: {
1.5       nicm      135:        struct cmd_if_shell_data        *cdata = job->data;
1.20      nicm      136:        struct cmd_q                    *cmdq = cdata->cmdq, *cmdq1;
1.1       nicm      137:        struct cmd_list                 *cmdlist;
1.14      nicm      138:        char                            *cause, *cmd;
1.1       nicm      139:
1.33      nicm      140:        if (cmdq->flags & CMD_Q_DEAD)
1.20      nicm      141:                return;
                    142:
                    143:        if (!WIFEXITED(job->status) || WEXITSTATUS(job->status) != 0)
1.13      nicm      144:                cmd = cdata->cmd_else;
1.20      nicm      145:        else
1.13      nicm      146:                cmd = cdata->cmd_if;
1.20      nicm      147:        if (cmd == NULL)
                    148:                return;
                    149:
                    150:        if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
1.1       nicm      151:                if (cause != NULL) {
1.20      nicm      152:                        cmdq_error(cmdq, "%s", cause);
1.15      nicm      153:                        free(cause);
1.1       nicm      154:                }
1.5       nicm      155:                return;
1.1       nicm      156:        }
                    157:
1.20      nicm      158:        cmdq1 = cmdq_new(cmdq->client);
                    159:        cmdq1->emptyfn = cmd_if_shell_done;
                    160:        cmdq1->data = cdata;
                    161:
1.31      nicm      162:        cdata->references++;
1.30      nicm      163:        cmdq_run(cmdq1, cmdlist, &cdata->mouse);
1.1       nicm      164:        cmd_list_free(cmdlist);
                    165: }
                    166:
                    167: void
1.20      nicm      168: cmd_if_shell_done(struct cmd_q *cmdq1)
                    169: {
                    170:        struct cmd_if_shell_data        *cdata = cmdq1->data;
                    171:        struct cmd_q                    *cmdq = cdata->cmdq;
1.25      nicm      172:
                    173:        if (cmdq1->client_exit >= 0)
                    174:                cmdq->client_exit = cmdq1->client_exit;
1.31      nicm      175:        cmdq_free(cmdq1);
                    176:
                    177:        if (--cdata->references != 0)
                    178:                return;
1.20      nicm      179:
                    180:        if (!cmdq_free(cmdq) && !cdata->bflag)
                    181:                cmdq_continue(cmdq);
                    182:
                    183:        free(cdata->cmd_else);
                    184:        free(cdata->cmd_if);
                    185:        free(cdata);
                    186: }
                    187:
                    188: void
1.5       nicm      189: cmd_if_shell_free(void *data)
1.1       nicm      190: {
1.5       nicm      191:        struct cmd_if_shell_data        *cdata = data;
1.20      nicm      192:        struct cmd_q                    *cmdq = cdata->cmdq;
                    193:
1.31      nicm      194:        if (--cdata->references != 0)
1.20      nicm      195:                return;
1.1       nicm      196:
1.20      nicm      197:        if (!cmdq_free(cmdq) && !cdata->bflag)
                    198:                cmdq_continue(cmdq);
1.1       nicm      199:
1.15      nicm      200:        free(cdata->cmd_else);
                    201:        free(cdata->cmd_if);
                    202:        free(cdata);
1.1       nicm      203: }