1*1dd5a954Snicm /* $OpenBSD: cmd-display-message.c,v 1.63 2023/02/05 21:15:32 nicm Exp $ */
29629a58fSnicm
39629a58fSnicm /*
49629a58fSnicm * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
59629a58fSnicm *
69629a58fSnicm * Permission to use, copy, modify, and distribute this software for any
79629a58fSnicm * purpose with or without fee is hereby granted, provided that the above
89629a58fSnicm * copyright notice and this permission notice appear in all copies.
99629a58fSnicm *
109629a58fSnicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
119629a58fSnicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129629a58fSnicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
139629a58fSnicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
149629a58fSnicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
159629a58fSnicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
169629a58fSnicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
179629a58fSnicm */
189629a58fSnicm
199629a58fSnicm #include <sys/types.h>
209629a58fSnicm
217d053cf9Snicm #include <stdlib.h>
229629a58fSnicm #include <time.h>
239629a58fSnicm
249629a58fSnicm #include "tmux.h"
259629a58fSnicm
269629a58fSnicm /*
279629a58fSnicm * Displays a message in the status line.
289629a58fSnicm */
299629a58fSnicm
301905ff33Snicm #define DISPLAY_MESSAGE_TEMPLATE \
311905ff33Snicm "[#{session_name}] #{window_index}:" \
321905ff33Snicm "#{window_name}, current pane #{pane_index} " \
331905ff33Snicm "- (%H:%M %d-%b-%y)"
341905ff33Snicm
3568e0a7f2Snicm static enum cmd_retval cmd_display_message_exec(struct cmd *,
3668e0a7f2Snicm struct cmdq_item *);
379629a58fSnicm
389629a58fSnicm const struct cmd_entry cmd_display_message_entry = {
39c057646bSnicm .name = "display-message",
40c057646bSnicm .alias = "display",
41c057646bSnicm
42a04d395bSnicm .args = { "ac:d:lINpt:F:v", 0, 1, NULL },
43a04d395bSnicm .usage = "[-aIlNpv] [-c target-client] [-d delay] [-F format] "
44c057646bSnicm CMD_TARGET_PANE_USAGE " [message]",
45c057646bSnicm
463d231979Snicm .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
478d471e80Snicm
48035dc73dSnicm .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL,
49c057646bSnicm .exec = cmd_display_message_exec
509629a58fSnicm };
519629a58fSnicm
5271431f24Snicm static void
cmd_display_message_each(const char * key,const char * value,void * arg)5371431f24Snicm cmd_display_message_each(const char *key, const char *value, void *arg)
5471431f24Snicm {
5571431f24Snicm struct cmdq_item *item = arg;
5671431f24Snicm
5771431f24Snicm cmdq_print(item, "%s=%s", key, value);
5871431f24Snicm }
5971431f24Snicm
60dc1f0f5fSnicm static enum cmd_retval
cmd_display_message_exec(struct cmd * self,struct cmdq_item * item)6168e0a7f2Snicm cmd_display_message_exec(struct cmd *self, struct cmdq_item *item)
629629a58fSnicm {
6390d7ba38Snicm struct args *args = cmd_get_args(self);
64040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
65035dc73dSnicm struct client *tc = cmdq_get_target_client(item), *c;
66040343aeSnicm struct session *s = target->s;
67040343aeSnicm struct winlink *wl = target->wl;
68040343aeSnicm struct window_pane *wp = target->wp;
699629a58fSnicm const char *template;
70aab3c1a6Snicm char *msg, *cause;
71*1dd5a954Snicm int delay = -1, flags, Nflag = args_has(args, 'N');
72e62f15caSnicm struct format_tree *ft;
731693b10bSnicm u_int count = args_count(args);
74*1dd5a954Snicm struct evbuffer *evb;
759629a58fSnicm
76aab3c1a6Snicm if (args_has(args, 'I')) {
773d231979Snicm if (wp == NULL)
783d231979Snicm return (CMD_RETURN_NORMAL);
79d110efb0Snicm switch (window_pane_start_input(wp, item, &cause)) {
80d110efb0Snicm case -1:
81aab3c1a6Snicm cmdq_error(item, "%s", cause);
82aab3c1a6Snicm free(cause);
83aab3c1a6Snicm return (CMD_RETURN_ERROR);
84d110efb0Snicm case 1:
85d110efb0Snicm return (CMD_RETURN_NORMAL);
86d110efb0Snicm case 0:
87aab3c1a6Snicm return (CMD_RETURN_WAIT);
88aab3c1a6Snicm }
89d110efb0Snicm }
90aab3c1a6Snicm
911693b10bSnicm if (args_has(args, 'F') && count != 0) {
9268e0a7f2Snicm cmdq_error(item, "only one of -F or argument must be given");
93a224d0d3Snicm return (CMD_RETURN_ERROR);
94e62f15caSnicm }
959629a58fSnicm
96247fdabfSnicm if (args_has(args, 'd')) {
97247fdabfSnicm delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
98247fdabfSnicm if (cause != NULL) {
99247fdabfSnicm cmdq_error(item, "delay %s", cause);
100247fdabfSnicm free(cause);
101247fdabfSnicm return (CMD_RETURN_ERROR);
102247fdabfSnicm }
103247fdabfSnicm }
104247fdabfSnicm
1051693b10bSnicm if (count != 0)
1061693b10bSnicm template = args_string(args, 0);
1071693b10bSnicm else
108e62f15caSnicm template = args_get(args, 'F');
109e62f15caSnicm if (template == NULL)
11061e1d212Snicm template = DISPLAY_MESSAGE_TEMPLATE;
111e62f15caSnicm
112b6908644Snicm /*
113b6908644Snicm * -c is intended to be the client where the message should be
114b6908644Snicm * displayed if -p is not given. But it makes sense to use it for the
115b6908644Snicm * formats too, assuming it matches the session. If it doesn't, use the
116b6908644Snicm * best client for the session.
117b6908644Snicm */
118035dc73dSnicm if (tc != NULL && tc->session == s)
119035dc73dSnicm c = tc;
1203d231979Snicm else if (s != NULL)
121035dc73dSnicm c = cmd_find_best_client(s);
1223d231979Snicm else
1233d231979Snicm c = NULL;
12490d7ba38Snicm if (args_has(args, 'v'))
125e1e244cdSnicm flags = FORMAT_VERBOSE;
126e1e244cdSnicm else
127e1e244cdSnicm flags = 0;
128040343aeSnicm ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, flags);
129035dc73dSnicm format_defaults(ft, c, s, wl, wp);
130e62f15caSnicm
13171431f24Snicm if (args_has(args, 'a')) {
13271431f24Snicm format_each(ft, cmd_display_message_each, item);
13371431f24Snicm return (CMD_RETURN_NORMAL);
13471431f24Snicm }
13571431f24Snicm
136a04d395bSnicm if (args_has(args, 'l'))
137a04d395bSnicm msg = xstrdup(template);
138a04d395bSnicm else
139a7d9196cSnicm msg = format_expand_time(ft, template);
140a04d395bSnicm
1413d231979Snicm if (cmdq_get_client(item) == NULL)
1423d231979Snicm cmdq_error(item, "%s", msg);
1433d231979Snicm else if (args_has(args, 'p'))
14468e0a7f2Snicm cmdq_print(item, "%s", msg);
145*1dd5a954Snicm else if (tc != NULL && (tc->flags & CLIENT_CONTROL)) {
146*1dd5a954Snicm evb = evbuffer_new();
147*1dd5a954Snicm if (evb == NULL)
148*1dd5a954Snicm fatalx("out of memory");
149*1dd5a954Snicm evbuffer_add_printf(evb, "%%message %s", msg);
150*1dd5a954Snicm server_client_print(tc, 0, evb);
151*1dd5a954Snicm evbuffer_free(evb);
152*1dd5a954Snicm } else if (tc != NULL)
153*1dd5a954Snicm status_message_set(tc, delay, 0, Nflag, "%s", msg);
1547d053cf9Snicm free(msg);
1556f99165fSnicm
156cfffdbe4Snicm format_free(ft);
157175d36ccSnicm
158a224d0d3Snicm return (CMD_RETURN_NORMAL);
1599629a58fSnicm }
160