xref: /minix3/external/bsd/tmux/dist/cmd-pipe-pane.c (revision b80da2a01d0bb632707b7b4e974aa32eaebbcc6f)
1 /* $Id: cmd-pipe-pane.c,v 1.1.1.2 2011/08/17 18:40:04 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 /*
31  * Open pipe to redirect pane output. If already open, close first.
32  */
33 
34 int	cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
35 
36 void	cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
37 
38 const struct cmd_entry cmd_pipe_pane_entry = {
39 	"pipe-pane", "pipep",
40 	"ot:", 0, 1,
41 	CMD_TARGET_PANE_USAGE "[-o] [command]",
42 	0,
43 	NULL,
44 	NULL,
45 	cmd_pipe_pane_exec
46 };
47 
48 int
49 cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
50 {
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;
56 
57 	if ((c = cmd_find_client(ctx, NULL)) == NULL)
58 		return (-1);
59 
60 	if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
61 		return (-1);
62 
63 	/* Destroy the old pipe. */
64 	old_fd = wp->pipe_fd;
65 	if (wp->pipe_fd != -1) {
66 		bufferevent_free(wp->pipe_event);
67 		close(wp->pipe_fd);
68 		wp->pipe_fd = -1;
69 	}
70 
71 	/* If no pipe command, that is enough. */
72 	if (args->argc == 0 || *args->argv[0] == '\0')
73 		return (0);
74 
75 	/*
76 	 * With -o, only open the new pipe if there was no previous one. This
77 	 * allows a pipe to be toggled with a single key, for example:
78 	 *
79 	 *	bind ^p pipep -o 'cat >>~/output'
80 	 */
81 	if (args_has(self->args, 'o') && old_fd != -1)
82 		return (0);
83 
84 	/* Open the new pipe. */
85 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
86 		ctx->error(ctx, "socketpair error: %s", strerror(errno));
87 		return (-1);
88 	}
89 
90 	/* Fork the child. */
91 	switch (fork()) {
92 	case -1:
93 		ctx->error(ctx, "fork error: %s", strerror(errno));
94 		return (-1);
95 	case 0:
96 		/* Child process. */
97 		close(pipe_fd[0]);
98 		clear_signals(1);
99 
100 		if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
101 			_exit(1);
102 		if (pipe_fd[1] != STDIN_FILENO)
103 			close(pipe_fd[1]);
104 
105 		null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
106 		if (dup2(null_fd, STDOUT_FILENO) == -1)
107 			_exit(1);
108 		if (dup2(null_fd, STDERR_FILENO) == -1)
109 			_exit(1);
110 		if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
111 			close(null_fd);
112 
113 		closefrom(STDERR_FILENO + 1);
114 
115 		command = status_replace(
116 		    c, NULL, NULL, NULL, args->argv[0], time(NULL), 0);
117 		execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
118 		_exit(1);
119 	default:
120 		/* Parent process. */
121 		close(pipe_fd[1]);
122 
123 		wp->pipe_fd = pipe_fd[0];
124 		wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
125 
126 		wp->pipe_event = bufferevent_new(wp->pipe_fd,
127 		    NULL, NULL, cmd_pipe_pane_error_callback, wp);
128 		bufferevent_enable(wp->pipe_event, EV_WRITE);
129 
130 		setblocking(wp->pipe_fd, 0);
131 		return (0);
132 	}
133 }
134 
135 /* ARGSUSED */
136 void
137 cmd_pipe_pane_error_callback(
138     unused struct bufferevent *bufev, unused short what, void *data)
139 {
140 	struct window_pane	*wp = data;
141 
142 	bufferevent_free(wp->pipe_event);
143 	close(wp->pipe_fd);
144 	wp->pipe_fd = -1;
145 }
146