Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / tmux / dist / cmd-pipe-pane.c
blob63e365aa754356981264fb2a49c7fb24263ee96c
1 /* Id */
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 <string.h>
25 #include <time.h>
26 #include <unistd.h>
28 #include "tmux.h"
31 * Open pipe to redirect pane output. If already open, close first.
34 enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
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 "ot:", 0, 1,
41 "[-o] " CMD_TARGET_PANE_USAGE " [command]",
43 NULL,
44 cmd_pipe_pane_exec
47 enum cmd_retval
48 cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
50 struct args *args = self->args;
51 struct client *c;
52 struct window_pane *wp;
53 char *command;
54 int old_fd, pipe_fd[2], null_fd;
56 if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
57 return (CMD_RETURN_ERROR);
58 c = cmd_find_client(cmdq, NULL, 1);
60 /* Destroy the old pipe. */
61 old_fd = wp->pipe_fd;
62 if (wp->pipe_fd != -1) {
63 bufferevent_free(wp->pipe_event);
64 close(wp->pipe_fd);
65 wp->pipe_fd = -1;
68 /* If no pipe command, that is enough. */
69 if (args->argc == 0 || *args->argv[0] == '\0')
70 return (CMD_RETURN_NORMAL);
73 * With -o, only open the new pipe if there was no previous one. This
74 * allows a pipe to be toggled with a single key, for example:
76 * bind ^p pipep -o 'cat >>~/output'
78 if (args_has(self->args, 'o') && old_fd != -1)
79 return (CMD_RETURN_NORMAL);
81 /* Open the new pipe. */
82 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
83 cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
84 return (CMD_RETURN_ERROR);
87 /* Fork the child. */
88 switch (fork()) {
89 case -1:
90 cmdq_error(cmdq, "fork error: %s", strerror(errno));
91 return (CMD_RETURN_ERROR);
92 case 0:
93 /* Child process. */
94 close(pipe_fd[0]);
95 clear_signals(1);
97 if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
98 _exit(1);
99 if (pipe_fd[1] != STDIN_FILENO)
100 close(pipe_fd[1]);
102 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
103 if (dup2(null_fd, STDOUT_FILENO) == -1)
104 _exit(1);
105 if (dup2(null_fd, STDERR_FILENO) == -1)
106 _exit(1);
107 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
108 close(null_fd);
110 closefrom(STDERR_FILENO + 1);
112 command = status_replace(
113 c, NULL, NULL, NULL, args->argv[0], time(NULL), 0);
114 execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
115 _exit(1);
116 default:
117 /* Parent process. */
118 close(pipe_fd[1]);
120 wp->pipe_fd = pipe_fd[0];
121 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
123 wp->pipe_event = bufferevent_new(wp->pipe_fd,
124 NULL, NULL, cmd_pipe_pane_error_callback, wp);
125 bufferevent_enable(wp->pipe_event, EV_WRITE);
127 setblocking(wp->pipe_fd, 0);
128 return (CMD_RETURN_NORMAL);
132 void
133 cmd_pipe_pane_error_callback(
134 unused struct bufferevent *bufev, unused short what, void *data)
136 struct window_pane *wp = data;
138 bufferevent_free(wp->pipe_event);
139 close(wp->pipe_fd);
140 wp->pipe_fd = -1;