summaryrefslogtreecommitdiffstats
path: root/server-fn.c
blob: f46a471593e96a62265badcb2036ccd219d3f1f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* $Id: server-fn.c,v 1.13 2007-10-03 12:34:16 nicm Exp $ */

/*
 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <string.h>
#include <unistd.h>

#include "tmux.h"

/* Find session from sessid. */
struct session *
server_find_sessid(struct sessid *sid, char **cause)
{
	struct session *s;
	u_int		i, n;

	if (*sid->name != '\0') {
		sid->name[(sizeof sid->name) - 1] = '\0';
		if ((s = session_find(sid->name)) == NULL) {
			xasprintf(cause, "session not found: %s", sid->name);
			return (NULL);
		}
		return (s);
	}

	if (sid->pid != -1) {
		if (sid->pid != getpid()) {
			xasprintf(cause, "wrong server: %lld", sid->pid);
			return (NULL);
		}
		if (sid->idx > ARRAY_LENGTH(&sessions)) {
			xasprintf(cause, "index out of range: %u", sid->idx);
			return (NULL);
		}
		if ((s = ARRAY_ITEM(&sessions, sid->idx)) == NULL) {
			xasprintf(cause, "session doesn't exist: %u", sid->idx);
			return (NULL);
		}
		return (s);
	}

	s = NULL;
	n = 0;
	for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
		if (ARRAY_ITEM(&sessions, i) != NULL) {
			s = ARRAY_ITEM(&sessions, i);
			n++;
		}
	}
	if (s == NULL) {
		xasprintf(cause, "no sessions");
		return (NULL);
	}
	if (n != 1) {
		xasprintf(cause, "multiple sessions and session not specified");
		return (NULL);
	}
	return (s);		
}

/* Write command to a client. */
void
server_write_client(
    struct client *c, enum hdrtype type, const void *buf, size_t len)
{
	struct hdr	 hdr;

	log_debug("writing %d to client %d", type, c->fd);

	hdr.type = type;
	hdr.size = len;

	buffer_write(c->out, &hdr, sizeof hdr);
	if (buf != NULL)
		buffer_write(c->out, buf, len);
}

/* Write command to a client with two buffers. */
void
server_write_client2(struct client *c, enum hdrtype type,
    const void *buf1, size_t len1, const void *buf2, size_t len2)
{
	struct hdr	 hdr;

	log_debug("writing %d to client %d", type, c->fd);

	hdr.type = type;
	hdr.size = len1 + len2;

	buffer_write(c->out, &hdr, sizeof hdr);
	if (buf1 != NULL)
		buffer_write(c->out, buf1, len1);
	if (buf2 != NULL)
		buffer_write(c->out, buf2, len2);
}

/* Write command to all clients attached to a specific window. */
void
server_write_clients(
    struct window *w, enum hdrtype type, const void *buf, size_t len)
{
	struct client	*c;
 	struct hdr	 hdr;
	u_int		 i;

	hdr.type = type;
	hdr.size = len;

	for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
		c = ARRAY_ITEM(&clients, i);
		if (c != NULL && c->session != NULL) {
			if (c->session->window == w) {
				log_debug(
				    "writing %d to clients: %d", type, c->fd);
				buffer_write(c->out, &hdr, sizeof hdr);
				if (buf != NULL)
					buffer_write(c->out, buf, len);
			}
		}
	}
}

/* Changed client window. */
void
server_window_changed(struct client *c)
{
	struct window	*w;

	w = c->session->window;
	if (c->sx != w->screen.sx || c->sy != w->screen.sy)
		window_resize(w, c->sx, c->sy);
	server_draw_client(c, 0, c->sy - 1);
}

/* Draw window on client. */
void
server_draw_client(struct client *c, u_int py_upper, u_int py_lower)
{
	struct hdr	hdr;
	size_t		size;

	buffer_ensure(c->out, sizeof hdr);
	buffer_add(c->out, sizeof hdr);
	size = BUFFER_USED(c->out);

	screen_draw(&c->session->window->screen, c->out, py_upper, py_lower);

	size = BUFFER_USED(c->out) - size;
	log_debug("redrawing screen, %zu bytes", size);
	if (size != 0) {
		hdr.type = MSG_DATA;
		hdr.size = size;
		memcpy(BUFFER_IN(c->out) - size - sizeof hdr, &hdr, sizeof hdr);
	} else
		buffer_reverse_add(c->out, sizeof hdr);

	server_draw_status(c);
}

/* Draw status line. */
void
server_draw_status(struct client *c)
{
	struct hdr	hdr;
	size_t		size;

	if (status_lines == 0)
		return;

	buffer_ensure(c->out, sizeof hdr);
	buffer_add(c->out, sizeof hdr);
	size = BUFFER_USED(c->out);

	status_write(c);

	size = BUFFER_USED(c->out) - size;
	hdr.type = MSG_DATA;
	hdr.size = size;
	memcpy(BUFFER_IN(c->out) - size - sizeof hdr, &hdr, sizeof hdr);
}

/* Send error message command to client. */
void
server_write_error(struct client *c, const char *fmt, ...)
{
	va_list	ap;
	char   *msg;

	va_start(ap, fmt);
	xvasprintf(&msg, fmt, ap);
	va_end(ap);

	server_write_client(c, MSG_ERROR, msg, strlen(msg));
	xfree(msg);
}

/* Write message command to a client. */
void
server_write_message(struct client *c, const char *fmt, ...)
{
	struct hdr	 hdr;
	va_list		 ap;
	char		*msg;
	size_t		 size;
	u_int		 i;

	buffer_ensure(c->out, sizeof hdr);
	buffer_add(c->out, sizeof hdr);
	size = BUFFER_USED(c->out);

	input_store_zero(c->out, CODE_CURSOROFF);
	input_store_two(c->out, CODE_CURSORMOVE, c->sy + status_lines, 1);
	input_store_two(c->out, CODE_ATTRIBUTES, ATTR_REVERSE, 0x88);
	va_start(ap, fmt);
	xvasprintf(&msg, fmt, ap);
	va_end(ap);
	if (strlen(msg) > c->sx - 1)
		msg[c->sx - 1] = '\0';
	buffer_write(c->out, msg, strlen(msg));
	for (i = strlen(msg); i < c->sx; i++)
		input_store8(c->out, ' ');
	xfree(msg);

	size = BUFFER_USED(c->out) - size;
	hdr.type = MSG_DATA;
	hdr.size = size;
	memcpy(BUFFER_IN(c->out) - size - sizeof hdr, &hdr, sizeof hdr);

	hdr.type = MSG_PAUSE;
	hdr.size = 0;
	buffer_write(c->out, &hdr, sizeof hdr);

	buffer_ensure(c->out, sizeof hdr);
	buffer_add(c->out, sizeof hdr);
	size = BUFFER_USED(c->out);

	if (status_lines == 0) {