cmd-capture-pane.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Jonathan Alvarado <radobobo@users.sourceforge.net>
  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 <stdlib.h>
  19. #include <string.h>
  20. #include "tmux.h"
  21. /*
  22. * Write the entire contents of a pane to a buffer or stdout.
  23. */
  24. enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_q *);
  25. char *cmd_capture_pane_append(char *, size_t *, char *, size_t);
  26. char *cmd_capture_pane_pending(struct args *, struct window_pane *,
  27. size_t *);
  28. char *cmd_capture_pane_history(struct args *, struct cmd_q *,
  29. struct window_pane *, size_t *);
  30. const struct cmd_entry cmd_capture_pane_entry = {
  31. .name = "capture-pane",
  32. .alias = "capturep",
  33. .args = { "ab:CeE:JpPqS:t:", 0, 0 },
  34. .usage = "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] "
  35. "[-S start-line]" CMD_TARGET_PANE_USAGE,
  36. .tflag = CMD_PANE,
  37. .flags = 0,
  38. .exec = cmd_capture_pane_exec
  39. };
  40. char *
  41. cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
  42. {
  43. buf = xrealloc(buf, *len + linelen + 1);
  44. memcpy(buf + *len, line, linelen);
  45. *len += linelen;
  46. return (buf);
  47. }
  48. char *
  49. cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
  50. size_t *len)
  51. {
  52. struct evbuffer *pending;
  53. char *buf, *line, tmp[5];
  54. size_t linelen;
  55. u_int i;
  56. pending = input_pending(wp);
  57. if (pending == NULL)
  58. return (xstrdup(""));
  59. line = EVBUFFER_DATA(pending);
  60. linelen = EVBUFFER_LENGTH(pending);
  61. buf = xstrdup("");
  62. if (args_has(args, 'C')) {
  63. for (i = 0; i < linelen; i++) {
  64. if (line[i] >= ' ') {
  65. tmp[0] = line[i];
  66. tmp[1] = '\0';
  67. } else
  68. xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
  69. buf = cmd_capture_pane_append(buf, len, tmp,
  70. strlen(tmp));
  71. }
  72. } else
  73. buf = cmd_capture_pane_append(buf, len, line, linelen);
  74. return (buf);
  75. }
  76. char *
  77. cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
  78. struct window_pane *wp, size_t *len)
  79. {
  80. struct grid *gd;
  81. const struct grid_line *gl;
  82. struct grid_cell *gc = NULL;
  83. int n, with_codes, escape_c0, join_lines;
  84. u_int i, sx, top, bottom, tmp;
  85. char *cause, *buf, *line;
  86. const char *Sflag, *Eflag;
  87. size_t linelen;
  88. sx = screen_size_x(&wp->base);
  89. if (args_has(args, 'a')) {
  90. gd = wp->saved_grid;
  91. if (gd == NULL) {
  92. if (!args_has(args, 'q')) {
  93. cmdq_error(cmdq, "no alternate screen");
  94. return (NULL);
  95. }
  96. return (xstrdup(""));
  97. }
  98. } else
  99. gd = wp->base.grid;
  100. Sflag = args_get(args, 'S');
  101. if (Sflag != NULL && strcmp(Sflag, "-") == 0)
  102. top = 0;
  103. else {
  104. n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
  105. if (cause != NULL) {
  106. top = gd->hsize;
  107. free(cause);
  108. } else if (n < 0 && (u_int) -n > gd->hsize)
  109. top = 0;
  110. else
  111. top = gd->hsize + n;
  112. if (top > gd->hsize + gd->sy - 1)
  113. top = gd->hsize + gd->sy - 1;
  114. }
  115. Eflag = args_get(args, 'E');
  116. if (Eflag != NULL && strcmp(Eflag, "-") == 0)
  117. bottom = gd->hsize + gd->sy - 1;
  118. else {
  119. n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
  120. if (cause != NULL) {
  121. bottom = gd->hsize + gd->sy - 1;
  122. free(cause);
  123. } else if (n < 0 && (u_int) -n > gd->hsize)
  124. bottom = 0;
  125. else
  126. bottom = gd->hsize + n;
  127. if (bottom > gd->hsize + gd->sy - 1)
  128. bottom = gd->hsize + gd->sy - 1;
  129. }
  130. if (bottom < top) {
  131. tmp = bottom;
  132. bottom = top;
  133. top = tmp;
  134. }
  135. with_codes = args_has(args, 'e');
  136. escape_c0 = args_has(args, 'C');
  137. join_lines = args_has(args, 'J');
  138. buf = NULL;
  139. for (i = top; i <= bottom; i++) {
  140. line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
  141. escape_c0, !join_lines);
  142. linelen = strlen(line);
  143. buf = cmd_capture_pane_append(buf, len, line, linelen);
  144. gl = grid_peek_line(gd, i);
  145. if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
  146. buf[(*len)++] = '\n';
  147. free(line);
  148. }
  149. return (buf);
  150. }
  151. enum cmd_retval
  152. cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
  153. {
  154. struct args *args = self->args;
  155. struct client *c;
  156. struct window_pane *wp = cmdq->state.tflag.wp;
  157. char *buf, *cause;
  158. const char *bufname;
  159. size_t len;
  160. len = 0;
  161. if (args_has(args, 'P'))
  162. buf = cmd_capture_pane_pending(args, wp, &len);
  163. else
  164. buf = cmd_capture_pane_history(args, cmdq, wp, &len);
  165. if (buf == NULL)
  166. return (CMD_RETURN_ERROR);
  167. if (args_has(args, 'p')) {
  168. c = cmdq->client;
  169. if (c == NULL ||
  170. (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
  171. cmdq_error(cmdq, "can't write to stdout");
  172. free(buf);
  173. return (CMD_RETURN_ERROR);
  174. }
  175. evbuffer_add(c->stdout_data, buf, len);
  176. free(buf);
  177. if (args_has(args, 'P') && len > 0)
  178. evbuffer_add(c->stdout_data, "\n", 1);
  179. server_client_push_stdout(c);
  180. } else {
  181. bufname = NULL;
  182. if (args_has(args, 'b'))
  183. bufname = args_get(args, 'b');
  184. if (paste_set(buf, len, bufname, &cause) != 0) {
  185. cmdq_error(cmdq, "%s", cause);
  186. free(cause);
  187. free(buf);
  188. return (CMD_RETURN_ERROR);
  189. }
  190. }
  191. return (CMD_RETURN_NORMAL);
  192. }