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

1.56    ! nicm        1: /* $OpenBSD: cmd-run-shell.c,v 1.55 2018/08/27 11:03:34 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.41      nicm       32: static enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
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.49      nicm       45:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.35      nicm       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 {
1.41      nicm       52:        char                    *cmd;
                     53:        struct cmdq_item        *item;
                     54:        int                      wp_id;
1.2       nicm       55: };
                     56:
1.38      nicm       57: static void
1.14      nicm       58: cmd_run_shell_print(struct job *job, const char *msg)
                     59: {
1.54      nicm       60:        struct cmd_run_shell_data       *cdata = job_get_data(job);
1.21      nicm       61:        struct window_pane              *wp = NULL;
1.43      nicm       62:        struct cmd_find_state            fs;
1.14      nicm       63:
1.21      nicm       64:        if (cdata->wp_id != -1)
                     65:                wp = window_pane_find_by_id(cdata->wp_id);
1.43      nicm       66:        if (wp == NULL) {
                     67:                if (cdata->item != NULL) {
                     68:                        cmdq_print(cdata->item, "%s", msg);
                     69:                        return;
                     70:                }
1.51      nicm       71:                if (cmd_find_from_nothing(&fs, 0) != 0)
1.43      nicm       72:                        return;
                     73:                wp = fs.wp;
                     74:                if (wp == NULL)
                     75:                        return;
1.14      nicm       76:        }
                     77:
1.56    ! nicm       78:        window_copy_init_for_output(wp);
        !            79:        window_copy_add(wp, "%s", msg);
1.14      nicm       80: }
                     81:
1.38      nicm       82: static enum cmd_retval
1.41      nicm       83: cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       84: {
1.10      nicm       85:        struct args                     *args = self->args;
1.2       nicm       86:        struct cmd_run_shell_data       *cdata;
1.49      nicm       87:        struct client                   *c = cmd_find_client(item, NULL, 1);
                     88:        struct session                  *s = item->target.s;
                     89:        struct winlink                  *wl = item->target.wl;
                     90:        struct window_pane              *wp = item->target.wp;
1.40      nicm       91:
1.37      nicm       92:        cdata = xcalloc(1, sizeof *cdata);
1.46      nicm       93:        cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp);
1.2       nicm       94:
1.40      nicm       95:        if (args_has(args, 't') && wp != NULL)
                     96:                cdata->wp_id = wp->id;
                     97:        else
                     98:                cdata->wp_id = -1;
                     99:
                    100:        if (!args_has(args, 'b'))
1.41      nicm      101:                cdata->item = item;
1.2       nicm      102:
1.55      nicm      103:        if (job_run(cdata->cmd, s, server_client_get_cwd(item->client, s), NULL,
                    104:            cmd_run_shell_callback, cmd_run_shell_free, cdata, 0) == NULL) {
                    105:                cmdq_error(item, "failed to run command: %s", cdata->cmd);
                    106:                free(cdata);
                    107:                return (CMD_RETURN_ERROR);
                    108:        }
1.2       nicm      109:
1.40      nicm      110:        if (args_has(args, 'b'))
1.18      nicm      111:                return (CMD_RETURN_NORMAL);
                    112:        return (CMD_RETURN_WAIT);
1.2       nicm      113: }
1.1       nicm      114:
1.38      nicm      115: static void
1.2       nicm      116: cmd_run_shell_callback(struct job *job)
                    117: {
1.54      nicm      118:        struct cmd_run_shell_data       *cdata = job_get_data(job);
                    119:        struct bufferevent              *event = job_get_event(job);
                    120:        char                            *cmd = cdata->cmd, *msg = NULL, *line;
1.5       nicm      121:        size_t                           size;
1.54      nicm      122:        int                              retcode, status;
1.7       nicm      123:
1.5       nicm      124:        do {
1.54      nicm      125:                if ((line = evbuffer_readline(event->input)) != NULL) {
1.18      nicm      126:                        cmd_run_shell_print(job, line);
1.16      nicm      127:                        free(line);
1.5       nicm      128:                }
                    129:        } while (line != NULL);
                    130:
1.54      nicm      131:        size = EVBUFFER_LENGTH(event->input);
1.5       nicm      132:        if (size != 0) {
                    133:                line = xmalloc(size + 1);
1.54      nicm      134:                memcpy(line, EVBUFFER_DATA(event->input), size);
1.5       nicm      135:                line[size] = '\0';
1.2       nicm      136:
1.14      nicm      137:                cmd_run_shell_print(job, line);
1.2       nicm      138:
1.12      nicm      139:                free(line);
1.1       nicm      140:        }
                    141:
1.54      nicm      142:        status = job_get_status(job);
                    143:        if (WIFEXITED(status)) {
                    144:                if ((retcode = WEXITSTATUS(status)) != 0)
1.2       nicm      145:                        xasprintf(&msg, "'%s' returned %d", cmd, retcode);
1.54      nicm      146:        } else if (WIFSIGNALED(status)) {
                    147:                retcode = WTERMSIG(status);
1.2       nicm      148:                xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1       nicm      149:        }
1.25      nicm      150:        if (msg != NULL)
                    151:                cmd_run_shell_print(job, msg);
                    152:        free(msg);
1.40      nicm      153:
1.41      nicm      154:        if (cdata->item != NULL)
                    155:                cdata->item->flags &= ~CMDQ_WAITING;
1.2       nicm      156: }
                    157:
1.38      nicm      158: static void
1.2       nicm      159: cmd_run_shell_free(void *data)
                    160: {
                    161:        struct cmd_run_shell_data       *cdata = data;
                    162:
1.12      nicm      163:        free(cdata->cmd);
                    164:        free(cdata);
1.1       nicm      165: }