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",
|
|
|
|
|
|
|
|
.args = { "b:", 1, 1 },
|
|
|
|
.usage = CMD_BUFFER_USAGE " path",
|
|
|
|
|
2016-10-14 22:14:22 +00:00
|
|
|
.flags = CMD_AFTERHOOK,
|
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 {
|
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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2016-10-16 17:55:14 +00:00
|
|
|
struct args *args = self->args;
|
|
|
|
struct cmd_load_buffer_data *cdata;
|
2018-07-11 08:29:21 +00:00
|
|
|
struct client *c = cmd_find_client(item, NULL, 1);
|
|
|
|
struct session *s = item->target.s;
|
|
|
|
struct winlink *wl = item->target.wl;
|
|
|
|
struct window_pane *wp = item->target.wp;
|
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
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
cdata = xmalloc(sizeof *cdata);
|
|
|
|
cdata->item = item;
|
|
|
|
if (bufname != NULL)
|
|
|
|
cdata->name = xstrdup(bufname);
|
|
|
|
else
|
|
|
|
cdata->name = NULL;
|
2011-01-04 00:42:46 +00:00
|
|
|
|
2018-07-11 08:29:21 +00:00
|
|
|
path = format_single(item, args->argv[0], c, s, wl, wp);
|
2019-12-12 11:39:56 +00:00
|
|
|
file_read(item->client, 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
|
|
|
}
|