Allow attach-session -t to accept a window and pane to select them on
[tmux-openbsd.git] / cmd-pipe-pane.c
blob9b29e5681ec74682c20c19415d8ecd61d46cf44a
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 enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
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 "ot:", 0, 1,
42 "[-o] " CMD_TARGET_PANE_USAGE " [command]",
44 NULL,
45 cmd_pipe_pane_exec
48 enum cmd_retval
49 cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
51 struct args *args = self->args;
52 struct client *c;
53 struct window_pane *wp;
54 char *command;
55 int old_fd, pipe_fd[2], null_fd;
57 if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
58 return (CMD_RETURN_ERROR);
59 c = cmd_find_client(cmdq, NULL, 1);
61 /* Destroy the old pipe. */
62 old_fd = wp->pipe_fd;
63 if (wp->pipe_fd != -1) {
64 bufferevent_free(wp->pipe_event);
65 close(wp->pipe_fd);
66 wp->pipe_fd = -1;
69 /* If no pipe command, that is enough. */
70 if (args->argc == 0 || *args->argv[0] == '\0')
71 return (CMD_RETURN_NORMAL);
74 * With -o, only open the new pipe if there was no previous one. This
75 * allows a pipe to be toggled with a single key, for example:
77 * bind ^p pipep -o 'cat >>~/output'
79 if (args_has(self->args, 'o') && old_fd != -1)
80 return (CMD_RETURN_NORMAL);
82 /* Open the new pipe. */
83 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
84 cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
85 return (CMD_RETURN_ERROR);
88 /* Fork the child. */
89 switch (fork()) {
90 case -1:
91 cmdq_error(cmdq, "fork error: %s", strerror(errno));
92 return (CMD_RETURN_ERROR);
93 case 0:
94 /* Child process. */
95 close(pipe_fd[0]);
96 clear_signals(1);
98 if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
99 _exit(1);
100 if (pipe_fd[1] != STDIN_FILENO)
101 close(pipe_fd[1]);
103 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
104 if (dup2(null_fd, STDOUT_FILENO) == -1)
105 _exit(1);
106 if (dup2(null_fd, STDERR_FILENO) == -1)
107 _exit(1);
108 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
109 close(null_fd);
111 closefrom(STDERR_FILENO + 1);
113 command = status_replace(
114 c, NULL, NULL, NULL, args->argv[0], time(NULL), 0);
115 execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
116 _exit(1);
117 default:
118 /* Parent process. */
119 close(pipe_fd[1]);
121 wp->pipe_fd = pipe_fd[0];
122 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
124 wp->pipe_event = bufferevent_new(wp->pipe_fd,
125 NULL, NULL, cmd_pipe_pane_error_callback, wp);
126 bufferevent_enable(wp->pipe_event, EV_WRITE);
128 setblocking(wp->pipe_fd, 0);
129 return (CMD_RETURN_NORMAL);
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;