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

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