Coverage Report

Created: 2026-10-17 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-save-buffer.c
Line
Count
Source
1
/* $OpenBSD: cmd-save-buffer.c,v 1.58 2026/10/09 13:12:14 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
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
#include <sys/stat.h>
21
22
#include <errno.h>
23
#include <fcntl.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <unistd.h>
27
28
#include "tmux.h"
29
30
/*
31
 * Saves a paste buffer to a file.
32
 */
33
34
static enum cmd_retval  cmd_save_buffer_exec(struct cmd *, struct cmdq_item *);
35
36
const struct cmd_entry cmd_save_buffer_entry = {
37
  .name = "save-buffer",
38
  .description = "Save a paste buffer to a file.",
39
  .alias = "saveb",
40
41
  .args = { "ab:", 1, 1, NULL },
42
  .usage = "[-a] " CMD_BUFFER_USAGE " path",
43
44
  .flags = CMD_AFTERHOOK,
45
  .exec = cmd_save_buffer_exec
46
};
47
48
const struct cmd_entry cmd_show_buffer_entry = {
49
  .name = "show-buffer",
50
  .description = "Print a paste buffer.",
51
  .alias = "showb",
52
53
  .args = { "b:", 0, 0, NULL },
54
  .usage = CMD_BUFFER_USAGE,
55
56
  .flags = CMD_AFTERHOOK,
57
  .exec = cmd_save_buffer_exec
58
};
59
60
static void
61
cmd_save_buffer_done(__unused struct client *c, const char *path, int error,
62
    __unused int closed, __unused struct evbuffer *buffer, void *data)
63
0
{
64
0
  struct cmdq_item  *item = data;
65
66
0
  if (!closed)
67
0
    return;
68
69
0
  if (error != 0)
70
0
    cmdq_error(item, "%s: %s", strerror(error), path);
71
0
  cmdq_continue(item);
72
0
}
73
74
static enum cmd_retval
75
cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
76
0
{
77
0
  struct args   *args = cmd_get_args(self);
78
0
  struct client   *c = cmdq_get_client(item);
79
0
  struct paste_buffer *pb;
80
0
  int      flags;
81
0
  const char    *bufname = args_get(args, 'b'), *bufdata;
82
0
  size_t       bufsize;
83
0
  char      *path;
84
0
  struct evbuffer   *evb;
85
86
0
  if (bufname == NULL) {
87
0
    if ((pb = paste_get_top(NULL)) == NULL) {
88
0
      cmdq_error(item, "no buffers");
89
0
      return (CMD_RETURN_ERROR);
90
0
    }
91
0
  } else {
92
0
    pb = paste_get_name(bufname);
93
0
    if (pb == NULL) {
94
0
      cmdq_error(item, "no buffer %s", bufname);
95
0
      return (CMD_RETURN_ERROR);
96
0
    }
97
0
  }
98
0
  bufdata = paste_buffer_data(pb, &bufsize);
99
100
0
  if (cmd_get_entry(self) == &cmd_show_buffer_entry) {
101
0
    if (c->session != NULL || (c->flags & CLIENT_CONTROL)) {
102
0
      evb = evbuffer_new();
103
0
      if (evb == NULL)
104
0
        fatalx("out of memory");
105
0
      evbuffer_add(evb, bufdata, bufsize);
106
0
      cmdq_print_data(item, evb);
107
0
      evbuffer_free(evb);
108
0
      return (CMD_RETURN_NORMAL);
109
0
    }
110
0
    path = xstrdup("-");
111
0
  } else
112
0
    path = format_single_from_target(item, args_string(args, 0));
113
0
  if (args_has(args, 'a'))
114
0
    flags = O_APPEND;
115
0
  else
116
0
    flags = O_TRUNC;
117
0
  file_write(cmdq_get_client(item), path, flags, bufdata, bufsize,
118
0
      cmd_save_buffer_done, item);
119
0
  free(path);
120
121
0
  return (CMD_RETURN_WAIT);
122
0
}