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

1.69    ! nicm        1: /* $OpenBSD: cmd-run-shell.c,v 1.68 2020/05/16 16:02:24 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.60      nicm       43:        .args = { "bd:t:", 0, 1 },
                     44:        .usage = "[-b] [-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.41      nicm       53:        char                    *cmd;
1.60      nicm       54:        char                    *cwd;
1.41      nicm       55:        struct cmdq_item        *item;
1.60      nicm       56:        struct session          *s;
1.41      nicm       57:        int                      wp_id;
1.60      nicm       58:        struct event             timer;
1.69    ! nicm       59:        int                      flags;
1.2       nicm       60: };
                     61:
1.38      nicm       62: static void
1.14      nicm       63: cmd_run_shell_print(struct job *job, const char *msg)
                     64: {
1.54      nicm       65:        struct cmd_run_shell_data       *cdata = job_get_data(job);
1.21      nicm       66:        struct window_pane              *wp = NULL;
1.43      nicm       67:        struct cmd_find_state            fs;
1.58      nicm       68:        struct window_mode_entry        *wme;
1.14      nicm       69:
1.21      nicm       70:        if (cdata->wp_id != -1)
                     71:                wp = window_pane_find_by_id(cdata->wp_id);
1.43      nicm       72:        if (wp == NULL) {
                     73:                if (cdata->item != NULL) {
                     74:                        cmdq_print(cdata->item, "%s", msg);
                     75:                        return;
                     76:                }
1.51      nicm       77:                if (cmd_find_from_nothing(&fs, 0) != 0)
1.43      nicm       78:                        return;
                     79:                wp = fs.wp;
                     80:                if (wp == NULL)
                     81:                        return;
1.14      nicm       82:        }
                     83:
1.58      nicm       84:        wme = TAILQ_FIRST(&wp->modes);
                     85:        if (wme == NULL || wme->mode != &window_view_mode)
1.64      nicm       86:                window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
1.56      nicm       87:        window_copy_add(wp, "%s", msg);
1.14      nicm       88: }
                     89:
1.38      nicm       90: static enum cmd_retval
1.41      nicm       91: cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       92: {
1.65      nicm       93:        struct args                     *args = cmd_get_args(self);
1.66      nicm       94:        struct cmd_find_state           *target = cmdq_get_target(item);
1.2       nicm       95:        struct cmd_run_shell_data       *cdata;
1.66      nicm       96:        struct session                  *s = target->s;
                     97:        struct window_pane              *wp = target->wp;
1.60      nicm       98:        const char                      *delay;
                     99:        double                           d;
                    100:        struct timeval                   tv;
                    101:        char                            *end;
1.40      nicm      102:
1.37      nicm      103:        cdata = xcalloc(1, sizeof *cdata);
1.60      nicm      104:        if (args->argc != 0)
1.67      nicm      105:                cdata->cmd = format_single_from_target(item, args->argv[0]);
1.2       nicm      106:
1.40      nicm      107:        if (args_has(args, 't') && wp != NULL)
                    108:                cdata->wp_id = wp->id;
                    109:        else
                    110:                cdata->wp_id = -1;
                    111:
                    112:        if (!args_has(args, 'b'))
1.41      nicm      113:                cdata->item = item;
1.69    ! nicm      114:        else
        !           115:                cdata->flags |= JOB_NOWAIT;
1.2       nicm      116:
1.66      nicm      117:        cdata->cwd = xstrdup(server_client_get_cwd(cmdq_get_client(item), s));
1.60      nicm      118:        cdata->s = s;
1.61      nicm      119:        if (s != NULL)
                    120:                session_add_ref(s, __func__);
1.60      nicm      121:
                    122:        evtimer_set(&cdata->timer, cmd_run_shell_timer, cdata);
                    123:
                    124:        if ((delay = args_get(args, 'd')) != NULL) {
                    125:                d = strtod(delay, &end);
                    126:                if (*end != '\0') {
                    127:                        cmdq_error(item, "invalid delay time: %s", delay);
                    128:                        cmd_run_shell_free(cdata);
                    129:                        return (CMD_RETURN_ERROR);
                    130:                }
                    131:                timerclear(&tv);
                    132:                tv.tv_sec = (time_t)d;
                    133:                tv.tv_usec = (d - (double)tv.tv_sec) * 1000000U;
                    134:                evtimer_add(&cdata->timer, &tv);
                    135:        } else
                    136:                cmd_run_shell_timer(-1, 0, cdata);
1.2       nicm      137:
1.40      nicm      138:        if (args_has(args, 'b'))
1.18      nicm      139:                return (CMD_RETURN_NORMAL);
                    140:        return (CMD_RETURN_WAIT);
1.2       nicm      141: }
1.1       nicm      142:
1.38      nicm      143: static void
1.60      nicm      144: cmd_run_shell_timer(__unused int fd, __unused short events, void* arg)
                    145: {
                    146:        struct cmd_run_shell_data       *cdata = arg;
                    147:
                    148:        if (cdata->cmd != NULL) {
                    149:                if (job_run(cdata->cmd, cdata->s, cdata->cwd, NULL,
1.69    ! nicm      150:                    cmd_run_shell_callback, cmd_run_shell_free, cdata,
        !           151:                    cdata->flags, -1, -1) == NULL)
1.60      nicm      152:                        cmd_run_shell_free(cdata);
                    153:        } else {
                    154:                if (cdata->item != NULL)
                    155:                        cmdq_continue(cdata->item);
                    156:                cmd_run_shell_free(cdata);
                    157:        }
                    158: }
                    159:
                    160: static void
1.2       nicm      161: cmd_run_shell_callback(struct job *job)
                    162: {
1.54      nicm      163:        struct cmd_run_shell_data       *cdata = job_get_data(job);
                    164:        struct bufferevent              *event = job_get_event(job);
1.63      nicm      165:        struct cmdq_item                *item = cdata->item;
1.54      nicm      166:        char                            *cmd = cdata->cmd, *msg = NULL, *line;
1.5       nicm      167:        size_t                           size;
1.54      nicm      168:        int                              retcode, status;
1.7       nicm      169:
1.5       nicm      170:        do {
1.54      nicm      171:                if ((line = evbuffer_readline(event->input)) != NULL) {
1.18      nicm      172:                        cmd_run_shell_print(job, line);
1.16      nicm      173:                        free(line);
1.5       nicm      174:                }
                    175:        } while (line != NULL);
                    176:
1.54      nicm      177:        size = EVBUFFER_LENGTH(event->input);
1.5       nicm      178:        if (size != 0) {
                    179:                line = xmalloc(size + 1);
1.54      nicm      180:                memcpy(line, EVBUFFER_DATA(event->input), size);
1.5       nicm      181:                line[size] = '\0';
1.2       nicm      182:
1.14      nicm      183:                cmd_run_shell_print(job, line);
1.2       nicm      184:
1.12      nicm      185:                free(line);
1.1       nicm      186:        }
                    187:
1.54      nicm      188:        status = job_get_status(job);
                    189:        if (WIFEXITED(status)) {
                    190:                if ((retcode = WEXITSTATUS(status)) != 0)
1.2       nicm      191:                        xasprintf(&msg, "'%s' returned %d", cmd, retcode);
1.54      nicm      192:        } else if (WIFSIGNALED(status)) {
                    193:                retcode = WTERMSIG(status);
1.2       nicm      194:                xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.63      nicm      195:                retcode += 128;
1.68      nicm      196:        } else
                    197:                retcode = 0;
1.25      nicm      198:        if (msg != NULL)
                    199:                cmd_run_shell_print(job, msg);
                    200:        free(msg);
1.40      nicm      201:
1.63      nicm      202:        if (item != NULL) {
1.66      nicm      203:                if (cmdq_get_client(item) != NULL &&
                    204:                    cmdq_get_client(item)->session == NULL)
                    205:                        cmdq_get_client(item)->retval = retcode;
1.63      nicm      206:                cmdq_continue(item);
                    207:        }
1.2       nicm      208: }
                    209:
1.38      nicm      210: static void
1.2       nicm      211: cmd_run_shell_free(void *data)
                    212: {
                    213:        struct cmd_run_shell_data       *cdata = data;
                    214:
1.60      nicm      215:        evtimer_del(&cdata->timer);
1.61      nicm      216:        if (cdata->s != NULL)
                    217:                session_remove_ref(cdata->s, __func__);
1.60      nicm      218:        free(cdata->cwd);
1.12      nicm      219:        free(cdata->cmd);
                    220:        free(cdata);
1.1       nicm      221: }