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 = {
|
|
|
|
"run-shell", "run",
|
2013-03-24 09:54:10 +00:00
|
|
|
"bt:", 1, 1,
|
|
|
|
"[-b] " CMD_TARGET_PANE_USAGE " shell-command",
|
2011-01-04 00:42:46 +00:00
|
|
|
0,
|
|
|
|
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;
|
2013-03-25 16:04:07 +00:00
|
|
|
struct client *c;
|
2013-03-25 11:43:01 +00:00
|
|
|
struct session *s = NULL;
|
|
|
|
struct winlink *wl = NULL;
|
|
|
|
struct window_pane *wp = NULL;
|
2013-03-24 09:33:35 +00:00
|
|
|
struct format_tree *ft;
|
2015-04-24 22:19:36 +00:00
|
|
|
int cwd;
|
2012-11-27 15:09:35 +00:00
|
|
|
|
2015-04-24 22:19:36 +00:00
|
|
|
if (args_has(args, 't')) {
|
2013-03-25 11:43:01 +00:00
|
|
|
wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
|
2015-04-24 22:19:36 +00:00
|
|
|
cwd = wp->cwd;
|
|
|
|
} else {
|
2013-03-25 16:04:07 +00:00
|
|
|
c = cmd_find_client(cmdq, NULL, 1);
|
|
|
|
if (c != NULL && c->session != NULL) {
|
|
|
|
s = c->session;
|
|
|
|
wl = s->curw;
|
|
|
|
wp = wl->window->active;
|
|
|
|
}
|
2015-04-24 22:19:36 +00:00
|
|
|
if (cmdq->client != NULL && cmdq->client->session == NULL)
|
|
|
|
cwd = cmdq->client->cwd;
|
|
|
|
else if (s != NULL)
|
|
|
|
cwd = s->cwd;
|
|
|
|
else
|
|
|
|
cwd = -1;
|
2013-03-25 16:04:07 +00:00
|
|
|
}
|
2009-10-11 08:58:05 +00:00
|
|
|
|
2013-03-24 09:33:35 +00:00
|
|
|
ft = format_create();
|
2015-02-05 10:29:43 +00:00
|
|
|
format_defaults(ft, NULL, s, wl, wp);
|
2013-03-24 09:33:35 +00:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
|
|
|
|
2009-10-11 08:58:05 +00:00
|
|
|
cdata = xmalloc(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
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
if (cmdq->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
|
|
|
}
|