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",
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
|
|
|
.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;
|
|
|
|
}
|
2017-08-30 10:33:57 +00:00
|
|
|
if (cmd_find_from_nothing(&fs, 0) != 0)
|
2016-11-12 19:05:53 +00:00
|
|
|
return;
|
|
|
|
wp = fs.wp;
|
|
|
|
if (wp == NULL)
|
|
|
|
return;
|
2012-11-27 15:09:35 +00:00
|
|
|
}
|
|
|
|
|
Rewrite of choose mode, both to simplify and tidy the code and to add
some modern features.
Now the common code is in mode-tree.c, which provides an API used by the
three modes now separated into window-{buffer,client,tree}.c. Buffer
mode shows buffers, client mode clients and tree mode a tree of
sessions, windows and panes.
Each mode has a common set of key bindings plus a few that are specific
to the mode. Other changes are:
- each mode has a preview pane: for buffers this is the buffer content
(very useful), for others it is a preview of the pane;
- items may be sorted in different ways ('O' key);
- multiple items may be tagged and an operation applied to all of them
(for example, to delete multiple buffers at once);
- in tree mode a command may be run on the selected item (session,
window, pane) or on tagged items (key ':');
- displayed items may be filtered in tree mode by using a format (this
is used to implement find-window) (key 'f');
- the custom format (-F) for the display is no longer available;
- shortcut keys change from 0-9, a-z, A-Z which was always a bit weird
with keys used for other uses to 0-9, M-a to M-z.
Now that the code is simpler, other improvements will come later.
Primary key bindings for each mode are documented under the commands in
the man page (choose-buffer, choose-client, choose-tree).
Parts written by Thomas Adam.
2017-05-30 21:44:59 +00:00
|
|
|
if (window_pane_set_mode(wp, &window_copy_mode, NULL, NULL) == 0)
|
2012-11-27 15:09:35 +00:00
|
|
|
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;
|
2017-04-22 10:22:39 +00:00
|
|
|
struct client *c = cmd_find_client(item, NULL, 1);
|
|
|
|
struct session *s = item->target.s;
|
|
|
|
struct winlink *wl = item->target.wl;
|
|
|
|
struct window_pane *wp = item->target.wp;
|
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-09 08:06:51 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2017-03-08 13:36:12 +00:00
|
|
|
cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp);
|
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
|
|
|
|
2017-04-20 09:20:22 +00:00
|
|
|
job_run(cdata->cmd, s, cwd, NULL, cmd_run_shell_callback,
|
2018-03-08 08:09:10 +00:00
|
|
|
cmd_run_shell_free, cdata, 0);
|
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
|
|
|
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
|
|
|
}
|
|
|
|
} 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
|
|
|
|
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
|
|
|
}
|