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

1.71    ! nicm        1: /* $OpenBSD: cmd-run-shell.c,v 1.70 2021/01/01 08:36: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.41      nicm       32: static enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
1.14      nicm       33:
1.60      nicm       34: static void    cmd_run_shell_timer(int, short, void *);
1.38      nicm       35: static void    cmd_run_shell_callback(struct job *);
                     36: static void    cmd_run_shell_free(void *);
                     37: static void    cmd_run_shell_print(struct job *, const char *);
1.2       nicm       38:
1.1       nicm       39: const struct cmd_entry cmd_run_shell_entry = {
1.34      nicm       40:        .name = "run-shell",
                     41:        .alias = "run",
                     42:
1.70      nicm       43:        .args = { "bd:Ct:", 0, 1 },
                     44:        .usage = "[-bC] [-d delay] " CMD_TARGET_PANE_USAGE " [shell-command]",
1.34      nicm       45:
1.49      nicm       46:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.35      nicm       47:
                     48:        .flags = 0,
1.34      nicm       49:        .exec = cmd_run_shell_exec
1.1       nicm       50: };
                     51:
1.2       nicm       52: struct cmd_run_shell_data {
1.70      nicm       53:        struct client           *client;
1.41      nicm       54:        char                    *cmd;
1.70      nicm       55:        int                      shell;
1.60      nicm       56:        char                    *cwd;
1.41      nicm       57:        struct cmdq_item        *item;
1.60      nicm       58:        struct session          *s;
1.41      nicm       59:        int                      wp_id;
1.60      nicm       60:        struct event             timer;
1.69      nicm       61:        int                      flags;
1.70      nicm       62:        struct cmd_parse_input   pi;
1.2       nicm       63: };
                     64:
1.38      nicm       65: static void
1.14      nicm       66: cmd_run_shell_print(struct job *job, const char *msg)
                     67: {
1.54      nicm       68:        struct cmd_run_shell_data       *cdata = job_get_data(job);
1.21      nicm       69:        struct window_pane              *wp = NULL;
1.43      nicm       70:        struct cmd_find_state            fs;
1.58      nicm       71:        struct window_mode_entry        *wme;
1.14      nicm       72:
1.21      nicm       73:        if (cdata->wp_id != -1)
                     74:                wp = window_pane_find_by_id(cdata->wp_id);
1.43      nicm       75:        if (wp == NULL) {
                     76:                if (cdata->item != NULL) {
                     77:                        cmdq_print(cdata->item, "%s", msg);
                     78:                        return;
                     79:                }
1.51      nicm       80:                if (cmd_find_from_nothing(&fs, 0) != 0)
1.43      nicm       81:                        return;
                     82:                wp = fs.wp;
                     83:                if (wp == NULL)
                     84:                        return;
1.14      nicm       85:        }
                     86:
1.58      nicm       87:        wme = TAILQ_FIRST(&wp->modes);
                     88:        if (wme == NULL || wme->mode != &window_view_mode)
1.64      nicm       89:                window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
1.56      nicm       90:        window_copy_add(wp, "%s", msg);
1.14      nicm       91: }
                     92:
1.38      nicm       93: static enum cmd_retval
1.41      nicm       94: cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       95: {
1.65      nicm       96:        struct args                     *args = cmd_get_args(self);
1.66      nicm       97:        struct cmd_find_state           *target = cmdq_get_target(item);
1.2       nicm       98:        struct cmd_run_shell_data       *cdata;
1.70      nicm       99:        struct client                   *tc = cmdq_get_target_client(item);
1.66      nicm      100:        struct session                  *s = target->s;
                    101:        struct window_pane              *wp = target->wp;
1.60      nicm      102:        const char                      *delay;
                    103:        double                           d;
                    104:        struct timeval                   tv;
                    105:        char                            *end;
1.70      nicm      106:        int                              wait = !args_has(args, 'b');
                    107:
                    108:        if ((delay = args_get(args, 'd')) != NULL) {
                    109:                d = strtod(delay, &end);
                    110:                if (*end != '\0') {
                    111:                        cmdq_error(item, "invalid delay time: %s", delay);
                    112:                        return (CMD_RETURN_ERROR);
                    113:                }
                    114:        } else if (args->argc == 0)
                    115:                return (CMD_RETURN_NORMAL);
1.40      nicm      116:
1.37      nicm      117:        cdata = xcalloc(1, sizeof *cdata);
1.60      nicm      118:        if (args->argc != 0)
1.67      nicm      119:                cdata->cmd = format_single_from_target(item, args->argv[0]);
1.2       nicm      120:
1.70      nicm      121:        cdata->shell = !args_has(args, 'C');
                    122:        if (!cdata->shell) {
                    123:                memset(&cdata->pi, 0, sizeof cdata->pi);
                    124:                cmd_get_source(self, &cdata->pi.file, &cdata->pi.line);
                    125:                if (wait)
                    126:                        cdata->pi.item = item;
                    127:                cdata->pi.c = tc;
                    128:                cmd_find_copy_state(&cdata->pi.fs, target);
                    129:        }
                    130:
1.40      nicm      131:        if (args_has(args, 't') && wp != NULL)
                    132:                cdata->wp_id = wp->id;
                    133:        else
                    134:                cdata->wp_id = -1;
                    135:
1.70      nicm      136:        if (wait) {
                    137:                cdata->client = cmdq_get_client(item);
1.41      nicm      138:                cdata->item = item;
1.70      nicm      139:        } else {
                    140:                cdata->client = tc;
1.69      nicm      141:                cdata->flags |= JOB_NOWAIT;
1.70      nicm      142:        }
                    143:        if (cdata->client != NULL)
                    144:                cdata->client->references++;
1.2       nicm      145:
1.66      nicm      146:        cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
1.70      nicm      147:
1.60      nicm      148:        cdata->s = s;
1.61      nicm      149:        if (s != NULL)
                    150:                session_add_ref(s, __func__);
1.60      nicm      151:
                    152:        evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
1.70      nicm      153:        if (delay != NULL) {
1.60      nicm      154:                timerclear(&tv);
                    155:                tv.tv_sec = (time_t)d;
                    156:                tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
                    157:                evtimer_add(&cdata->timer, &tv);
                    158:        } else
1.70      nicm      159:                event_active(&cdata->timer, EV_TIMEOUT, 1);
1.2       nicm      160:
1.70      nicm      161:        if (!wait)
1.18      nicm      162:                return (CMD_RETURN_NORMAL);
                    163:        return (CMD_RETURN_WAIT);
1.2       nicm      164: }
1.1       nicm      165:
1.38      nicm      166: static void
1.60      nicm      167: cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
                    168: {
                    169:        struct cmd_run_shell_data       *cdata = arg;
1.70      nicm      170:        struct client                   *c = cdata->client;
                    171:        const char                      *cmd = cdata->cmd;
                    172:        char                            *error;
                    173:        struct cmdq_item                *item = cdata->item;
                    174:        enum cmd_parse_status            status;
1.60      nicm      175:
1.70      nicm      176:        if (cmd != NULL && cdata->shell) {
1.71    ! nicm      177:                if (job_run(cmd, 0, NULL, cdata->s, cdata->cwd, NULL,
1.69      nicm      178:                    cmd_run_shell_callback, cmd_run_shell_free, cdata,
                    179:                    cdata->flags, -1, -1) == NULL)
1.60      nicm      180:                        cmd_run_shell_free(cdata);
1.70      nicm      181:                return;
                    182:        }
                    183:
                    184:        if (cmd != NULL) {
                    185:                if (item != NULL) {
                    186:                        status = cmd_parse_and_insert(cmd, &cdata->pi, item,
                    187:                            cmdq_get_state(item), &error);
                    188:                } else {
                    189:                        status = cmd_parse_and_append(cmd, &cdata->pi, c, NULL,
                    190:                            &error);
                    191:                }
                    192:                if (status == CMD_PARSE_ERROR) {
                    193:                       cmdq_error(cdata->item, "%s", error);
                    194:                       free(error);
                    195:                }
1.60      nicm      196:        }
1.70      nicm      197:
                    198:        if (cdata->item != NULL)
                    199:                cmdq_continue(cdata->item);
                    200:        cmd_run_shell_free(cdata);
1.60      nicm      201: }
                    202:
                    203: static void
1.2       nicm      204: cmd_run_shell_callback(struct job *job)
                    205: {
1.54      nicm      206:        struct cmd_run_shell_data       *cdata = job_get_data(job);
                    207:        struct bufferevent              *event = job_get_event(job);
1.63      nicm      208:        struct cmdq_item                *item = cdata->item;
1.54      nicm      209:        char                            *cmd = cdata->cmd, *msg = NULL, *line;
1.5       nicm      210:        size_t                           size;
1.54      nicm      211:        int                              retcode, status;
1.7       nicm      212:
1.5       nicm      213:        do {
1.54      nicm      214:                if ((line = evbuffer_readline(event->input)) != NULL) {
1.18      nicm      215:                        cmd_run_shell_print(job, line);
1.16      nicm      216:                        free(line);
1.5       nicm      217:                }
                    218:        } while (line != NULL);
                    219:
1.54      nicm      220:        size = EVBUFFER_LENGTH(event->input);
1.5       nicm      221:        if (size != 0) {
                    222:                line = xmalloc(size + 1);
1.54      nicm      223:                memcpy(line, EVBUFFER_DATA(event->input), size);
1.5       nicm      224:                line[size] = '\0';
1.2       nicm      225:
1.14      nicm      226:                cmd_run_shell_print(job, line);
1.2       nicm      227:
1.12      nicm      228:                free(line);
1.1       nicm      229:        }
                    230:
1.54      nicm      231:        status = job_get_status(job);
                    232:        if (WIFEXITED(status)) {
                    233:                if ((retcode = WEXITSTATUS(status)) != 0)
1.2       nicm      234:                        xasprintf(&msg, "'%s' returned %d", cmd, retcode);
1.54      nicm      235:        } else if (WIFSIGNALED(status)) {
                    236:                retcode = WTERMSIG(status);
1.2       nicm      237:                xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.63      nicm      238:                retcode += 128;
1.68      nicm      239:        } else
                    240:                retcode = 0;
1.25      nicm      241:        if (msg != NULL)
                    242:                cmd_run_shell_print(job, msg);
                    243:        free(msg);
1.40      nicm      244:
1.63      nicm      245:        if (item != NULL) {
1.66      nicm      246:                if (cmdq_get_client(item) != NULL &&
                    247:                    cmdq_get_client(item)->session == NULL)
                    248:                        cmdq_get_client(item)->retval = retcode;
1.63      nicm      249:                cmdq_continue(item);
                    250:        }
1.2       nicm      251: }
                    252:
1.38      nicm      253: static void
1.2       nicm      254: cmd_run_shell_free(void *data)
                    255: {
                    256:        struct cmd_run_shell_data       *cdata = data;
                    257:
1.60      nicm      258:        evtimer_del(&cdata->timer);
1.61      nicm      259:        if (cdata->s != NULL)
                    260:                session_remove_ref(cdata->s, __func__);
1.70      nicm      261:        if (cdata->client != NULL)
                    262:                server_client_unref(cdata->client);
1.60      nicm      263:        free(cdata->cwd);
1.12      nicm      264:        free(cdata->cmd);
                    265:        free(cdata);
1.1       nicm      266: }