diff options
-rw-r--r-- | usr.bin/tmux/Makefile | 5 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-pipe-pane.c | 127 | ||||
-rw-r--r-- | usr.bin/tmux/cmd.c | 3 | ||||
-rw-r--r-- | usr.bin/tmux/server.c | 19 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 26 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 7 | ||||
-rw-r--r-- | usr.bin/tmux/window.c | 19 |
7 files changed, 199 insertions, 7 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index 82d27d1a33c..d0c890619f5 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.21 2009/10/10 17:19:38 nicm Exp $ +# $OpenBSD: Makefile,v 1.22 2009/10/11 10:04:27 nicm Exp $ PROG= tmux SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ @@ -28,7 +28,8 @@ SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ cmd-run-shell.c cmd-suspend-client.c cmd-swap-pane.c cmd-swap-window.c \ cmd-switch-client.c cmd-unbind-key.c cmd-unlink-window.c \ cmd-set-environment.c cmd-show-environment.c cmd-choose-client.c \ - cmd-up-pane.c cmd-display-message.c cmd-display-panes.c cmd.c \ + cmd-up-pane.c cmd-display-message.c cmd-display-panes.c \ + cmd-pipe-pane.c cmd.c \ colour.c environ.c grid-view.c grid.c input-keys.c \ imsg.c imsg-buffer.c input.c key-bindings.c key-string.c \ layout-set.c layout.c log.c job.c \ diff --git a/usr.bin/tmux/cmd-pipe-pane.c b/usr.bin/tmux/cmd-pipe-pane.c new file mode 100644 index 00000000000..ff1ee43440c --- /dev/null +++ b/usr.bin/tmux/cmd-pipe-pane.c @@ -0,0 +1,127 @@ +/* $OpenBSD: cmd-pipe-pane.c,v 1.1 2009/10/11 10:04:27 nicm Exp $ */ + +/* + * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> + * + * 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 <errno.h> +#include <fcntl.h> +#include <paths.h> +#include <string.h> +#include <unistd.h> + +#include "tmux.h" + +/* + * Open pipe to redirect pane output. If already open, close first. + */ + +int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *); + +const struct cmd_entry cmd_pipe_pane_entry = { + "pipe-pane", "pipep", + CMD_TARGET_PANE_USAGE "[-o] [command]", + CMD_ARG01, CMD_CHFLAG('o'), + cmd_target_init, + cmd_target_parse, + cmd_pipe_pane_exec, + cmd_target_free, + cmd_target_print +}; + +int +cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_target_data *data = self->data; + struct winlink *wl; + struct window_pane *wp; + int old_fd, pipe_fd[2], null_fd, mode; + + if ((wl = cmd_find_pane(ctx, data->target, NULL, &wp)) == NULL) + return (-1); + + /* Destroy the old pipe. */ + old_fd = wp->pipe_fd; + if (wp->pipe_fd != -1) { + buffer_destroy(wp->pipe_buf); + close(wp->pipe_fd); + wp->pipe_fd = -1; + } + + /* If no pipe command, that is enough. */ + if (data->arg == NULL || *data->arg == '\0') + return (0); + + /* + * With -o, only open the new pipe if there was no previous one. This + * allows a pipe to be toggled with a single key, for example: + * + * bind ^p pipep -o 'cat >>~/output' + */ + if (data->chflags & CMD_CHFLAG('o') && old_fd != -1) + return (0); + + /* Open the new pipe. */ + if (pipe(pipe_fd) != 0) { + ctx->error(ctx, "pipe error: %s", strerror(errno)); + return (-1); + } + + /* Fork the child. */ + switch (fork()) { + case -1: + ctx->error(ctx, "fork error: %s", strerror(errno)); + return (-1); + case 0: + /* Child process. */ + close(pipe_fd[0]); + sigreset(); + + if (dup2(pipe_fd[1], STDIN_FILENO) == -1) + _exit(1); + if (pipe_fd[1] != STDIN_FILENO) + close(pipe_fd[1]); + + null_fd = open(_PATH_DEVNULL, O_WRONLY, 0); + if (dup2(null_fd, STDOUT_FILENO) == -1) + _exit(1); + if (dup2(null_fd, STDERR_FILENO) == -1) + _exit(1); + if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO) + close(null_fd); + + execl(_PATH_BSHELL, "sh", "-c", data->arg, (char *) NULL); + _exit(1); + default: + /* Parent process. */ + close(pipe_fd[1]); + + wp->pipe_fd = pipe_fd[0]; + wp->pipe_buf = buffer_create(BUFSIZ); + wp->pipe_off = BUFFER_USED(wp->in); + + if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1) + fatal("fcntl failed"); + if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1) + fatal("fcntl failed"); + if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1) + fatal("fcntl failed"); + return (0); + } + + return (0); +} diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c index 8b8f07f60d6..e2d290840b7 100644 --- a/usr.bin/tmux/cmd.c +++ b/usr.bin/tmux/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.23 2009/10/10 17:19:38 nicm Exp $ */ +/* $OpenBSD: cmd.c,v 1.24 2009/10/11 10:04:27 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -71,6 +71,7 @@ const struct cmd_entry *cmd_table[] = { &cmd_next_layout_entry, &cmd_next_window_entry, &cmd_paste_buffer_entry, + &cmd_pipe_pane_entry, &cmd_previous_layout_entry, &cmd_previous_window_entry, &cmd_refresh_client_entry, diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c index b57cf4afd7d..094ce14388f 100644 --- a/usr.bin/tmux/server.c +++ b/usr.bin/tmux/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.54 2009/10/11 08:58:05 nicm Exp $ */ +/* $OpenBSD: server.c,v 1.55 2009/10/11 10:04:27 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -572,6 +572,13 @@ server_fill_windows(void) if (BUFFER_USED(wp->out) > 0) events |= POLLOUT; server_poll_add(wp->fd, events); + + if (wp->pipe_fd == -1) + continue; + events = 0; + if (BUFFER_USED(wp->pipe_buf) > 0) + events |= POLLOUT; + server_poll_add(wp->pipe_fd, events); } } } @@ -600,6 +607,16 @@ server_handle_windows(void) wp->fd = -1; } else server_handle_window(w, wp); + + if (wp->pipe_fd == -1) + continue; + if ((pfd = server_poll_lookup(wp->pipe_fd)) == NULL) + continue; + if (buffer_poll(pfd, NULL, wp->pipe_buf) != 0) { + buffer_destroy(wp->pipe_buf); + close(wp->pipe_fd); + wp->pipe_fd = -1; + } } server_check_window(w); diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index 0dae7e24db4..90aa82b7d7b 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.106 2009/10/11 08:58:05 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.107 2009/10/11 10:04:27 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> .\" @@ -840,6 +840,30 @@ Move to the next window in the session. If .Fl a is used, move to the next window with a bell, activity or content alert. +.It Xo Ic pipe-pane +.Op Fl o +.Op Fl t Ar target-pane +.Op Ar command +.Xc +.D1 (alias: Ic pipep ) +Pipe any output sent by the program in +.Ar target-pane +to a shell command. +A pane may only be piped to one command at a time, any existing pipe is +closed before +.Ar command +is executed. +If no +.Ar command +is given, the current pipe (if any) is closed. +.Pp +The +.Fl o +option only opens a new pipe if no previous pipe exists, allowing a pipe to +be toggled with a single key, for example: +.Bd -literal -offset indent +bind-key C-p pipe-pane -o 'cat >>~/output' +.Ed .It Xo Ic previous-window .Op Fl a .Op Fl t Ar target-session diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 8e0b32866e0..8ee656f31a1 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.130 2009/10/11 07:20:16 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.131 2009/10/11 10:04:27 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -715,6 +715,10 @@ struct window_pane { struct input_ctx ictx; + int pipe_fd; + struct buffer *pipe_buf; + size_t pipe_off; + struct screen *screen; struct screen base; @@ -1394,6 +1398,7 @@ extern const struct cmd_entry cmd_new_window_entry; extern const struct cmd_entry cmd_next_layout_entry; extern const struct cmd_entry cmd_next_window_entry; extern const struct cmd_entry cmd_paste_buffer_entry; +extern const struct cmd_entry cmd_pipe_pane_entry; extern const struct cmd_entry cmd_previous_layout_entry; extern const struct cmd_entry cmd_previous_window_entry; extern const struct cmd_entry cmd_refresh_client_entry; diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index f77e7cd758c..2dbc0673324 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.31 2009/10/11 07:01:10 nicm Exp $ */ +/* $OpenBSD: window.c,v 1.32 2009/10/11 10:04:27 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -425,6 +425,10 @@ window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) wp->sx = sx; wp->sy = sy; + wp->pipe_fd = -1; + wp->pipe_buf = NULL; + wp->pipe_off = 0; + wp->saved_grid = NULL; screen_init(&wp->base, sx, sy, hlimit); @@ -448,6 +452,11 @@ window_pane_destroy(struct window_pane *wp) if (wp->saved_grid != NULL) grid_destroy(wp->saved_grid); + if (wp->pipe_fd != -1) { + buffer_destroy(wp->pipe_buf); + close(wp->pipe_fd); + } + buffer_destroy(wp->in); buffer_destroy(wp->out); @@ -621,7 +630,15 @@ window_pane_reset_mode(struct window_pane *wp) void window_pane_parse(struct window_pane *wp) { + size_t new_size; + + new_size = BUFFER_USED(wp->in) - wp->pipe_off; + if (wp->pipe_fd != -1 && new_size > 0) + buffer_write(wp->pipe_buf, BUFFER_OUT(wp->in), new_size); + input_parse(wp); + + wp->pipe_off = BUFFER_USED(wp->in); } void |