xref: /minix3/external/bsd/tmux/dist/cmd-show-messages.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* Id */
2eda6f593SDavid van Moolenbroek 
3eda6f593SDavid van Moolenbroek /*
4eda6f593SDavid van Moolenbroek  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5eda6f593SDavid van Moolenbroek  *
6eda6f593SDavid van Moolenbroek  * Permission to use, copy, modify, and distribute this software for any
7eda6f593SDavid van Moolenbroek  * purpose with or without fee is hereby granted, provided that the above
8eda6f593SDavid van Moolenbroek  * copyright notice and this permission notice appear in all copies.
9eda6f593SDavid van Moolenbroek  *
10eda6f593SDavid van Moolenbroek  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11eda6f593SDavid van Moolenbroek  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12eda6f593SDavid van Moolenbroek  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13eda6f593SDavid van Moolenbroek  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14eda6f593SDavid van Moolenbroek  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15eda6f593SDavid van Moolenbroek  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16eda6f593SDavid van Moolenbroek  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17eda6f593SDavid van Moolenbroek  */
18eda6f593SDavid van Moolenbroek 
19eda6f593SDavid van Moolenbroek #include <sys/types.h>
20eda6f593SDavid van Moolenbroek 
21eda6f593SDavid van Moolenbroek #include <string.h>
22eda6f593SDavid van Moolenbroek #include <time.h>
23*0a6a1f1dSLionel Sambuc #include <unistd.h>
24eda6f593SDavid van Moolenbroek 
25eda6f593SDavid van Moolenbroek #include "tmux.h"
26eda6f593SDavid van Moolenbroek 
27eda6f593SDavid van Moolenbroek /*
28eda6f593SDavid van Moolenbroek  * Show client message log.
29eda6f593SDavid van Moolenbroek  */
30eda6f593SDavid van Moolenbroek 
31*0a6a1f1dSLionel Sambuc enum cmd_retval	 cmd_show_messages_exec(struct cmd *, struct cmd_q *);
32eda6f593SDavid van Moolenbroek 
33eda6f593SDavid van Moolenbroek const struct cmd_entry cmd_show_messages_entry = {
34eda6f593SDavid van Moolenbroek 	"show-messages", "showmsgs",
35*0a6a1f1dSLionel Sambuc 	"IJTt:", 0, 0,
36*0a6a1f1dSLionel Sambuc 	"[-IJT] " CMD_TARGET_CLIENT_USAGE,
37eda6f593SDavid van Moolenbroek 	0,
38eda6f593SDavid van Moolenbroek 	NULL,
39eda6f593SDavid van Moolenbroek 	cmd_show_messages_exec
40eda6f593SDavid van Moolenbroek };
41eda6f593SDavid van Moolenbroek 
42*0a6a1f1dSLionel Sambuc const struct cmd_entry cmd_server_info_entry = {
43*0a6a1f1dSLionel Sambuc 	"server-info", "info",
44*0a6a1f1dSLionel Sambuc 	"", 0, 0,
45*0a6a1f1dSLionel Sambuc 	"",
46*0a6a1f1dSLionel Sambuc 	0,
47*0a6a1f1dSLionel Sambuc 	NULL,
48*0a6a1f1dSLionel Sambuc 	cmd_show_messages_exec
49*0a6a1f1dSLionel Sambuc };
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc void	cmd_show_messages_server(struct cmd_q *);
52*0a6a1f1dSLionel Sambuc void	cmd_show_messages_terminals(struct cmd_q *);
53*0a6a1f1dSLionel Sambuc void	cmd_show_messages_jobs(struct cmd_q *);
54*0a6a1f1dSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc void
cmd_show_messages_server(struct cmd_q * cmdq)56*0a6a1f1dSLionel Sambuc cmd_show_messages_server(struct cmd_q *cmdq)
57*0a6a1f1dSLionel Sambuc {
58*0a6a1f1dSLionel Sambuc 	char	*tim;
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	tim = ctime(&start_time);
61*0a6a1f1dSLionel Sambuc 	*strchr(tim, '\n') = '\0';
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc 	cmdq_print(cmdq, "started %s", tim);
64*0a6a1f1dSLionel Sambuc 	cmdq_print(cmdq, "socket path %s", socket_path);
65*0a6a1f1dSLionel Sambuc 	cmdq_print(cmdq, "debug level %d", debug_level);
66*0a6a1f1dSLionel Sambuc 	cmdq_print(cmdq, "protocol version %d", PROTOCOL_VERSION);
67*0a6a1f1dSLionel Sambuc }
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc void
cmd_show_messages_terminals(struct cmd_q * cmdq)70*0a6a1f1dSLionel Sambuc cmd_show_messages_terminals(struct cmd_q *cmdq)
71*0a6a1f1dSLionel Sambuc {
72*0a6a1f1dSLionel Sambuc 	struct tty_term				*term;
73*0a6a1f1dSLionel Sambuc 	const struct tty_term_code_entry	*ent;
74*0a6a1f1dSLionel Sambuc 	struct tty_code				*code;
75*0a6a1f1dSLionel Sambuc 	u_int					 i, n;
76*0a6a1f1dSLionel Sambuc 	char					 out[80];
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc 	n = 0;
79*0a6a1f1dSLionel Sambuc 	LIST_FOREACH(term, &tty_terms, entry) {
80*0a6a1f1dSLionel Sambuc 		cmdq_print(cmdq,
81*0a6a1f1dSLionel Sambuc 		    "Terminal %u: %s [references=%u, flags=0x%x]:",
82*0a6a1f1dSLionel Sambuc 		    n, term->name, term->references, term->flags);
83*0a6a1f1dSLionel Sambuc 		n++;
84*0a6a1f1dSLionel Sambuc 		for (i = 0; i < NTTYCODE; i++) {
85*0a6a1f1dSLionel Sambuc 			ent = &tty_term_codes[i];
86*0a6a1f1dSLionel Sambuc 			code = &term->codes[ent->code];
87*0a6a1f1dSLionel Sambuc 			switch (code->type) {
88*0a6a1f1dSLionel Sambuc 			case TTYCODE_NONE:
89*0a6a1f1dSLionel Sambuc 				cmdq_print(cmdq, "%4u: %s: [missing]",
90*0a6a1f1dSLionel Sambuc 				    ent->code, ent->name);
91*0a6a1f1dSLionel Sambuc 				break;
92*0a6a1f1dSLionel Sambuc 			case TTYCODE_STRING:
93*0a6a1f1dSLionel Sambuc 				strnvis(out, sizeof out, code->value.string,
94*0a6a1f1dSLionel Sambuc 				    VIS_OCTAL|VIS_TAB|VIS_NL);
95*0a6a1f1dSLionel Sambuc 				cmdq_print(cmdq, "%4u: %s: (string) %s",
96*0a6a1f1dSLionel Sambuc 				    ent->code, ent->name, out);
97*0a6a1f1dSLionel Sambuc 				break;
98*0a6a1f1dSLionel Sambuc 			case TTYCODE_NUMBER:
99*0a6a1f1dSLionel Sambuc 				cmdq_print(cmdq, "%4u: %s: (number) %d",
100*0a6a1f1dSLionel Sambuc 				    ent->code, ent->name, code->value.number);
101*0a6a1f1dSLionel Sambuc 				break;
102*0a6a1f1dSLionel Sambuc 			case TTYCODE_FLAG:
103*0a6a1f1dSLionel Sambuc 				cmdq_print(cmdq, "%4u: %s: (flag) %s",
104*0a6a1f1dSLionel Sambuc 				    ent->code, ent->name,
105*0a6a1f1dSLionel Sambuc 				    code->value.flag ? "true" : "false");
106*0a6a1f1dSLionel Sambuc 				break;
107*0a6a1f1dSLionel Sambuc 			}
108*0a6a1f1dSLionel Sambuc 		}
109*0a6a1f1dSLionel Sambuc 	}
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc void
cmd_show_messages_jobs(struct cmd_q * cmdq)113*0a6a1f1dSLionel Sambuc cmd_show_messages_jobs(struct cmd_q *cmdq)
114*0a6a1f1dSLionel Sambuc {
115*0a6a1f1dSLionel Sambuc 	struct job	*job;
116*0a6a1f1dSLionel Sambuc 	u_int		 n;
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	n = 0;
119*0a6a1f1dSLionel Sambuc 	LIST_FOREACH(job, &all_jobs, lentry) {
120*0a6a1f1dSLionel Sambuc 		cmdq_print(cmdq,
121*0a6a1f1dSLionel Sambuc 		    "Job %u: %s [fd=%d, pid=%d, status=%d]",
122*0a6a1f1dSLionel Sambuc 		    n, job->cmd, job->fd, job->pid, job->status);
123*0a6a1f1dSLionel Sambuc 		n++;
124*0a6a1f1dSLionel Sambuc 	}
125*0a6a1f1dSLionel Sambuc }
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc enum cmd_retval
cmd_show_messages_exec(struct cmd * self,struct cmd_q * cmdq)128*0a6a1f1dSLionel Sambuc cmd_show_messages_exec(struct cmd *self, struct cmd_q *cmdq)
129eda6f593SDavid van Moolenbroek {
130eda6f593SDavid van Moolenbroek 	struct args		*args = self->args;
131eda6f593SDavid van Moolenbroek 	struct client		*c;
132eda6f593SDavid van Moolenbroek 	struct message_entry	*msg;
133eda6f593SDavid van Moolenbroek 	char			*tim;
134eda6f593SDavid van Moolenbroek 	u_int			 i;
135*0a6a1f1dSLionel Sambuc 	int			 done;
136eda6f593SDavid van Moolenbroek 
137*0a6a1f1dSLionel Sambuc 	done = 0;
138*0a6a1f1dSLionel Sambuc 	if (args_has(args, 'I') || self->entry == &cmd_server_info_entry) {
139*0a6a1f1dSLionel Sambuc 		cmd_show_messages_server(cmdq);
140*0a6a1f1dSLionel Sambuc 		done = 1;
141*0a6a1f1dSLionel Sambuc 	}
142*0a6a1f1dSLionel Sambuc 	if (args_has(args, 'T') || self->entry == &cmd_server_info_entry) {
143*0a6a1f1dSLionel Sambuc 		if (done)
144*0a6a1f1dSLionel Sambuc 			cmdq_print(cmdq, "%s", "");
145*0a6a1f1dSLionel Sambuc 		cmd_show_messages_terminals(cmdq);
146*0a6a1f1dSLionel Sambuc 		done = 1;
147*0a6a1f1dSLionel Sambuc 	}
148*0a6a1f1dSLionel Sambuc 	if (args_has(args, 'J') || self->entry == &cmd_server_info_entry) {
149*0a6a1f1dSLionel Sambuc 		if (done)
150*0a6a1f1dSLionel Sambuc 			cmdq_print(cmdq, "%s", "");
151*0a6a1f1dSLionel Sambuc 		cmd_show_messages_jobs(cmdq);
152*0a6a1f1dSLionel Sambuc 		done = 1;
153*0a6a1f1dSLionel Sambuc 	}
154*0a6a1f1dSLionel Sambuc 	if (done)
155*0a6a1f1dSLionel Sambuc 		return (CMD_RETURN_NORMAL);
156*0a6a1f1dSLionel Sambuc 
157*0a6a1f1dSLionel Sambuc 	if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
158*0a6a1f1dSLionel Sambuc 		return (CMD_RETURN_ERROR);
159eda6f593SDavid van Moolenbroek 
160eda6f593SDavid van Moolenbroek 	for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
161eda6f593SDavid van Moolenbroek 		msg = &ARRAY_ITEM(&c->message_log, i);
162eda6f593SDavid van Moolenbroek 
163eda6f593SDavid van Moolenbroek 		tim = ctime(&msg->msg_time);
164eda6f593SDavid van Moolenbroek 		*strchr(tim, '\n') = '\0';
165eda6f593SDavid van Moolenbroek 
166*0a6a1f1dSLionel Sambuc 		cmdq_print(cmdq, "%s %s", tim, msg->msg);
167eda6f593SDavid van Moolenbroek 	}
168eda6f593SDavid van Moolenbroek 
169*0a6a1f1dSLionel Sambuc 	return (CMD_RETURN_NORMAL);
170eda6f593SDavid van Moolenbroek }
171