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

1.31    ! nicm        1: /* $OpenBSD: cmd-run-shell.c,v 1.30 2015/10/31 08:13:58 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.18      nicm       32: enum cmd_retval         cmd_run_shell_exec(struct cmd *, struct cmd_q *);
1.14      nicm       33:
                     34: void   cmd_run_shell_callback(struct job *);
                     35: void   cmd_run_shell_free(void *);
                     36: 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 = {
                     39:        "run-shell", "run",
1.18      nicm       40:        "bt:", 1, 1,
                     41:        "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
1.10      nicm       42:        0,
                     43:        cmd_run_shell_exec
1.1       nicm       44: };
                     45:
1.2       nicm       46: struct cmd_run_shell_data {
                     47:        char            *cmd;
1.18      nicm       48:        struct cmd_q    *cmdq;
                     49:        int              bflag;
1.21      nicm       50:        int              wp_id;
1.2       nicm       51: };
                     52:
1.14      nicm       53: void
                     54: cmd_run_shell_print(struct job *job, const char *msg)
                     55: {
                     56:        struct cmd_run_shell_data       *cdata = job->data;
1.21      nicm       57:        struct window_pane              *wp = NULL;
1.14      nicm       58:
1.21      nicm       59:        if (cdata->wp_id != -1)
                     60:                wp = window_pane_find_by_id(cdata->wp_id);
1.14      nicm       61:        if (wp == NULL) {
1.18      nicm       62:                cmdq_print(cdata->cmdq, "%s", msg);
1.14      nicm       63:                return;
                     64:        }
                     65:
                     66:        if (window_pane_set_mode(wp, &window_copy_mode) == 0)
                     67:                window_copy_init_for_output(wp);
                     68:        if (wp->mode == &window_copy_mode)
                     69:                window_copy_add(wp, "%s", msg);
                     70: }
                     71:
1.13      nicm       72: enum cmd_retval
1.18      nicm       73: cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       74: {
1.10      nicm       75:        struct args                     *args = self->args;
1.2       nicm       76:        struct cmd_run_shell_data       *cdata;
1.17      nicm       77:        char                            *shellcmd;
1.23      nicm       78:        struct client                   *c;
1.21      nicm       79:        struct session                  *s = NULL;
                     80:        struct winlink                  *wl = NULL;
                     81:        struct window_pane              *wp = NULL;
1.17      nicm       82:        struct format_tree              *ft;
1.30      nicm       83:        const char                      *cwd;
1.14      nicm       84:
1.28      nicm       85:        if (args_has(args, 't')) {
1.21      nicm       86:                wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
1.28      nicm       87:                cwd = wp->cwd;
                     88:        } else {
1.23      nicm       89:                c = cmd_find_client(cmdq, NULL, 1);
                     90:                if (c != NULL && c->session != NULL) {
                     91:                        s = c->session;
                     92:                        wl = s->curw;
                     93:                        wp = wl->window->active;
                     94:                }
1.28      nicm       95:                if (cmdq->client != NULL && cmdq->client->session == NULL)
                     96:                        cwd = cmdq->client->cwd;
                     97:                else if (s != NULL)
                     98:                        cwd = s->cwd;
                     99:                else
1.30      nicm      100:                        cwd = NULL;
1.23      nicm      101:        }
1.2       nicm      102:
1.31    ! nicm      103:        ft = format_create(0);
1.27      nicm      104:        format_defaults(ft, NULL, s, wl, wp);
1.17      nicm      105:        shellcmd = format_expand(ft, args->argv[0]);
                    106:        format_free(ft);
                    107:
1.2       nicm      108:        cdata = xmalloc(sizeof *cdata);
1.17      nicm      109:        cdata->cmd = shellcmd;
1.18      nicm      110:        cdata->bflag = args_has(args, 'b');
1.21      nicm      111:        cdata->wp_id = wp != NULL ? (int) wp->id : -1;
1.2       nicm      112:
1.18      nicm      113:        cdata->cmdq = cmdq;
                    114:        cmdq->references++;
1.2       nicm      115:
1.28      nicm      116:        job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
                    117:            cdata);
1.2       nicm      118:
1.18      nicm      119:        if (cdata->bflag)
                    120:                return (CMD_RETURN_NORMAL);
                    121:        return (CMD_RETURN_WAIT);
1.2       nicm      122: }
1.1       nicm      123:
1.2       nicm      124: void
                    125: cmd_run_shell_callback(struct job *job)
                    126: {
                    127:        struct cmd_run_shell_data       *cdata = job->data;
1.18      nicm      128:        struct cmd_q                    *cmdq = cdata->cmdq;
1.5       nicm      129:        char                            *cmd, *msg, *line;
                    130:        size_t                           size;
1.2       nicm      131:        int                              retcode;
1.5       nicm      132:        u_int                            lines;
1.7       nicm      133:
1.29      nicm      134:        if (cmdq->flags & CMD_Q_DEAD)
1.7       nicm      135:                return;
1.18      nicm      136:        cmd = cdata->cmd;
1.2       nicm      137:
1.5       nicm      138:        lines = 0;
                    139:        do {
                    140:                if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.18      nicm      141:                        cmd_run_shell_print(job, line);
1.16      nicm      142:                        free(line);
1.5       nicm      143:                        lines++;
                    144:                }
                    145:        } while (line != NULL);
                    146:
                    147:        size = EVBUFFER_LENGTH(job->event->input);
                    148:        if (size != 0) {
                    149:                line = xmalloc(size + 1);
                    150:                memcpy(line, EVBUFFER_DATA(job->event->input), size);
                    151:                line[size] = '\0';
1.2       nicm      152:
1.14      nicm      153:                cmd_run_shell_print(job, line);
1.5       nicm      154:                lines++;
1.2       nicm      155:
1.12      nicm      156:                free(line);
1.1       nicm      157:        }
                    158:
                    159:        msg = NULL;
1.2       nicm      160:        if (WIFEXITED(job->status)) {
                    161:                if ((retcode = WEXITSTATUS(job->status)) != 0)
                    162:                        xasprintf(&msg, "'%s' returned %d", cmd, retcode);
                    163:        } else if (WIFSIGNALED(job->status)) {
                    164:                retcode = WTERMSIG(job->status);
                    165:                xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1       nicm      166:        }
1.25      nicm      167:        if (msg != NULL)
                    168:                cmd_run_shell_print(job, msg);
                    169:        free(msg);
1.2       nicm      170: }
                    171:
                    172: void
                    173: cmd_run_shell_free(void *data)
                    174: {
                    175:        struct cmd_run_shell_data       *cdata = data;
1.18      nicm      176:        struct cmd_q                    *cmdq = cdata->cmdq;
1.2       nicm      177:
1.18      nicm      178:        if (!cmdq_free(cmdq) && !cdata->bflag)
                    179:                cmdq_continue(cmdq);
1.2       nicm      180:
1.12      nicm      181:        free(cdata->cmd);
                    182:        free(cdata);
1.1       nicm      183: }