xref: /openbsd-src/usr.bin/tmux/cmd-display-panes.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /* $OpenBSD: cmd-display-panes.c,v 1.20 2017/08/16 12:12:54 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@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 <ctype.h>
22 #include <stdlib.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Display panes on a client.
28  */
29 
30 static enum cmd_retval	cmd_display_panes_exec(struct cmd *,
31 			    struct cmdq_item *);
32 
33 static void		cmd_display_panes_callback(struct client *,
34 			    struct window_pane *);
35 
36 const struct cmd_entry cmd_display_panes_entry = {
37 	.name = "display-panes",
38 	.alias = "displayp",
39 
40 	.args = { "d:t:", 0, 1 },
41 	.usage = "[-d duration] " CMD_TARGET_CLIENT_USAGE,
42 
43 	.flags = CMD_AFTERHOOK,
44 	.exec = cmd_display_panes_exec
45 };
46 
47 static enum cmd_retval
48 cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
49 {
50 	struct args	*args = self->args;
51 	struct client	*c;
52 	struct session	*s;
53 	u_int		 delay;
54 	char		*cause;
55 
56 	if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
57 		return (CMD_RETURN_ERROR);
58 
59 	if (c->identify_callback != NULL)
60 		return (CMD_RETURN_NORMAL);
61 
62 	c->identify_callback = cmd_display_panes_callback;
63 	if (args->argc != 0)
64 		c->identify_callback_data = xstrdup(args->argv[0]);
65 	else
66 		c->identify_callback_data = xstrdup("select-pane -t '%%'");
67 	s = c->session;
68 
69 	if (args_has(args, 'd')) {
70 		delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
71 		if (cause != NULL) {
72 			cmdq_error(item, "delay %s", cause);
73 			free(cause);
74 			return (CMD_RETURN_ERROR);
75 		}
76 	} else
77 		delay = options_get_number(s->options, "display-panes-time");
78 	server_client_set_identify(c, delay);
79 
80 	return (CMD_RETURN_NORMAL);
81 }
82 
83 static enum cmd_retval
84 cmd_display_panes_error(struct cmdq_item *item, void *data)
85 {
86 	char	*error = data;
87 
88 	cmdq_error(item, "%s", error);
89 	free(error);
90 
91 	return (CMD_RETURN_NORMAL);
92 }
93 
94 static void
95 cmd_display_panes_callback(struct client *c, struct window_pane *wp)
96 {
97 	struct cmd_list		*cmdlist;
98 	struct cmdq_item	*new_item;
99 	char			*template, *cmd, *expanded, *cause;
100 
101 	template = c->identify_callback_data;
102 	if (wp == NULL)
103 		goto out;
104 	xasprintf(&expanded, "%%%u", wp->id);
105 	cmd = cmd_template_replace(template, expanded, 1);
106 
107 	cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
108 	if (cmdlist == NULL) {
109 		if (cause != NULL) {
110 			new_item = cmdq_get_callback(cmd_display_panes_error,
111 			    cause);
112 		} else
113 			new_item = NULL;
114 	} else {
115 		new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
116 		cmd_list_free(cmdlist);
117 	}
118 
119 	if (new_item != NULL)
120 		cmdq_append(c, new_item);
121 
122 	free(cmd);
123 	free(expanded);
124 
125 out:
126 	free(c->identify_callback_data);
127 	c->identify_callback_data = NULL;
128 	c->identify_callback = NULL;
129 }
130