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