Skip to content
Newer
Older
100644 142 lines (116 sloc) 3.35 KB
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
1 /* $Id: cmd-run-shell.c 2553 2011-07-09 09:42:33Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
2
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
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
33 void cmd_run_shell_callback(struct job *);
34 void cmd_run_shell_free(void *);
35
36 const struct cmd_entry cmd_run_shell_entry = {
37 "run-shell", "run",
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
38 "", 1, 1,
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
39 "command",
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
40 0,
41 NULL,
42 NULL,
43 cmd_run_shell_exec
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
44 };
45
46 struct cmd_run_shell_data {
47 char *cmd;
48 struct cmd_ctx ctx;
49 };
50
51 int
52 cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
53 {
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
54 struct args *args = self->args;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
55 struct cmd_run_shell_data *cdata;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
56 const char *shellcmd = args->argv[0];
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
57
58 cdata = xmalloc(sizeof *cdata);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
59 cdata->cmd = xstrdup(args->argv[0]);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
60 memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
61
62 if (ctx->cmdclient != NULL)
63 ctx->cmdclient->references++;
64 if (ctx->curclient != NULL)
65 ctx->curclient->references++;
66
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored Jul 9, 2011
67 job_run(shellcmd, cmd_run_shell_callback, cmd_run_shell_free, cdata);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
68
69 return (1); /* don't let client exit */
70 }
71
72 void
73 cmd_run_shell_callback(struct job *job)
74 {
75 struct cmd_run_shell_data *cdata = job->data;
76 struct cmd_ctx *ctx = &cdata->ctx;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
77 char *cmd, *msg, *line;
78 size_t size;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
79 int retcode;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
80 u_int lines;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
81
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored Jul 23, 2010
82 if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD)
83 return;
84 if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD)
85 return;
86
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
87 lines = 0;
88 do {
89 if ((line = evbuffer_readline(job->event->input)) != NULL) {
90 ctx->print(ctx, "%s", line);
91 lines++;
92 }
93 } while (line != NULL);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
94
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
95 size = EVBUFFER_LENGTH(job->event->input);
96 if (size != 0) {
97 line = xmalloc(size + 1);
98 memcpy(line, EVBUFFER_DATA(job->event->input), size);
99 line[size] = '\0';
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
100
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
101 ctx->print(ctx, "%s", line);
102 lines++;
103
104 xfree(line);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
105 }
106
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
107 cmd = cdata->cmd;
108
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
109 msg = NULL;
110 if (WIFEXITED(job->status)) {
111 if ((retcode = WEXITSTATUS(job->status)) != 0)
112 xasprintf(&msg, "'%s' returned %d", cmd, retcode);
113 } else if (WIFSIGNALED(job->status)) {
114 retcode = WTERMSIG(job->status);
115 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
116 }
117 if (msg != NULL) {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored Mar 13, 2010
118 if (lines != 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
119 ctx->print(ctx, "%s", msg);
120 else
121 ctx->info(ctx, "%s", msg);
122 xfree(msg);
123 }
124 }
125
126 void
127 cmd_run_shell_free(void *data)
128 {
129 struct cmd_run_shell_data *cdata = data;
130 struct cmd_ctx *ctx = &cdata->ctx;
131
132 if (ctx->cmdclient != NULL) {
133 ctx->cmdclient->references--;
77c63de @orebokech Import upstream version 1.4
orebokech authored Dec 28, 2010
134 ctx->cmdclient->flags |= CLIENT_EXIT;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored Nov 22, 2009
135 }
136 if (ctx->curclient != NULL)
137 ctx->curclient->references--;
138
139 xfree(cdata->cmd);
140 xfree(cdata);
141 }
Something went wrong with that request. Please try again.