Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-capture-pane.c
Line
Count
Source
1
/* $OpenBSD: cmd-capture-pane.c,v 1.70 2026/10/09 13:12:14 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Jonathan Alvarado <radobobo@users.sourceforge.net>
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
21
#include <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Write the entire contents of a pane to a buffer or stdout.
28
 */
29
30
static enum cmd_retval  cmd_capture_pane_exec(struct cmd *, struct cmdq_item *);
31
32
static char *cmd_capture_pane_append(char *, size_t *, const char *,
33
         size_t);
34
static char *cmd_capture_pane_pending(struct args *, struct window_pane *,
35
         size_t *);
36
static char *cmd_capture_pane_history(struct args *, struct cmdq_item *,
37
         struct window_pane *, size_t *);
38
static char *cmd_capture_pane_hyperlinks(struct grid *, struct screen *,
39
         u_int, u_int *, u_int *, size_t *);
40
41
const struct cmd_entry cmd_capture_pane_entry = {
42
  .name = "capture-pane",
43
  .description = "Capture pane content.",
44
  .alias = "capturep",
45
46
  .args = { "ab:CeE:FHIJLMNpPqRS:Tt:", 0, 0, NULL },
47
  .usage = "[-aCeFHIJLMNpPqRT] " CMD_BUFFER_USAGE " [-E end-line] "
48
     "[-S start-line] " CMD_TARGET_PANE_USAGE,
49
50
  .target = { 't', CMD_FIND_PANE, 0 },
51
52
  .flags = CMD_AFTERHOOK,
53
  .exec = cmd_capture_pane_exec
54
};
55
56
const struct cmd_entry cmd_clear_history_entry = {
57
  .name = "clear-history",
58
  .description = "Clear a pane's history.",
59
  .alias = "clearhist",
60
61
  .args = { "Ht:", 0, 0, NULL },
62
  .usage = "[-H] " CMD_TARGET_PANE_USAGE,
63
64
  .target = { 't', CMD_FIND_PANE, 0 },
65
66
  .flags = CMD_AFTERHOOK,
67
  .exec = cmd_capture_pane_exec
68
};
69
70
static char *
71
cmd_capture_pane_append(char *buf, size_t *len, const char *line,
72
    size_t linelen)
73
0
{
74
0
  buf = xrealloc(buf, *len + linelen + 1);
75
0
  memcpy(buf + *len, line, linelen);
76
0
  *len += linelen;
77
0
  return (buf);
78
0
}
79
80
static char *
81
cmd_capture_pane_cell(struct screen *s, u_int xx, u_int yy)
82
0
{
83
0
  struct grid   *gd = s->grid;
84
0
  struct hyperlinks *hl = s->hyperlinks;
85
0
  struct grid_cell   gc;
86
0
  char      *line, *data, *link, *linkid, *f, *b, *u;
87
0
  char       c[UTF8_SIZE + 1];
88
0
  const char    *uri, *iid;
89
0
  u_int      flags;
90
91
0
  grid_get_cell(gd, xx, yy, &gc);
92
93
0
  memcpy(c, gc.data.data, gc.data.size);
94
0
  c[gc.data.size] = '\0';
95
0
  utf8_stravis(&data, c, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
96
97
0
  if (gc.link != 0 && hyperlinks_get(hl, gc.link, &uri, &iid, NULL)) {
98
0
    xasprintf(&link, "%s", uri);
99
0
    if (iid != NULL && *iid != '\0')
100
0
      xasprintf(&linkid, "%s", iid);
101
0
    else
102
0
      xasprintf(&linkid, "NONE");
103
0
  } else {
104
0
    xasprintf(&link, "NONE");
105
0
    xasprintf(&linkid, "NONE");
106
0
  }
107
108
0
  flags = gc.flags;
109
0
  if (gc.fg & COLOUR_FLAG_256)
110
0
    flags |= GRID_FLAG_FG256;
111
0
  if (gc.bg & COLOUR_FLAG_256)
112
0
    flags |= GRID_FLAG_BG256;
113
114
0
  xasprintf(&f, "%s[%x]", colour_tostring(gc.fg), gc.fg);
115
0
  xasprintf(&b, "%s[%x]", colour_tostring(gc.bg), gc.bg);
116
0
  xasprintf(&u, "%s[%x]", colour_tostring(gc.us), gc.us);
117
118
0
  xasprintf(&line, "\t\tC %u,%u data=(%u,%u,%s) flags=%s[%x] "
119
0
      "attr=%s[%x] fg=%s bg=%s us=%s link=%s linkid=%s\n",
120
0
      yy, xx, gc.data.width, gc.data.size, data,
121
0
      grid_cell_flags_string(flags), flags,
122
0
      grid_cell_attr_string(gc.attr), gc.attr, f, b, u, link, linkid);
123
124
0
  free(f);
125
0
  free(b);
126
0
  free(u);
127
0
  free(link);
128
0
  free(linkid);
129
0
  free(data);
130
0
  return (line);
131
0
}
132
133
static char *
134
cmd_capture_pane_grid(struct window_pane *wp, size_t *len)
135
0
{
136
0
  struct screen   *s = &wp->base;
137
0
  struct grid   *gd = s->grid;
138
0
  struct grid_line  *gl;
139
0
  struct osc133_data  *od;
140
0
  char      *buf = xstrdup(""), *line;
141
0
  char       p[11];
142
0
  u_int      yy, xx, total = gd->hsize + gd->sy;
143
144
0
  xasprintf(&line, "G %ux%u (%u/%u)\n", gd->sx, gd->sy, gd->hsize,
145
0
      gd->hlimit);
146
0
  buf = cmd_capture_pane_append(buf, len, line, strlen(line));
147
0
  free(line);
148
149
0
  for (yy = 0; yy < total; yy++) {
150
0
    gl = grid_get_line(gd, yy);
151
0
    if (yy < gd->hsize)
152
0
      snprintf(p, sizeof p, "-");
153
0
    else
154
0
      snprintf(p, sizeof p, "%u", yy - gd->hsize);
155
0
    od = &gl->osc133_data;
156
0
    if (gl->flags & GRID_LINE_OSC133_FLAGS) {
157
0
      xasprintf(&line, "\tL %u (%s) flags=%s[%x] "
158
0
          "%u/%u osc133=%u,%u,%u,%u,%u\n", yy, p,
159
0
          grid_line_flags_string(gl->flags), gl->flags,
160
0
          gl->cellused, gl->cellsize, od->prompt_col,
161
0
          od->cmd_col, od->out_start_col,
162
0
          od->out_end_col, od->exit_status);
163
0
    } else {
164
0
      xasprintf(&line, "\tL %u (%s) flags=%s[%x] "
165
0
          "%u/%u\n", yy, p,
166
0
          grid_line_flags_string(gl->flags), gl->flags,
167
0
          gl->cellused, gl->cellsize);
168
0
    }
169
0
    buf = cmd_capture_pane_append(buf, len, line, strlen(line));
170
0
    free(line);
171
172
0
    for (xx = 0; xx < gd->sx; xx++) {
173
0
      line = cmd_capture_pane_cell(s, xx, yy);
174
0
      buf = cmd_capture_pane_append(buf, len, line,
175
0
          strlen(line));
176
0
      free(line);
177
0
    }
178
0
  }
179
0
  return (buf);
180
0
}
181
182
static char *
183
cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
184
    size_t *len)
185
0
{
186
0
  struct evbuffer *pending;
187
0
  char    *buf, *line, tmp[5];
188
0
  size_t     linelen;
189
0
  u_int    i;
190
191
0
  pending = input_pending(wp->ictx);
192
0
  if (pending == NULL)
193
0
    return (xstrdup(""));
194
195
0
  line = EVBUFFER_DATA(pending);
196
0
  linelen = EVBUFFER_LENGTH(pending);
197
198
0
  buf = xstrdup("");
199
0
  if (args_has(args, 'C')) {
200
0
    for (i = 0; i < linelen; i++) {
201
0
      if (line[i] >= ' ' && line[i] != '\\') {
202
0
        tmp[0] = line[i];
203
0
        tmp[1] = '\0';
204
0
      } else
205
0
        snprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
206
0
      buf = cmd_capture_pane_append(buf, len, tmp,
207
0
          strlen(tmp));
208
0
    }
209
0
  } else
210
0
    buf = cmd_capture_pane_append(buf, len, line, linelen);
211
0
  return (buf);
212
0
}
213
214
static char *
215
cmd_capture_pane_hyperlinks(struct grid *gd, struct screen *s, u_int py,
216
    u_int *links, u_int *nlinks, size_t *len)
217
0
{
218
0
  const struct grid_line  *gl = grid_peek_line(gd, py);
219
0
  struct grid_cell   gc;
220
0
  const char    *uri;
221
0
  char      *line = xstrdup("");
222
0
  u_int      i, j;
223
224
0
  *len = 0;
225
226
0
  if (s->hyperlinks == NULL || (~gl->flags & GRID_LINE_HYPERLINK))
227
0
    return (line);
228
229
0
  for (i = 0; i < gl->cellused; i++) {
230
0
    grid_get_cell(gd, i, py, &gc);
231
0
    if (gc.link == 0)
232
0
      continue;
233
0
    for (j = 0; j < *nlinks; j++) {
234
0
      if (links[j] == gc.link)
235
0
        break;
236
0
    }
237
0
    if (j != *nlinks)
238
0
      continue;
239
240
0
    if (!hyperlinks_get(s->hyperlinks, gc.link, &uri, NULL, NULL))
241
0
      continue;
242
243
0
    if (*nlinks == gd->sx)
244
0
      break;
245
0
    links[(*nlinks)++] = gc.link;
246
247
0
    if (*len != 0)
248
0
      line = cmd_capture_pane_append(line, len, " ", 1);
249
0
    line = cmd_capture_pane_append(line, len, uri, strlen(uri));
250
0
  }
251
0
  return (line);
252
0
}
253
254
static char *
255
cmd_capture_pane_history(struct args *args, struct cmdq_item *item,
256
    struct window_pane *wp, size_t *len)
257
0
{
258
0
  struct grid     *gd;
259
0
  const struct grid_line    *gl;
260
0
  struct screen     *s;
261
0
  struct grid_cell    *gc = NULL;
262
0
  struct window_mode_entry  *wme;
263
0
  int        n, join_lines, number_lines, flags = 0;
264
0
  int        show_flags, show_time, hyperlinks;
265
0
  u_int       *links = NULL, nlinks = 0;
266
0
  u_int        i, sx, top, bottom, tmp;
267
0
  char        *cause, *buf = NULL, *line, b[64], *cp;
268
0
  const char      *Sflag, *Eflag;
269
0
  size_t         linelen;
270
271
0
  sx = screen_size_x(&wp->base);
272
0
  if (args_has(args, 'a')) {
273
0
    gd = wp->base.saved_grid;
274
0
    if (gd == NULL) {
275
0
      if (!args_has(args, 'q')) {
276
0
        cmdq_error(item, "no alternate screen");
277
0
        return (NULL);
278
0
      }
279
0
      return (xstrdup(""));
280
0
    }
281
0
    s = &wp->base;
282
0
  } else if (args_has(args, 'M')) {
283
0
    wme = TAILQ_FIRST(&wp->modes);
284
0
    if (wme != NULL && wme->mode->get_screen != NULL) {
285
0
      s = wme->mode->get_screen (wme);
286
0
      gd = s->grid;
287
0
    } else {
288
0
      s = &wp->base;
289
0
      gd = wp->base.grid;
290
0
    }
291
0
  } else {
292
0
    s = &wp->base;
293
0
    gd = wp->base.grid;
294
0
  }
295
296
0
  Sflag = args_get(args, 'S');
297
0
  if (Sflag != NULL && strcmp(Sflag, "-") == 0)
298
0
    top = 0;
299
0
  else {
300
0
    n = args_strtonum_and_expand(args, 'S', INT_MIN, SHRT_MAX,
301
0
      item, &cause);
302
0
    if (cause != NULL) {
303
0
      top = gd->hsize;
304
0
      free(cause);
305
0
    } else if (n < 0 && (u_int)-n > gd->hsize)
306
0
      top = 0;
307
0
    else
308
0
      top = gd->hsize + n;
309
0
    if (top > gd->hsize + gd->sy - 1)
310
0
      top = gd->hsize + gd->sy - 1;
311
0
  }
312
313
0
  Eflag = args_get(args, 'E');
314
0
  if (Eflag != NULL && strcmp(Eflag, "-") == 0)
315
0
    bottom = gd->hsize + gd->sy - 1;
316
0
  else {
317
0
    n = args_strtonum_and_expand(args, 'E', INT_MIN, SHRT_MAX,
318
0
      item, &cause);
319
0
    if (cause != NULL) {
320
0
      bottom = gd->hsize + gd->sy - 1;
321
0
      free(cause);
322
0
    } else if (n < 0 && (u_int)-n > gd->hsize)
323
0
      bottom = 0;
324
0
    else
325
0
      bottom = gd->hsize + n;
326
0
    if (bottom > gd->hsize + gd->sy - 1)
327
0
      bottom = gd->hsize + gd->sy - 1;
328
0
  }
329
330
0
  if (bottom < top) {
331
0
    tmp = bottom;
332
0
    bottom = top;
333
0
    top = tmp;
334
0
  }
335
336
0
  join_lines = args_has(args, 'J');
337
0
  if (args_has(args, 'e'))
338
0
    flags |= GRID_STRING_WITH_SEQUENCES;
339
0
  if (args_has(args, 'C'))
340
0
    flags |= GRID_STRING_ESCAPE_SEQUENCES;
341
0
  if (!join_lines && !args_has(args, 'T'))
342
0
    flags |= GRID_STRING_EMPTY_CELLS;
343
0
  if (!join_lines && !args_has(args, 'N'))
344
0
    flags |= GRID_STRING_TRIM_SPACES;
345
0
  number_lines = args_has(args, 'L');
346
0
  show_flags = args_has(args, 'F');
347
0
  show_time = args_has(args, 'I');
348
0
  hyperlinks = args_has(args, 'H');
349
0
  if (hyperlinks)
350
0
    links = xreallocarray(NULL, gd->sx, sizeof *links);
351
352
0
  for (i = top; i <= bottom; i++) {
353
0
    if (hyperlinks) {
354
0
      line = cmd_capture_pane_hyperlinks(gd, s, i, links,
355
0
          &nlinks, &linelen);
356
0
    } else {
357
0
      line = grid_string_cells(gd, 0, i, sx, &gc, flags, s);
358
0
      linelen = strlen(line);
359
0
    }
360
0
    if (hyperlinks && linelen == 0) {
361
0
      free(line);
362
0
      continue;
363
0
    }
364
0
    gl = grid_peek_line(gd, i);
365
366
0
    if (number_lines) {
367
0
      if (i >= gd->hsize)
368
0
        n = i - gd->hsize;
369
0
      else
370
0
        n = (int)i - (int)gd->hsize;
371
0
      n = snprintf(b, sizeof b, "%d ", n);
372
0
      if (n >= 0)
373
0
        buf = cmd_capture_pane_append(buf, len, b, n);
374
0
    }
375
0
    if (show_time) {
376
0
      n = snprintf(b, sizeof b, "%llu ",
377
0
          (unsigned long long)grid_line_time(gl));
378
0
      if (n >= 0)
379
0
        buf = cmd_capture_pane_append(buf, len, b, n);
380
0
    }
381
0
    if (show_flags) {
382
0
      cp = b;
383
0
      *cp = '\0';
384
385
0
      if (gl->flags & GRID_LINE_DEAD)
386
0
        *cp++ = 'D';
387
0
      if (gl->flags & GRID_LINE_HYPERLINK)
388
0
        *cp++ = 'H';
389
0
      if (gl->flags & GRID_LINE_START_OUTPUT)
390
0
        *cp++ = 'O';
391
0
      if (gl->flags & GRID_LINE_START_PROMPT)
392
0
        *cp++ = 'P';
393
0
      if (gl->flags & GRID_LINE_WRAPPED)
394
0
        *cp++ = 'W';
395
0
      if (gl->flags & GRID_LINE_EXTENDED)
396
0
        *cp++ = 'X';
397
0
      if (b == cp)
398
0
        *cp++ = '-';
399
0
      *cp++ = ' ';
400
0
      *cp = '\0';
401
0
      buf = cmd_capture_pane_append(buf, len, b, strlen (b));
402
0
    }
403
0
    buf = cmd_capture_pane_append(buf, len, line, linelen);
404
405
0
    if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
406
0
      buf[(*len)++] = '\n';
407
408
0
    free(line);
409
0
  }
410
0
  free(links);
411
0
  if (buf == NULL)
412
0
    buf = xstrdup("");
413
0
  return (buf);
414
0
}
415
416
static enum cmd_retval
417
cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item)
418
0
{
419
0
  struct args   *args = cmd_get_args(self);
420
0
  struct client   *c = cmdq_get_client(item);
421
0
  struct window_pane  *wp = cmdq_get_target(item)->wp;
422
0
  char      *buf, *cause;
423
0
  const char    *bufname;
424
0
  size_t       len;
425
426
0
  if (cmd_get_entry(self) == &cmd_clear_history_entry) {
427
0
    window_pane_reset_mode_all(wp);
428
0
    grid_clear_history(wp->base.grid);
429
0
    if (args_has(args, 'H'))
430
0
      screen_reset_hyperlinks(wp->screen);
431
0
    server_redraw_window(wp->window);
432
0
    return (CMD_RETURN_NORMAL);
433
0
  }
434
435
0
  len = 0;
436
0
  if (args_has(args, 'R'))
437
0
    buf = cmd_capture_pane_grid(wp, &len);
438
0
  else if (args_has(args, 'P') && !args_has(args, 'H'))
439
0
    buf = cmd_capture_pane_pending(args, wp, &len);
440
0
  else
441
0
    buf = cmd_capture_pane_history(args, item, wp, &len);
442
0
  if (buf == NULL)
443
0
    return (CMD_RETURN_ERROR);
444
445
0
  if (args_has(args, 'p')) {
446
0
    if (len > 0 && buf[len - 1] == '\n')
447
0
      len--;
448
0
    if (c->flags & CLIENT_CONTROL)
449
0
      control_write(c, "%.*s", (int)len, buf);
450
0
    else {
451
0
      if (!file_can_print(c)) {
452
0
        cmdq_error(item, "can't write to client");
453
0
        free(buf);
454
0
        return (CMD_RETURN_ERROR);
455
0
      }
456
0
      file_print_buffer(c, buf, len);
457
0
      file_print(c, "\n");
458
0
    }
459
0
    free(buf);
460
0
  } else {
461
0
    bufname = NULL;
462
0
    if (args_has(args, 'b'))
463
0
      bufname = args_get(args, 'b');
464
465
0
    if (paste_set(buf, len, bufname, &cause) != 0) {
466
0
      cmdq_error(item, "%s", cause);
467
0
      free(cause);
468
0
      free(buf);
469
0
      return (CMD_RETURN_ERROR);
470
0
    }
471
0
  }
472
473
0
  return (CMD_RETURN_NORMAL);
474
0
}