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

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