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.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmdq_item *);
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void cmd_run_shell_callback(struct job *);
|
|
|
|
static void cmd_run_shell_free(void *);
|
|
|
|
static 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 {
|
2016-10-16 19:04:05 +00:00
|
|
|
char *cmd;
|
|
|
|
struct cmdq_item *item;
|
|
|
|
int wp_id;
|
2009-10-11 08:58:05 +00:00
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2012-11-27 15:09:35 +00:00
|
|
|
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;
|
2016-11-12 19:05:53 +00:00
|
|
|
struct cmd_find_state fs;
|
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);
|
2016-11-12 19:05:53 +00:00
|
|
|
if (wp == NULL) {
|
|
|
|
if (cdata->item != NULL) {
|
|
|
|
cmdq_print(cdata->item, "%s", msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (cmd_find_current (&fs, NULL, CMD_FIND_QUIET) != 0)
|
|
|
|
return;
|
|
|
|
wp = fs.wp;
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
2012-11-27 15:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
|
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;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct session *s = item->state.tflag.s;
|
|
|
|
struct winlink *wl = item->state.tflag.wl;
|
|
|
|
struct window_pane *wp = item->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
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->client != NULL && item->client->session == NULL)
|
|
|
|
cwd = item->client->cwd;
|
2015-12-13 14:32:38 +00:00
|
|
|
else if (s != NULL)
|
|
|
|
cwd = s->cwd;
|
|
|
|
else
|
|
|
|
cwd = NULL;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
ft = format_create(item, 0);
|
|
|
|
format_defaults(ft, item->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;
|
2016-10-15 23:06:39 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (args_has(args, 't') && wp != NULL)
|
|
|
|
cdata->wp_id = wp->id;
|
|
|
|
else
|
|
|
|
cdata->wp_id = -1;
|
|
|
|
|
|
|
|
if (!args_has(args, 'b'))
|
2016-10-16 19:04:05 +00:00
|
|
|
cdata->item = item;
|
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
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (args_has(args, 'b'))
|
2013-03-24 09:54:10 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-10-11 08:58:05 +00:00
|
|
|
cmd_run_shell_callback(struct job *job)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = job->data;
|
2016-10-16 17:55:14 +00:00
|
|
|
char *cmd = cdata->cmd, *msg, *line;
|
2009-11-04 21:04:43 +00:00
|
|
|
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
|
|
|
|
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);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (cdata->item != NULL)
|
|
|
|
cdata->item->flags &= ~CMDQ_WAITING;
|
2009-10-11 08:58:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2009-10-11 08:58:05 +00:00
|
|
|
cmd_run_shell_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = data;
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-09-20 19:15:01 +00:00
|
|
|
}
|