Rename some imsg bits to make namespace collisions less likely buf to
[tmux-openbsd.git] / cmd-pipe-pane.c
blob0d7c3ff61b56cf2a1f48d1e8609c528ba70dc371
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 <unistd.h>
28 #include "tmux.h"
31 * Open pipe to redirect pane output. If already open, close first.
34 int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
36 void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
38 const struct cmd_entry cmd_pipe_pane_entry = {
39 "pipe-pane", "pipep",
40 CMD_TARGET_PANE_USAGE "[-o] [command]",
41 CMD_ARG01, "o",
42 cmd_target_init,
43 cmd_target_parse,
44 cmd_pipe_pane_exec,
45 cmd_target_free,
46 cmd_target_print
49 int
50 cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
52 struct cmd_target_data *data = self->data;
53 struct window_pane *wp;
54 int old_fd, pipe_fd[2], null_fd, mode;
56 if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
57 return (-1);
59 /* Destroy the old pipe. */
60 old_fd = wp->pipe_fd;
61 if (wp->pipe_fd != -1) {
62 bufferevent_free(wp->pipe_event);
63 close(wp->pipe_fd);
64 wp->pipe_fd = -1;
67 /* If no pipe command, that is enough. */
68 if (data->arg == NULL || *data->arg == '\0')
69 return (0);
72 * With -o, only open the new pipe if there was no previous one. This
73 * allows a pipe to be toggled with a single key, for example:
75 * bind ^p pipep -o 'cat >>~/output'
77 if (cmd_check_flag(data->chflags, 'o') && old_fd != -1)
78 return (0);
80 /* Open the new pipe. */
81 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
82 ctx->error(ctx, "socketpair error: %s", strerror(errno));
83 return (-1);
86 /* Fork the child. */
87 switch (fork()) {
88 case -1:
89 ctx->error(ctx, "fork error: %s", strerror(errno));
90 return (-1);
91 case 0:
92 /* Child process. */
93 close(pipe_fd[0]);
94 clear_signals();
96 if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
97 _exit(1);
98 if (pipe_fd[1] != STDIN_FILENO)
99 close(pipe_fd[1]);
101 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
102 if (dup2(null_fd, STDOUT_FILENO) == -1)
103 _exit(1);
104 if (dup2(null_fd, STDERR_FILENO) == -1)
105 _exit(1);
106 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
107 close(null_fd);
109 execl(_PATH_BSHELL, "sh", "-c", data->arg, (char *) NULL);
110 _exit(1);
111 default:
112 /* Parent process. */
113 close(pipe_fd[1]);
115 wp->pipe_fd = pipe_fd[0];
116 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
118 wp->pipe_event = bufferevent_new(wp->pipe_fd,
119 NULL, NULL, cmd_pipe_pane_error_callback, wp);
120 bufferevent_enable(wp->pipe_event, EV_WRITE);
122 if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1)
123 fatal("fcntl failed");
124 if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
125 fatal("fcntl failed");
126 if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
127 fatal("fcntl failed");
128 return (0);
132 /* ARGSUSED */
133 void
134 cmd_pipe_pane_error_callback(
135 unused struct bufferevent *bufev, unused short what, void *data)
137 struct window_pane *wp = data;
139 bufferevent_free(wp->pipe_event);
140 close(wp->pipe_fd);
141 wp->pipe_fd = -1;