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

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