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