cmd-pipe-pane.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include "tmux.h"
  26. /*
  27. * Open pipe to redirect pane output. If already open, close first.
  28. */
  29. enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
  30. void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
  31. const struct cmd_entry cmd_pipe_pane_entry = {
  32. .name = "pipe-pane",
  33. .alias = "pipep",
  34. .args = { "ot:", 0, 1 },
  35. .usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
  36. .tflag = CMD_PANE,
  37. .flags = 0,
  38. .exec = cmd_pipe_pane_exec
  39. };
  40. enum cmd_retval
  41. cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
  42. {
  43. struct args *args = self->args;
  44. struct client *c = cmdq->state.c;
  45. struct window_pane *wp = cmdq->state.tflag.wp;
  46. struct session *s = cmdq->state.tflag.s;
  47. struct winlink *wl = cmdq->state.tflag.wl;
  48. char *cmd;
  49. int old_fd, pipe_fd[2], null_fd;
  50. struct format_tree *ft;
  51. /* Destroy the old pipe. */
  52. old_fd = wp->pipe_fd;
  53. if (wp->pipe_fd != -1) {
  54. bufferevent_free(wp->pipe_event);
  55. close(wp->pipe_fd);
  56. wp->pipe_fd = -1;
  57. }
  58. /* If no pipe command, that is enough. */
  59. if (args->argc == 0 || *args->argv[0] == '\0')
  60. return (CMD_RETURN_NORMAL);
  61. /*
  62. * With -o, only open the new pipe if there was no previous one. This
  63. * allows a pipe to be toggled with a single key, for example:
  64. *
  65. * bind ^p pipep -o 'cat >>~/output'
  66. */
  67. if (args_has(self->args, 'o') && old_fd != -1)
  68. return (CMD_RETURN_NORMAL);
  69. /* Open the new pipe. */
  70. if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
  71. cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
  72. return (CMD_RETURN_ERROR);
  73. }
  74. /* Expand the command. */
  75. ft = format_create(cmdq, 0);
  76. format_defaults(ft, c, s, wl, wp);
  77. cmd = format_expand_time(ft, args->argv[0], time(NULL));
  78. format_free(ft);
  79. /* Fork the child. */
  80. switch (fork()) {
  81. case -1:
  82. cmdq_error(cmdq, "fork error: %s", strerror(errno));
  83. free(cmd);
  84. return (CMD_RETURN_ERROR);
  85. case 0:
  86. /* Child process. */
  87. close(pipe_fd[0]);
  88. clear_signals(1);
  89. if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
  90. _exit(1);
  91. if (pipe_fd[1] != STDIN_FILENO)
  92. close(pipe_fd[1]);
  93. null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
  94. if (dup2(null_fd, STDOUT_FILENO) == -1)
  95. _exit(1);
  96. if (dup2(null_fd, STDERR_FILENO) == -1)
  97. _exit(1);
  98. if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
  99. close(null_fd);
  100. closefrom(STDERR_FILENO + 1);
  101. execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
  102. _exit(1);
  103. default:
  104. /* Parent process. */
  105. close(pipe_fd[1]);
  106. wp->pipe_fd = pipe_fd[0];
  107. wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
  108. wp->pipe_event = bufferevent_new(wp->pipe_fd,
  109. NULL, NULL, cmd_pipe_pane_error_callback, wp);
  110. bufferevent_enable(wp->pipe_event, EV_WRITE);
  111. setblocking(wp->pipe_fd, 0);
  112. free(cmd);
  113. return (CMD_RETURN_NORMAL);
  114. }
  115. }
  116. void
  117. cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
  118. __unused short what, void *data)
  119. {
  120. struct window_pane *wp = data;
  121. bufferevent_free(wp->pipe_event);
  122. close(wp->pipe_fd);
  123. wp->pipe_fd = -1;
  124. }