xref: /openbsd-src/usr.bin/tmux/cmd-show-messages.c (revision 6a13ef69787db04ae501a22e92fa10865b44fd7c)
1 /* $OpenBSD: cmd-show-messages.c,v 1.23 2016/10/16 19:04:05 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 <string.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <vis.h>
25 
26 #include "tmux.h"
27 
28 /*
29  * Show client message log.
30  */
31 
32 static enum cmd_retval	cmd_show_messages_exec(struct cmd *,
33 			    struct cmdq_item *);
34 
35 const struct cmd_entry cmd_show_messages_entry = {
36 	.name = "show-messages",
37 	.alias = "showmsgs",
38 
39 	.args = { "JTt:", 0, 0 },
40 	.usage = "[-JT] " CMD_TARGET_CLIENT_USAGE,
41 
42 	.tflag = CMD_CLIENT,
43 
44 	.flags = CMD_AFTERHOOK,
45 	.exec = cmd_show_messages_exec
46 };
47 
48 const struct cmd_entry cmd_server_info_entry = {
49 	.name = "server-info",
50 	.alias = "info",
51 
52 	.args = { "", 0, 0 },
53 	.usage = "",
54 
55 	.flags = CMD_AFTERHOOK,
56 	.exec = cmd_show_messages_exec
57 };
58 
59 static int	cmd_show_messages_terminals(struct cmdq_item *, int);
60 static int	cmd_show_messages_jobs(struct cmdq_item *, int);
61 
62 static int
63 cmd_show_messages_terminals(struct cmdq_item *item, int blank)
64 {
65 	struct tty_term	*term;
66 	u_int		 i, n;
67 
68 	n = 0;
69 	LIST_FOREACH(term, &tty_terms, entry) {
70 		if (blank) {
71 			cmdq_print(item, "%s", "");
72 			blank = 0;
73 		}
74 		cmdq_print(item, "Terminal %u: %s [references=%u, flags=0x%x]:",
75 		    n, term->name, term->references, term->flags);
76 		n++;
77 		for (i = 0; i < tty_term_ncodes(); i++)
78 			cmdq_print(item, "%s", tty_term_describe(term, i));
79 	}
80 	return (n != 0);
81 }
82 
83 static int
84 cmd_show_messages_jobs(struct cmdq_item *item, int blank)
85 {
86 	struct job	*job;
87 	u_int		 n;
88 
89 	n = 0;
90 	LIST_FOREACH(job, &all_jobs, lentry) {
91 		if (blank) {
92 			cmdq_print(item, "%s", "");
93 			blank = 0;
94 		}
95 		cmdq_print(item, "Job %u: %s [fd=%d, pid=%d, status=%d]",
96 		    n, job->cmd, job->fd, job->pid, job->status);
97 		n++;
98 	}
99 	return (n != 0);
100 }
101 
102 static enum cmd_retval
103 cmd_show_messages_exec(struct cmd *self, struct cmdq_item *item)
104 {
105 	struct args		*args = self->args;
106 	struct client		*c = item->state.c;
107 	struct message_entry	*msg;
108 	char			*tim;
109 	int			 done, blank;
110 
111 	done = blank = 0;
112 	if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) {
113 		blank = cmd_show_messages_terminals(item, blank);
114 		done = 1;
115 	}
116 	if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) {
117 		cmd_show_messages_jobs(item, blank);
118 		done = 1;
119 	}
120 	if (done)
121 		return (CMD_RETURN_NORMAL);
122 
123 	TAILQ_FOREACH(msg, &c->message_log, entry) {
124 		tim = ctime(&msg->msg_time);
125 		*strchr(tim, '\n') = '\0';
126 
127 		cmdq_print(item, "%s %s", tim, msg->msg);
128 	}
129 
130 	return (CMD_RETURN_NORMAL);
131 }
132