2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2009-09-21 00:20:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
2009-10-12 02:26:06 +02:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@openbsd.org>
|
2009-09-21 00:20:10 +02: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-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-09-21 00:20:10 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Runs a command without a window.
|
|
|
|
*/
|
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
enum cmd_retval cmd_run_shell_exec(struct cmd *, struct cmd_q *);
|
2012-11-27 16:09:35 +01: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-12 02:25:25 +02:00
|
|
|
|
2009-09-21 00:20:10 +02:00
|
|
|
const struct cmd_entry cmd_run_shell_entry = {
|
|
|
|
"run-shell", "run",
|
2013-02-23 23:25:58 +01:00
|
|
|
"bt:", 1, 1,
|
|
|
|
"[-b] " CMD_TARGET_PANE_USAGE " shell-command",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
cmd_run_shell_exec
|
2009-09-21 00:20:10 +02:00
|
|
|
};
|
|
|
|
|
2009-10-12 02:25:25 +02:00
|
|
|
struct cmd_run_shell_data {
|
|
|
|
char *cmd;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq;
|
|
|
|
int bflag;
|
2013-03-11 10:43:56 +01:00
|
|
|
int wp_id;
|
2009-10-12 02:25:25 +02:00
|
|
|
};
|
|
|
|
|
2012-11-27 16:09:35 +01:00
|
|
|
void
|
|
|
|
cmd_run_shell_print(struct job *job, const char *msg)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = job->data;
|
2013-03-11 10:43:56 +01:00
|
|
|
struct window_pane *wp = NULL;
|
2012-11-27 16:09:35 +01:00
|
|
|
|
2013-03-11 10:43:56 +01:00
|
|
|
if (cdata->wp_id != -1)
|
|
|
|
wp = window_pane_find_by_id(cdata->wp_id);
|
2012-11-27 16:09:35 +01:00
|
|
|
if (wp == NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_print(cdata->cmdq, "%s", msg);
|
2012-11-27 16:09:35 +01: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 21:37:32 +02:00
|
|
|
enum cmd_retval
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
|
2009-09-21 00:20:10 +02:00
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
2009-10-12 02:25:25 +02:00
|
|
|
struct cmd_run_shell_data *cdata;
|
2013-02-23 23:42:49 +01:00
|
|
|
char *shellcmd;
|
2013-03-25 17:04:07 +01:00
|
|
|
struct client *c;
|
2013-03-11 10:43:56 +01:00
|
|
|
struct session *s = NULL;
|
|
|
|
struct winlink *wl = NULL;
|
|
|
|
struct window_pane *wp = NULL;
|
2013-02-23 20:24:42 +01:00
|
|
|
struct format_tree *ft;
|
2012-11-27 16:09:35 +01:00
|
|
|
|
2013-03-11 10:43:56 +01:00
|
|
|
if (args_has(args, 't'))
|
|
|
|
wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp);
|
2013-03-25 17:04:07 +01:00
|
|
|
else {
|
|
|
|
c = cmd_find_client(cmdq, NULL, 1);
|
|
|
|
if (c != NULL && c->session != NULL) {
|
|
|
|
s = c->session;
|
|
|
|
wl = s->curw;
|
|
|
|
wp = wl->window->active;
|
|
|
|
}
|
|
|
|
}
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-02-23 20:24:42 +01:00
|
|
|
ft = format_create();
|
2013-03-11 10:43:56 +01:00
|
|
|
if (s != NULL)
|
|
|
|
format_session(ft, s);
|
|
|
|
if (s != NULL && wl != NULL)
|
|
|
|
format_winlink(ft, s, wl);
|
|
|
|
if (wp != NULL)
|
|
|
|
format_window_pane(ft, wp);
|
2013-02-23 20:24:42 +01:00
|
|
|
shellcmd = format_expand(ft, args->argv[0]);
|
|
|
|
format_free(ft);
|
|
|
|
|
2009-10-12 02:25:25 +02:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
2013-02-23 20:24:42 +01:00
|
|
|
cdata->cmd = shellcmd;
|
2013-02-23 23:25:58 +01:00
|
|
|
cdata->bflag = args_has(args, 'b');
|
2013-03-11 10:43:56 +01:00
|
|
|
cdata->wp_id = wp != NULL ? (int) wp->id : -1;
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
cdata->cmdq = cmdq;
|
|
|
|
cmdq->references++;
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-03-21 15:24:33 +01:00
|
|
|
job_run(shellcmd, s, cmd_run_shell_callback, cmd_run_shell_free, cdata);
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cdata->bflag)
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
return (CMD_RETURN_WAIT);
|
2009-10-12 02:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_run_shell_callback(struct job *job)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = job->data;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
2009-11-08 23:56:04 +01:00
|
|
|
char *cmd, *msg, *line;
|
|
|
|
size_t size;
|
2009-10-12 02:25:25 +02:00
|
|
|
int retcode;
|
2009-11-08 23:56:04 +01:00
|
|
|
u_int lines;
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (cmdq->dead)
|
2010-06-06 02:04:59 +02:00
|
|
|
return;
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd = cdata->cmd;
|
2010-06-06 02:04:59 +02:00
|
|
|
|
2009-11-08 23:56:04 +01:00
|
|
|
lines = 0;
|
|
|
|
do {
|
|
|
|
if ((line = evbuffer_readline(job->event->input)) != NULL) {
|
2013-02-23 23:25:58 +01:00
|
|
|
cmd_run_shell_print(job, line);
|
2013-02-21 00:15:21 +01:00
|
|
|
free(line);
|
2009-11-08 23:56:04 +01:00
|
|
|
lines++;
|
|
|
|
}
|
|
|
|
} while (line != NULL);
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2009-11-08 23:56:04 +01: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-21 00:20:10 +02:00
|
|
|
|
2012-11-27 16:09:35 +01:00
|
|
|
cmd_run_shell_print(job, line);
|
2009-11-08 23:56:04 +01:00
|
|
|
lines++;
|
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(line);
|
2009-09-21 00:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg = NULL;
|
2009-10-12 02:25:25 +02: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-21 00:20:10 +02:00
|
|
|
}
|
|
|
|
if (msg != NULL) {
|
2012-11-27 16:09:35 +01:00
|
|
|
if (lines == 0)
|
2013-02-23 23:25:58 +01:00
|
|
|
cmdq_info(cmdq, "%s", msg);
|
2012-11-27 16:09:35 +01:00
|
|
|
else
|
|
|
|
cmd_run_shell_print(job, msg);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(msg);
|
2009-09-21 00:20:10 +02:00
|
|
|
}
|
2009-10-12 02:25:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_run_shell_free(void *data)
|
|
|
|
{
|
|
|
|
struct cmd_run_shell_data *cdata = data;
|
2013-02-23 23:25:58 +01:00
|
|
|
struct cmd_q *cmdq = cdata->cmdq;
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2013-02-23 23:25:58 +01:00
|
|
|
if (!cmdq_free(cmdq) && !cdata->bflag)
|
|
|
|
cmdq_continue(cmdq);
|
2009-10-12 02:25:25 +02:00
|
|
|
|
2012-07-11 21:34:16 +02:00
|
|
|
free(cdata->cmd);
|
|
|
|
free(cdata);
|
2009-09-21 00:20:10 +02:00
|
|
|
}
|