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

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