Remove a bit of text that makes exit-unattached description unclear.
[tmux-openbsd.git] / cmd-run-shell.c
blob189eeac9745a43308044d1ad78949e5065d89c76
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
5 * Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
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.
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.
20 #include <sys/types.h>
21 #include <sys/wait.h>
23 #include <string.h>
25 #include "tmux.h"
28 * Runs a command without a window.
31 int cmd_run_shell_exec(struct cmd *, struct cmd_ctx *);
33 void cmd_run_shell_callback(struct job *);
34 void cmd_run_shell_free(void *);
36 const struct cmd_entry cmd_run_shell_entry = {
37 "run-shell", "run",
38 "", 1, 1,
39 "command",
41 NULL,
42 NULL,
43 cmd_run_shell_exec
46 struct cmd_run_shell_data {
47 char *cmd;
48 struct cmd_ctx ctx;
51 int
52 cmd_run_shell_exec(struct cmd *self, struct cmd_ctx *ctx)
54 struct args *args = self->args;
55 struct cmd_run_shell_data *cdata;
56 struct job *job;
58 cdata = xmalloc(sizeof *cdata);
59 cdata->cmd = xstrdup(args->argv[0]);
60 memcpy(&cdata->ctx, ctx, sizeof cdata->ctx);
62 if (ctx->cmdclient != NULL)
63 ctx->cmdclient->references++;
64 if (ctx->curclient != NULL)
65 ctx->curclient->references++;
67 job = job_add(NULL, 0, NULL,
68 args->argv[0], cmd_run_shell_callback, cmd_run_shell_free, cdata);
69 job_run(job);
71 return (1); /* don't let client exit */
74 void
75 cmd_run_shell_callback(struct job *job)
77 struct cmd_run_shell_data *cdata = job->data;
78 struct cmd_ctx *ctx = &cdata->ctx;
79 char *cmd, *msg, *line;
80 size_t size;
81 int retcode;
82 u_int lines;
84 if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD)
85 return;
86 if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD)
87 return;
89 lines = 0;
90 do {
91 if ((line = evbuffer_readline(job->event->input)) != NULL) {
92 ctx->print(ctx, "%s", line);
93 lines++;
95 } while (line != NULL);
97 size = EVBUFFER_LENGTH(job->event->input);
98 if (size != 0) {
99 line = xmalloc(size + 1);
100 memcpy(line, EVBUFFER_DATA(job->event->input), size);
101 line[size] = '\0';
103 ctx->print(ctx, "%s", line);
104 lines++;
106 xfree(line);
109 cmd = cdata->cmd;
111 msg = NULL;
112 if (WIFEXITED(job->status)) {
113 if ((retcode = WEXITSTATUS(job->status)) != 0)
114 xasprintf(&msg, "'%s' returned %d", cmd, retcode);
115 } else if (WIFSIGNALED(job->status)) {
116 retcode = WTERMSIG(job->status);
117 xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
119 if (msg != NULL) {
120 if (lines != 0)
121 ctx->print(ctx, "%s", msg);
122 else
123 ctx->info(ctx, "%s", msg);
124 xfree(msg);
128 void
129 cmd_run_shell_free(void *data)
131 struct cmd_run_shell_data *cdata = data;
132 struct cmd_ctx *ctx = &cdata->ctx;
134 if (ctx->cmdclient != NULL) {
135 ctx->cmdclient->references--;
136 ctx->cmdclient->flags |= CLIENT_EXIT;
138 if (ctx->curclient != NULL)
139 ctx->curclient->references--;
141 xfree(cdata->cmd);
142 xfree(cdata);