xref: /openbsd-src/usr.bin/tmux/cmd-display-panes.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1 /* $OpenBSD: cmd-display-panes.c,v 1.22 2018/11/15 10:38:53 kn 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 " [template]",
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 	s = c->session;
59 
60 	if (c->identify_callback != NULL)
61 		return (CMD_RETURN_NORMAL);
62 
63 	c->identify_callback = cmd_display_panes_callback;
64 	if (args->argc != 0)
65 		c->identify_callback_data = xstrdup(args->argv[0]);
66 	else
67 		c->identify_callback_data = xstrdup("select-pane -t '%%'");
68 	c->identify_callback_item = item;
69 
70 	if (args_has(args, 'd')) {
71 		delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
72 		if (cause != NULL) {
73 			cmdq_error(item, "delay %s", cause);
74 			free(cause);
75 			return (CMD_RETURN_ERROR);
76 		}
77 	} else
78 		delay = options_get_number(s->options, "display-panes-time");
79 	server_client_set_identify(c, delay);
80 
81 	return (CMD_RETURN_WAIT);
82 }
83 
84 static enum cmd_retval
85 cmd_display_panes_error(struct cmdq_item *item, void *data)
86 {
87 	char	*error = data;
88 
89 	cmdq_error(item, "%s", error);
90 	free(error);
91 
92 	return (CMD_RETURN_NORMAL);
93 }
94 
95 static void
96 cmd_display_panes_callback(struct client *c, struct window_pane *wp)
97 {
98 	struct cmd_list		*cmdlist;
99 	struct cmdq_item	*new_item;
100 	char			*cmd, *expanded, *cause;
101 
102 	if (wp == NULL)
103 		goto out;
104 
105 	xasprintf(&expanded, "%%%u", wp->id);
106 	cmd = cmd_template_replace(c->identify_callback_data, expanded, 1);
107 
108 	cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
109 	if (cmdlist == NULL && cause != NULL)
110 		new_item = cmdq_get_callback(cmd_display_panes_error, cause);
111 	else if (cmdlist == NULL)
112 		new_item = NULL;
113 	else {
114 		new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
115 		cmd_list_free(cmdlist);
116 	}
117 
118 	if (new_item != NULL)
119 		cmdq_insert_after(c->identify_callback_item, new_item);
120 
121 	free(cmd);
122 	free(expanded);
123 
124 out:
125 	c->identify_callback_item->flags &= ~CMDQ_WAITING;
126 	c->identify_callback_item = NULL;
127 
128 	free(c->identify_callback_data);
129 	c->identify_callback_data = NULL;
130 
131 	c->identify_callback = NULL;
132 }
133