xref: /netbsd-src/external/bsd/tmux/dist/cmd-pipe-pane.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 
29 #include "tmux.h"
30 
31 /*
32  * Open pipe to redirect pane output. If already open, close first.
33  */
34 
35 enum cmd_retval	 cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
36 
37 void	cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
38 
39 const struct cmd_entry cmd_pipe_pane_entry = {
40 	.name = "pipe-pane",
41 	.alias = "pipep",
42 
43 	.args = { "ot:", 0, 1 },
44 	.usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
45 
46 	.tflag = CMD_PANE,
47 
48 	.flags = 0,
49 	.exec = cmd_pipe_pane_exec
50 };
51 
52 enum cmd_retval
53 cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
54 {
55 	struct args		*args = self->args;
56 	struct client		*c = cmdq->state.c;
57 	struct window_pane	*wp = cmdq->state.tflag.wp;
58 	struct session		*s = cmdq->state.tflag.s;
59 	struct winlink		*wl = cmdq->state.tflag.wl;
60 	char			*cmd;
61 	int			 old_fd, pipe_fd[2], null_fd;
62 	struct format_tree	*ft;
63 
64 	/* Destroy the old pipe. */
65 	old_fd = wp->pipe_fd;
66 	if (wp->pipe_fd != -1) {
67 		bufferevent_free(wp->pipe_event);
68 		close(wp->pipe_fd);
69 		wp->pipe_fd = -1;
70 	}
71 
72 	/* If no pipe command, that is enough. */
73 	if (args->argc == 0 || *args->argv[0] == '\0')
74 		return (CMD_RETURN_NORMAL);
75 
76 	/*
77 	 * With -o, only open the new pipe if there was no previous one. This
78 	 * allows a pipe to be toggled with a single key, for example:
79 	 *
80 	 *	bind ^p pipep -o 'cat >>~/output'
81 	 */
82 	if (args_has(self->args, 'o') && old_fd != -1)
83 		return (CMD_RETURN_NORMAL);
84 
85 	/* Open the new pipe. */
86 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
87 		cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
88 		return (CMD_RETURN_ERROR);
89 	}
90 
91 	/* Expand the command. */
92 	ft = format_create(cmdq, 0);
93 	format_defaults(ft, c, s, wl, wp);
94 	cmd = format_expand_time(ft, args->argv[0], time(NULL));
95 	format_free(ft);
96 
97 	/* Fork the child. */
98 	switch (fork()) {
99 	case -1:
100 		cmdq_error(cmdq, "fork error: %s", strerror(errno));
101 
102 		free(cmd);
103 		return (CMD_RETURN_ERROR);
104 	case 0:
105 		/* Child process. */
106 		close(pipe_fd[0]);
107 		clear_signals(1);
108 
109 		if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
110 			_exit(1);
111 		if (pipe_fd[1] != STDIN_FILENO)
112 			close(pipe_fd[1]);
113 
114 		null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
115 		if (dup2(null_fd, STDOUT_FILENO) == -1)
116 			_exit(1);
117 		if (dup2(null_fd, STDERR_FILENO) == -1)
118 			_exit(1);
119 		if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
120 			close(null_fd);
121 
122 		closefrom(STDERR_FILENO + 1);
123 
124 		execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
125 		_exit(1);
126 	default:
127 		/* Parent process. */
128 		close(pipe_fd[1]);
129 
130 		wp->pipe_fd = pipe_fd[0];
131 		wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
132 
133 		wp->pipe_event = bufferevent_new(wp->pipe_fd,
134 		    NULL, NULL, cmd_pipe_pane_error_callback, wp);
135 		bufferevent_enable(wp->pipe_event, EV_WRITE);
136 
137 		setblocking(wp->pipe_fd, 0);
138 
139 		free(cmd);
140 		return (CMD_RETURN_NORMAL);
141 	}
142 }
143 
144 void
145 cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
146     __unused short what, void *data)
147 {
148 	struct window_pane	*wp = data;
149 
150 	bufferevent_free(wp->pipe_event);
151 	close(wp->pipe_fd);
152 	wp->pipe_fd = -1;
153 }
154