1 /* $OpenBSD: cmd-display-panes.c,v 1.23 2019/02/06 07:36:06 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 = { "bd:t:", 0, 1 }, 41 .usage = "[-b] [-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 if (args_has(args, 'b')) 69 c->identify_callback_item = NULL; 70 else 71 c->identify_callback_item = item; 72 73 if (args_has(args, 'd')) { 74 delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause); 75 if (cause != NULL) { 76 cmdq_error(item, "delay %s", cause); 77 free(cause); 78 return (CMD_RETURN_ERROR); 79 } 80 } else 81 delay = options_get_number(s->options, "display-panes-time"); 82 server_client_set_identify(c, delay); 83 84 if (args_has(args, 'b')) 85 return (CMD_RETURN_NORMAL); 86 return (CMD_RETURN_WAIT); 87 } 88 89 static enum cmd_retval 90 cmd_display_panes_error(struct cmdq_item *item, void *data) 91 { 92 char *error = data; 93 94 cmdq_error(item, "%s", error); 95 free(error); 96 97 return (CMD_RETURN_NORMAL); 98 } 99 100 static void 101 cmd_display_panes_callback(struct client *c, struct window_pane *wp) 102 { 103 struct cmd_list *cmdlist; 104 struct cmdq_item *new_item; 105 char *cmd, *expanded, *cause; 106 107 if (wp == NULL) 108 goto out; 109 110 xasprintf(&expanded, "%%%u", wp->id); 111 cmd = cmd_template_replace(c->identify_callback_data, expanded, 1); 112 113 cmdlist = cmd_string_parse(cmd, NULL, 0, &cause); 114 if (cmdlist == NULL && cause != NULL) 115 new_item = cmdq_get_callback(cmd_display_panes_error, cause); 116 else if (cmdlist == NULL) 117 new_item = NULL; 118 else { 119 new_item = cmdq_get_command(cmdlist, NULL, NULL, 0); 120 cmd_list_free(cmdlist); 121 } 122 123 if (new_item != NULL) { 124 if (c->identify_callback_item != NULL) 125 cmdq_insert_after(c->identify_callback_item, new_item); 126 else 127 cmdq_append(c, new_item); 128 } 129 130 free(cmd); 131 free(expanded); 132 133 out: 134 if (c->identify_callback_item != NULL) { 135 c->identify_callback_item->flags &= ~CMDQ_WAITING; 136 c->identify_callback_item = NULL; 137 } 138 139 free(c->identify_callback_data); 140 c->identify_callback_data = NULL; 141 142 c->identify_callback = NULL; 143 } 144