xref: /netbsd-src/external/bsd/tmux/dist/cmd-show-messages.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
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 <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 	.name = "show-messages",
35 	.alias = "showmsgs",
36 
37 	.args = { "JTt:", 0, 0 },
38 	.usage = "[-JT] " CMD_TARGET_CLIENT_USAGE,
39 
40 	.tflag = CMD_CLIENT,
41 
42 	.flags = 0,
43 	.exec = cmd_show_messages_exec
44 };
45 
46 const struct cmd_entry cmd_server_info_entry = {
47 	.name = "server-info",
48 	.alias = "info",
49 
50 	.args = { "", 0, 0 },
51 	.usage = "",
52 
53 	.flags = 0,
54 	.exec = cmd_show_messages_exec
55 };
56 
57 int	cmd_show_messages_terminals(struct cmd_q *, int);
58 int	cmd_show_messages_jobs(struct cmd_q *, int);
59 
60 int
61 cmd_show_messages_terminals(struct cmd_q *cmdq, int blank)
62 {
63 	struct tty_term	*term;
64 	u_int		 i, n;
65 
66 	n = 0;
67 	LIST_FOREACH(term, &tty_terms, entry) {
68 		if (blank) {
69 			cmdq_print(cmdq, "%s", "");
70 			blank = 0;
71 		}
72 		cmdq_print(cmdq, "Terminal %u: %s [references=%u, flags=0x%x]:",
73 		    n, term->name, term->references, term->flags);
74 		n++;
75 		for (i = 0; i < tty_term_ncodes(); i++)
76 			cmdq_print(cmdq, "%s", tty_term_describe(term, i));
77 	}
78 	return (n != 0);
79 }
80 
81 int
82 cmd_show_messages_jobs(struct cmd_q *cmdq, int blank)
83 {
84 	struct job	*job;
85 	u_int		 n;
86 
87 	n = 0;
88 	LIST_FOREACH(job, &all_jobs, lentry) {
89 		if (blank) {
90 			cmdq_print(cmdq, "%s", "");
91 			blank = 0;
92 		}
93 		cmdq_print(cmdq, "Job %u: %s [fd=%d, pid=%d, status=%d]",
94 		    n, job->cmd, job->fd, job->pid, job->status);
95 		n++;
96 	}
97 	return (n != 0);
98 }
99 
100 enum cmd_retval
101 cmd_show_messages_exec(struct cmd *self, struct cmd_q *cmdq)
102 {
103 	struct args		*args = self->args;
104 	struct client		*c = cmdq->state.c;
105 	struct message_entry	*msg;
106 	char			*tim;
107 	int			 done, blank;
108 
109 	done = blank = 0;
110 	if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) {
111 		blank = cmd_show_messages_terminals(cmdq, blank);
112 		done = 1;
113 	}
114 	if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) {
115 		cmd_show_messages_jobs(cmdq, blank);
116 		done = 1;
117 	}
118 	if (done)
119 		return (CMD_RETURN_NORMAL);
120 
121 	TAILQ_FOREACH(msg, &c->message_log, entry) {
122 		tim = ctime(&msg->msg_time);
123 		*strchr(tim, '\n') = '\0';
124 
125 		cmdq_print(cmdq, "%s %s", tim, msg->msg);
126 	}
127 
128 	return (CMD_RETURN_NORMAL);
129 }
130