xref: /openbsd-src/usr.bin/tmux/cmd-show-messages.c (revision 5ad04d351680822078003e2b066cfc9680d6157d)
1 /* $OpenBSD: cmd-show-messages.c,v 1.9 2014/02/14 13:59:01 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 	NULL,
40 	cmd_show_messages_exec
41 };
42 
43 const struct cmd_entry cmd_server_info_entry = {
44 	"server-info", "info",
45 	"", 0, 0,
46 	"",
47 	0,
48 	NULL,
49 	cmd_show_messages_exec
50 };
51 
52 void	cmd_show_messages_server(struct cmd_q *);
53 void	cmd_show_messages_terminals(struct cmd_q *);
54 void	cmd_show_messages_jobs(struct cmd_q *);
55 
56 void
57 cmd_show_messages_server(struct cmd_q *cmdq)
58 {
59 	char	*tim;
60 
61 	tim = ctime(&start_time);
62 	*strchr(tim, '\n') = '\0';
63 
64 	cmdq_print(cmdq, "started %s", tim);
65 	cmdq_print(cmdq, "socket path %s", socket_path);
66 	cmdq_print(cmdq, "debug level %d", debug_level);
67 	cmdq_print(cmdq, "protocol version %d", PROTOCOL_VERSION);
68 }
69 
70 void
71 cmd_show_messages_terminals(struct cmd_q *cmdq)
72 {
73 	struct tty_term				*term;
74 	const struct tty_term_code_entry	*ent;
75 	struct tty_code				*code;
76 	u_int					 i, n;
77 	char					 out[80];
78 
79 	n = 0;
80 	LIST_FOREACH(term, &tty_terms, entry) {
81 		cmdq_print(cmdq,
82 		    "Terminal %u: %s [references=%u, flags=0x%x]:",
83 		    n, term->name, term->references, term->flags);
84 		n++;
85 		for (i = 0; i < NTTYCODE; i++) {
86 			ent = &tty_term_codes[i];
87 			code = &term->codes[ent->code];
88 			switch (code->type) {
89 			case TTYCODE_NONE:
90 				cmdq_print(cmdq, "%4u: %s: [missing]",
91 				    ent->code, ent->name);
92 				break;
93 			case TTYCODE_STRING:
94 				strnvis(out, code->value.string, sizeof out,
95 				    VIS_OCTAL|VIS_TAB|VIS_NL);
96 				cmdq_print(cmdq, "%4u: %s: (string) %s",
97 				    ent->code, ent->name, out);
98 				break;
99 			case TTYCODE_NUMBER:
100 				cmdq_print(cmdq, "%4u: %s: (number) %d",
101 				    ent->code, ent->name, code->value.number);
102 				break;
103 			case TTYCODE_FLAG:
104 				cmdq_print(cmdq, "%4u: %s: (flag) %s",
105 				    ent->code, ent->name,
106 				    code->value.flag ? "true" : "false");
107 				break;
108 			}
109 		}
110 	}
111 }
112 
113 void
114 cmd_show_messages_jobs(struct cmd_q *cmdq)
115 {
116 	struct job	*job;
117 	u_int		 n;
118 
119 	n = 0;
120 	LIST_FOREACH(job, &all_jobs, lentry) {
121 		cmdq_print(cmdq,
122 		    "Job %u: %s [fd=%d, pid=%d, status=%d]",
123 		    n, job->cmd, job->fd, job->pid, job->status);
124 		n++;
125 	}
126 }
127 
128 enum cmd_retval
129 cmd_show_messages_exec(struct cmd *self, struct cmd_q *cmdq)
130 {
131 	struct args		*args = self->args;
132 	struct client		*c;
133 	struct message_entry	*msg;
134 	char			*tim;
135 	u_int			 i;
136 	int			 done;
137 
138 	done = 0;
139 	if (args_has(args, 'I') || self->entry == &cmd_server_info_entry) {
140 		cmd_show_messages_server(cmdq);
141 		done = 1;
142 	}
143 	if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) {
144 		if (done)
145 			cmdq_print(cmdq, "%s", "");
146 		cmd_show_messages_terminals(cmdq);
147 		done = 1;
148 	}
149 	if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) {
150 		if (done)
151 			cmdq_print(cmdq, "%s", "");
152 		cmd_show_messages_jobs(cmdq);
153 		done = 1;
154 	}
155 	if (done)
156 		return (CMD_RETURN_NORMAL);
157 
158 	if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
159 		return (CMD_RETURN_ERROR);
160 
161 	for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
162 		msg = &ARRAY_ITEM(&c->message_log, i);
163 
164 		tim = ctime(&msg->msg_time);
165 		*strchr(tim, '\n') = '\0';
166 
167 		cmdq_print(cmdq, "%s %s", tim, msg->msg);
168 	}
169 
170 	return (CMD_RETURN_NORMAL);
171 }
172