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