Annotation of src/usr.bin/tmux/cmd-run-shell.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: cmd-run-shell.c,v 1.6 2009/11/13 19:53:29 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:
23: #include <string.h>
24:
25: #include "tmux.h"
26:
27: /*
28: * Runs a command without a window.
29: */
30:
31: int cmd_run_shell_exec(struct cmd *, struct cmd_ctx *);
32:
1.2 nicm 33: void cmd_run_shell_callback(struct job *);
34: void cmd_run_shell_free(void *);
35:
1.1 nicm 36: const struct cmd_entry cmd_run_shell_entry = {
37: "run-shell", "run",
38: "command",
1.6 nicm 39: CMD_ARG1, "",
1.1 nicm 40: cmd_target_init,
41: cmd_target_parse,
42: cmd_run_shell_exec,
43: cmd_target_free,
44: cmd_target_print
45: };
46:
1.2 nicm 47: struct cmd_run_shell_data {
48: char *cmd;
49: struct cmd_ctx ctx;
50: };
51:
1.1 nicm 52: int
53: cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
54: {
1.2 nicm 55: struct cmd_target_data *data = self->data;
56: struct cmd_run_shell_data *cdata;
57: struct job *job;
58:
59: cdata = xmalloc(sizeof *cdata);
60: cdata->cmd = xstrdup(data->arg);
61: memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
62:
63: if (ctx->cmdclient != NULL)
64: ctx->cmdclient->references++;
65: if (ctx->curclient != NULL)
66: ctx->curclient->references++;
67:
1.4 nicm 68: job = job_add(NULL, 0, NULL,
1.2 nicm 69: data->arg, cmd_run_shell_callback, cmd_run_shell_free, cdata);
70: job_run(job);
71:
72: return (1); /* don't let client exit */
73: }
1.1 nicm 74:
1.2 nicm 75: void
76: cmd_run_shell_callback(struct job *job)
77: {
78: struct cmd_run_shell_data *cdata = job->data;
79: struct cmd_ctx *ctx = &cdata->ctx;
1.5 nicm 80: char *cmd, *msg, *line;
81: size_t size;
1.2 nicm 82: int retcode;
1.5 nicm 83: u_int lines;
1.7 ! nicm 84:
! 85: if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD)
! 86: return;
! 87: if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD)
! 88: return;
1.2 nicm 89:
1.5 nicm 90: lines = 0;
91: do {
92: if ((line = evbuffer_readline(job->event->input)) != NULL) {
93: ctx->print(ctx, "%s", line);
94: lines++;
95: }
96: } while (line != NULL);
97:
98: size = EVBUFFER_LENGTH(job->event->input);
99: if (size != 0) {
100: line = xmalloc(size + 1);
101: memcpy(line, EVBUFFER_DATA(job->event->input), size);
102: line[size] = '\0';
1.2 nicm 103:
1.5 nicm 104: ctx->print(ctx, "%s", line);
105: lines++;
1.2 nicm 106:
1.5 nicm 107: xfree(line);
1.1 nicm 108: }
109:
1.5 nicm 110: cmd = cdata->cmd;
111:
1.1 nicm 112: msg = NULL;
1.2 nicm 113: if (WIFEXITED(job->status)) {
114: if ((retcode = WEXITSTATUS(job->status)) != 0)
115: xasprintf(&msg, "'%s' returned %d", cmd, retcode);
116: } else if (WIFSIGNALED(job->status)) {
117: retcode = WTERMSIG(job->status);
118: xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
1.1 nicm 119: }
120: if (msg != NULL) {
1.5 nicm 121: if (lines != 0)
1.1 nicm 122: ctx->print(ctx, "%s", msg);
123: else
124: ctx->info(ctx, "%s", msg);
125: xfree(msg);
126: }
1.2 nicm 127: }
128:
129: void
130: cmd_run_shell_free(void *data)
131: {
132: struct cmd_run_shell_data *cdata = data;
133: struct cmd_ctx *ctx = &cdata->ctx;
134:
135: if (ctx->cmdclient != NULL) {
136: ctx->cmdclient->references--;
137: server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
138: }
139: if (ctx->curclient != NULL)
140: ctx->curclient->references--;
141:
142: xfree(cdata->cmd);
143: xfree(cdata);
1.1 nicm 144: }