cmd-save-buffer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
  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 <sys/stat.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "tmux.h"
  25. /*
  26. * Saves a paste buffer to a file.
  27. */
  28. enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
  29. const struct cmd_entry cmd_save_buffer_entry = {
  30. .name = "save-buffer",
  31. .alias = "saveb",
  32. .args = { "ab:", 1, 1 },
  33. .usage = "[-a] " CMD_BUFFER_USAGE " path",
  34. .flags = 0,
  35. .exec = cmd_save_buffer_exec
  36. };
  37. const struct cmd_entry cmd_show_buffer_entry = {
  38. .name = "show-buffer",
  39. .alias = "showb",
  40. .args = { "b:", 0, 0 },
  41. .usage = CMD_BUFFER_USAGE,
  42. .flags = 0,
  43. .exec = cmd_save_buffer_exec
  44. };
  45. enum cmd_retval
  46. cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
  47. {
  48. struct args *args = self->args;
  49. struct client *c = cmdq->client;
  50. struct session *s;
  51. struct paste_buffer *pb;
  52. const char *path, *bufname, *bufdata, *start, *end, *cwd;
  53. const char *flags;
  54. char *msg, *file, resolved[PATH_MAX];
  55. size_t size, used, msglen, bufsize;
  56. FILE *f;
  57. if (!args_has(args, 'b')) {
  58. if ((pb = paste_get_top(NULL)) == NULL) {
  59. cmdq_error(cmdq, "no buffers");
  60. return (CMD_RETURN_ERROR);
  61. }
  62. } else {
  63. bufname = args_get(args, 'b');
  64. pb = paste_get_name(bufname);
  65. if (pb == NULL) {
  66. cmdq_error(cmdq, "no buffer %s", bufname);
  67. return (CMD_RETURN_ERROR);
  68. }
  69. }
  70. bufdata = paste_buffer_data(pb, &bufsize);
  71. if (self->entry == &cmd_show_buffer_entry)
  72. path = "-";
  73. else
  74. path = args->argv[0];
  75. if (strcmp(path, "-") == 0) {
  76. if (c == NULL) {
  77. cmdq_error(cmdq, "can't write to stdout");
  78. return (CMD_RETURN_ERROR);
  79. }
  80. if (c->session == NULL || (c->flags & CLIENT_CONTROL))
  81. goto do_stdout;
  82. goto do_print;
  83. }
  84. if (c != NULL && c->session == NULL && c->cwd != NULL)
  85. cwd = c->cwd;
  86. else if ((s = c->session) != NULL && s->cwd != NULL)
  87. cwd = s->cwd;
  88. else
  89. cwd = ".";
  90. flags = "wb";
  91. if (args_has(self->args, 'a'))
  92. flags = "ab";
  93. if (*path == '/')
  94. file = xstrdup(path);
  95. else
  96. xasprintf(&file, "%s/%s", cwd, path);
  97. if (realpath(file, resolved) == NULL &&
  98. strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) {
  99. cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG));
  100. return (CMD_RETURN_ERROR);
  101. }
  102. f = fopen(resolved, flags);
  103. free(file);
  104. if (f == NULL) {
  105. cmdq_error(cmdq, "%s: %s", resolved, strerror(errno));
  106. return (CMD_RETURN_ERROR);
  107. }
  108. if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
  109. cmdq_error(cmdq, "%s: write error", resolved);
  110. fclose(f);
  111. return (CMD_RETURN_ERROR);
  112. }
  113. fclose(f);
  114. return (CMD_RETURN_NORMAL);
  115. do_stdout:
  116. evbuffer_add(c->stdout_data, bufdata, bufsize);
  117. server_client_push_stdout(c);
  118. return (CMD_RETURN_NORMAL);
  119. do_print:
  120. if (bufsize > (INT_MAX / 4) - 1) {
  121. cmdq_error(cmdq, "buffer too big");
  122. return (CMD_RETURN_ERROR);
  123. }
  124. msg = NULL;
  125. used = 0;
  126. while (used != bufsize) {
  127. start = bufdata + used;
  128. end = memchr(start, '\n', bufsize - used);
  129. if (end != NULL)
  130. size = end - start;
  131. else
  132. size = bufsize - used;
  133. msglen = size * 4 + 1;
  134. msg = xrealloc(msg, msglen);
  135. strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
  136. cmdq_print(cmdq, "%s", msg);
  137. used += size + (end != NULL);
  138. }
  139. free(msg);
  140. return (CMD_RETURN_NORMAL);
  141. }