2009-09-20 19:15:01 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-11 09:04:33 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-09-20 19:15:01 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-09-20 19:15:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Runs a command without a window.
|
|
|
|
*/
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_q *);
|
2012-11-27 15:09:35 +00:00
|
|
|
|
|
|
|
void cmd_run_shell_callback(struct job *);
|
|
|
|
void cmd_run_shell_free(void *);
|
|
|
|
void cmd_run_shell_print(struct job *, const char *);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2009-09-20 19:15:01 +00:00
|
|
|
const struct cmd_entry cmd_run_shell_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "run-shell",
|
|
|
|
.alias = "run",
|
|
|
|
|
|
|
|
.args = { "bt:", 1, 1 },
|
|
|
|
.usage = "[-b] " CMD_TARGET_PANE_USAGE " shell-command",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_PANE_CANFAIL,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_run_shell_exec
|
2009-09-20 19:15:01 +00:00
|
|
|
};
|
|
|
|
|
2009-10-11 08:58:05 +00:00
|
|
|
struct cmd_run_shell_data {
|
|
|
|
char *cmd;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct cmd_q *cmdq;
|
|
|
|
int bflag;
|
2013-03-25 11:43:01 +00:00
|
|
|
int wp_id;
|
2009-10-11 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2012-11-27 15:09:35 +00:00
|
|
|
void
|
|
|
|
cmd_run_shell_print(struct job *job, const char *msg)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = job->data;
|
2013-03-25 11:43:01 +00:00
|
|
|
struct window_pane *wp = NULL;
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2013-03-25 11:43:01 +00:00
|
|
|
if (cdata->wp_id != -1)
|
|
|
|
wp = window_pane_find_by_id(cdata->wp_id);
|
2012-11-27 15:09:35 +00:00
|
|
|
if (wp == NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_print(cdata->cmdq, "%s", msg);
|
2012-11-27 15:09:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window_pane_set_mode(wp, &window_copy_mode) == 0)
|
|
|
|
window_copy_init_for_output(wp);
|
|
|
|
if (wp->mode == &window_copy_mode)
|
|
|
|
window_copy_add(wp, "%s", msg);
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
enum cmd_retval
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-09-20 19:15:01 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2009-10-11 08:58:05 +00:00
|
|
|
struct cmd_run_shell_data *cdata;
|
2013-03-24 09:33:35 +00:00
|
|
|
char *shellcmd;
|
2015-12-13 14:32:38 +00:00
|
|
|
struct session *s = cmdq->state.tflag.s;
|
|
|
|
struct winlink *wl = cmdq->state.tflag.wl;
|
|
|
|
struct window_pane *wp = cmdq->state.tflag.wp;
|
2013-03-24 09:33:35 +00:00
|
|
|
struct format_tree *ft;
|
2015-10-31 08:13:58 +00:00
|
|
|
const char *cwd;
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2015-12-13 14:32:38 +00:00
|
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL)
|
|
|
|
cwd = cmdq->client->cwd;
|
|
|
|
else if (s != NULL)
|
|
|
|
cwd = s->cwd;
|
|
|
|
else
|
|
|
|
cwd = NULL;
|
2015-12-11 12:27:36 +00:00
|
|
|
ft = format_create(cmdq, 0);
|
2016-02-12 12:24:52 +00:00
|
|
|
format_defaults(ft, cmdq->state.c, s, wl, wp);
|
2013-03-24 09:33:35 +00:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
|
|
|
|
2016-10-09 08:06:51 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2013-03-24 09:33:35 +00:00
|
|
|
cdata->cmd = shellcmd;
|
2013-03-24 09:54:10 +00:00
|
|
|
cdata->bflag = args_has(args, 'b');
|
2013-03-25 11:43:01 +00:00
|
|
|
cdata->wp_id = wp != NULL ? (int) wp->id : -1;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
cdata->cmdq = cmdq;
|
|
|
|
cmdq->references++;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2015-04-24 22:19:36 +00:00
|
|
|
job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
|
|
|
|
cdata);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (cdata->bflag)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_run_shell_callback(struct job *job)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = job->data;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
2009-11-04 21:04:43 +00:00
|
|
|
char *cmd, *msg, *line;
|
|
|
|
size_t size;
|
2009-10-11 08:58:05 +00:00
|
|
|
int retcode;
|
2009-11-04 21:04:43 +00:00
|
|
|
u_int lines;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2015-09-16 22:24:54 +00:00
|
|
|
if (cmdq->flags & CMD_Q_DEAD)
|
2010-05-25 20:05:25 +00:00
|
|
|
return;
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd = cdata->cmd;
|
2010-05-25 20:05:25 +00:00
|
|
|
|
2009-11-04 21:04:43 +00:00
|
|
|
lines = 0;
|
|
|
|
do {
|
|
|
|
if ((line = evbuffer_readline(job->event->input)) != NULL) {
|
2013-03-24 09:54:10 +00:00
|
|
|
cmd_run_shell_print(job, line);
|
2013-03-22 15:55:22 +00:00
|
|
|
free(line);
|
2009-11-04 21:04:43 +00:00
|
|
|
lines++;
|
|
|
|
}
|
|
|
|
} while (line != NULL);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2009-11-04 21:04:43 +00:00
|
|
|
size = EVBUFFER_LENGTH(job->event->input);
|
|
|
|
if (size != 0) {
|
|
|
|
line = xmalloc(size + 1);
|
|
|
|
memcpy(line, EVBUFFER_DATA(job->event->input), size);
|
|
|
|
line[size] = '\0';
|
2009-09-20 19:15:01 +00:00
|
|
|
|
2012-11-27 15:09:35 +00:00
|
|
|
cmd_run_shell_print(job, line);
|
2009-11-04 21:04:43 +00:00
|
|
|
lines++;
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(line);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg = NULL;
|
2009-10-11 08:58:05 +00:00
|
|
|
if (WIFEXITED(job->status)) {
|
|
|
|
if ((retcode = WEXITSTATUS(job->status)) != 0)
|
|
|
|
xasprintf(&msg, "'%s' returned %d", cmd, retcode);
|
|
|
|
} else if (WIFSIGNALED(job->status)) {
|
|
|
|
retcode = WTERMSIG(job->status);
|
|
|
|
xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|
2014-04-17 07:55:43 +00:00
|
|
|
if (msg != NULL)
|
|
|
|
cmd_run_shell_print(job, msg);
|
|
|
|
free(msg);
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_run_shell_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = data;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|