Flag to flush all key bindings from Rob Paisley.
[tmux-openbsd.git] / cmd-pipe-pane.c
blobd287d3b62bba6c22192404eed6e02904280ea4eb
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <paths.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
29 #include "tmux.h"
32 * Open pipe to redirect pane output. If already open, close first.
35 int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
37 void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
39 const struct cmd_entry cmd_pipe_pane_entry = {
40 "pipe-pane", "pipep",
41 CMD_TARGET_PANE_USAGE "[-o] [command]",
42 CMD_ARG01, "o",
43 cmd_target_init,
44 cmd_target_parse,
45 cmd_pipe_pane_exec,
46 cmd_target_free,
47 cmd_target_print
50 int
51 cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
53 struct cmd_target_data *data = self->data;
54 struct client *c;
55 struct window_pane *wp;
56 char *command;
57 int old_fd, pipe_fd[2], null_fd, mode;
59 if ((c = cmd_find_client(ctx, NULL)) == NULL)
60 return (-1);
62 if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
63 return (-1);
65 /* Destroy the old pipe. */
66 old_fd = wp->pipe_fd;
67 if (wp->pipe_fd != -1) {
68 bufferevent_free(wp->pipe_event);
69 close(wp->pipe_fd);
70 wp->pipe_fd = -1;
73 /* If no pipe command, that is enough. */
74 if (data->arg == NULL || *data->arg == '\0')
75 return (0);
78 * With -o, only open the new pipe if there was no previous one. This
79 * allows a pipe to be toggled with a single key, for example:
81 * bind ^p pipep -o 'cat >>~/output'
83 if (cmd_check_flag(data->chflags, 'o') && old_fd != -1)
84 return (0);
86 /* Open the new pipe. */
87 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
88 ctx->error(ctx, "socketpair error: %s", strerror(errno));
89 return (-1);
92 /* Fork the child. */
93 switch (fork()) {
94 case -1:
95 ctx->error(ctx, "fork error: %s", strerror(errno));
96 return (-1);
97 case 0:
98 /* Child process. */
99 close(pipe_fd[0]);
100 clear_signals(1);
102 if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
103 _exit(1);
104 if (pipe_fd[1] != STDIN_FILENO)
105 close(pipe_fd[1]);
107 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
108 if (dup2(null_fd, STDOUT_FILENO) == -1)
109 _exit(1);
110 if (dup2(null_fd, STDERR_FILENO) == -1)
111 _exit(1);
112 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
113 close(null_fd);
115 closefrom(STDERR_FILENO + 1);
117 command = status_replace(c, NULL, data->arg, time(NULL), 0);
118 execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
119 _exit(1);
120 default:
121 /* Parent process. */
122 close(pipe_fd[1]);
124 wp->pipe_fd = pipe_fd[0];
125 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
127 wp->pipe_event = bufferevent_new(wp->pipe_fd,
128 NULL, NULL, cmd_pipe_pane_error_callback, wp);
129 bufferevent_enable(wp->pipe_event, EV_WRITE);
131 if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1)
132 fatal("fcntl failed");
133 if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
134 fatal("fcntl failed");
135 return (0);
139 /* ARGSUSED */
140 void
141 cmd_pipe_pane_error_callback(
142 unused struct bufferevent *bufev, unused short what, void *data)
144 struct window_pane *wp = data;
146 bufferevent_free(wp->pipe_event);
147 close(wp->pipe_fd);
148 wp->pipe_fd = -1;