xref: /netbsd-src/external/bsd/tmux/dist/cmd-show-messages.c (revision 46548964d21002ed96d2f9f7eeb6357c81c3425c)
199e242abSchristos /* $OpenBSD$ */
2698d5317Sjmmv 
3698d5317Sjmmv /*
4f26e8bc9Schristos  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv  *
6698d5317Sjmmv  * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv  * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv  * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv  *
10698d5317Sjmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv  */
18698d5317Sjmmv 
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv 
21e271dbb8Schristos #include <stdlib.h>
22698d5317Sjmmv #include <string.h>
23928fc495Schristos #include <unistd.h>
24698d5317Sjmmv 
25698d5317Sjmmv #include "tmux.h"
26698d5317Sjmmv 
27698d5317Sjmmv /*
28e271dbb8Schristos  * Show message log.
29698d5317Sjmmv  */
30698d5317Sjmmv 
31e271dbb8Schristos #define SHOW_MESSAGES_TEMPLATE \
32e271dbb8Schristos 	"#{t/p:message_time}: #{message_text}"
33e271dbb8Schristos 
34e9a2d6faSchristos static enum cmd_retval	cmd_show_messages_exec(struct cmd *,
35e9a2d6faSchristos 			    struct cmdq_item *);
36698d5317Sjmmv 
37698d5317Sjmmv const struct cmd_entry cmd_show_messages_entry = {
38f26e8bc9Schristos 	.name = "show-messages",
39f26e8bc9Schristos 	.alias = "showmsgs",
40f26e8bc9Schristos 
41*46548964Swiz 	.args = { "JTt:", 0, 0, NULL },
42f26e8bc9Schristos 	.usage = "[-JT] " CMD_TARGET_CLIENT_USAGE,
43f26e8bc9Schristos 
44e271dbb8Schristos 	.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
45f26e8bc9Schristos 	.exec = cmd_show_messages_exec
46698d5317Sjmmv };
47698d5317Sjmmv 
48e9a2d6faSchristos static int
cmd_show_messages_terminals(struct cmd * self,struct cmdq_item * item,int blank)49e271dbb8Schristos cmd_show_messages_terminals(struct cmd *self, struct cmdq_item *item, int blank)
50928fc495Schristos {
51e271dbb8Schristos 	struct args	*args = cmd_get_args(self);
52e271dbb8Schristos 	struct client	*tc = cmdq_get_target_client(item);
53928fc495Schristos 	struct tty_term	*term;
54928fc495Schristos 	u_int		 i, n;
55928fc495Schristos 
56928fc495Schristos 	n = 0;
57928fc495Schristos 	LIST_FOREACH(term, &tty_terms, entry) {
58e271dbb8Schristos 		if (args_has(args, 't') && term != tc->tty.term)
59e271dbb8Schristos 			continue;
6099e242abSchristos 		if (blank) {
61e9a2d6faSchristos 			cmdq_print(item, "%s", "");
6299e242abSchristos 			blank = 0;
6399e242abSchristos 		}
64e271dbb8Schristos 		cmdq_print(item, "Terminal %u: %s for %s, flags=0x%x:", n,
65e271dbb8Schristos 		    term->name, term->tty->client->name, term->flags);
66928fc495Schristos 		n++;
6799e242abSchristos 		for (i = 0; i < tty_term_ncodes(); i++)
68e9a2d6faSchristos 			cmdq_print(item, "%s", tty_term_describe(term, i));
69928fc495Schristos 	}
7099e242abSchristos 	return (n != 0);
71928fc495Schristos }
72928fc495Schristos 
73e9a2d6faSchristos static enum cmd_retval
cmd_show_messages_exec(struct cmd * self,struct cmdq_item * item)74e9a2d6faSchristos cmd_show_messages_exec(struct cmd *self, struct cmdq_item *item)
75698d5317Sjmmv {
76e271dbb8Schristos 	struct args		*args = cmd_get_args(self);
77698d5317Sjmmv 	struct message_entry	*msg;
78e271dbb8Schristos 	char			*s;
7999e242abSchristos 	int			 done, blank;
80e271dbb8Schristos 	struct format_tree	*ft;
81fe99a117Schristos 
8299e242abSchristos 	done = blank = 0;
83e9a2d6faSchristos 	if (args_has(args, 'T')) {
84e271dbb8Schristos 		blank = cmd_show_messages_terminals(self, item, blank);
85928fc495Schristos 		done = 1;
86928fc495Schristos 	}
87e9a2d6faSchristos 	if (args_has(args, 'J')) {
880a274e86Schristos 		job_print_summary(item, blank);
89928fc495Schristos 		done = 1;
90928fc495Schristos 	}
91928fc495Schristos 	if (done)
92928fc495Schristos 		return (CMD_RETURN_NORMAL);
93928fc495Schristos 
94e271dbb8Schristos 	ft = format_create_from_target(item);
95e271dbb8Schristos 	TAILQ_FOREACH_REVERSE(msg, &message_log, message_list, entry) {
96e271dbb8Schristos 		format_add(ft, "message_text", "%s", msg->msg);
97e271dbb8Schristos 		format_add(ft, "message_number", "%u", msg->msg_num);
98e271dbb8Schristos 		format_add_tv(ft, "message_time", &msg->msg_time);
99698d5317Sjmmv 
100e271dbb8Schristos 		s = format_expand(ft, SHOW_MESSAGES_TEMPLATE);
101e271dbb8Schristos 		cmdq_print(item, "%s", s);
102e271dbb8Schristos 		free(s);
103698d5317Sjmmv 	}
104e271dbb8Schristos 	format_free(ft);
105698d5317Sjmmv 
106928fc495Schristos 	return (CMD_RETURN_NORMAL);
107698d5317Sjmmv }
108