Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1007 lines (852 sloc) 25.662 kB
b016948 @orebokech Import upstream snapshot from SVN r2608
orebokech authored
1 /* $Id: server-client.c 2583 2011-08-21 12:39:34Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
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
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
21 #include <event.h>
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
22 #include <fcntl.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include "tmux.h"
28
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
29 void server_client_handle_key(int, struct mouse_event *, void *);
30 void server_client_repeat_timer(int, short, void *);
77c63de @orebokech Import upstream version 1.4
orebokech authored
31 void server_client_check_exit(struct client *);
32 void server_client_check_backoff(struct client *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
33 void server_client_check_redraw(struct client *);
34 void server_client_set_title(struct client *);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
35 void server_client_reset_state(struct client *);
77c63de @orebokech Import upstream version 1.4
orebokech authored
36 void server_client_in_callback(struct bufferevent *, short, void *);
37 void server_client_out_callback(struct bufferevent *, short, void *);
38 void server_client_err_callback(struct bufferevent *, short, void *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
39
40 int server_client_msg_dispatch(struct client *);
41 void server_client_msg_command(struct client *, struct msg_command_data *);
42 void server_client_msg_identify(
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
43 struct client *, struct msg_identify_data *, int);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
44 void server_client_msg_shell(struct client *);
45
46 void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
47 void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
48 void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
49
50 /* Create a new client. */
51 void
52 server_client_create(int fd)
53 {
54 struct client *c;
55 u_int i;
56
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
57 setblocking(fd, 0);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
58
59 c = xcalloc(1, sizeof *c);
60 c->references = 0;
61 imsg_init(&c->ibuf, fd);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
62 server_update_event(c);
63
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
64 if (gettimeofday(&c->creation_time, NULL) != 0)
65 fatal("gettimeofday failed");
66 memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
67
77c63de @orebokech Import upstream version 1.4
orebokech authored
68 c->stdin_event = NULL;
69 c->stdout_event = NULL;
70 c->stderr_event = NULL;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
71
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
72 c->tty.fd = -1;
73 c->title = NULL;
74
75 c->session = NULL;
77c63de @orebokech Import upstream version 1.4
orebokech authored
76 c->last_session = NULL;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
77 c->tty.sx = 80;
78 c->tty.sy = 24;
79
80 screen_init(&c->status, c->tty.sx, 1, 0);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
81 RB_INIT(&c->status_new);
82 RB_INIT(&c->status_old);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
83
84 c->message_string = NULL;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
85 ARRAY_INIT(&c->message_log);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
86
87 c->prompt_string = NULL;
88 c->prompt_buffer = NULL;
89 c->prompt_index = 0;
90
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
91 c->last_mouse.b = MOUSE_UP;
92 c->last_mouse.x = c->last_mouse.y = -1;
93
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
94 evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
95
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
96 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
97 if (ARRAY_ITEM(&clients, i) == NULL) {
98 ARRAY_SET(&clients, i, c);
99 return;
100 }
101 }
102 ARRAY_ADD(&clients, c);
103 log_debug("new client %d", fd);
104 }
105
106 /* Lost a client. */
107 void
108 server_client_lost(struct client *c)
109 {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
110 struct message_entry *msg;
111 u_int i;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
112
113 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
114 if (ARRAY_ITEM(&clients, i) == c)
115 ARRAY_SET(&clients, i, NULL);
116 }
117 log_debug("lost client %d", c->ibuf.fd);
118
119 /*
120 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
121 * and tty_free might close an unrelated fd.
122 */
123 if (c->flags & CLIENT_TERMINAL)
124 tty_free(&c->tty);
125
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
126 if (c->stdin_fd != -1) {
127 setblocking(c->stdin_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
128 close(c->stdin_fd);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
129 }
77c63de @orebokech Import upstream version 1.4
orebokech authored
130 if (c->stdin_event != NULL)
131 bufferevent_free(c->stdin_event);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
132 if (c->stdout_fd != -1) {
133 setblocking(c->stdout_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
134 close(c->stdout_fd);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
135 }
77c63de @orebokech Import upstream version 1.4
orebokech authored
136 if (c->stdout_event != NULL)
137 bufferevent_free(c->stdout_event);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
138 if (c->stderr_fd != -1) {
139 setblocking(c->stderr_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
140 close(c->stderr_fd);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
141 }
77c63de @orebokech Import upstream version 1.4
orebokech authored
142 if (c->stderr_event != NULL)
143 bufferevent_free(c->stderr_event);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
144
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
145 status_free_jobs(&c->status_new);
146 status_free_jobs(&c->status_old);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
147 screen_free(&c->status);
148
149 if (c->title != NULL)
150 xfree(c->title);
151
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
152 evtimer_del(&c->repeat_timer);
153
154 evtimer_del(&c->identify_timer);
155
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
156 if (c->message_string != NULL)
157 xfree(c->message_string);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
158 evtimer_del(&c->message_timer);
159 for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
160 msg = &ARRAY_ITEM(&c->message_log, i);
161 xfree(msg->msg);
162 }
163 ARRAY_FREE(&c->message_log);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
164
165 if (c->prompt_string != NULL)
166 xfree(c->prompt_string);
167 if (c->prompt_buffer != NULL)
168 xfree(c->prompt_buffer);
169
170 if (c->cwd != NULL)
171 xfree(c->cwd);
172
b016948 @orebokech Import upstream snapshot from SVN r2608
orebokech authored
173 environ_free(&c->environ);
174
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
175 close(c->ibuf.fd);
176 imsg_clear(&c->ibuf);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
177 event_del(&c->event);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
178
179 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
180 if (ARRAY_ITEM(&dead_clients, i) == NULL) {
181 ARRAY_SET(&dead_clients, i, c);
182 break;
183 }
184 }
185 if (i == ARRAY_LENGTH(&dead_clients))
186 ARRAY_ADD(&dead_clients, c);
187 c->flags |= CLIENT_DEAD;
188
189 recalculate_sizes();
77c63de @orebokech Import upstream version 1.4
orebokech authored
190 server_check_unattached();
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
191 server_update_socket();
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
192 }
193
194 /* Process a single client event. */
195 void
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
196 server_client_callback(int fd, short events, void *data)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
197 {
198 struct client *c = data;
199
200 if (c->flags & CLIENT_DEAD)
201 return;
202
203 if (fd == c->ibuf.fd) {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
204 if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
205 goto client_lost;
206
207 if (c->flags & CLIENT_BAD) {
208 if (c->ibuf.w.queued == 0)
209 goto client_lost;
210 return;
211 }
212
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
213 if (events & EV_READ && server_client_msg_dispatch(c) != 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
214 goto client_lost;
215 }
216
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
217 server_update_event(c);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
218 return;
219
220 client_lost:
221 server_client_lost(c);
222 }
223
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
224 /* Handle client status timer. */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
225 void
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
226 server_client_status_timer(void)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
227 {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
228 struct client *c;
229 struct session *s;
230 struct timeval tv;
231 u_int i;
232 int interval;
233 time_t difference;
234
235 if (gettimeofday(&tv, NULL) != 0)
236 fatal("gettimeofday failed");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
237
238 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
239 c = ARRAY_ITEM(&clients, i);
240 if (c == NULL || c->session == NULL)
241 continue;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
242 if (c->message_string != NULL || c->prompt_string != NULL) {
243 /*
244 * Don't need timed redraw for messages/prompts so bail
245 * now. The status timer isn't reset when they are
246 * redrawn anyway.
247 */
248 continue;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
249 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
250 s = c->session;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
251
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
252 if (!options_get_number(&s->options, "status"))
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
253 continue;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
254 interval = options_get_number(&s->options, "status-interval");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
255
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
256 difference = tv.tv_sec - c->status_timer.tv_sec;
257 if (difference >= interval) {
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
258 status_update_jobs(c);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
259 c->flags |= CLIENT_STATUS;
260 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
261 }
262 }
263
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
264 /* Handle data key input from client. */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
265 void
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
266 server_client_handle_key(int key, struct mouse_event *mouse, void *data)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
267 {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
268 struct client *c = data;
269 struct session *s;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
270 struct window *w;
271 struct window_pane *wp;
272 struct options *oo;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
273 struct timeval tv;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
274 struct key_binding *bd;
275 struct keylist *keylist;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
276 int xtimeout, isprefix;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
277 u_int i;
278
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
279 /* Check the client is good to accept input. */
280 if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
281 return;
282 if (c->session == NULL)
283 return;
284 s = c->session;
285
286 /* Update the activity timer. */
287 if (gettimeofday(&c->activity_time, NULL) != 0)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
288 fatal("gettimeofday failed");
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
289 memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
290
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
291 w = c->session->curw->window;
292 wp = w->active;
293 oo = &c->session->options;
294
295 /* Special case: number keys jump to pane in identify mode. */
296 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
297 if (c->flags & CLIENT_READONLY)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
298 return;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
299 wp = window_pane_at_index(w, key - '0');
300 if (wp != NULL && window_pane_visible(wp))
301 window_set_active_pane(w, wp);
302 server_clear_identify(c);
303 return;
304 }
305
306 /* Handle status line. */
307 if (!(c->flags & CLIENT_READONLY)) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
308 status_message_clear(c);
309 server_clear_identify(c);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
310 }
311 if (c->prompt_string != NULL) {
312 if (!(c->flags & CLIENT_READONLY))
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
313 status_prompt_key(c, key);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
314 return;
315 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
316
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
317 /* Check for mouse keys. */
318 if (key == KEYC_MOUSE) {
319 if (c->flags & CLIENT_READONLY)
320 return;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
321 if (options_get_number(oo, "mouse-select-pane") &&
322 ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
323 wp->mode != &window_copy_mode)) {
324 /*
325 * Allow pane switching in copy mode only by mouse down
326 * (click).
327 */
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
328 window_set_active_at(w, mouse->x, mouse->y);
329 server_redraw_window_borders(w);
330 wp = w->active;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
331 }
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
332 if (mouse->y + 1 == c->tty.sy &&
333 options_get_number(oo, "mouse-select-window") &&
334 options_get_number(oo, "status")) {
335 if (mouse->b == MOUSE_UP &&
336 c->last_mouse.b != MOUSE_UP) {
337 status_set_window_at(c, mouse->x);
338 return;
339 }
340 if (mouse->b & MOUSE_45) {
341 if ((mouse->b & MOUSE_BUTTON) == MOUSE_1) {
342 session_previous(c->session, 0);
343 server_redraw_session(s);
344 }
345 if ((mouse->b & MOUSE_BUTTON) == MOUSE_2) {
346 session_next(c->session, 0);
347 server_redraw_session(s);
348 }
349 return;
350 }
351 }
352 if (options_get_number(oo, "mouse-resize-pane"))
353 layout_resize_pane_mouse(c, mouse);
354 memcpy(&c->last_mouse, mouse, sizeof c->last_mouse);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
355 window_pane_mouse(wp, c->session, mouse);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
356 return;
357 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
358
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
359 /* Is this a prefix key? */
360 keylist = options_get_data(&c->session->options, "prefix");
361 isprefix = 0;
362 for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
363 if (key == ARRAY_ITEM(keylist, i)) {
364 isprefix = 1;
365 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
366 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
367 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
368
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
369 /* No previous prefix key. */
370 if (!(c->flags & CLIENT_PREFIX)) {
371 if (isprefix)
372 c->flags |= CLIENT_PREFIX;
373 else {
374 /* Try as a non-prefix key binding. */
375 if ((bd = key_bindings_lookup(key)) == NULL) {
376 if (!(c->flags & CLIENT_READONLY))
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
377 window_pane_key(wp, c->session, key);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
378 } else
379 key_bindings_dispatch(bd, c);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
380 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
381 return;
382 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
383
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
384 /* Prefix key already pressed. Reset prefix and lookup key. */
385 c->flags &= ~CLIENT_PREFIX;
386 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
387 /* If repeating, treat this as a key, else ignore. */
388 if (c->flags & CLIENT_REPEAT) {
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
389 c->flags &= ~CLIENT_REPEAT;
390 if (isprefix)
391 c->flags |= CLIENT_PREFIX;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
392 else if (!(c->flags & CLIENT_READONLY))
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
393 window_pane_key(wp, c->session, key);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
394 }
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
395 return;
396 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
397
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
398 /* If already repeating, but this key can't repeat, skip it. */
399 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
400 c->flags &= ~CLIENT_REPEAT;
401 if (isprefix)
402 c->flags |= CLIENT_PREFIX;
403 else if (!(c->flags & CLIENT_READONLY))
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
404 window_pane_key(wp, c->session, key);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
405 return;
406 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
407
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
408 /* If this key can repeat, reset the repeat flags and timer. */
409 xtimeout = options_get_number(&c->session->options, "repeat-time");
410 if (xtimeout != 0 && bd->can_repeat) {
411 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
412
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
413 tv.tv_sec = xtimeout / 1000;
414 tv.tv_usec = (xtimeout % 1000) * 1000L;
415 evtimer_del(&c->repeat_timer);
416 evtimer_add(&c->repeat_timer, &tv);
417 }
418
419 /* Dispatch the command. */
420 key_bindings_dispatch(bd, c);
421 }
422
423 /* Client functions that need to happen every loop. */
424 void
425 server_client_loop(void)
426 {
427 struct client *c;
428 struct window *w;
429 struct window_pane *wp;
430 u_int i;
431
432 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
433 c = ARRAY_ITEM(&clients, i);
77c63de @orebokech Import upstream version 1.4
orebokech authored
434 if (c == NULL)
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
435 continue;
436
77c63de @orebokech Import upstream version 1.4
orebokech authored
437 server_client_check_exit(c);
438 if (c->session != NULL) {
439 server_client_check_redraw(c);
440 server_client_reset_state(c);
441 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
442 }
443
444 /*
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
445 * Any windows will have been redrawn as part of clients, so clear
446 * their flags now.
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
447 */
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
448 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
449 w = ARRAY_ITEM(&windows, i);
450 if (w == NULL)
451 continue;
452
453 w->flags &= ~WINDOW_REDRAW;
454 TAILQ_FOREACH(wp, &w->panes, entry)
455 wp->flags &= ~PANE_REDRAW;
456 }
457 }
458
459 /*
460 * Update cursor position and mode settings. The scroll region and attributes
461 * are cleared when idle (waiting for an event) as this is the most likely time
462 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
463 * compromise between excessive resets and likelihood of an interrupt.
464 *
465 * tty_region/tty_reset/tty_update_mode already take care of not resetting
466 * things that are already in their default state.
467 */
468 void
469 server_client_reset_state(struct client *c)
470 {
471 struct window *w = c->session->curw->window;
472 struct window_pane *wp = w->active;
473 struct screen *s = wp->screen;
474 struct options *oo = &c->session->options;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
475 struct options *wo = &w->options;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
476 int status, mode;
477
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
478 if (c->flags & CLIENT_SUSPENDED)
479 return;
480
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
481 tty_region(&c->tty, 0, c->tty.sy - 1);
482
483 status = options_get_number(oo, "status");
484 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
485 tty_cursor(&c->tty, 0, 0);
486 else
487 tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
488
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
489 /*
490 * Resizing panes with the mouse requires at least button mode to give
491 * a smooth appearance.
492 */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
493 mode = s->mode;
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
494 if ((c->last_mouse.b & MOUSE_RESIZE_PANE) &&
495 !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
496 mode |= MODE_MOUSE_BUTTON;
497
498 /*
499 * Any mode will do for mouse-select-pane, but set standard mode if
500 * none.
501 */
502 if ((mode & ALL_MOUSE_MODES) == 0) {
503 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
504 options_get_number(oo, "mouse-select-pane"))
505 mode |= MODE_MOUSE_STANDARD;
506 else if (options_get_number(oo, "mouse-resize-pane"))
507 mode |= MODE_MOUSE_STANDARD;
508 else if (options_get_number(oo, "mouse-select-window"))
509 mode |= MODE_MOUSE_STANDARD;
510 else if (options_get_number(wo, "mode-mouse"))
511 mode |= MODE_MOUSE_STANDARD;
512 }
513
514 /*
515 * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
516 * user has set mouse-utf8 and any mouse mode is in effect, turn on
517 * UTF-8 mouse input. If the receiving terminal hasn't requested it
518 * (that is, it isn't in s->mode), then it'll be converted in
519 * input_mouse.
520 */
521 if ((c->tty.flags & TTY_UTF8) &&
522 (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
523 mode |= MODE_MOUSE_UTF8;
524 else
525 mode &= ~MODE_MOUSE_UTF8;
526
527 /* Set the terminal mode and reset attributes. */
528 tty_update_mode(&c->tty, mode, s);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
529 tty_reset(&c->tty);
530 }
531
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
532 /* Repeat time callback. */
533 /* ARGSUSED */
534 void
535 server_client_repeat_timer(unused int fd, unused short events, void *data)
536 {
537 struct client *c = data;
538
539 if (c->flags & CLIENT_REPEAT)
540 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
541 }
542
77c63de @orebokech Import upstream version 1.4
orebokech authored
543 /* Check if client should be exited. */
544 void
545 server_client_check_exit(struct client *c)
546 {
547 struct msg_exit_data exitdata;
548
549 if (!(c->flags & CLIENT_EXIT))
550 return;
551
552 if (c->stdout_fd != -1 && c->stdout_event != NULL &&
553 EVBUFFER_LENGTH(c->stdout_event->output) != 0)
554 return;
555 if (c->stderr_fd != -1 && c->stderr_event != NULL &&
556 EVBUFFER_LENGTH(c->stderr_event->output) != 0)
557 return;
558
559 exitdata.retcode = c->retcode;
560 server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
561
562 c->flags &= ~CLIENT_EXIT;
563 }
564
565 /*
566 * Check if the client should backoff. During backoff, data from external
567 * programs is not written to the terminal. When the existing data drains, the
568 * client is redrawn.
569 *
570 * There are two backoff phases - both the tty and client have backoff flags -
571 * the first to allow existing data to drain and the latter to ensure backoff
572 * is disabled until the redraw has finished and prevent the redraw triggering
573 * another backoff.
574 */
575 void
576 server_client_check_backoff(struct client *c)
577 {
578 struct tty *tty = &c->tty;
579 size_t used;
580
581 used = EVBUFFER_LENGTH(tty->event->output);
582
583 /*
584 * If in the second backoff phase (redrawing), don't check backoff
585 * until the redraw has completed (or enough of it to drop below the
586 * backoff threshold).
587 */
588 if (c->flags & CLIENT_BACKOFF) {
589 if (used > BACKOFF_THRESHOLD)
590 return;
591 c->flags &= ~CLIENT_BACKOFF;
592 return;
593 }
594
595 /* Once drained, allow data through again and schedule redraw. */
596 if (tty->flags & TTY_BACKOFF) {
597 if (used != 0)
598 return;
599 tty->flags &= ~TTY_BACKOFF;
600 c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
601 return;
602 }
603
604 /* If too much data, start backoff. */
605 if (used > BACKOFF_THRESHOLD)
606 tty->flags |= TTY_BACKOFF;
607 }
608
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
609 /* Check for client redraws. */
610 void
611 server_client_check_redraw(struct client *c)
612 {
613 struct session *s = c->session;
614 struct window_pane *wp;
615 int flags, redraw;
616
617 flags = c->tty.flags & TTY_FREEZE;
618 c->tty.flags &= ~TTY_FREEZE;
619
620 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
621 if (options_get_number(&s->options, "set-titles"))
622 server_client_set_title(c);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
623
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
624 if (c->message_string != NULL)
625 redraw = status_message_redraw(c);
626 else if (c->prompt_string != NULL)
627 redraw = status_prompt_redraw(c);
628 else
629 redraw = status_redraw(c);
630 if (!redraw)
631 c->flags &= ~CLIENT_STATUS;
632 }
633
634 if (c->flags & CLIENT_REDRAW) {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
635 screen_redraw_screen(c, 0, 0);
636 c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
77c63de @orebokech Import upstream version 1.4
orebokech authored
637 } else if (c->flags & CLIENT_REDRAWWINDOW) {
638 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
639 screen_redraw_pane(c, wp);
640 c->flags &= ~CLIENT_REDRAWWINDOW;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
641 } else {
642 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
643 if (wp->flags & PANE_REDRAW)
644 screen_redraw_pane(c, wp);
645 }
646 }
647
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
648 if (c->flags & CLIENT_BORDERS)
649 screen_redraw_screen(c, 0, 1);
650
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
651 if (c->flags & CLIENT_STATUS)
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
652 screen_redraw_screen(c, 1, 0);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
653
654 c->tty.flags |= flags;
655
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
656 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
657 }
658
659 /* Set client title. */
660 void
661 server_client_set_title(struct client *c)
662 {
663 struct session *s = c->session;
664 const char *template;
665 char *title;
666
667 template = options_get_string(&s->options, "set-titles-string");
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
668
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
669 title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
670 if (c->title == NULL || strcmp(title, c->title) != 0) {
671 if (c->title != NULL)
672 xfree(c->title);
673 c->title = xstrdup(title);
674 tty_set_title(&c->tty, c->title);
675 }
676 xfree(title);
677 }
678
77c63de @orebokech Import upstream version 1.4
orebokech authored
679 /*
680 * Error callback for client stdin. Caller must increase reference count when
681 * enabling event!
682 */
683 void
684 server_client_in_callback(
685 unused struct bufferevent *bufev, unused short what, void *data)
686 {
687 struct client *c = data;
688
689 c->references--;
690 if (c->flags & CLIENT_DEAD)
691 return;
692
693 bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
694 setblocking(c->stdin_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
695 close(c->stdin_fd);
696 c->stdin_fd = -1;
697
698 if (c->stdin_callback != NULL)
699 c->stdin_callback(c, c->stdin_data);
700 }
701
702 /* Error callback for client stdout. */
703 void
704 server_client_out_callback(
705 unused struct bufferevent *bufev, unused short what, unused void *data)
706 {
707 struct client *c = data;
708
709 bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
710 setblocking(c->stdout_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
711 close(c->stdout_fd);
712 c->stdout_fd = -1;
713 }
714
715 /* Error callback for client stderr. */
716 void
717 server_client_err_callback(
718 unused struct bufferevent *bufev, unused short what, unused void *data)
719 {
720 struct client *c = data;
721
722 bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
723 setblocking(c->stderr_fd, 1);
77c63de @orebokech Import upstream version 1.4
orebokech authored
724 close(c->stderr_fd);
725 c->stderr_fd = -1;
726 }
727
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
728 /* Dispatch message from client. */
729 int
730 server_client_msg_dispatch(struct client *c)
731 {
732 struct imsg imsg;
733 struct msg_command_data commanddata;
734 struct msg_identify_data identifydata;
735 struct msg_environ_data environdata;
736 ssize_t n, datalen;
737
738 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
739 return (-1);
740
741 for (;;) {
742 if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
743 return (-1);
744 if (n == 0)
745 return (0);
746 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
747
748 if (imsg.hdr.peerid != PROTOCOL_VERSION) {
749 server_write_client(c, MSG_VERSION, NULL, 0);
750 c->flags |= CLIENT_BAD;
751 imsg_free(&imsg);
752 continue;
753 }
754
755 log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
756 switch (imsg.hdr.type) {
757 case MSG_COMMAND:
758 if (datalen != sizeof commanddata)
759 fatalx("bad MSG_COMMAND size");
760 memcpy(&commanddata, imsg.data, sizeof commanddata);
761
762 server_client_msg_command(c, &commanddata);
763 break;
764 case MSG_IDENTIFY:
765 if (datalen != sizeof identifydata)
766 fatalx("bad MSG_IDENTIFY size");
767 if (imsg.fd == -1)
768 fatalx("MSG_IDENTIFY missing fd");
769 memcpy(&identifydata, imsg.data, sizeof identifydata);
770
77c63de @orebokech Import upstream version 1.4
orebokech authored
771 c->stdin_fd = imsg.fd;
772 c->stdin_event = bufferevent_new(c->stdin_fd,
773 NULL, NULL, server_client_in_callback, c);
774 if (c->stdin_event == NULL)
775 fatalx("failed to create stdin event");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
776 setblocking(c->stdin_fd, 0);
77c63de @orebokech Import upstream version 1.4
orebokech authored
777
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
778 server_client_msg_identify(c, &identifydata, imsg.fd);
779 break;
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
780 case MSG_STDOUT:
781 if (datalen != 0)
782 fatalx("bad MSG_STDOUT size");
783 if (imsg.fd == -1)
784 fatalx("MSG_STDOUT missing fd");
785
77c63de @orebokech Import upstream version 1.4
orebokech authored
786 c->stdout_fd = imsg.fd;
787 c->stdout_event = bufferevent_new(c->stdout_fd,
788 NULL, NULL, server_client_out_callback, c);
789 if (c->stdout_event == NULL)
790 fatalx("failed to create stdout event");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
791 setblocking(c->stdout_fd, 0);
77c63de @orebokech Import upstream version 1.4
orebokech authored
792
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
793 break;
794 case MSG_STDERR:
795 if (datalen != 0)
796 fatalx("bad MSG_STDERR size");
797 if (imsg.fd == -1)
798 fatalx("MSG_STDERR missing fd");
799
77c63de @orebokech Import upstream version 1.4
orebokech authored
800 c->stderr_fd = imsg.fd;
801 c->stderr_event = bufferevent_new(c->stderr_fd,
802 NULL, NULL, server_client_err_callback, c);
803 if (c->stderr_event == NULL)
804 fatalx("failed to create stderr event");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
805 setblocking(c->stderr_fd, 0);
77c63de @orebokech Import upstream version 1.4
orebokech authored
806
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
807 break;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
808 case MSG_RESIZE:
809 if (datalen != 0)
810 fatalx("bad MSG_RESIZE size");
811
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
812 if (tty_resize(&c->tty)) {
813 recalculate_sizes();
814 server_redraw_client(c);
815 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
816 break;
817 case MSG_EXITING:
818 if (datalen != 0)
819 fatalx("bad MSG_EXITING size");
820
821 c->session = NULL;
822 tty_close(&c->tty);
823 server_write_client(c, MSG_EXITED, NULL, 0);
824 break;
825 case MSG_WAKEUP:
826 case MSG_UNLOCK:
827 if (datalen != 0)
828 fatalx("bad MSG_WAKEUP size");
829
830 if (!(c->flags & CLIENT_SUSPENDED))
831 break;
832 c->flags &= ~CLIENT_SUSPENDED;
833
834 if (gettimeofday(&c->activity_time, NULL) != 0)
835 fatal("gettimeofday");
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
836 if (c->session != NULL)
837 session_update_activity(c->session);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
838
839 tty_start_tty(&c->tty);
840 server_redraw_client(c);
841 recalculate_sizes();
842 break;
843 case MSG_ENVIRON:
844 if (datalen != sizeof environdata)
845 fatalx("bad MSG_ENVIRON size");
846 memcpy(&environdata, imsg.data, sizeof environdata);
847
848 environdata.var[(sizeof environdata.var) - 1] = '\0';
849 if (strchr(environdata.var, '=') != NULL)
850 environ_put(&c->environ, environdata.var);
851 break;
852 case MSG_SHELL:
853 if (datalen != 0)
854 fatalx("bad MSG_SHELL size");
855
856 server_client_msg_shell(c);
857 break;
858 default:
859 fatalx("unexpected message");
860 }
861
862 imsg_free(&imsg);
863 }
864 }
865
866 /* Callback to send error message to client. */
867 void printflike2
868 server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
869 {
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
870 va_list ap;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
871
872 va_start(ap, fmt);
77c63de @orebokech Import upstream version 1.4
orebokech authored
873 evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
874 va_end(ap);
875
77c63de @orebokech Import upstream version 1.4
orebokech authored
876 bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
877 ctx->cmdclient->retcode = 1;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
878 }
879
880 /* Callback to send print message to client. */
881 void printflike2
882 server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
883 {
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
884 va_list ap;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
885
886 va_start(ap, fmt);
77c63de @orebokech Import upstream version 1.4
orebokech authored
887 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
888 va_end(ap);
889
77c63de @orebokech Import upstream version 1.4
orebokech authored
890 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
891 }
892
893 /* Callback to send print message to client, if not quiet. */
894 void printflike2
895 server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
896 {
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
897 va_list ap;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
898
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
899 if (options_get_number(&global_options, "quiet"))
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
900 return;
901
902 va_start(ap, fmt);
77c63de @orebokech Import upstream version 1.4
orebokech authored
903 evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
904 va_end(ap);
905
77c63de @orebokech Import upstream version 1.4
orebokech authored
906 bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
907 }
908
909 /* Handle command message. */
910 void
911 server_client_msg_command(struct client *c, struct msg_command_data *data)
912 {
77c63de @orebokech Import upstream version 1.4
orebokech authored
913 struct cmd_ctx ctx;
914 struct cmd_list *cmdlist = NULL;
915 int argc;
916 char **argv, *cause;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
917
918 ctx.error = server_client_msg_error;
919 ctx.print = server_client_msg_print;
920 ctx.info = server_client_msg_info;
921
922 ctx.msgdata = data;
923 ctx.curclient = NULL;
924
925 ctx.cmdclient = c;
926
927 argc = data->argc;
928 data->argv[(sizeof data->argv) - 1] = '\0';
929 if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
930 server_client_msg_error(&ctx, "command too long");
931 goto error;
932 }
933
934 if (argc == 0) {
935 argc = 1;
936 argv = xcalloc(1, sizeof *argv);
937 *argv = xstrdup("new-session");
938 }
939
940 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
941 server_client_msg_error(&ctx, "%s", cause);
942 cmd_free_argv(argc, argv);
943 goto error;
944 }
945 cmd_free_argv(argc, argv);
946
77c63de @orebokech Import upstream version 1.4
orebokech authored
947 if (cmd_list_exec(cmdlist, &ctx) != 1)
948 c->flags |= CLIENT_EXIT;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
949 cmd_list_free(cmdlist);
950 return;
951
952 error:
953 if (cmdlist != NULL)
954 cmd_list_free(cmdlist);
77c63de @orebokech Import upstream version 1.4
orebokech authored
955 c->flags |= CLIENT_EXIT;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
956 }
957
958 /* Handle identify message. */
959 void
960 server_client_msg_identify(
961 struct client *c, struct msg_identify_data *data, int fd)
962 {
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
963 int tty_fd;
964
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
965 c->cwd = NULL;
966 data->cwd[(sizeof data->cwd) - 1] = '\0';
967 if (*data->cwd != '\0')
968 c->cwd = xstrdup(data->cwd);
969
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
970 if (!isatty(fd))
971 return;
972 if ((tty_fd = dup(fd)) == -1)
973 fatal("dup failed");
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
974 data->term[(sizeof data->term) - 1] = '\0';
fb53a63 @kfebert Imported Upstream version 1.3
kfebert authored
975 tty_init(&c->tty, tty_fd, data->term);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
976 if (data->flags & IDENTIFY_UTF8)
977 c->tty.flags |= TTY_UTF8;
978 if (data->flags & IDENTIFY_256COLOURS)
979 c->tty.term_flags |= TERM_256COLOURS;
980 else if (data->flags & IDENTIFY_88COLOURS)
981 c->tty.term_flags |= TERM_88COLOURS;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
982 c->tty.key_callback = server_client_handle_key;
983 c->tty.key_data = c;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
984
985 tty_resize(&c->tty);
986
987 c->flags |= CLIENT_TERMINAL;
988 }
989
990 /* Handle shell message. */
991 void
992 server_client_msg_shell(struct client *c)
993 {
994 struct msg_shell_data data;
995 const char *shell;
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
996
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
997 shell = options_get_string(&global_s_options, "default-shell");
998
999 if (*shell == '\0' || areshell(shell))
1000 shell = _PATH_BSHELL;
1001 if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
1002 strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
1003
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
1004 server_write_client(c, MSG_SHELL, &data, sizeof data);
1005 c->flags |= CLIENT_BAD; /* it will die after exec */
1006 }
Something went wrong with that request. Please try again.