Annotation of src/usr.bin/tmux/server-client.c, Revision 1.233
1.233 ! nicm 1: /* $OpenBSD: server-client.c,v 1.232 2017/05/16 12:57:26 nicm Exp $ */
1.1 nicm 2:
3: /*
1.181 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.92 nicm 20: #include <sys/ioctl.h>
1.162 nicm 21: #include <sys/uio.h>
1.1 nicm 22:
1.114 benno 23: #include <errno.h>
1.12 nicm 24: #include <event.h>
1.1 nicm 25: #include <fcntl.h>
1.162 nicm 26: #include <imsg.h>
1.98 nicm 27: #include <paths.h>
28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
1.4 nicm 30: #include <time.h>
1.1 nicm 31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
1.190 nicm 35: static void server_client_free(int, short, void *);
36: static void server_client_check_focus(struct window_pane *);
37: static void server_client_check_resize(struct window_pane *);
38: static key_code server_client_check_mouse(struct client *);
39: static void server_client_repeat_timer(int, short, void *);
1.192 nicm 40: static void server_client_click_timer(int, short, void *);
1.190 nicm 41: static void server_client_check_exit(struct client *);
42: static void server_client_check_redraw(struct client *);
43: static void server_client_set_title(struct client *);
44: static void server_client_reset_state(struct client *);
45: static int server_client_assume_paste(struct session *);
46:
47: static void server_client_dispatch(struct imsg *, void *);
48: static void server_client_dispatch_command(struct client *, struct imsg *);
49: static void server_client_dispatch_identify(struct client *, struct imsg *);
50: static void server_client_dispatch_shell(struct client *);
1.233 ! nicm 51:
! 52: /* Number of attached clients. */
! 53: u_int
! 54: server_client_how_many(void)
! 55: {
! 56: struct client *c;
! 57: u_int n;
! 58:
! 59: n = 0;
! 60: TAILQ_FOREACH(c, &clients, entry) {
! 61: if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
! 62: n++;
! 63: }
! 64: return (n);
! 65: }
1.140 nicm 66:
1.220 nicm 67: /* Identify mode callback. */
1.214 nicm 68: static void
1.232 nicm 69: server_client_callback_identify(__unused int fd, __unused short events,
70: void *data)
1.214 nicm 71: {
72: server_client_clear_identify(data, NULL);
73: }
74:
75: /* Set identify mode on client. */
76: void
77: server_client_set_identify(struct client *c)
78: {
79: struct timeval tv;
80: int delay;
81:
82: delay = options_get_number(c->session->options, "display-panes-time");
83: tv.tv_sec = delay / 1000;
84: tv.tv_usec = (delay % 1000) * 1000L;
85:
86: if (event_initialized(&c->identify_timer))
87: evtimer_del(&c->identify_timer);
88: evtimer_set(&c->identify_timer, server_client_callback_identify, c);
89: evtimer_add(&c->identify_timer, &tv);
90:
91: c->flags |= CLIENT_IDENTIFY;
92: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
93: server_redraw_client(c);
94: }
95:
96: /* Clear identify mode on client. */
97: void
98: server_client_clear_identify(struct client *c, struct window_pane *wp)
99: {
100: if (~c->flags & CLIENT_IDENTIFY)
101: return;
102: c->flags &= ~CLIENT_IDENTIFY;
103:
104: if (c->identify_callback != NULL)
105: c->identify_callback(c, wp);
106:
107: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
108: server_redraw_client(c);
109: }
110:
1.140 nicm 111: /* Check if this client is inside this server. */
112: int
113: server_client_check_nested(struct client *c)
114: {
115: struct environ_entry *envent;
116: struct window_pane *wp;
117:
1.164 nicm 118: envent = environ_find(c->environ, "TMUX");
1.140 nicm 119: if (envent == NULL || *envent->value == '\0')
120: return (0);
121:
122: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 123: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 124: return (1);
125: }
126: return (0);
127: }
1.1 nicm 128:
1.132 nicm 129: /* Set client key table. */
130: void
1.178 nicm 131: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 132: {
1.178 nicm 133: if (name == NULL)
134: name = server_client_get_key_table(c);
135:
1.132 nicm 136: key_bindings_unref_table(c->keytable);
137: c->keytable = key_bindings_get_table(name, 1);
138: c->keytable->references++;
139: }
140:
1.178 nicm 141: /* Get default key table. */
142: const char *
143: server_client_get_key_table(struct client *c)
144: {
145: struct session *s = c->session;
146: const char *name;
147:
148: if (s == NULL)
149: return ("root");
150:
151: name = options_get_string(s->options, "key-table");
152: if (*name == '\0')
153: return ("root");
154: return (name);
155: }
156:
1.223 nicm 157: /* Is this table the default key table? */
158: static int
159: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191 nicm 160: {
1.223 nicm 161: return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191 nicm 162: }
163:
1.1 nicm 164: /* Create a new client. */
165: void
166: server_client_create(int fd)
167: {
168: struct client *c;
169:
1.49 nicm 170: setblocking(fd, 0);
1.1 nicm 171:
172: c = xcalloc(1, sizeof *c);
1.141 nicm 173: c->references = 1;
1.162 nicm 174: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 175:
1.10 nicm 176: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 177: fatal("gettimeofday failed");
1.11 nicm 178: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 179:
1.164 nicm 180: c->environ = environ_create();
1.1 nicm 181:
1.146 nicm 182: c->fd = -1;
1.165 nicm 183: c->cwd = NULL;
1.145 nicm 184:
1.194 nicm 185: TAILQ_INIT(&c->queue);
1.94 nicm 186:
1.116 nicm 187: c->stdin_data = evbuffer_new();
188: c->stdout_data = evbuffer_new();
189: c->stderr_data = evbuffer_new();
1.33 nicm 190:
1.1 nicm 191: c->tty.fd = -1;
192: c->title = NULL;
193:
194: c->session = NULL;
1.45 nicm 195: c->last_session = NULL;
1.1 nicm 196: c->tty.sx = 80;
197: c->tty.sy = 24;
198:
199: screen_init(&c->status, c->tty.sx, 1, 0);
200:
201: c->message_string = NULL;
1.136 nicm 202: TAILQ_INIT(&c->message_log);
1.1 nicm 203:
204: c->prompt_string = NULL;
205: c->prompt_buffer = NULL;
206: c->prompt_index = 0;
207:
1.93 nicm 208: c->flags |= CLIENT_FOCUSED;
209:
1.132 nicm 210: c->keytable = key_bindings_get_table("root", 1);
211: c->keytable->references++;
212:
1.17 nicm 213: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 214: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 215:
1.135 nicm 216: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 217: log_debug("new client %p", c);
1.72 nicm 218: }
219:
220: /* Open client terminal if needed. */
221: int
1.119 nicm 222: server_client_open(struct client *c, char **cause)
1.72 nicm 223: {
1.75 nicm 224: if (c->flags & CLIENT_CONTROL)
225: return (0);
1.120 nicm 226:
227: if (strcmp(c->ttyname, "/dev/tty") == 0) {
228: *cause = xstrdup("can't use /dev/tty");
229: return (-1);
230: }
1.75 nicm 231:
1.72 nicm 232: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 233: *cause = xstrdup("not a terminal");
1.72 nicm 234: return (-1);
235: }
236:
1.119 nicm 237: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 238: return (-1);
239:
240: return (0);
1.1 nicm 241: }
242:
243: /* Lost a client. */
244: void
245: server_client_lost(struct client *c)
246: {
1.136 nicm 247: struct message_entry *msg, *msg1;
1.1 nicm 248:
1.141 nicm 249: c->flags |= CLIENT_DEAD;
250:
1.214 nicm 251: server_client_clear_identify(c, NULL);
1.141 nicm 252: status_prompt_clear(c);
253: status_message_clear(c);
254:
255: if (c->stdin_callback != NULL)
256: c->stdin_callback(c, 1, c->stdin_callback_data);
257:
1.135 nicm 258: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 259: log_debug("lost client %p", c);
1.1 nicm 260:
261: /*
262: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
263: * and tty_free might close an unrelated fd.
264: */
265: if (c->flags & CLIENT_TERMINAL)
266: tty_free(&c->tty);
1.109 nicm 267: free(c->ttyname);
268: free(c->term);
1.1 nicm 269:
1.113 nicm 270: evbuffer_free(c->stdin_data);
271: evbuffer_free(c->stdout_data);
1.97 nicm 272: if (c->stderr_data != c->stdout_data)
1.126 nicm 273: evbuffer_free(c->stderr_data);
1.33 nicm 274:
1.148 nicm 275: if (event_initialized(&c->status_timer))
276: evtimer_del(&c->status_timer);
1.1 nicm 277: screen_free(&c->status);
278:
1.77 nicm 279: free(c->title);
1.165 nicm 280: free((void *)c->cwd);
1.1 nicm 281:
1.17 nicm 282: evtimer_del(&c->repeat_timer);
1.192 nicm 283: evtimer_del(&c->click_timer);
1.17 nicm 284:
1.132 nicm 285: key_bindings_unref_table(c->keytable);
286:
1.69 nicm 287: if (event_initialized(&c->identify_timer))
288: evtimer_del(&c->identify_timer);
1.15 nicm 289:
1.77 nicm 290: free(c->message_string);
1.116 nicm 291: if (event_initialized(&c->message_timer))
1.69 nicm 292: evtimer_del(&c->message_timer);
1.136 nicm 293: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 294: free(msg->msg);
1.136 nicm 295: TAILQ_REMOVE(&c->message_log, msg, entry);
296: free(msg);
1.21 nicm 297: }
1.1 nicm 298:
1.77 nicm 299: free(c->prompt_string);
300: free(c->prompt_buffer);
1.61 nicm 301:
1.228 nicm 302: format_lost_client(c);
1.164 nicm 303: environ_free(c->environ);
1.1 nicm 304:
1.162 nicm 305: proc_remove_peer(c->peer);
306: c->peer = NULL;
1.13 nicm 307:
1.142 nicm 308: server_client_unref(c);
1.71 nicm 309:
310: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 311:
312: recalculate_sizes();
1.41 nicm 313: server_check_unattached();
1.19 nicm 314: server_update_socket();
1.141 nicm 315: }
316:
317: /* Remove reference from a client. */
318: void
1.142 nicm 319: server_client_unref(struct client *c)
1.141 nicm 320: {
1.157 nicm 321: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 322:
323: c->references--;
324: if (c->references == 0)
325: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
326: }
327:
328: /* Free dead client. */
1.190 nicm 329: static void
1.170 nicm 330: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 331: {
332: struct client *c = arg;
333:
1.157 nicm 334: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 335:
1.194 nicm 336: if (!TAILQ_EMPTY(&c->queue))
337: fatalx("queue not empty");
338:
1.216 nicm 339: if (c->references == 0) {
340: free((void *)c->name);
1.141 nicm 341: free(c);
1.216 nicm 342: }
1.9 nicm 343: }
344:
1.220 nicm 345: /* Suspend a client. */
346: void
347: server_client_suspend(struct client *c)
348: {
1.232 nicm 349: struct session *s = c->session;
1.220 nicm 350:
351: if (s == NULL || (c->flags & CLIENT_DETACHING))
352: return;
353:
354: tty_stop_tty(&c->tty);
355: c->flags |= CLIENT_SUSPENDED;
356: proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
357: }
358:
1.174 nicm 359: /* Detach a client. */
360: void
361: server_client_detach(struct client *c, enum msgtype msgtype)
362: {
1.232 nicm 363: struct session *s = c->session;
1.174 nicm 364:
1.220 nicm 365: if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174 nicm 366: return;
367:
1.220 nicm 368: c->flags |= CLIENT_DETACHING;
1.196 nicm 369: notify_client("client-detached", c);
1.174 nicm 370: proc_send_s(c->peer, msgtype, s->name);
1.207 nicm 371: }
372:
1.208 nicm 373: /* Execute command to replace a client. */
1.207 nicm 374: void
375: server_client_exec(struct client *c, const char *cmd)
376: {
377: struct session *s = c->session;
1.208 nicm 378: char *msg;
379: const char *shell;
1.207 nicm 380: size_t cmdsize, shellsize;
381:
382: if (*cmd == '\0')
383: return;
384: cmdsize = strlen(cmd) + 1;
385:
386: if (s != NULL)
387: shell = options_get_string(s->options, "default-shell");
388: else
389: shell = options_get_string(global_s_options, "default-shell");
390: shellsize = strlen(shell) + 1;
391:
392: msg = xmalloc(cmdsize + shellsize);
393: memcpy(msg, cmd, cmdsize);
394: memcpy(msg + cmdsize, shell, shellsize);
395:
396: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
397: free(msg);
1.174 nicm 398: }
399:
1.66 nicm 400: /* Check for mouse keys. */
1.190 nicm 401: static key_code
1.131 nicm 402: server_client_check_mouse(struct client *c)
1.66 nicm 403: {
1.192 nicm 404: struct session *s = c->session;
405: struct mouse_event *m = &c->tty.mouse;
406: struct window *w;
407: struct window_pane *wp;
408: u_int x, y, b;
409: int flag;
410: key_code key;
411: struct timeval tv;
1.209 nicm 412: enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
413: enum { NOWHERE, PANE, STATUS, BORDER } where;
414:
415: type = NOTYPE;
416: where = NOWHERE;
1.131 nicm 417:
418: log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
419: m->lx, m->ly, c->tty.mouse_drag_flag);
420:
421: /* What type of event is this? */
1.209 nicm 422: if ((m->sgr_type != ' ' &&
423: MOUSE_DRAG(m->sgr_b) &&
424: MOUSE_BUTTONS(m->sgr_b) == 3) ||
425: (m->sgr_type == ' ' &&
426: MOUSE_DRAG(m->b) &&
427: MOUSE_BUTTONS(m->b) == 3 &&
428: MOUSE_BUTTONS(m->lb) == 3)) {
429: type = MOVE;
430: x = m->x, y = m->y, b = 0;
431: log_debug("move at %u,%u", x, y);
432: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 433: type = DRAG;
434: if (c->tty.mouse_drag_flag) {
435: x = m->x, y = m->y, b = m->b;
436: log_debug("drag update at %u,%u", x, y);
437: } else {
438: x = m->lx, y = m->ly, b = m->lb;
439: log_debug("drag start at %u,%u", x, y);
440: }
441: } else if (MOUSE_WHEEL(m->b)) {
442: type = WHEEL;
443: x = m->x, y = m->y, b = m->b;
444: log_debug("wheel at %u,%u", x, y);
1.200 nicm 445: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 446: type = UP;
447: x = m->x, y = m->y, b = m->lb;
448: log_debug("up at %u,%u", x, y);
449: } else {
1.192 nicm 450: if (c->flags & CLIENT_DOUBLECLICK) {
451: evtimer_del(&c->click_timer);
452: c->flags &= ~CLIENT_DOUBLECLICK;
453: if (m->b == c->click_button) {
454: type = DOUBLE;
455: x = m->x, y = m->y, b = m->b;
456: log_debug("double-click at %u,%u", x, y);
457: flag = CLIENT_TRIPLECLICK;
458: goto add_timer;
459: }
460: } else if (c->flags & CLIENT_TRIPLECLICK) {
461: evtimer_del(&c->click_timer);
462: c->flags &= ~CLIENT_TRIPLECLICK;
463: if (m->b == c->click_button) {
464: type = TRIPLE;
465: x = m->x, y = m->y, b = m->b;
466: log_debug("triple-click at %u,%u", x, y);
467: goto have_event;
468: }
469: }
470:
1.131 nicm 471: type = DOWN;
472: x = m->x, y = m->y, b = m->b;
473: log_debug("down at %u,%u", x, y);
1.192 nicm 474: flag = CLIENT_DOUBLECLICK;
475:
476: add_timer:
477: if (KEYC_CLICK_TIMEOUT != 0) {
478: c->flags |= flag;
479: c->click_button = m->b;
480:
481: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
482: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
483: evtimer_del(&c->click_timer);
484: evtimer_add(&c->click_timer, &tv);
485: }
1.131 nicm 486: }
1.192 nicm 487:
488: have_event:
1.131 nicm 489: if (type == NOTYPE)
1.177 nicm 490: return (KEYC_UNKNOWN);
1.131 nicm 491:
492: /* Always save the session. */
493: m->s = s->id;
494:
495: /* Is this on the status line? */
496: m->statusat = status_at_line(c);
497: if (m->statusat != -1 && y == (u_int)m->statusat) {
498: w = status_get_window_at(c, x);
499: if (w == NULL)
1.177 nicm 500: return (KEYC_UNKNOWN);
1.131 nicm 501: m->w = w->id;
502: where = STATUS;
503: } else
504: m->w = -1;
505:
506: /* Not on status line. Adjust position and check for border or pane. */
507: if (where == NOWHERE) {
508: if (m->statusat == 0 && y > 0)
509: y--;
510: else if (m->statusat > 0 && y >= (u_int)m->statusat)
511: y = m->statusat - 1;
512:
513: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
514: if ((wp->xoff + wp->sx == x &&
515: wp->yoff <= 1 + y &&
516: wp->yoff + wp->sy >= y) ||
517: (wp->yoff + wp->sy == y &&
518: wp->xoff <= 1 + x &&
519: wp->xoff + wp->sx >= x))
520: break;
521: }
522: if (wp != NULL)
523: where = BORDER;
524: else {
525: wp = window_get_active_at(s->curw->window, x, y);
1.173 nicm 526: if (wp != NULL) {
1.131 nicm 527: where = PANE;
1.173 nicm 528: log_debug("mouse at %u,%u is on pane %%%u",
529: x, y, wp->id);
530: }
1.131 nicm 531: }
532: if (where == NOWHERE)
1.177 nicm 533: return (KEYC_UNKNOWN);
1.131 nicm 534: m->wp = wp->id;
535: m->w = wp->window->id;
536: } else
537: m->wp = -1;
538:
539: /* Stop dragging if needed. */
1.200 nicm 540: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 541: if (c->tty.mouse_drag_release != NULL)
542: c->tty.mouse_drag_release(c, m);
543:
544: c->tty.mouse_drag_update = NULL;
545: c->tty.mouse_drag_release = NULL;
546:
1.182 nicm 547: /*
1.183 nicm 548: * End a mouse drag by passing a MouseDragEnd key corresponding
549: * to the button that started the drag.
1.182 nicm 550: */
551: switch (c->tty.mouse_drag_flag) {
552: case 1:
553: if (where == PANE)
1.183 nicm 554: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 555: if (where == STATUS)
1.183 nicm 556: key = KEYC_MOUSEDRAGEND1_STATUS;
1.182 nicm 557: if (where == BORDER)
1.183 nicm 558: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 559: break;
560: case 2:
561: if (where == PANE)
1.183 nicm 562: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 563: if (where == STATUS)
1.183 nicm 564: key = KEYC_MOUSEDRAGEND2_STATUS;
1.182 nicm 565: if (where == BORDER)
1.183 nicm 566: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 567: break;
568: case 3:
569: if (where == PANE)
1.183 nicm 570: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 571: if (where == STATUS)
1.183 nicm 572: key = KEYC_MOUSEDRAGEND3_STATUS;
1.182 nicm 573: if (where == BORDER)
1.183 nicm 574: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 575: break;
576: default:
577: key = KEYC_MOUSE;
578: break;
579: }
1.131 nicm 580: c->tty.mouse_drag_flag = 0;
1.182 nicm 581:
582: return (key);
1.131 nicm 583: }
584:
585: /* Convert to a key binding. */
1.177 nicm 586: key = KEYC_UNKNOWN;
1.131 nicm 587: switch (type) {
588: case NOTYPE:
589: break;
1.209 nicm 590: case MOVE:
591: if (where == PANE)
592: key = KEYC_MOUSEMOVE_PANE;
593: if (where == STATUS)
594: key = KEYC_MOUSEMOVE_STATUS;
595: if (where == BORDER)
596: key = KEYC_MOUSEMOVE_BORDER;
597: break;
1.131 nicm 598: case DRAG:
1.204 nicm 599: if (c->tty.mouse_drag_update != NULL)
600: key = KEYC_DRAGGING;
601: else {
1.131 nicm 602: switch (MOUSE_BUTTONS(b)) {
603: case 0:
604: if (where == PANE)
605: key = KEYC_MOUSEDRAG1_PANE;
606: if (where == STATUS)
607: key = KEYC_MOUSEDRAG1_STATUS;
608: if (where == BORDER)
609: key = KEYC_MOUSEDRAG1_BORDER;
610: break;
611: case 1:
612: if (where == PANE)
613: key = KEYC_MOUSEDRAG2_PANE;
614: if (where == STATUS)
615: key = KEYC_MOUSEDRAG2_STATUS;
616: if (where == BORDER)
617: key = KEYC_MOUSEDRAG2_BORDER;
618: break;
619: case 2:
620: if (where == PANE)
621: key = KEYC_MOUSEDRAG3_PANE;
622: if (where == STATUS)
623: key = KEYC_MOUSEDRAG3_STATUS;
624: if (where == BORDER)
625: key = KEYC_MOUSEDRAG3_BORDER;
626: break;
627: }
628: }
1.66 nicm 629:
1.182 nicm 630: /*
631: * Begin a drag by setting the flag to a non-zero value that
632: * corresponds to the mouse button in use.
633: */
634: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 635: break;
636: case WHEEL:
637: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
638: if (where == PANE)
639: key = KEYC_WHEELUP_PANE;
640: if (where == STATUS)
641: key = KEYC_WHEELUP_STATUS;
642: if (where == BORDER)
643: key = KEYC_WHEELUP_BORDER;
644: } else {
645: if (where == PANE)
646: key = KEYC_WHEELDOWN_PANE;
647: if (where == STATUS)
648: key = KEYC_WHEELDOWN_STATUS;
649: if (where == BORDER)
650: key = KEYC_WHEELDOWN_BORDER;
651: }
652: break;
653: case UP:
654: switch (MOUSE_BUTTONS(b)) {
655: case 0:
656: if (where == PANE)
657: key = KEYC_MOUSEUP1_PANE;
658: if (where == STATUS)
659: key = KEYC_MOUSEUP1_STATUS;
660: if (where == BORDER)
661: key = KEYC_MOUSEUP1_BORDER;
662: break;
663: case 1:
664: if (where == PANE)
665: key = KEYC_MOUSEUP2_PANE;
666: if (where == STATUS)
667: key = KEYC_MOUSEUP2_STATUS;
668: if (where == BORDER)
669: key = KEYC_MOUSEUP2_BORDER;
670: break;
671: case 2:
672: if (where == PANE)
673: key = KEYC_MOUSEUP3_PANE;
674: if (where == STATUS)
675: key = KEYC_MOUSEUP3_STATUS;
676: if (where == BORDER)
677: key = KEYC_MOUSEUP3_BORDER;
678: break;
679: }
680: break;
681: case DOWN:
682: switch (MOUSE_BUTTONS(b)) {
683: case 0:
684: if (where == PANE)
685: key = KEYC_MOUSEDOWN1_PANE;
686: if (where == STATUS)
687: key = KEYC_MOUSEDOWN1_STATUS;
688: if (where == BORDER)
689: key = KEYC_MOUSEDOWN1_BORDER;
690: break;
691: case 1:
692: if (where == PANE)
693: key = KEYC_MOUSEDOWN2_PANE;
694: if (where == STATUS)
695: key = KEYC_MOUSEDOWN2_STATUS;
696: if (where == BORDER)
697: key = KEYC_MOUSEDOWN2_BORDER;
698: break;
699: case 2:
700: if (where == PANE)
701: key = KEYC_MOUSEDOWN3_PANE;
702: if (where == STATUS)
703: key = KEYC_MOUSEDOWN3_STATUS;
704: if (where == BORDER)
705: key = KEYC_MOUSEDOWN3_BORDER;
706: break;
1.66 nicm 707: }
1.131 nicm 708: break;
1.192 nicm 709: case DOUBLE:
710: switch (MOUSE_BUTTONS(b)) {
711: case 0:
712: if (where == PANE)
713: key = KEYC_DOUBLECLICK1_PANE;
714: if (where == STATUS)
715: key = KEYC_DOUBLECLICK1_STATUS;
716: if (where == BORDER)
717: key = KEYC_DOUBLECLICK1_BORDER;
718: break;
719: case 1:
720: if (where == PANE)
721: key = KEYC_DOUBLECLICK2_PANE;
722: if (where == STATUS)
723: key = KEYC_DOUBLECLICK2_STATUS;
724: if (where == BORDER)
725: key = KEYC_DOUBLECLICK2_BORDER;
726: break;
727: case 2:
728: if (where == PANE)
729: key = KEYC_DOUBLECLICK3_PANE;
730: if (where == STATUS)
731: key = KEYC_DOUBLECLICK3_STATUS;
732: if (where == BORDER)
733: key = KEYC_DOUBLECLICK3_BORDER;
734: break;
735: }
736: break;
737: case TRIPLE:
738: switch (MOUSE_BUTTONS(b)) {
739: case 0:
740: if (where == PANE)
741: key = KEYC_TRIPLECLICK1_PANE;
742: if (where == STATUS)
743: key = KEYC_TRIPLECLICK1_STATUS;
744: if (where == BORDER)
745: key = KEYC_TRIPLECLICK1_BORDER;
746: break;
747: case 1:
748: if (where == PANE)
749: key = KEYC_TRIPLECLICK2_PANE;
750: if (where == STATUS)
751: key = KEYC_TRIPLECLICK2_STATUS;
752: if (where == BORDER)
753: key = KEYC_TRIPLECLICK2_BORDER;
754: break;
755: case 2:
756: if (where == PANE)
757: key = KEYC_TRIPLECLICK3_PANE;
758: if (where == STATUS)
759: key = KEYC_TRIPLECLICK3_STATUS;
760: if (where == BORDER)
761: key = KEYC_TRIPLECLICK3_BORDER;
762: break;
763: }
764: break;
1.66 nicm 765: }
1.177 nicm 766: if (key == KEYC_UNKNOWN)
767: return (KEYC_UNKNOWN);
1.66 nicm 768:
1.131 nicm 769: /* Apply modifiers if any. */
770: if (b & MOUSE_MASK_META)
771: key |= KEYC_ESCAPE;
772: if (b & MOUSE_MASK_CTRL)
773: key |= KEYC_CTRL;
774: if (b & MOUSE_MASK_SHIFT)
775: key |= KEYC_SHIFT;
1.66 nicm 776:
1.131 nicm 777: return (key);
1.66 nicm 778: }
779:
1.82 nicm 780: /* Is this fast enough to probably be a paste? */
1.190 nicm 781: static int
1.82 nicm 782: server_client_assume_paste(struct session *s)
783: {
784: struct timeval tv;
1.84 nicm 785: int t;
1.82 nicm 786:
1.163 nicm 787: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 788: return (0);
1.82 nicm 789:
790: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 791: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
792: log_debug("session %s pasting (flag %d)", s->name,
793: !!(s->flags & SESSION_PASTING));
794: if (s->flags & SESSION_PASTING)
795: return (1);
796: s->flags |= SESSION_PASTING;
797: return (0);
798: }
799: log_debug("session %s not pasting", s->name);
800: s->flags &= ~SESSION_PASTING;
1.83 nicm 801: return (0);
1.82 nicm 802: }
803:
1.18 nicm 804: /* Handle data key input from client. */
805: void
1.168 nicm 806: server_client_handle_key(struct client *c, key_code key)
1.18 nicm 807: {
1.131 nicm 808: struct mouse_event *m = &c->tty.mouse;
1.132 nicm 809: struct session *s = c->session;
1.18 nicm 810: struct window *w;
811: struct window_pane *wp;
812: struct timeval tv;
1.223 nicm 813: struct key_table *table, *first;
1.132 nicm 814: struct key_binding bd_find, *bd;
815: int xtimeout;
1.201 nicm 816: struct cmd_find_state fs;
1.18 nicm 817:
818: /* Check the client is good to accept input. */
1.132 nicm 819: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18 nicm 820: return;
1.132 nicm 821: w = s->curw->window;
1.18 nicm 822:
823: /* Update the activity timer. */
824: if (gettimeofday(&c->activity_time, NULL) != 0)
825: fatal("gettimeofday failed");
1.149 nicm 826: session_update_activity(s, &c->activity_time);
1.18 nicm 827:
1.132 nicm 828: /* Number keys jump to pane in identify mode. */
1.25 nicm 829: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 830: if (c->flags & CLIENT_READONLY)
831: return;
1.95 nicm 832: window_unzoom(w);
1.18 nicm 833: wp = window_pane_at_index(w, key - '0');
1.187 nicm 834: if (wp != NULL && !window_pane_visible(wp))
835: wp = NULL;
1.214 nicm 836: server_client_clear_identify(c, wp);
1.18 nicm 837: return;
838: }
839:
840: /* Handle status line. */
1.30 nicm 841: if (!(c->flags & CLIENT_READONLY)) {
842: status_message_clear(c);
1.214 nicm 843: server_client_clear_identify(c, NULL);
1.30 nicm 844: }
1.18 nicm 845: if (c->prompt_string != NULL) {
1.193 nicm 846: if (c->flags & CLIENT_READONLY)
847: return;
848: if (status_prompt_key(c, key) == 0)
849: return;
1.18 nicm 850: }
851:
852: /* Check for mouse keys. */
1.204 nicm 853: m->valid = 0;
1.18 nicm 854: if (key == KEYC_MOUSE) {
1.30 nicm 855: if (c->flags & CLIENT_READONLY)
856: return;
1.131 nicm 857: key = server_client_check_mouse(c);
1.177 nicm 858: if (key == KEYC_UNKNOWN)
1.131 nicm 859: return;
860:
861: m->valid = 1;
862: m->key = key;
1.203 nicm 863:
864: /*
1.204 nicm 865: * Mouse drag is in progress, so fire the callback (now that
866: * the mouse event is valid).
1.203 nicm 867: */
1.204 nicm 868: if (key == KEYC_DRAGGING) {
869: c->tty.mouse_drag_update(c, m);
1.203 nicm 870: return;
1.204 nicm 871: }
1.131 nicm 872: } else
873: m->valid = 0;
1.18 nicm 874:
1.202 nicm 875: /* Find affected pane. */
1.227 nicm 876: if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m) != 0)
877: cmd_find_from_session(&fs, s);
878: wp = fs.wp;
1.202 nicm 879:
880: /* Forward mouse keys if disabled. */
1.206 nicm 881: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.202 nicm 882: goto forward;
883:
1.132 nicm 884: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 885: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
886: goto forward;
1.18 nicm 887:
1.191 nicm 888: /*
889: * Work out the current key table. If the pane is in a mode, use
890: * the mode table instead of the default key table.
891: */
1.223 nicm 892: if (server_client_is_default_key_table(c, c->keytable) &&
893: wp != NULL &&
894: wp->mode != NULL &&
895: wp->mode->key_table != NULL)
896: table = key_bindings_get_table(wp->mode->key_table(wp), 1);
897: else
1.191 nicm 898: table = c->keytable;
1.223 nicm 899: first = table;
1.191 nicm 900:
1.205 nicm 901: /*
902: * The prefix always takes precedence and forces a switch to the prefix
903: * table, unless we are already there.
904: */
905: if ((key == (key_code)options_get_number(s->options, "prefix") ||
906: key == (key_code)options_get_number(s->options, "prefix2")) &&
907: strcmp(table->name, "prefix") != 0) {
908: server_client_set_key_table(c, "prefix");
909: server_status_client(c);
910: return;
911: }
912:
1.223 nicm 913: retry:
914: /* Log key table. */
915: if (wp == NULL)
916: log_debug("key table %s (no pane)", table->name);
917: else
918: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225 nicm 919: if (c->flags & CLIENT_REPEAT)
920: log_debug("currently repeating");
1.223 nicm 921:
1.132 nicm 922: /* Try to see if there is a key binding in the current table. */
1.229 nicm 923: bd_find.key = (key & ~KEYC_XTERM);
1.191 nicm 924: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132 nicm 925: if (bd != NULL) {
926: /*
927: * Key was matched in this table. If currently repeating but a
928: * non-repeating binding was found, stop repeating and try
929: * again in the root table.
930: */
1.222 nicm 931: if ((c->flags & CLIENT_REPEAT) &&
932: (~bd->flags & KEY_BINDING_REPEAT)) {
1.178 nicm 933: server_client_set_key_table(c, NULL);
1.132 nicm 934: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 935: server_status_client(c);
1.223 nicm 936: table = c->keytable;
1.132 nicm 937: goto retry;
1.18 nicm 938: }
1.223 nicm 939: log_debug("found in key table %s", table->name);
1.82 nicm 940:
1.132 nicm 941: /*
942: * Take a reference to this table to make sure the key binding
943: * doesn't disappear.
944: */
945: table->references++;
946:
947: /*
948: * If this is a repeating key, start the timer. Otherwise reset
949: * the client back to the root table.
950: */
1.163 nicm 951: xtimeout = options_get_number(s->options, "repeat-time");
1.222 nicm 952: if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132 nicm 953: c->flags |= CLIENT_REPEAT;
954:
955: tv.tv_sec = xtimeout / 1000;
956: tv.tv_usec = (xtimeout % 1000) * 1000L;
957: evtimer_del(&c->repeat_timer);
958: evtimer_add(&c->repeat_timer, &tv);
959: } else {
1.18 nicm 960: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 961: server_client_set_key_table(c, NULL);
1.18 nicm 962: }
1.132 nicm 963: server_status_client(c);
964:
1.227 nicm 965: /* Execute the key binding. */
1.231 nicm 966: key_bindings_dispatch(bd, NULL, c, m, &fs);
1.132 nicm 967: key_bindings_unref_table(table);
1.18 nicm 968: return;
969: }
970:
1.132 nicm 971: /*
1.223 nicm 972: * No match in this table. If not in the root table or if repeating,
973: * switch the client back to the root table and try again.
1.132 nicm 974: */
1.223 nicm 975: log_debug("not found in key table %s", table->name);
976: if (!server_client_is_default_key_table(c, table) ||
977: (c->flags & CLIENT_REPEAT)) {
1.178 nicm 978: server_client_set_key_table(c, NULL);
1.18 nicm 979: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 980: server_status_client(c);
1.223 nicm 981: table = c->keytable;
1.132 nicm 982: goto retry;
1.18 nicm 983: }
984:
1.223 nicm 985: /*
986: * No match in the root table either. If this wasn't the first table
987: * tried, don't pass the key to the pane.
988: */
989: if (first != table) {
1.178 nicm 990: server_client_set_key_table(c, NULL);
1.132 nicm 991: server_status_client(c);
1.161 nicm 992: return;
993: }
994:
995: forward:
996: if (c->flags & CLIENT_READONLY)
997: return;
998: if (wp != NULL)
1.132 nicm 999: window_pane_key(wp, c, s, key, m);
1.18 nicm 1000: }
1001:
1.2 nicm 1002: /* Client functions that need to happen every loop. */
1003: void
1004: server_client_loop(void)
1005: {
1006: struct client *c;
1007: struct window *w;
1008: struct window_pane *wp;
1.197 nicm 1009: int focus;
1.2 nicm 1010:
1.135 nicm 1011: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1012: server_client_check_exit(c);
1013: if (c->session != NULL) {
1014: server_client_check_redraw(c);
1015: server_client_reset_state(c);
1016: }
1.2 nicm 1017: }
1018:
1019: /*
1020: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 1021: * their flags now. Also check pane focus and resize.
1.2 nicm 1022: */
1.197 nicm 1023: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1024: RB_FOREACH(w, windows, &windows) {
1.91 nicm 1025: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 1026: if (wp->fd != -1) {
1.197 nicm 1027: if (focus)
1028: server_client_check_focus(wp);
1.101 nicm 1029: server_client_check_resize(wp);
1030: }
1.2 nicm 1031: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1032: }
1.150 nicm 1033: check_window_name(w);
1.2 nicm 1034: }
1.92 nicm 1035: }
1036:
1.211 nicm 1037: /* Resize timer event. */
1.188 nicm 1038: static void
1039: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 1040: {
1.188 nicm 1041: struct window_pane *wp = data;
1042: struct winsize ws;
1043:
1044: evtimer_del(&wp->resize_timer);
1.92 nicm 1045:
1.101 nicm 1046: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 1047: return;
1048:
1049: memset(&ws, 0, sizeof ws);
1050: ws.ws_col = wp->sx;
1051: ws.ws_row = wp->sy;
1052:
1.100 nicm 1053: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 1054: fatal("ioctl failed");
1055:
1056: wp->flags &= ~PANE_RESIZE;
1.188 nicm 1057: }
1058:
1059: /* Check if pane should be resized. */
1.190 nicm 1060: static void
1.188 nicm 1061: server_client_check_resize(struct window_pane *wp)
1062: {
1063: struct timeval tv = { .tv_usec = 250000 };
1064:
1065: if (!(wp->flags & PANE_RESIZE))
1066: return;
1067:
1068: if (!event_initialized(&wp->resize_timer))
1069: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1070:
1071: /*
1072: * The first resize should happen immediately, so if the timer is not
1073: * running, do it now.
1074: */
1075: if (!evtimer_pending(&wp->resize_timer, NULL))
1076: server_client_resize_event(-1, 0, wp);
1077:
1078: /*
1079: * If the pane is in the alternate screen, let the timer expire and
1080: * resize to give the application a chance to redraw. If not, keep
1081: * pushing the timer back.
1082: */
1083: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
1084: return;
1085: evtimer_del(&wp->resize_timer);
1086: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 1087: }
1088:
1089: /* Check whether pane should be focused. */
1.190 nicm 1090: static void
1.91 nicm 1091: server_client_check_focus(struct window_pane *wp)
1092: {
1.93 nicm 1093: struct client *c;
1.102 nicm 1094: int push;
1095:
1096: /* Do we need to push the focus state? */
1097: push = wp->flags & PANE_FOCUSPUSH;
1098: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1099:
1100: /* If we don't care about focus, forget it. */
1101: if (!(wp->base.mode & MODE_FOCUSON))
1102: return;
1103:
1104: /* If we're not the active pane in our window, we're not focused. */
1105: if (wp->window->active != wp)
1106: goto not_focused;
1107:
1108: /* If we're in a mode, we're not focused. */
1109: if (wp->screen != &wp->base)
1110: goto not_focused;
1111:
1112: /*
1.93 nicm 1113: * If our window is the current window in any focused clients with an
1114: * attached session, we're focused.
1.91 nicm 1115: */
1.135 nicm 1116: TAILQ_FOREACH(c, &clients, entry) {
1117: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1118: continue;
1.93 nicm 1119: if (c->session->flags & SESSION_UNATTACHED)
1120: continue;
1121:
1122: if (c->session->curw->window == wp->window)
1.91 nicm 1123: goto focused;
1124: }
1125:
1126: not_focused:
1.102 nicm 1127: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 1128: bufferevent_write(wp->event, "\033[O", 3);
1129: wp->flags &= ~PANE_FOCUSED;
1130: return;
1131:
1132: focused:
1.102 nicm 1133: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 1134: bufferevent_write(wp->event, "\033[I", 3);
1135: wp->flags |= PANE_FOCUSED;
1.2 nicm 1136: }
1137:
1.18 nicm 1138: /*
1139: * Update cursor position and mode settings. The scroll region and attributes
1140: * are cleared when idle (waiting for an event) as this is the most likely time
1141: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1142: * compromise between excessive resets and likelihood of an interrupt.
1143: *
1144: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1145: * things that are already in their default state.
1146: */
1.190 nicm 1147: static void
1.18 nicm 1148: server_client_reset_state(struct client *c)
1.1 nicm 1149: {
1.18 nicm 1150: struct window *w = c->session->curw->window;
1.209 nicm 1151: struct window_pane *wp = w->active, *loop;
1.18 nicm 1152: struct screen *s = wp->screen;
1.163 nicm 1153: struct options *oo = c->session->options;
1.66 nicm 1154: int status, mode, o;
1.60 nicm 1155:
1.186 nicm 1156: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1157: return;
1158:
1.199 nicm 1159: tty_region_off(&c->tty);
1160: tty_margin_off(&c->tty);
1.1 nicm 1161:
1162: status = options_get_number(oo, "status");
1163: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
1164: tty_cursor(&c->tty, 0, 0);
1.66 nicm 1165: else {
1.126 nicm 1166: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 1167: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
1168: }
1.1 nicm 1169:
1.50 nicm 1170: /*
1.131 nicm 1171: * Set mouse mode if requested. To support dragging, always use button
1172: * mode.
1.57 nicm 1173: */
1174: mode = s->mode;
1.209 nicm 1175: if (options_get_number(oo, "mouse")) {
1176: mode &= ~ALL_MOUSE_MODES;
1177: TAILQ_FOREACH(loop, &w->panes, entry) {
1178: if (loop->screen->mode & MODE_MOUSE_ALL)
1179: mode |= MODE_MOUSE_ALL;
1180: }
1181: if (~mode & MODE_MOUSE_ALL)
1182: mode |= MODE_MOUSE_BUTTON;
1183: }
1.215 nicm 1184:
1185: /* Clear bracketed paste mode if at the prompt. */
1186: if (c->prompt_string != NULL)
1187: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1188:
1189: /* Set the terminal mode and reset attributes. */
1.59 nicm 1190: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1191: tty_reset(&c->tty);
1.17 nicm 1192: }
1193:
1194: /* Repeat time callback. */
1.190 nicm 1195: static void
1.170 nicm 1196: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1197: {
1198: struct client *c = data;
1199:
1.85 nicm 1200: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1201: server_client_set_key_table(c, NULL);
1.132 nicm 1202: c->flags &= ~CLIENT_REPEAT;
1203: server_status_client(c);
1.85 nicm 1204: }
1.192 nicm 1205: }
1206:
1207: /* Double-click callback. */
1208: static void
1209: server_client_click_timer(__unused int fd, __unused short events, void *data)
1210: {
1211: struct client *c = data;
1212:
1213: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1214: }
1215:
1.36 nicm 1216: /* Check if client should be exited. */
1.190 nicm 1217: static void
1.36 nicm 1218: server_client_check_exit(struct client *c)
1219: {
1220: if (!(c->flags & CLIENT_EXIT))
1221: return;
1222:
1.73 nicm 1223: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1224: return;
1225: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1226: return;
1.73 nicm 1227: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1228: return;
1229:
1.162 nicm 1230: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1231: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1232: }
1233:
1.218 nicm 1234: /* Redraw timer callback. */
1235: static void
1236: server_client_redraw_timer(__unused int fd, __unused short events,
1237: __unused void* data)
1238: {
1239: log_debug("redraw timer fired");
1240: }
1241:
1.1 nicm 1242: /* Check for client redraws. */
1.190 nicm 1243: static void
1.1 nicm 1244: server_client_check_redraw(struct client *c)
1245: {
1246: struct session *s = c->session;
1.137 nicm 1247: struct tty *tty = &c->tty;
1.1 nicm 1248: struct window_pane *wp;
1.232 nicm 1249: int needed, flags, masked;
1.218 nicm 1250: struct timeval tv = { .tv_usec = 1000 };
1251: static struct event ev;
1252: size_t left;
1.1 nicm 1253:
1.86 nicm 1254: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1255: return;
1.218 nicm 1256:
1257: /*
1258: * If there is outstanding data, defer the redraw until it has been
1259: * consumed. We can just add a timer to get out of the event loop and
1260: * end up back here.
1261: */
1262: needed = 0;
1263: if (c->flags & CLIENT_REDRAW)
1264: needed = 1;
1265: else {
1266: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1267: if (wp->flags & PANE_REDRAW) {
1268: needed = 1;
1269: break;
1270: }
1271: }
1272: }
1273: if (needed) {
1274: left = EVBUFFER_LENGTH(tty->out);
1275: if (left != 0) {
1276: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1277: if (evtimer_initialized(&ev) && evtimer_pending(&ev, NULL))
1278: return;
1279: log_debug("redraw timer started");
1280: evtimer_set(&ev, server_client_redraw_timer, NULL);
1281: evtimer_add(&ev, &tv);
1282:
1283: /*
1284: * We may have got here for a single pane redraw, but
1285: * force a full redraw next time in case other panes
1286: * have been updated.
1287: */
1288: c->flags |= CLIENT_REDRAW;
1289: return;
1290: }
1291: if (evtimer_initialized(&ev))
1292: evtimer_del(&ev);
1293: log_debug("%s: redraw needed", c->name);
1294: }
1.79 nicm 1295:
1.1 nicm 1296: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 1297: if (options_get_number(s->options, "set-titles"))
1.1 nicm 1298: server_client_set_title(c);
1.189 nicm 1299: screen_redraw_update(c); /* will adjust flags */
1.1 nicm 1300: }
1301:
1.219 nicm 1302: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1303: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137 nicm 1304:
1.1 nicm 1305: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 1306: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 1307: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 1308: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 1309: } else {
1310: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1311: if (wp->flags & PANE_REDRAW) {
1312: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1313: screen_redraw_pane(c, wp);
1.137 nicm 1314: }
1.1 nicm 1315: }
1316: }
1317:
1.185 nicm 1318: masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
1319: if (masked != 0)
1.137 nicm 1320: tty_update_mode(tty, tty->mode, NULL);
1.185 nicm 1321: if (masked == CLIENT_BORDERS)
1.115 nicm 1322: screen_redraw_screen(c, 0, 0, 1);
1.185 nicm 1323: else if (masked == CLIENT_STATUS)
1.115 nicm 1324: screen_redraw_screen(c, 0, 1, 0);
1.185 nicm 1325: else if (masked != 0)
1326: screen_redraw_screen(c, 0, 1, 1);
1.1 nicm 1327:
1.137 nicm 1328: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1329: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1330:
1.153 nicm 1331: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
1332: CLIENT_STATUSFORCE);
1.230 nicm 1333:
1334: if (needed) {
1335: /*
1336: * We would have deferred the redraw unless the output buffer
1337: * was empty, so we can record how many bytes the redraw
1338: * generated.
1339: */
1340: c->redraw = EVBUFFER_LENGTH(tty->out);
1341: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1342: }
1.1 nicm 1343: }
1344:
1345: /* Set client title. */
1.190 nicm 1346: static void
1.1 nicm 1347: server_client_set_title(struct client *c)
1348: {
1.128 nicm 1349: struct session *s = c->session;
1350: const char *template;
1351: char *title;
1352: struct format_tree *ft;
1.1 nicm 1353:
1.163 nicm 1354: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1355:
1.228 nicm 1356: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1357: format_defaults(ft, c, NULL, NULL, NULL);
1358:
1359: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 1360: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1361: free(c->title);
1.1 nicm 1362: c->title = xstrdup(title);
1363: tty_set_title(&c->tty, c->title);
1364: }
1.77 nicm 1365: free(title);
1.128 nicm 1366:
1367: format_free(ft);
1.1 nicm 1368: }
1369:
1370: /* Dispatch message from client. */
1.190 nicm 1371: static void
1.162 nicm 1372: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1373: {
1.162 nicm 1374: struct client *c = arg;
1.73 nicm 1375: struct msg_stdin_data stdindata;
1.108 nicm 1376: const char *data;
1.162 nicm 1377: ssize_t datalen;
1.149 nicm 1378: struct session *s;
1.1 nicm 1379:
1.162 nicm 1380: if (c->flags & CLIENT_DEAD)
1381: return;
1382:
1383: if (imsg == NULL) {
1384: server_client_lost(c);
1385: return;
1386: }
1.1 nicm 1387:
1.162 nicm 1388: data = imsg->data;
1389: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1390:
1.162 nicm 1391: switch (imsg->hdr.type) {
1392: case MSG_IDENTIFY_FLAGS:
1393: case MSG_IDENTIFY_TERM:
1394: case MSG_IDENTIFY_TTYNAME:
1395: case MSG_IDENTIFY_CWD:
1396: case MSG_IDENTIFY_STDIN:
1397: case MSG_IDENTIFY_ENVIRON:
1398: case MSG_IDENTIFY_CLIENTPID:
1399: case MSG_IDENTIFY_DONE:
1400: server_client_dispatch_identify(c, imsg);
1401: break;
1402: case MSG_COMMAND:
1403: server_client_dispatch_command(c, imsg);
1404: break;
1405: case MSG_STDIN:
1406: if (datalen != sizeof stdindata)
1407: fatalx("bad MSG_STDIN size");
1408: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1409:
1.162 nicm 1410: if (c->stdin_callback == NULL)
1.33 nicm 1411: break;
1.162 nicm 1412: if (stdindata.size <= 0)
1413: c->stdin_closed = 1;
1414: else {
1415: evbuffer_add(c->stdin_data, stdindata.data,
1416: stdindata.size);
1417: }
1418: c->stdin_callback(c, c->stdin_closed,
1419: c->stdin_callback_data);
1420: break;
1421: case MSG_RESIZE:
1422: if (datalen != 0)
1423: fatalx("bad MSG_RESIZE size");
1.1 nicm 1424:
1.162 nicm 1425: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1426: break;
1.162 nicm 1427: if (tty_resize(&c->tty)) {
1428: recalculate_sizes();
1429: server_redraw_client(c);
1430: }
1.174 nicm 1431: if (c->session != NULL)
1.196 nicm 1432: notify_client("client-resized", c);
1.162 nicm 1433: break;
1434: case MSG_EXITING:
1435: if (datalen != 0)
1436: fatalx("bad MSG_EXITING size");
1.1 nicm 1437:
1.162 nicm 1438: c->session = NULL;
1439: tty_close(&c->tty);
1440: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1441: break;
1442: case MSG_WAKEUP:
1443: case MSG_UNLOCK:
1444: if (datalen != 0)
1445: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1446:
1.162 nicm 1447: if (!(c->flags & CLIENT_SUSPENDED))
1448: break;
1449: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1450:
1.162 nicm 1451: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1452: break;
1.162 nicm 1453: s = c->session;
1.1 nicm 1454:
1.162 nicm 1455: if (gettimeofday(&c->activity_time, NULL) != 0)
1456: fatal("gettimeofday failed");
1457:
1458: tty_start_tty(&c->tty);
1459: server_redraw_client(c);
1460: recalculate_sizes();
1.184 nicm 1461:
1462: if (s != NULL)
1463: session_update_activity(s, &c->activity_time);
1.162 nicm 1464: break;
1465: case MSG_SHELL:
1466: if (datalen != 0)
1467: fatalx("bad MSG_SHELL size");
1.1 nicm 1468:
1.162 nicm 1469: server_client_dispatch_shell(c);
1470: break;
1.1 nicm 1471: }
1472: }
1473:
1.194 nicm 1474: /* Callback when command is done. */
1475: static enum cmd_retval
1.195 nicm 1476: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1477: {
1.195 nicm 1478: struct client *c = item->client;
1.194 nicm 1479:
1480: if (~c->flags & CLIENT_ATTACHED)
1481: c->flags |= CLIENT_EXIT;
1482: return (CMD_RETURN_NORMAL);
1483: }
1484:
1485: /* Show an error message. */
1486: static enum cmd_retval
1.195 nicm 1487: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1488: {
1489: char *error = data;
1490:
1.195 nicm 1491: cmdq_error(item, "%s", error);
1.194 nicm 1492: free(error);
1493:
1494: return (CMD_RETURN_NORMAL);
1495: }
1496:
1.1 nicm 1497: /* Handle command message. */
1.190 nicm 1498: static void
1.162 nicm 1499: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1500: {
1.108 nicm 1501: struct msg_command_data data;
1502: char *buf;
1503: size_t len;
1504: struct cmd_list *cmdlist = NULL;
1505: int argc;
1506: char **argv, *cause;
1507:
1508: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1509: fatalx("bad MSG_COMMAND size");
1510: memcpy(&data, imsg->data, sizeof data);
1511:
1.124 nicm 1512: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1513: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1514: if (len > 0 && buf[len - 1] != '\0')
1515: fatalx("bad MSG_COMMAND string");
1516:
1517: argc = data.argc;
1518: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1519: cause = xstrdup("command too long");
1.1 nicm 1520: goto error;
1521: }
1522:
1523: if (argc == 0) {
1524: argc = 1;
1525: argv = xcalloc(1, sizeof *argv);
1526: *argv = xstrdup("new-session");
1527: }
1528:
1.94 nicm 1529: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1530: cmd_free_argv(argc, argv);
1531: goto error;
1532: }
1533: cmd_free_argv(argc, argv);
1534:
1.194 nicm 1535: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1536: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1537: cmd_list_free(cmdlist);
1538: return;
1539:
1540: error:
1.194 nicm 1541: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1542:
1.1 nicm 1543: if (cmdlist != NULL)
1544: cmd_list_free(cmdlist);
1.88 nicm 1545:
1.36 nicm 1546: c->flags |= CLIENT_EXIT;
1.1 nicm 1547: }
1548:
1549: /* Handle identify message. */
1.190 nicm 1550: static void
1.162 nicm 1551: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1552: {
1.165 nicm 1553: const char *data, *home;
1.232 nicm 1554: size_t datalen;
1.109 nicm 1555: int flags;
1.216 nicm 1556: char *name;
1.109 nicm 1557:
1558: if (c->flags & CLIENT_IDENTIFIED)
1559: fatalx("out-of-order identify message");
1560:
1561: data = imsg->data;
1562: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1563:
1564: switch (imsg->hdr.type) {
1565: case MSG_IDENTIFY_FLAGS:
1566: if (datalen != sizeof flags)
1567: fatalx("bad MSG_IDENTIFY_FLAGS size");
1568: memcpy(&flags, data, sizeof flags);
1569: c->flags |= flags;
1.158 nicm 1570: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1571: break;
1572: case MSG_IDENTIFY_TERM:
1.110 nicm 1573: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1574: fatalx("bad MSG_IDENTIFY_TERM string");
1575: c->term = xstrdup(data);
1.158 nicm 1576: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1577: break;
1578: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1579: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1580: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1581: c->ttyname = xstrdup(data);
1.158 nicm 1582: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1583: break;
1584: case MSG_IDENTIFY_CWD:
1.155 nicm 1585: if (datalen == 0 || data[datalen - 1] != '\0')
1586: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1587: if (access(data, X_OK) == 0)
1588: c->cwd = xstrdup(data);
1589: else if ((home = find_home()) != NULL)
1590: c->cwd = xstrdup(home);
1591: else
1592: c->cwd = xstrdup("/");
1.158 nicm 1593: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1594: break;
1595: case MSG_IDENTIFY_STDIN:
1596: if (datalen != 0)
1597: fatalx("bad MSG_IDENTIFY_STDIN size");
1598: c->fd = imsg->fd;
1.158 nicm 1599: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1600: break;
1601: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1602: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1603: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1604: if (strchr(data, '=') != NULL)
1.164 nicm 1605: environ_put(c->environ, data);
1.158 nicm 1606: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1607: break;
1608: case MSG_IDENTIFY_CLIENTPID:
1609: if (datalen != sizeof c->pid)
1610: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1611: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1612: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1613: break;
1614: default:
1615: break;
1616: }
1617:
1618: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1619: return;
1620: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1621:
1.216 nicm 1622: if (*c->ttyname != '\0')
1623: name = xstrdup(c->ttyname);
1624: else
1625: xasprintf(&name, "client-%ld", (long)c->pid);
1626: c->name = name;
1627: log_debug("client %p name is %s", c, c->name);
1628:
1.109 nicm 1629: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1630: c->stdin_callback = control_callback;
1.109 nicm 1631:
1.97 nicm 1632: evbuffer_free(c->stderr_data);
1633: c->stderr_data = c->stdout_data;
1.109 nicm 1634:
1635: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1636: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1637: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1638:
1639: c->tty.fd = -1;
1640:
1.109 nicm 1641: close(c->fd);
1642: c->fd = -1;
1643:
1.75 nicm 1644: return;
1645: }
1.1 nicm 1646:
1.109 nicm 1647: if (c->fd == -1)
1.104 nicm 1648: return;
1.145 nicm 1649: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1650: close(c->fd);
1651: c->fd = -1;
1.80 nicm 1652: return;
1653: }
1.109 nicm 1654: if (c->flags & CLIENT_UTF8)
1.1 nicm 1655: c->tty.flags |= TTY_UTF8;
1.109 nicm 1656: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1657: c->tty.term_flags |= TERM_256COLOURS;
1658:
1659: tty_resize(&c->tty);
1660:
1.109 nicm 1661: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1662: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1663: }
1664:
1665: /* Handle shell message. */
1.190 nicm 1666: static void
1.162 nicm 1667: server_client_dispatch_shell(struct client *c)
1.1 nicm 1668: {
1.107 nicm 1669: const char *shell;
1.25 nicm 1670:
1.163 nicm 1671: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1672: if (*shell == '\0' || areshell(shell))
1673: shell = _PATH_BSHELL;
1.162 nicm 1674: proc_send_s(c->peer, MSG_SHELL, shell);
1.25 nicm 1675:
1.162 nicm 1676: proc_kill_peer(c->peer);
1.169 nicm 1677: }
1678:
1679: /* Event callback to push more stdout data if any left. */
1680: static void
1.170 nicm 1681: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1682: {
1683: struct client *c = arg;
1684:
1685: if (~c->flags & CLIENT_DEAD)
1686: server_client_push_stdout(c);
1687: server_client_unref(c);
1688: }
1689:
1690: /* Push stdout to client if possible. */
1691: void
1692: server_client_push_stdout(struct client *c)
1693: {
1694: struct msg_stdout_data data;
1.232 nicm 1695: size_t sent, left;
1.169 nicm 1696:
1697: left = EVBUFFER_LENGTH(c->stdout_data);
1698: while (left != 0) {
1699: sent = left;
1700: if (sent > sizeof data.data)
1701: sent = sizeof data.data;
1702: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1703: data.size = sent;
1704:
1705: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1706: break;
1707: evbuffer_drain(c->stdout_data, sent);
1708:
1709: left = EVBUFFER_LENGTH(c->stdout_data);
1710: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1711: sent, left);
1712: }
1713: if (left != 0) {
1714: c->references++;
1715: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1716: log_debug("%s: client %p, queued", __func__, c);
1717: }
1718: }
1719:
1720: /* Event callback to push more stderr data if any left. */
1721: static void
1.170 nicm 1722: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1723: {
1724: struct client *c = arg;
1725:
1726: if (~c->flags & CLIENT_DEAD)
1727: server_client_push_stderr(c);
1728: server_client_unref(c);
1729: }
1730:
1731: /* Push stderr to client if possible. */
1732: void
1733: server_client_push_stderr(struct client *c)
1734: {
1735: struct msg_stderr_data data;
1.232 nicm 1736: size_t sent, left;
1.169 nicm 1737:
1738: if (c->stderr_data == c->stdout_data) {
1739: server_client_push_stdout(c);
1740: return;
1741: }
1742:
1743: left = EVBUFFER_LENGTH(c->stderr_data);
1744: while (left != 0) {
1745: sent = left;
1746: if (sent > sizeof data.data)
1747: sent = sizeof data.data;
1748: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1749: data.size = sent;
1750:
1751: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1752: break;
1753: evbuffer_drain(c->stderr_data, sent);
1754:
1755: left = EVBUFFER_LENGTH(c->stderr_data);
1756: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1757: sent, left);
1758: }
1759: if (left != 0) {
1760: c->references++;
1761: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1762: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 1763: }
1764: }
1765:
1766: /* Add to client message log. */
1767: void
1768: server_client_add_message(struct client *c, const char *fmt, ...)
1769: {
1770: struct message_entry *msg, *msg1;
1771: char *s;
1772: va_list ap;
1773: u_int limit;
1774:
1775: va_start(ap, fmt);
1776: xvasprintf(&s, fmt, ap);
1777: va_end(ap);
1778:
1.216 nicm 1779: log_debug("message %s (client %p)", s, c);
1.212 nicm 1780:
1781: msg = xcalloc(1, sizeof *msg);
1782: msg->msg_time = time(NULL);
1783: msg->msg_num = c->message_next++;
1784: msg->msg = s;
1785: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1786:
1787: limit = options_get_number(global_options, "message-limit");
1788: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1789: if (msg->msg_num + limit >= c->message_next)
1790: break;
1791: free(msg->msg);
1792: TAILQ_REMOVE(&c->message_log, msg, entry);
1793: free(msg);
1.169 nicm 1794: }
1.213 nicm 1795: }
1796:
1797: /* Get client working directory. */
1798: const char *
1799: server_client_get_cwd(struct client *c)
1800: {
1801: struct session *s;
1802:
1803: if (c != NULL && c->session == NULL && c->cwd != NULL)
1804: return (c->cwd);
1805: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
1806: return (s->cwd);
1807: return (".");
1808: }
1809:
1810: /* Resolve an absolute path or relative to client working directory. */
1811: char *
1812: server_client_get_path(struct client *c, const char *file)
1813: {
1814: char *path, resolved[PATH_MAX];
1815:
1816: if (*file == '/')
1817: path = xstrdup(file);
1818: else
1819: xasprintf(&path, "%s/%s", server_client_get_cwd(c), file);
1820: if (realpath(path, resolved) == NULL)
1821: return (path);
1822: free(path);
1823: return (xstrdup(resolved));
1.1 nicm 1824: }