Home | History | Annotate | Line # | Download | only in dist
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      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 
     21 #include <stdlib.h>
     22 #include <string.h>
     23 
     24 #include "tmux.h"
     25 
     26 /*
     27  * Add, set, append to or delete a paste buffer.
     28  */
     29 
     30 static enum cmd_retval	cmd_set_buffer_exec(struct cmd *, struct cmdq_item *);
     31 
     32 const struct cmd_entry cmd_set_buffer_entry = {
     33 	.name = "set-buffer",
     34 	.alias = "setb",
     35 
     36 	.args = { "ab:t:n:w", 0, 1, NULL },
     37 	.usage = "[-aw] " CMD_BUFFER_USAGE " [-n new-buffer-name] "
     38 	         CMD_TARGET_CLIENT_USAGE " [data]",
     39 
     40 	.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
     41 	.exec = cmd_set_buffer_exec
     42 };
     43 
     44 const struct cmd_entry cmd_delete_buffer_entry = {
     45 	.name = "delete-buffer",
     46 	.alias = "deleteb",
     47 
     48 	.args = { "b:", 0, 0, NULL },
     49 	.usage = CMD_BUFFER_USAGE,
     50 
     51 	.flags = CMD_AFTERHOOK,
     52 	.exec = cmd_set_buffer_exec
     53 };
     54 
     55 static enum cmd_retval
     56 cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item)
     57 {
     58 	struct args		*args = cmd_get_args(self);
     59 	struct client		*tc = cmdq_get_target_client(item);
     60 	struct paste_buffer	*pb;
     61 	char			*bufdata, *cause;
     62 	const char		*bufname, *olddata;
     63 	size_t			 bufsize, newsize;
     64 
     65 	bufname = args_get(args, 'b');
     66 	if (bufname == NULL)
     67 		pb = NULL;
     68 	else
     69 		pb = paste_get_name(bufname);
     70 
     71 	if (cmd_get_entry(self) == &cmd_delete_buffer_entry) {
     72 		if (pb == NULL) {
     73 			if (bufname != NULL) {
     74 				cmdq_error(item, "unknown buffer: %s", bufname);
     75 				return (CMD_RETURN_ERROR);
     76 			}
     77 			pb = paste_get_top(&bufname);
     78 		}
     79 		if (pb == NULL) {
     80 			cmdq_error(item, "no buffer");
     81 			return (CMD_RETURN_ERROR);
     82 		}
     83 		paste_free(pb);
     84 		return (CMD_RETURN_NORMAL);
     85 	}
     86 
     87 	if (args_has(args, 'n')) {
     88 		if (pb == NULL) {
     89 			if (bufname != NULL) {
     90 				cmdq_error(item, "unknown buffer: %s", bufname);
     91 				return (CMD_RETURN_ERROR);
     92 			}
     93 			pb = paste_get_top(&bufname);
     94 		}
     95 		if (pb == NULL) {
     96 			cmdq_error(item, "no buffer");
     97 			return (CMD_RETURN_ERROR);
     98 		}
     99 		if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) {
    100 			cmdq_error(item, "%s", cause);
    101 			free(cause);
    102 			return (CMD_RETURN_ERROR);
    103 		}
    104 		return (CMD_RETURN_NORMAL);
    105 	}
    106 
    107 	if (args_count(args) != 1) {
    108 		cmdq_error(item, "no data specified");
    109 		return (CMD_RETURN_ERROR);
    110 	}
    111 	if ((newsize = strlen(args_string(args, 0))) == 0)
    112 		return (CMD_RETURN_NORMAL);
    113 
    114 	bufsize = 0;
    115 	bufdata = NULL;
    116 
    117 	if (args_has(args, 'a') && pb != NULL) {
    118 		olddata = paste_buffer_data(pb, &bufsize);
    119 		bufdata = xmalloc(bufsize);
    120 		memcpy(bufdata, olddata, bufsize);
    121 	}
    122 
    123 	bufdata = xrealloc(bufdata, bufsize + newsize);
    124 	memcpy(bufdata + bufsize, args_string(args, 0), newsize);
    125 	bufsize += newsize;
    126 
    127 	if (paste_set(bufdata, bufsize, bufname, &cause) != 0) {
    128 		cmdq_error(item, "%s", cause);
    129 		free(bufdata);
    130 		free(cause);
    131 		return (CMD_RETURN_ERROR);
    132 	}
    133 	if (args_has(args, 'w') && tc != NULL)
    134  		tty_set_selection(&tc->tty, "", bufdata, bufsize);
    135 
    136 	return (CMD_RETURN_NORMAL);
    137 }
    138