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