Coverage Report

Created: 2026-07-30 07:05

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