2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-06-28 22:10:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <errno.h>
|
2013-10-10 12:26:34 +00:00
|
|
|
#include <fcntl.h>
|
2010-02-22 20:33:12 +00:00
|
|
|
#include <stdio.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-06-28 22:10:42 +00:00
|
|
|
#include <unistd.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
2011-10-23 00:49:25 +00:00
|
|
|
* Loads a paste buffer from a file.
|
2009-06-01 22:58:49 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_load_buffer_exec(struct cmd *, struct cmdq_item *);
|
2016-10-10 21:51:39 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_load_buffer_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "load-buffer",
|
|
|
|
.alias = "loadb",
|
|
|
|
|
2020-09-02 13:46:35 +00:00
|
|
|
.args = { "b:t:w", 1, 1 },
|
|
|
|
.usage = CMD_BUFFER_USAGE " " CMD_TARGET_CLIENT_USAGE " path",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
2020-09-02 13:46:35 +00:00
|
|
|
.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_load_buffer_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
struct cmd_load_buffer_data {
|
2020-09-02 13:46:35 +00:00
|
|
|
struct client *client;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2019-12-12 11:39:56 +00:00
|
|
|
char *name;
|
2016-10-16 17:55:14 +00:00
|
|
|
};
|
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
static void
|
|
|
|
cmd_load_buffer_done(__unused struct client *c, const char *path, int error,
|
|
|
|
int closed, struct evbuffer *buffer, void *data)
|
|
|
|
{
|
|
|
|
struct cmd_load_buffer_data *cdata = data;
|
2020-09-02 13:46:35 +00:00
|
|
|
struct client *tc = cdata->client;
|
2019-12-12 11:39:56 +00:00
|
|
|
struct cmdq_item *item = cdata->item;
|
|
|
|
void *bdata = EVBUFFER_DATA(buffer);
|
|
|
|
size_t bsize = EVBUFFER_LENGTH(buffer);
|
|
|
|
void *copy;
|
|
|
|
char *cause;
|
|
|
|
|
|
|
|
if (!closed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
cmdq_error(item, "%s: %s", path, strerror(error));
|
|
|
|
else if (bsize != 0) {
|
|
|
|
copy = xmalloc(bsize);
|
|
|
|
memcpy(copy, bdata, bsize);
|
|
|
|
if (paste_set(copy, bsize, cdata->name, &cause) != 0) {
|
|
|
|
cmdq_error(item, "%s", cause);
|
|
|
|
free(cause);
|
|
|
|
free(copy);
|
2020-09-02 13:46:35 +00:00
|
|
|
} else if (tc != NULL &&
|
|
|
|
tc->session != NULL &&
|
|
|
|
(~tc->flags & CLIENT_DEAD))
|
|
|
|
tty_set_selection(&tc->tty, copy, bsize);
|
|
|
|
if (tc != NULL)
|
|
|
|
server_client_unref(tc);
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
|
|
|
cmdq_continue(item);
|
|
|
|
|
|
|
|
free(cdata->name);
|
|
|
|
free(cdata);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2020-09-02 13:46:35 +00:00
|
|
|
struct client *tc = cmdq_get_target_client(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
struct cmd_load_buffer_data *cdata;
|
2019-12-12 11:39:56 +00:00
|
|
|
const char *bufname = args_get(args, 'b');
|
|
|
|
char *path;
|
2014-05-13 07:34:35 +00:00
|
|
|
|
2020-09-04 12:24:25 +00:00
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
2019-12-12 11:39:56 +00:00
|
|
|
cdata->item = item;
|
|
|
|
if (bufname != NULL)
|
|
|
|
cdata->name = xstrdup(bufname);
|
2020-09-02 13:46:35 +00:00
|
|
|
if (args_has(args, 'w') && tc != NULL) {
|
|
|
|
cdata->client = tc;
|
|
|
|
cdata->client->references++;
|
|
|
|
}
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2020-04-13 20:51:57 +00:00
|
|
|
path = format_single_from_target(item, args->argv[0]);
|
2020-04-13 10:59:58 +00:00
|
|
|
file_read(cmdq_get_client(item), path, cmd_load_buffer_done, cdata);
|
2018-07-31 13:06:44 +00:00
|
|
|
free(path);
|
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
return (CMD_RETURN_WAIT);
|
2010-07-24 20:11:59 +00:00
|
|
|
}
|