xref: /openbsd-src/usr.bin/tmux/cmd-show-messages.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /* $OpenBSD: cmd-show-messages.c,v 1.13 2015/07/28 15:18:10 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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 enum cmd_retval	 cmd_show_messages_exec(struct cmd *, struct cmd_q *);
33 
34 const struct cmd_entry cmd_show_messages_entry = {
35 	"show-messages", "showmsgs",
36 	"IJTt:", 0, 0,
37 	"[-IJT] " CMD_TARGET_CLIENT_USAGE,
38 	0,
39 	cmd_show_messages_exec
40 };
41 
42 const struct cmd_entry cmd_server_info_entry = {
43 	"server-info", "info",
44 	"", 0, 0,
45 	"",
46 	0,
47 	cmd_show_messages_exec
48 };
49 
50 int	cmd_show_messages_server(struct cmd_q *);
51 int	cmd_show_messages_terminals(struct cmd_q *, int);
52 int	cmd_show_messages_jobs(struct cmd_q *, int);
53 
54 int
55 cmd_show_messages_server(struct cmd_q *cmdq)
56 {
57 	char	*tim;
58 
59 	tim = ctime(&start_time);
60 	*strchr(tim, '\n') = '\0';
61 
62 	cmdq_print(cmdq, "started %s", tim);
63 	cmdq_print(cmdq, "socket path %s", socket_path);
64 	cmdq_print(cmdq, "debug level %d", debug_level);
65 	cmdq_print(cmdq, "protocol version %d", PROTOCOL_VERSION);
66 
67 	return (1);
68 }
69 
70 int
71 cmd_show_messages_terminals(struct cmd_q *cmdq, int blank)
72 {
73 	struct tty_term	*term;
74 	u_int		 i, n;
75 
76 	n = 0;
77 	LIST_FOREACH(term, &tty_terms, entry) {
78 		if (blank) {
79 			cmdq_print(cmdq, "%s", "");
80 			blank = 0;
81 		}
82 		cmdq_print(cmdq, "Terminal %u: %s [references=%u, flags=0x%x]:",
83 		    n, term->name, term->references, term->flags);
84 		n++;
85 		for (i = 0; i < tty_term_ncodes(); i++)
86 			cmdq_print(cmdq, "%s", tty_term_describe(term, i));
87 	}
88 	return (n != 0);
89 }
90 
91 int
92 cmd_show_messages_jobs(struct cmd_q *cmdq, int blank)
93 {
94 	struct job	*job;
95 	u_int		 n;
96 
97 	n = 0;
98 	LIST_FOREACH(job, &all_jobs, lentry) {
99 		if (blank) {
100 			cmdq_print(cmdq, "%s", "");
101 			blank = 0;
102 		}
103 		cmdq_print(cmdq, "Job %u: %s [fd=%d, pid=%d, status=%d]",
104 		    n, job->cmd, job->fd, job->pid, job->status);
105 		n++;
106 	}
107 	return (n != 0);
108 }
109 
110 enum cmd_retval
111 cmd_show_messages_exec(struct cmd *self, struct cmd_q *cmdq)
112 {
113 	struct args		*args = self->args;
114 	struct client		*c;
115 	struct message_entry	*msg;
116 	char			*tim;
117 	int			 done, blank;
118 
119 	done = blank = 0;
120 	if (args_has(args, 'I') || self->entry == &cmd_server_info_entry) {
121 		blank = cmd_show_messages_server(cmdq);
122 		done = 1;
123 	}
124 	if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) {
125 		blank = cmd_show_messages_terminals(cmdq, blank);
126 		done = 1;
127 	}
128 	if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) {
129 		cmd_show_messages_jobs(cmdq, blank);
130 		done = 1;
131 	}
132 	if (done)
133 		return (CMD_RETURN_NORMAL);
134 
135 	if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
136 		return (CMD_RETURN_ERROR);
137 
138 	TAILQ_FOREACH(msg, &c->message_log, entry) {
139 		tim = ctime(&msg->msg_time);
140 		*strchr(tim, '\n') = '\0';
141 
142 		cmdq_print(cmdq, "%s %s", tim, msg->msg);
143 	}
144 
145 	return (CMD_RETURN_NORMAL);
146 }
147