cmd-load-buffer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include "tmux.h"
  25. /*
  26. * Loads a paste buffer from a file.
  27. */
  28. enum cmd_retval cmd_load_buffer_exec(struct cmd *, struct cmd_q *);
  29. void cmd_load_buffer_callback(struct client *, int, void *);
  30. const struct cmd_entry cmd_load_buffer_entry = {
  31. .name = "load-buffer",
  32. .alias = "loadb",
  33. .args = { "b:", 1, 1 },
  34. .usage = CMD_BUFFER_USAGE " path",
  35. .flags = 0,
  36. .exec = cmd_load_buffer_exec
  37. };
  38. enum cmd_retval
  39. cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
  40. {
  41. struct args *args = self->args;
  42. struct client *c = cmdq->client;
  43. struct session *s;
  44. FILE *f;
  45. const char *path, *bufname, *cwd;
  46. char *pdata, *new_pdata, *cause, *file, resolved[PATH_MAX];
  47. size_t psize;
  48. int ch, error;
  49. bufname = NULL;
  50. if (args_has(args, 'b'))
  51. bufname = args_get(args, 'b');
  52. path = args->argv[0];
  53. if (strcmp(path, "-") == 0) {
  54. error = server_set_stdin_callback(c, cmd_load_buffer_callback,
  55. (void *)bufname, &cause);
  56. if (error != 0) {
  57. cmdq_error(cmdq, "%s: %s", path, cause);
  58. free(cause);
  59. return (CMD_RETURN_ERROR);
  60. }
  61. return (CMD_RETURN_WAIT);
  62. }
  63. if (c != NULL && c->session == NULL && c->cwd != NULL)
  64. cwd = c->cwd;
  65. else if ((s = c->session) != NULL && s->cwd != NULL)
  66. cwd = s->cwd;
  67. else
  68. cwd = ".";
  69. if (*path == '/')
  70. file = xstrdup(path);
  71. else
  72. xasprintf(&file, "%s/%s", cwd, path);
  73. if (realpath(file, resolved) == NULL &&
  74. strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) {
  75. cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG));
  76. return (CMD_RETURN_ERROR);
  77. }
  78. f = fopen(resolved, "rb");
  79. free(file);
  80. if (f == NULL) {
  81. cmdq_error(cmdq, "%s: %s", resolved, strerror(errno));
  82. return (CMD_RETURN_ERROR);
  83. }
  84. pdata = NULL;
  85. psize = 0;
  86. while ((ch = getc(f)) != EOF) {
  87. /* Do not let the server die due to memory exhaustion. */
  88. if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
  89. cmdq_error(cmdq, "realloc error: %s", strerror(errno));
  90. goto error;
  91. }
  92. pdata = new_pdata;
  93. pdata[psize++] = ch;
  94. }
  95. if (ferror(f)) {
  96. cmdq_error(cmdq, "%s: read error", resolved);
  97. goto error;
  98. }
  99. if (pdata != NULL)
  100. pdata[psize] = '\0';
  101. fclose(f);
  102. if (paste_set(pdata, psize, bufname, &cause) != 0) {
  103. cmdq_error(cmdq, "%s", cause);
  104. free(pdata);
  105. free(cause);
  106. return (CMD_RETURN_ERROR);
  107. }
  108. return (CMD_RETURN_NORMAL);
  109. error:
  110. free(pdata);
  111. if (f != NULL)
  112. fclose(f);
  113. return (CMD_RETURN_ERROR);
  114. }
  115. void
  116. cmd_load_buffer_callback(struct client *c, int closed, void *data)
  117. {
  118. const char *bufname = data;
  119. char *pdata, *cause, *saved;
  120. size_t psize;
  121. if (!closed)
  122. return;
  123. c->stdin_callback = NULL;
  124. server_client_unref(c);
  125. if (c->flags & CLIENT_DEAD)
  126. return;
  127. psize = EVBUFFER_LENGTH(c->stdin_data);
  128. if (psize == 0 || (pdata = malloc(psize + 1)) == NULL)
  129. goto out;
  130. memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
  131. pdata[psize] = '\0';
  132. evbuffer_drain(c->stdin_data, psize);
  133. if (paste_set(pdata, psize, bufname, &cause) != 0) {
  134. /* No context so can't use server_client_msg_error. */
  135. if (~c->flags & CLIENT_UTF8) {
  136. saved = cause;
  137. cause = utf8_sanitize(saved);
  138. free(saved);
  139. }
  140. evbuffer_add_printf(c->stderr_data, "%s", cause);
  141. server_client_push_stderr(c);
  142. free(pdata);
  143. free(cause);
  144. }
  145. out:
  146. cmdq_continue(c->cmdq);
  147. }