Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / session.c
blob2545d868cb4863df56ad66c50b6fb451c1b49bdc
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
22 #include <paths.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <time.h>
28 #include "tmux.h"
30 /* Global session list. */
31 struct sessions sessions;
32 struct sessions dead_sessions;
33 u_int next_session;
34 struct session_groups session_groups;
36 struct winlink *session_next_alert(struct winlink *);
37 struct winlink *session_previous_alert(struct winlink *);
39 RB_GENERATE(sessions, session, entry, session_cmp);
41 int
42 session_cmp(struct session *s1, struct session *s2)
44 return (strcmp(s1->name, s2->name));
48 * Find if session is still alive. This is true if it is still on the global
49 * sessions list.
51 int
52 session_alive(struct session *s)
54 struct session *s_loop;
56 RB_FOREACH(s_loop, sessions, &sessions) {
57 if (s_loop == s)
58 return (1);
60 return (0);
63 /* Find session by name. */
64 struct session *
65 session_find(const char *name)
67 struct session s;
69 s.name = (char *) name;
70 return (RB_FIND(sessions, &sessions, &s));
73 /* Find session by index. */
74 struct session *
75 session_find_by_index(u_int idx)
77 struct session *s;
79 RB_FOREACH(s, sessions, &sessions) {
80 if (s->idx == idx)
81 return (s);
83 return (NULL);
86 /* Create a new session. */
87 struct session *
88 session_create(const char *name, const char *cmd, const char *cwd,
89 struct environ *env, struct termios *tio, int idx, u_int sx, u_int sy,
90 char **cause)
92 struct session *s;
94 s = xmalloc(sizeof *s);
95 s->references = 0;
96 s->flags = 0;
98 if (gettimeofday(&s->creation_time, NULL) != 0)
99 fatal("gettimeofday failed");
100 session_update_activity(s);
102 s->cwd = xstrdup(cwd);
104 s->curw = NULL;
105 TAILQ_INIT(&s->lastw);
106 RB_INIT(&s->windows);
108 options_init(&s->options, &global_s_options);
109 environ_init(&s->environ);
110 if (env != NULL)
111 environ_copy(env, &s->environ);
113 s->tio = NULL;
114 if (tio != NULL) {
115 s->tio = xmalloc(sizeof *s->tio);
116 memcpy(s->tio, tio, sizeof *s->tio);
119 s->sx = sx;
120 s->sy = sy;
122 if (name != NULL) {
123 s->name = xstrdup(name);
124 s->idx = next_session++;
125 } else {
126 s->name = NULL;
127 do {
128 s->idx = next_session++;
129 if (s->name != NULL)
130 xfree (s->name);
131 xasprintf(&s->name, "%u", s->idx);
132 } while (RB_FIND(sessions, &sessions, s) != NULL);
134 RB_INSERT(sessions, &sessions, s);
136 if (cmd != NULL) {
137 if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
138 session_destroy(s);
139 return (NULL);
141 session_select(s, RB_ROOT(&s->windows)->idx);
144 log_debug("session %s created", s->name);
146 return (s);
149 /* Destroy a session. */
150 void
151 session_destroy(struct session *s)
153 log_debug("session %s destroyed", s->name);
155 RB_REMOVE(sessions, &sessions, s);
157 if (s->tio != NULL)
158 xfree(s->tio);
160 session_group_remove(s);
161 environ_free(&s->environ);
162 options_free(&s->options);
164 while (!TAILQ_EMPTY(&s->lastw))
165 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
166 while (!RB_EMPTY(&s->windows))
167 winlink_remove(&s->windows, RB_ROOT(&s->windows));
169 xfree(s->cwd);
171 RB_INSERT(sessions, &dead_sessions, s);
174 /* Check a session name is valid: not empty and no colons. */
176 session_check_name(const char *name)
178 return (*name != '\0' && strchr(name, ':') == NULL);
181 /* Update session active time. */
182 void
183 session_update_activity(struct session *s)
185 if (gettimeofday(&s->activity_time, NULL) != 0)
186 fatal("gettimeofday");
189 /* Find the next usable session. */
190 struct session *
191 session_next_session(struct session *s)
193 struct session *s2;
195 if (RB_EMPTY(&sessions) || !session_alive(s))
196 return (NULL);
198 s2 = RB_NEXT(sessions, &sessions, s);
199 if (s2 == NULL)
200 s2 = RB_MIN(sessions, &sessions);
201 if (s2 == s)
202 return (NULL);
203 return (s2);
206 /* Find the previous usable session. */
207 struct session *
208 session_previous_session(struct session *s)
210 struct session *s2;
212 if (RB_EMPTY(&sessions) || !session_alive(s))
213 return (NULL);
215 s2 = RB_PREV(sessions, &sessions, s);
216 if (s2 == NULL)
217 s2 = RB_MAX(sessions, &sessions);
218 if (s2 == s)
219 return (NULL);
220 return (s2);
223 /* Create a new window on a session. */
224 struct winlink *
225 session_new(struct session *s,
226 const char *name, const char *cmd, const char *cwd, int idx, char **cause)
228 struct window *w;
229 struct winlink *wl;
230 struct environ env;
231 const char *shell;
232 u_int hlimit;
234 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
235 xasprintf(cause, "index in use: %d", idx);
236 return (NULL);
239 environ_init(&env);
240 environ_copy(&global_environ, &env);
241 environ_copy(&s->environ, &env);
242 server_fill_environ(s, &env);
244 shell = options_get_string(&s->options, "default-shell");
245 if (*shell == '\0' || areshell(shell))
246 shell = _PATH_BSHELL;
248 hlimit = options_get_number(&s->options, "history-limit");
249 w = window_create(
250 name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
251 if (w == NULL) {
252 winlink_remove(&s->windows, wl);
253 environ_free(&env);
254 return (NULL);
256 winlink_set_window(wl, w);
257 environ_free(&env);
259 if (options_get_number(&s->options, "set-remain-on-exit"))
260 options_set_number(&w->options, "remain-on-exit", 1);
262 session_group_synchronize_from(s);
263 return (wl);
266 /* Attach a window to a session. */
267 struct winlink *
268 session_attach(struct session *s, struct window *w, int idx, char **cause)
270 struct winlink *wl;
272 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
273 xasprintf(cause, "index in use: %d", idx);
274 return (NULL);
276 winlink_set_window(wl, w);
278 session_group_synchronize_from(s);
279 return (wl);
282 /* Detach a window from a session. */
284 session_detach(struct session *s, struct winlink *wl)
286 if (s->curw == wl &&
287 session_last(s) != 0 && session_previous(s, 0) != 0)
288 session_next(s, 0);
290 wl->flags &= ~WINLINK_ALERTFLAGS;
291 winlink_stack_remove(&s->lastw, wl);
292 winlink_remove(&s->windows, wl);
293 session_group_synchronize_from(s);
294 if (RB_EMPTY(&s->windows)) {
295 session_destroy(s);
296 return (1);
298 return (0);
301 /* Return if session has window. */
302 struct winlink *
303 session_has(struct session *s, struct window *w)
305 struct winlink *wl;
307 RB_FOREACH(wl, winlinks, &s->windows) {
308 if (wl->window == w)
309 return (wl);
311 return (NULL);
314 struct winlink *
315 session_next_alert(struct winlink *wl)
317 while (wl != NULL) {
318 if (wl->flags & WINLINK_ALERTFLAGS)
319 break;
320 wl = winlink_next(wl);
322 return (wl);
325 /* Move session to next window. */
327 session_next(struct session *s, int alert)
329 struct winlink *wl;
331 if (s->curw == NULL)
332 return (-1);
334 wl = winlink_next(s->curw);
335 if (alert)
336 wl = session_next_alert(wl);
337 if (wl == NULL) {
338 wl = RB_MIN(winlinks, &s->windows);
339 if (alert && ((wl = session_next_alert(wl)) == NULL))
340 return (-1);
342 if (wl == s->curw)
343 return (1);
344 winlink_stack_remove(&s->lastw, wl);
345 winlink_stack_push(&s->lastw, s->curw);
346 s->curw = wl;
347 wl->flags &= ~WINLINK_ALERTFLAGS;
348 return (0);
351 struct winlink *
352 session_previous_alert(struct winlink *wl)
354 while (wl != NULL) {
355 if (wl->flags & WINLINK_ALERTFLAGS)
356 break;
357 wl = winlink_previous(wl);
359 return (wl);
362 /* Move session to previous window. */
364 session_previous(struct session *s, int alert)
366 struct winlink *wl;
368 if (s->curw == NULL)
369 return (-1);
371 wl = winlink_previous(s->curw);
372 if (alert)
373 wl = session_previous_alert(wl);
374 if (wl == NULL) {
375 wl = RB_MAX(winlinks, &s->windows);
376 if (alert && (wl = session_previous_alert(wl)) == NULL)
377 return (-1);
379 if (wl == s->curw)
380 return (1);
381 winlink_stack_remove(&s->lastw, wl);
382 winlink_stack_push(&s->lastw, s->curw);
383 s->curw = wl;
384 wl->flags &= ~WINLINK_ALERTFLAGS;
385 return (0);
388 /* Move session to specific window. */
390 session_select(struct session *s, int idx)
392 struct winlink *wl;
394 wl = winlink_find_by_index(&s->windows, idx);
395 if (wl == NULL)
396 return (-1);
397 if (wl == s->curw)
398 return (1);
399 winlink_stack_remove(&s->lastw, wl);
400 winlink_stack_push(&s->lastw, s->curw);
401 s->curw = wl;
402 wl->flags &= ~WINLINK_ALERTFLAGS;
403 return (0);
406 /* Move session to last used window. */
408 session_last(struct session *s)
410 struct winlink *wl;
412 wl = TAILQ_FIRST(&s->lastw);
413 if (wl == NULL)
414 return (-1);
415 if (wl == s->curw)
416 return (1);
418 winlink_stack_remove(&s->lastw, wl);
419 winlink_stack_push(&s->lastw, s->curw);
420 s->curw = wl;
421 wl->flags &= ~WINLINK_ALERTFLAGS;
422 return (0);
425 /* Find the session group containing a session. */
426 struct session_group *
427 session_group_find(struct session *target)
429 struct session_group *sg;
430 struct session *s;
432 TAILQ_FOREACH(sg, &session_groups, entry) {
433 TAILQ_FOREACH(s, &sg->sessions, gentry) {
434 if (s == target)
435 return (sg);
438 return (NULL);
441 /* Find session group index. */
442 u_int
443 session_group_index(struct session_group *sg)
445 struct session_group *sg2;
446 u_int i;
448 i = 0;
449 TAILQ_FOREACH(sg2, &session_groups, entry) {
450 if (sg == sg2)
451 return (i);
452 i++;
455 fatalx("session group not found");
459 * Add a session to the session group containing target, creating it if
460 * necessary.
462 void
463 session_group_add(struct session *target, struct session *s)
465 struct session_group *sg;
467 if ((sg = session_group_find(target)) == NULL) {
468 sg = xmalloc(sizeof *sg);
469 TAILQ_INSERT_TAIL(&session_groups, sg, entry);
470 TAILQ_INIT(&sg->sessions);
471 TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
473 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
476 /* Remove a session from its group and destroy the group if empty. */
477 void
478 session_group_remove(struct session *s)
480 struct session_group *sg;
482 if ((sg = session_group_find(s)) == NULL)
483 return;
484 TAILQ_REMOVE(&sg->sessions, s, gentry);
485 if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
486 TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
487 if (TAILQ_EMPTY(&sg->sessions)) {
488 TAILQ_REMOVE(&session_groups, sg, entry);
489 xfree(sg);
493 /* Synchronize a session to its session group. */
494 void
495 session_group_synchronize_to(struct session *s)
497 struct session_group *sg;
498 struct session *target;
500 if ((sg = session_group_find(s)) == NULL)
501 return;
503 target = NULL;
504 TAILQ_FOREACH(target, &sg->sessions, gentry) {
505 if (target != s)
506 break;
508 session_group_synchronize1(target, s);
511 /* Synchronize a session group to a session. */
512 void
513 session_group_synchronize_from(struct session *target)
515 struct session_group *sg;
516 struct session *s;
518 if ((sg = session_group_find(target)) == NULL)
519 return;
521 TAILQ_FOREACH(s, &sg->sessions, gentry) {
522 if (s != target)
523 session_group_synchronize1(target, s);
528 * Synchronize a session with a target session. This means destroying all
529 * winlinks then recreating them, then updating the current window, last window
530 * stack and alerts.
532 void
533 session_group_synchronize1(struct session *target, struct session *s)
535 struct winlinks old_windows, *ww;
536 struct winlink_stack old_lastw;
537 struct winlink *wl, *wl2;
539 /* Don't do anything if the session is empty (it'll be destroyed). */
540 ww = &target->windows;
541 if (RB_EMPTY(ww))
542 return;
544 /* If the current window has vanished, move to the next now. */
545 if (s->curw != NULL &&
546 winlink_find_by_index(ww, s->curw->idx) == NULL &&
547 session_last(s) != 0 && session_previous(s, 0) != 0)
548 session_next(s, 0);
550 /* Save the old pointer and reset it. */
551 memcpy(&old_windows, &s->windows, sizeof old_windows);
552 RB_INIT(&s->windows);
554 /* Link all the windows from the target. */
555 RB_FOREACH(wl, winlinks, ww) {
556 wl2 = winlink_add(&s->windows, wl->idx);
557 winlink_set_window(wl2, wl->window);
558 wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
561 /* Fix up the current window. */
562 if (s->curw != NULL)
563 s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
564 else
565 s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
567 /* Fix up the last window stack. */
568 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
569 TAILQ_INIT(&s->lastw);
570 TAILQ_FOREACH(wl, &old_lastw, sentry) {
571 wl2 = winlink_find_by_index(&s->windows, wl->idx);
572 if (wl2 != NULL)
573 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
576 /* Then free the old winlinks list. */
577 while (!RB_EMPTY(&old_windows)) {
578 wl = RB_ROOT(&old_windows);
579 winlink_remove(&old_windows, wl);