2009-12-01 18:42:38 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Jonathan Alvarado <radobobo@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-12-30 23:16:18 +00:00
|
|
|
#include <stdlib.h>
|
2009-12-01 18:42:38 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2013-03-21 18:43:34 +00:00
|
|
|
* Write the entire contents of a pane to a buffer or stdout.
|
2009-12-01 18:42:38 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmdq_item *);
|
2009-12-01 18:42:38 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static char *cmd_capture_pane_append(char *, size_t *, char *, size_t);
|
|
|
|
static char *cmd_capture_pane_pending(struct args *, struct window_pane *,
|
|
|
|
size_t *);
|
|
|
|
static char *cmd_capture_pane_history(struct args *, struct cmdq_item *,
|
|
|
|
struct window_pane *, size_t *);
|
2013-03-25 11:42:19 +00:00
|
|
|
|
2009-12-01 18:42:38 +00:00
|
|
|
const struct cmd_entry cmd_capture_pane_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "capture-pane",
|
|
|
|
.alias = "capturep",
|
|
|
|
|
2023-09-01 16:40:38 +00:00
|
|
|
.args = { "ab:CeE:JNpPqS:Tt:", 0, 0, NULL },
|
2022-09-28 07:55:29 +00:00
|
|
|
.usage = "[-aCeJNpPqT] " CMD_BUFFER_USAGE " [-E end-line] "
|
2019-03-14 06:36:21 +00:00
|
|
|
"[-S start-line] " CMD_TARGET_PANE_USAGE,
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
2015-12-14 00:31:54 +00:00
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_capture_pane_exec
|
2009-12-01 18:42:38 +00:00
|
|
|
};
|
|
|
|
|
2017-02-16 12:18:38 +00:00
|
|
|
const struct cmd_entry cmd_clear_history_entry = {
|
|
|
|
.name = "clear-history",
|
|
|
|
.alias = "clearhist",
|
|
|
|
|
2022-06-30 09:55:53 +00:00
|
|
|
.args = { "Ht:", 0, 0, NULL },
|
|
|
|
.usage = "[-H] " CMD_TARGET_PANE_USAGE,
|
2017-02-16 12:18:38 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
.target = { 't', CMD_FIND_PANE, 0 },
|
2017-02-16 12:18:38 +00:00
|
|
|
|
|
|
|
.flags = CMD_AFTERHOOK,
|
|
|
|
.exec = cmd_capture_pane_exec
|
|
|
|
};
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static char *
|
2013-03-25 11:42:19 +00:00
|
|
|
cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
|
|
|
|
{
|
2014-10-08 17:35:58 +00:00
|
|
|
buf = xrealloc(buf, *len + linelen + 1);
|
2013-03-25 11:42:19 +00:00
|
|
|
memcpy(buf + *len, line, linelen);
|
|
|
|
*len += linelen;
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static char *
|
2013-03-25 11:42:19 +00:00
|
|
|
cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
|
|
|
|
size_t *len)
|
|
|
|
{
|
2015-05-08 16:18:04 +00:00
|
|
|
struct evbuffer *pending;
|
|
|
|
char *buf, *line, tmp[5];
|
|
|
|
size_t linelen;
|
|
|
|
u_int i;
|
2013-03-25 11:42:19 +00:00
|
|
|
|
2020-03-19 14:03:48 +00:00
|
|
|
pending = input_pending(wp->ictx);
|
2015-05-08 16:18:04 +00:00
|
|
|
if (pending == NULL)
|
2013-03-25 11:42:19 +00:00
|
|
|
return (xstrdup(""));
|
|
|
|
|
2015-05-08 16:18:04 +00:00
|
|
|
line = EVBUFFER_DATA(pending);
|
|
|
|
linelen = EVBUFFER_LENGTH(pending);
|
2013-03-25 11:42:19 +00:00
|
|
|
|
|
|
|
buf = xstrdup("");
|
|
|
|
if (args_has(args, 'C')) {
|
|
|
|
for (i = 0; i < linelen; i++) {
|
2017-01-02 22:42:19 +00:00
|
|
|
if (line[i] >= ' ' && line[i] != '\\') {
|
2013-03-25 11:42:19 +00:00
|
|
|
tmp[0] = line[i];
|
|
|
|
tmp[1] = '\0';
|
|
|
|
} else
|
2015-03-31 17:45:10 +00:00
|
|
|
xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
|
2013-03-25 11:42:19 +00:00
|
|
|
buf = cmd_capture_pane_append(buf, len, tmp,
|
|
|
|
strlen(tmp));
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
buf = cmd_capture_pane_append(buf, len, line, linelen);
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static char *
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_capture_pane_history(struct args *args, struct cmdq_item *item,
|
2013-03-25 11:42:19 +00:00
|
|
|
struct window_pane *wp, size_t *len)
|
2009-12-01 18:42:38 +00:00
|
|
|
{
|
2011-03-28 20:17:39 +00:00
|
|
|
struct grid *gd;
|
2013-03-22 15:56:11 +00:00
|
|
|
const struct grid_line *gl;
|
2013-03-25 11:42:19 +00:00
|
|
|
struct grid_cell *gc = NULL;
|
2022-09-28 07:55:29 +00:00
|
|
|
int n, join_lines, flags = 0;
|
2013-03-25 11:42:19 +00:00
|
|
|
u_int i, sx, top, bottom, tmp;
|
|
|
|
char *cause, *buf, *line;
|
2014-06-20 10:46:59 +00:00
|
|
|
const char *Sflag, *Eflag;
|
2013-03-25 11:42:19 +00:00
|
|
|
size_t linelen;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
sx = screen_size_x(&wp->base);
|
2013-03-25 10:05:35 +00:00
|
|
|
if (args_has(args, 'a')) {
|
2020-03-31 07:00:34 +00:00
|
|
|
gd = wp->base.saved_grid;
|
2013-03-25 11:42:19 +00:00
|
|
|
if (gd == NULL) {
|
|
|
|
if (!args_has(args, 'q')) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no alternate screen");
|
2013-03-25 11:42:19 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
return (xstrdup(""));
|
2013-03-25 10:05:35 +00:00
|
|
|
}
|
2013-03-25 11:42:19 +00:00
|
|
|
} else
|
|
|
|
gd = wp->base.grid;
|
|
|
|
|
2014-06-20 10:46:59 +00:00
|
|
|
Sflag = args_get(args, 'S');
|
|
|
|
if (Sflag != NULL && strcmp(Sflag, "-") == 0)
|
2013-03-25 11:42:19 +00:00
|
|
|
top = 0;
|
2014-06-20 10:46:59 +00:00
|
|
|
else {
|
2022-06-07 10:02:19 +00:00
|
|
|
n = args_strtonum_and_expand(args, 'S', INT_MIN, SHRT_MAX,
|
|
|
|
item, &cause);
|
2014-06-20 10:46:59 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
top = gd->hsize;
|
|
|
|
free(cause);
|
|
|
|
} else if (n < 0 && (u_int) -n > gd->hsize)
|
|
|
|
top = 0;
|
|
|
|
else
|
|
|
|
top = gd->hsize + n;
|
|
|
|
if (top > gd->hsize + gd->sy - 1)
|
|
|
|
top = gd->hsize + gd->sy - 1;
|
|
|
|
}
|
2013-03-25 11:42:19 +00:00
|
|
|
|
2014-06-20 10:46:59 +00:00
|
|
|
Eflag = args_get(args, 'E');
|
|
|
|
if (Eflag != NULL && strcmp(Eflag, "-") == 0)
|
2013-03-25 11:42:19 +00:00
|
|
|
bottom = gd->hsize + gd->sy - 1;
|
2014-06-20 10:46:59 +00:00
|
|
|
else {
|
2022-06-07 10:02:19 +00:00
|
|
|
n = args_strtonum_and_expand(args, 'E', INT_MIN, SHRT_MAX,
|
|
|
|
item, &cause);
|
2014-06-20 10:46:59 +00:00
|
|
|
if (cause != NULL) {
|
|
|
|
bottom = gd->hsize + gd->sy - 1;
|
|
|
|
free(cause);
|
|
|
|
} else if (n < 0 && (u_int) -n > gd->hsize)
|
|
|
|
bottom = 0;
|
|
|
|
else
|
|
|
|
bottom = gd->hsize + n;
|
|
|
|
if (bottom > gd->hsize + gd->sy - 1)
|
|
|
|
bottom = gd->hsize + gd->sy - 1;
|
|
|
|
}
|
2013-03-25 11:42:19 +00:00
|
|
|
|
|
|
|
if (bottom < top) {
|
|
|
|
tmp = bottom;
|
|
|
|
bottom = top;
|
|
|
|
top = tmp;
|
2013-03-25 10:05:35 +00:00
|
|
|
}
|
2009-12-01 18:42:38 +00:00
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
join_lines = args_has(args, 'J');
|
2022-09-28 07:55:29 +00:00
|
|
|
if (args_has(args, 'e'))
|
|
|
|
flags |= GRID_STRING_WITH_SEQUENCES;
|
|
|
|
if (args_has(args, 'C'))
|
|
|
|
flags |= GRID_STRING_ESCAPE_SEQUENCES;
|
|
|
|
if (!join_lines && !args_has(args, 'T'))
|
|
|
|
flags |= GRID_STRING_EMPTY_CELLS;
|
|
|
|
if (!join_lines && !args_has(args, 'N'))
|
|
|
|
flags |= GRID_STRING_TRIM_SPACES;
|
2013-03-25 11:42:19 +00:00
|
|
|
|
2009-12-01 18:42:38 +00:00
|
|
|
buf = NULL;
|
2013-03-25 11:42:19 +00:00
|
|
|
for (i = top; i <= bottom; i++) {
|
2022-09-28 07:55:29 +00:00
|
|
|
line = grid_string_cells(gd, 0, i, sx, &gc, flags, wp->screen);
|
2013-03-25 11:42:19 +00:00
|
|
|
linelen = strlen(line);
|
2009-12-01 18:42:38 +00:00
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
buf = cmd_capture_pane_append(buf, len, line, linelen);
|
2011-03-28 20:17:39 +00:00
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
gl = grid_peek_line(gd, i);
|
|
|
|
if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
|
|
|
|
buf[(*len)++] = '\n';
|
2013-03-22 15:56:11 +00:00
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
return (buf);
|
|
|
|
}
|
2013-03-22 15:56:11 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item)
|
2013-03-25 11:42:19 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-04-13 10:59:58 +00:00
|
|
|
struct client *c = cmdq_get_client(item);
|
|
|
|
struct window_pane *wp = cmdq_get_target(item)->wp;
|
2013-03-25 11:42:19 +00:00
|
|
|
char *buf, *cause;
|
2014-05-13 07:34:35 +00:00
|
|
|
const char *bufname;
|
2013-03-25 11:42:19 +00:00
|
|
|
size_t len;
|
2009-12-01 18:42:38 +00:00
|
|
|
|
2020-04-13 08:26:27 +00:00
|
|
|
if (cmd_get_entry(self) == &cmd_clear_history_entry) {
|
2019-03-12 11:16:49 +00:00
|
|
|
window_pane_reset_mode_all(wp);
|
2017-02-16 12:18:38 +00:00
|
|
|
grid_clear_history(wp->base.grid);
|
2022-06-30 09:55:53 +00:00
|
|
|
if (args_has(args, 'H'))
|
|
|
|
screen_reset_hyperlinks(wp->screen);
|
2017-02-16 12:18:38 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2013-03-25 11:42:19 +00:00
|
|
|
len = 0;
|
|
|
|
if (args_has(args, 'P'))
|
|
|
|
buf = cmd_capture_pane_pending(args, wp, &len);
|
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
buf = cmd_capture_pane_history(args, item, wp, &len);
|
2013-03-25 11:42:19 +00:00
|
|
|
if (buf == NULL)
|
|
|
|
return (CMD_RETURN_ERROR);
|
2009-12-01 18:42:38 +00:00
|
|
|
|
2013-03-21 18:43:34 +00:00
|
|
|
if (args_has(args, 'p')) {
|
2020-06-01 09:43:00 +00:00
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
|
|
|
len--;
|
|
|
|
if (c->flags & CLIENT_CONTROL)
|
|
|
|
control_write(c, "%.*s", (int)len, buf);
|
|
|
|
else {
|
|
|
|
if (!file_can_print(c)) {
|
|
|
|
cmdq_error(item, "can't write to client");
|
|
|
|
free(buf);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
file_print_buffer(c, buf, len);
|
|
|
|
file_print(c, "\n");
|
2015-10-07 09:52:58 +00:00
|
|
|
free(buf);
|
2013-03-21 18:43:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-13 07:34:35 +00:00
|
|
|
bufname = NULL;
|
|
|
|
if (args_has(args, 'b'))
|
|
|
|
bufname = args_get(args, 'b');
|
2013-03-21 18:43:34 +00:00
|
|
|
|
2014-05-13 07:34:35 +00:00
|
|
|
if (paste_set(buf, len, bufname, &cause) != 0) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "%s", cause);
|
2014-05-13 07:34:35 +00:00
|
|
|
free(cause);
|
2015-10-07 09:52:58 +00:00
|
|
|
free(buf);
|
2013-03-21 18:43:34 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
}
|
2010-12-30 23:16:18 +00:00
|
|
|
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2010-12-30 23:16:18 +00:00
|
|
|
}
|