Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.14
1.14 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.13 2012/07/11 07:10:15 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.13 nicm 32: enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_ctx *);
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.14 ! nicm 40: "t:", 1, 1,
! 41: CMD_TARGET_PANE_USAGE " 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;
50: struct cmd_ctx ctx;
1.14 ! nicm 51: u_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;
! 58: struct cmd_ctx *ctx = &cdata->ctx;
! 59: struct window_pane *wp;
! 60:
! 61: wp = window_pane_find_by_id(cdata->wp_id);
! 62: if (wp == NULL) {
! 63: ctx->print(ctx, "%s", msg);
! 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.1 nicm 74: cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
75: {
1.10 nicm 76: struct args *args = self->args;
1.2 nicm 77: struct cmd_run_shell_data *cdata;
1.11 nicm 78: const char *shellcmd = args->argv[0];
1.14 ! nicm 79: struct window_pane *wp;
! 80:
! 81: if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
! 82: return (CMD_RETURN_ERROR);
1.2 nicm 83:
84: cdata = xmalloc(sizeof *cdata);
1.10 nicm 85: cdata->cmd = xstrdup(args->argv[0]);
1.14 ! nicm 86: cdata->wp_id = wp->id;
1.2 nicm 87: memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
88:
89: if (ctx->cmdclient != NULL)
90: ctx->cmdclient->references++;
91: if (ctx->curclient != NULL)
92: ctx->curclient->references++;
93:
1.11 nicm 94: job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata);
1.2 nicm 95:
1.13 nicm 96: return (CMD_RETURN_YIELD); /* don't let client exit */
1.2 nicm 97: }
1.1 nicm 98:
1.2 nicm 99: void
100: cmd_run_shell_callback(struct job *job)
101: {
102: struct cmd_run_shell_data *cdata = job->data;
103: struct cmd_ctx *ctx = &cdata->ctx;
1.5 nicm 104: char *cmd, *msg, *line;
105: size_t size;
1.2 nicm 106: int retcode;
1.5 nicm 107: u_int lines;
1.7 nicm 108:
109: if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD)
110: return;
111: if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD)
112: return;
1.2 nicm 113:
1.5 nicm 114: lines = 0;
115: do {
116: if ((line = evbuffer_readline(job->event->input)) != NULL) {
1.14 ! nicm 117: cmd_run_shell_print (job, line);
1.5 nicm 118: lines++;
119: }
120: } while (line != NULL);
121:
122: size = EVBUFFER_LENGTH(job->event->input);
123: if (size != 0) {
124: line = xmalloc(size + 1);
125: memcpy(line, EVBUFFER_DATA(job->event->input), size);
126: line[size] = '\0';
1.2 nicm 127:
1.14 ! nicm 128: cmd_run_shell_print(job, line);
1.5 nicm 129: lines++;
1.2 nicm 130:
1.12 nicm 131: free(line);
1.1 nicm 132: }
133:
1.5 nicm 134: cmd = cdata->cmd;
135:
1.1 nicm 136: msg = NULL;
1.2 nicm 137: if (WIFEXITED(job->status)) {
138: if ((retcode = WEXITSTATUS(job->status)) != 0)
139: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
140: } else if (WIFSIGNALED(job->status)) {
141: retcode = WTERMSIG(job->status);
142: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1 nicm 143: }
144: if (msg != NULL) {
1.14 ! nicm 145: if (lines == 0)
! 146: ctx->info(ctx, "%s", msg);
1.1 nicm 147: else
1.14 ! nicm 148: cmd_run_shell_print(job, msg);
1.12 nicm 149: free(msg);
1.1 nicm 150: }
1.2 nicm 151: }
152:
153: void
154: cmd_run_shell_free(void *data)
155: {
156: struct cmd_run_shell_data *cdata = data;
157: struct cmd_ctx *ctx = &cdata->ctx;
158:
159: if (ctx->cmdclient != NULL) {
160: ctx->cmdclient->references--;
1.9 nicm 161: ctx->cmdclient->flags |= CLIENT_EXIT;
1.2 nicm 162: }
163: if (ctx->curclient != NULL)
164: ctx->curclient->references--;
165:
1.12 nicm 166: free(cdata->cmd);
167: free(cdata);
1.1 nicm 168: }