[BACK]Return to cmd-run-shell.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.38

1.38    ! nicm        1: /* $OpenBSD: cmd-run-shell.c,v 1.37 2016/10/09 08:06:51 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
1.3       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>
                     21: #include <sys/wait.h>
                     22:
1.12      nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: /*
                     29:  * Runs a command without a window.
                     30:  */
                     31:
1.38    ! nicm       32: static enum cmd_retval  cmd_run_shell_exec(struct cmd *, struct cmd_q *);
1.14      nicm       33:
1.38    ! nicm       34: static void    cmd_run_shell_callback(struct job *);
        !            35: static void    cmd_run_shell_free(void *);
        !            36: static void    cmd_run_shell_print(struct job *, const char *);
1.2       nicm       37:
1.1       nicm       38: const struct cmd_entry cmd_run_shell_entry = {
1.34      nicm       39:        .name = "run-shell",
                     40:        .alias = "run",
                     41:
                     42:        .args = { "bt:", 1, 1 },
                     43:        .usage = "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
                     44:
1.35      nicm       45:        .tflag = CMD_PANE_CANFAIL,
                     46:
                     47:        .flags = 0,
1.34      nicm       48:        .exec = cmd_run_shell_exec
1.1       nicm       49: };
                     50:
1.2       nicm       51: struct cmd_run_shell_data {
                     52:        char            *cmd;
1.18      nicm       53:        struct cmd_q    *cmdq;
                     54:        int              bflag;
1.21      nicm       55:        int              wp_id;
1.2       nicm       56: };
                     57:
1.38    ! nicm       58: static void
1.14      nicm       59: cmd_run_shell_print(struct job *job, const char *msg)
                     60: {
                     61:        struct cmd_run_shell_data       *cdata = job->data;
1.21      nicm       62:        struct window_pane              *wp = NULL;
1.14      nicm       63:
1.21      nicm       64:        if (cdata->wp_id != -1)
                     65:                wp = window_pane_find_by_id(cdata->wp_id);
1.14      nicm       66:        if (wp == NULL) {
1.18      nicm       67:                cmdq_print(cdata->cmdq, "%s", msg);
1.14      nicm       68:                return;
                     69:        }
                     70:
                     71:        if (window_pane_set_mode(wp, &window_copy_mode) == 0)
                     72:                window_copy_init_for_output(wp);
                     73:        if (wp->mode == &window_copy_mode)
                     74:                window_copy_add(wp, "%s", msg);
                     75: }
                     76:
1.38    ! nicm       77: static enum cmd_retval
1.18      nicm       78: cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       79: {
1.10      nicm       80:        struct args                     *args = self->args;
1.2       nicm       81:        struct cmd_run_shell_data       *cdata;
1.17      nicm       82:        char                            *shellcmd;
1.33      nicm       83:        struct session                  *s = cmdq->state.tflag.s;
                     84:        struct winlink                  *wl = cmdq->state.tflag.wl;
                     85:        struct window_pane              *wp = cmdq->state.tflag.wp;
1.17      nicm       86:        struct format_tree              *ft;
1.30      nicm       87:        const char                      *cwd;
1.14      nicm       88:
1.33      nicm       89:        if (cmdq->client != NULL && cmdq->client->session == NULL)
                     90:                cwd = cmdq->client->cwd;
                     91:        else if (s != NULL)
                     92:                cwd = s->cwd;
                     93:        else
                     94:                cwd = NULL;
1.32      nicm       95:        ft = format_create(cmdq, 0);
1.36      nicm       96:        format_defaults(ft, cmdq->state.c, s, wl, wp);
1.17      nicm       97:        shellcmd = format_expand(ft, args->argv[0]);
                     98:        format_free(ft);
                     99:
1.37      nicm      100:        cdata = xcalloc(1, sizeof *cdata);
1.17      nicm      101:        cdata->cmd = shellcmd;
1.18      nicm      102:        cdata->bflag = args_has(args, 'b');
1.21      nicm      103:        cdata->wp_id = wp != NULL ? (int) wp->id : -1;
1.2       nicm      104:
1.18      nicm      105:        cdata->cmdq = cmdq;
                    106:        cmdq->references++;
1.2       nicm      107:
1.28      nicm      108:        job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
                    109:            cdata);
1.2       nicm      110:
1.18      nicm      111:        if (cdata->bflag)
                    112:                return (CMD_RETURN_NORMAL);
                    113:        return (CMD_RETURN_WAIT);
1.2       nicm      114: }
1.1       nicm      115:
1.38    ! nicm      116: static void
1.2       nicm      117: cmd_run_shell_callback(struct job *job)
                    118: {
                    119:        struct cmd_run_shell_data       *cdata = job->data;
1.18      nicm      120:        struct cmd_q                    *cmdq = cdata->cmdq;
1.5       nicm      121:        char                            *cmd, *msg, *line;
                    122:        size_t                           size;
1.2       nicm      123:        int                              retcode;
1.5       nicm      124:        u_int                            lines;
1.7       nicm      125:
1.29      nicm      126:        if (cmdq->flags & CMD_Q_DEAD)
1.7       nicm      127:                return;
1.18      nicm      128:        cmd = cdata->cmd;
1.2       nicm      129:
1.5       nicm      130:        lines = 0;
                    131:        do {
                    132:                if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.18      nicm      133:                        cmd_run_shell_print(job, line);
1.16      nicm      134:                        free(line);
1.5       nicm      135:                        lines++;
                    136:                }
                    137:        } while (line != NULL);
                    138:
                    139:        size = EVBUFFER_LENGTH(job->event->input);
                    140:        if (size != 0) {
                    141:                line = xmalloc(size + 1);
                    142:                memcpy(line, EVBUFFER_DATA(job->event->input), size);
                    143:                line[size] = '\0';
1.2       nicm      144:
1.14      nicm      145:                cmd_run_shell_print(job, line);
1.5       nicm      146:                lines++;
1.2       nicm      147:
1.12      nicm      148:                free(line);
1.1       nicm      149:        }
                    150:
                    151:        msg = NULL;
1.2       nicm      152:        if (WIFEXITED(job->status)) {
                    153:                if ((retcode = WEXITSTATUS(job->status)) != 0)
                    154:                        xasprintf(&msg, "'%s' returned %d", cmd, retcode);
                    155:        } else if (WIFSIGNALED(job->status)) {
                    156:                retcode = WTERMSIG(job->status);
                    157:                xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1       nicm      158:        }
1.25      nicm      159:        if (msg != NULL)
                    160:                cmd_run_shell_print(job, msg);
                    161:        free(msg);
1.2       nicm      162: }
                    163:
1.38    ! nicm      164: static void
1.2       nicm      165: cmd_run_shell_free(void *data)
                    166: {
                    167:        struct cmd_run_shell_data       *cdata = data;
1.18      nicm      168:        struct cmd_q                    *cmdq = cdata->cmdq;
1.2       nicm      169:
1.18      nicm      170:        if (!cmdq_free(cmdq) && !cdata->bflag)
                    171:                cmdq_continue(cmdq);
1.2       nicm      172:
1.12      nicm      173:        free(cdata->cmd);
                    174:        free(cdata);
1.1       nicm      175: }