xref: /openbsd/usr.bin/tmux/cmd-show-messages.c (revision 4cfece93)
1 /* $OpenBSD: cmd-show-messages.c,v 1.35 2020/05/16 16:50:55 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 <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <unistd.h>
25 #include <vis.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Show message log.
31  */
32 
33 #define SHOW_MESSAGES_TEMPLATE \
34 	"#{t/p:message_time}: #{message_text}"
35 
36 static enum cmd_retval	cmd_show_messages_exec(struct cmd *,
37 			    struct cmdq_item *);
38 
39 const struct cmd_entry cmd_show_messages_entry = {
40 	.name = "show-messages",
41 	.alias = "showmsgs",
42 
43 	.args = { "JTt:", 0, 0 },
44 	.usage = "[-JT] " CMD_TARGET_CLIENT_USAGE,
45 
46 	.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
47 	.exec = cmd_show_messages_exec
48 };
49 
50 static int
51 cmd_show_messages_terminals(struct cmd *self, struct cmdq_item *item, int blank)
52 {
53 	struct args	*args = cmd_get_args(self);
54 	struct client	*tc = cmdq_get_target_client(item);
55 	struct tty_term	*term;
56 	u_int		 i, n;
57 
58 	n = 0;
59 	LIST_FOREACH(term, &tty_terms, entry) {
60 		if (args_has(args, 't') && term != tc->tty.term)
61 			continue;
62 		if (blank) {
63 			cmdq_print(item, "%s", "");
64 			blank = 0;
65 		}
66 		cmdq_print(item, "Terminal %u: %s for %s, flags=0x%x:", n,
67 		    term->name, term->tty->client->name, term->flags);
68 		n++;
69 		for (i = 0; i < tty_term_ncodes(); i++)
70 			cmdq_print(item, "%s", tty_term_describe(term, i));
71 	}
72 	return (n != 0);
73 }
74 
75 static enum cmd_retval
76 cmd_show_messages_exec(struct cmd *self, struct cmdq_item *item)
77 {
78 	struct args		*args = cmd_get_args(self);
79 	struct message_entry	*msg;
80 	char			*s;
81 	int			 done, blank;
82 	struct format_tree	*ft;
83 
84 	done = blank = 0;
85 	if (args_has(args, 'T')) {
86 		blank = cmd_show_messages_terminals(self, item, blank);
87 		done = 1;
88 	}
89 	if (args_has(args, 'J')) {
90 		job_print_summary(item, blank);
91 		done = 1;
92 	}
93 	if (done)
94 		return (CMD_RETURN_NORMAL);
95 
96 	ft = format_create_from_target(item);
97 	TAILQ_FOREACH_REVERSE(msg, &message_log, message_list, entry) {
98 		format_add(ft, "message_text", "%s", msg->msg);
99 		format_add(ft, "message_number", "%u", msg->msg_num);
100 		format_add_tv(ft, "message_time", &msg->msg_time);
101 
102 		s = format_expand(ft, SHOW_MESSAGES_TEMPLATE);
103 		cmdq_print(item, "%s", s);
104 		free(s);
105 	}
106 	format_free(ft);
107 
108 	return (CMD_RETURN_NORMAL);
109 }
110