Move the user-visible parts of all options (names, types, limit, default
[tmux-openbsd.git] / session.c
blobfc0d9f071faf4ddce6507a6a1cce3b7dcae81592
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 s->idx = next_session++;
123 if (name != NULL)
124 s->name = xstrdup(name);
125 else
126 xasprintf(&s->name, "%u", s->idx);
127 RB_INSERT(sessions, &sessions, s);
129 if (cmd != NULL) {
130 if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
131 session_destroy(s);
132 return (NULL);
134 session_select(s, RB_ROOT(&s->windows)->idx);
137 log_debug("session %s created", s->name);
139 return (s);
142 /* Destroy a session. */
143 void
144 session_destroy(struct session *s)
146 log_debug("session %s destroyed", s->name);
148 RB_REMOVE(sessions, &sessions, s);
150 if (s->tio != NULL)
151 xfree(s->tio);
153 session_group_remove(s);
154 environ_free(&s->environ);
155 options_free(&s->options);
157 while (!TAILQ_EMPTY(&s->lastw))
158 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
159 while (!RB_EMPTY(&s->windows))
160 winlink_remove(&s->windows, RB_ROOT(&s->windows));
162 xfree(s->cwd);
164 RB_INSERT(sessions, &dead_sessions, s);
167 /* Update session active time. */
168 void
169 session_update_activity(struct session *s)
171 if (gettimeofday(&s->activity_time, NULL) != 0)
172 fatal("gettimeofday");
175 /* Find the next usable session. */
176 struct session *
177 session_next_session(struct session *s)
179 struct session *s2;
181 if (RB_EMPTY(&sessions) || !session_alive(s))
182 return (NULL);
184 s2 = s;
185 do {
186 s2 = RB_NEXT(sessions, &sessions, s2);
187 if (s2 == NULL)
188 s2 = RB_MIN(sessions, &sessions);
189 } while (s2 != s);
190 if (s2 == s)
191 return (NULL);
192 return (s2);
195 /* Find the previous usable session. */
196 struct session *
197 session_previous_session(struct session *s)
199 struct session *s2;
201 if (RB_EMPTY(&sessions) || !session_alive(s))
202 return (NULL);
204 s2 = s;
205 do {
206 s2 = RB_PREV(sessions, &sessions, s2);
207 if (s2 == NULL)
208 s2 = RB_MAX(sessions, &sessions);
209 } while (s2 != s);
210 if (s2 == s)
211 return (NULL);
212 return (s2);
215 /* Create a new window on a session. */
216 struct winlink *
217 session_new(struct session *s,
218 const char *name, const char *cmd, const char *cwd, int idx, char **cause)
220 struct window *w;
221 struct environ env;
222 const char *shell;
223 u_int hlimit;
225 environ_init(&env);
226 environ_copy(&global_environ, &env);
227 environ_copy(&s->environ, &env);
228 server_fill_environ(s, &env);
230 shell = options_get_string(&s->options, "default-shell");
231 if (*shell == '\0' || areshell(shell))
232 shell = _PATH_BSHELL;
234 hlimit = options_get_number(&s->options, "history-limit");
235 w = window_create(
236 name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
237 if (w == NULL) {
238 environ_free(&env);
239 return (NULL);
241 environ_free(&env);
243 if (options_get_number(&s->options, "set-remain-on-exit"))
244 options_set_number(&w->options, "remain-on-exit", 1);
246 return (session_attach(s, w, idx, cause));
249 /* Attach a window to a session. */
250 struct winlink *
251 session_attach(struct session *s, struct window *w, int idx, char **cause)
253 struct winlink *wl;
255 if ((wl = winlink_add(&s->windows, w, idx)) == NULL)
256 xasprintf(cause, "index in use: %d", idx);
257 session_group_synchronize_from(s);
258 return (wl);
261 /* Detach a window from a session. */
263 session_detach(struct session *s, struct winlink *wl)
265 if (s->curw == wl &&
266 session_last(s) != 0 && session_previous(s, 0) != 0)
267 session_next(s, 0);
269 wl->flags &= ~WINLINK_ALERTFLAGS;
270 winlink_stack_remove(&s->lastw, wl);
271 winlink_remove(&s->windows, wl);
272 session_group_synchronize_from(s);
273 if (RB_EMPTY(&s->windows)) {
274 session_destroy(s);
275 return (1);
277 return (0);
280 /* Return if session has window. */
281 struct winlink *
282 session_has(struct session *s, struct window *w)
284 struct winlink *wl;
286 RB_FOREACH(wl, winlinks, &s->windows) {
287 if (wl->window == w)
288 return (wl);
290 return (NULL);
293 struct winlink *
294 session_next_alert(struct winlink *wl)
296 while (wl != NULL) {
297 if (wl->flags & WINLINK_ALERTFLAGS)
298 break;
299 wl = winlink_next(wl);
301 return (wl);
304 /* Move session to next window. */
306 session_next(struct session *s, int alert)
308 struct winlink *wl;
310 if (s->curw == NULL)
311 return (-1);
313 wl = winlink_next(s->curw);
314 if (alert)
315 wl = session_next_alert(wl);
316 if (wl == NULL) {
317 wl = RB_MIN(winlinks, &s->windows);
318 if (alert && ((wl = session_next_alert(wl)) == NULL))
319 return (-1);
321 if (wl == s->curw)
322 return (1);
323 winlink_stack_remove(&s->lastw, wl);
324 winlink_stack_push(&s->lastw, s->curw);
325 s->curw = wl;
326 wl->flags &= ~WINLINK_ALERTFLAGS;
327 return (0);
330 struct winlink *
331 session_previous_alert(struct winlink *wl)
333 while (wl != NULL) {
334 if (wl->flags & WINLINK_ALERTFLAGS)
335 break;
336 wl = winlink_previous(wl);
338 return (wl);
341 /* Move session to previous window. */
343 session_previous(struct session *s, int alert)
345 struct winlink *wl;
347 if (s->curw == NULL)
348 return (-1);
350 wl = winlink_previous(s->curw);
351 if (alert)
352 wl = session_previous_alert(wl);
353 if (wl == NULL) {
354 wl = RB_MAX(winlinks, &s->windows);
355 if (alert && (wl = session_previous_alert(wl)) == NULL)
356 return (-1);
358 if (wl == s->curw)
359 return (1);
360 winlink_stack_remove(&s->lastw, wl);
361 winlink_stack_push(&s->lastw, s->curw);
362 s->curw = wl;
363 wl->flags &= ~WINLINK_ALERTFLAGS;
364 return (0);
367 /* Move session to specific window. */
369 session_select(struct session *s, int idx)
371 struct winlink *wl;
373 wl = winlink_find_by_index(&s->windows, idx);
374 if (wl == NULL)
375 return (-1);
376 if (wl == s->curw)
377 return (1);
378 winlink_stack_remove(&s->lastw, wl);
379 winlink_stack_push(&s->lastw, s->curw);
380 s->curw = wl;
381 wl->flags &= ~WINLINK_ALERTFLAGS;
382 return (0);
385 /* Move session to last used window. */
387 session_last(struct session *s)
389 struct winlink *wl;
391 wl = TAILQ_FIRST(&s->lastw);
392 if (wl == NULL)
393 return (-1);
394 if (wl == s->curw)
395 return (1);
397 winlink_stack_remove(&s->lastw, wl);
398 winlink_stack_push(&s->lastw, s->curw);
399 s->curw = wl;
400 wl->flags &= ~WINLINK_ALERTFLAGS;
401 return (0);
404 /* Find the session group containing a session. */
405 struct session_group *
406 session_group_find(struct session *target)
408 struct session_group *sg;
409 struct session *s;
411 TAILQ_FOREACH(sg, &session_groups, entry) {
412 TAILQ_FOREACH(s, &sg->sessions, gentry) {
413 if (s == target)
414 return (sg);
417 return (NULL);
420 /* Find session group index. */
421 u_int
422 session_group_index(struct session_group *sg)
424 struct session_group *sg2;
425 u_int i;
427 i = 0;
428 TAILQ_FOREACH(sg2, &session_groups, entry) {
429 if (sg == sg2)
430 return (i);
431 i++;
434 fatalx("session group not found");
438 * Add a session to the session group containing target, creating it if
439 * necessary.
441 void
442 session_group_add(struct session *target, struct session *s)
444 struct session_group *sg;
446 if ((sg = session_group_find(target)) == NULL) {
447 sg = xmalloc(sizeof *sg);
448 TAILQ_INSERT_TAIL(&session_groups, sg, entry);
449 TAILQ_INIT(&sg->sessions);
450 TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
452 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
455 /* Remove a session from its group and destroy the group if empty. */
456 void
457 session_group_remove(struct session *s)
459 struct session_group *sg;
461 if ((sg = session_group_find(s)) == NULL)
462 return;
463 TAILQ_REMOVE(&sg->sessions, s, gentry);
464 if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
465 TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
466 if (TAILQ_EMPTY(&sg->sessions)) {
467 TAILQ_REMOVE(&session_groups, sg, entry);
468 xfree(sg);
472 /* Synchronize a session to its session group. */
473 void
474 session_group_synchronize_to(struct session *s)
476 struct session_group *sg;
477 struct session *target;
479 if ((sg = session_group_find(s)) == NULL)
480 return;
482 target = NULL;
483 TAILQ_FOREACH(target, &sg->sessions, gentry) {
484 if (target != s)
485 break;
487 session_group_synchronize1(target, s);
490 /* Synchronize a session group to a session. */
491 void
492 session_group_synchronize_from(struct session *target)
494 struct session_group *sg;
495 struct session *s;
497 if ((sg = session_group_find(target)) == NULL)
498 return;
500 TAILQ_FOREACH(s, &sg->sessions, gentry) {
501 if (s != target)
502 session_group_synchronize1(target, s);
507 * Synchronize a session with a target session. This means destroying all
508 * winlinks then recreating them, then updating the current window, last window
509 * stack and alerts.
511 void
512 session_group_synchronize1(struct session *target, struct session *s)
514 struct winlinks old_windows, *ww;
515 struct winlink_stack old_lastw;
516 struct winlink *wl, *wl2;
518 /* Don't do anything if the session is empty (it'll be destroyed). */
519 ww = &target->windows;
520 if (RB_EMPTY(ww))
521 return;
523 /* If the current window has vanished, move to the next now. */
524 if (s->curw != NULL &&
525 winlink_find_by_index(ww, s->curw->idx) == NULL &&
526 session_last(s) != 0 && session_previous(s, 0) != 0)
527 session_next(s, 0);
529 /* Save the old pointer and reset it. */
530 memcpy(&old_windows, &s->windows, sizeof old_windows);
531 RB_INIT(&s->windows);
533 /* Link all the windows from the target. */
534 RB_FOREACH(wl, winlinks, ww) {
535 wl2 = winlink_add(&s->windows, wl->window, wl->idx);
536 wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
539 /* Fix up the current window. */
540 if (s->curw != NULL)
541 s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
542 else
543 s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
545 /* Fix up the last window stack. */
546 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
547 TAILQ_INIT(&s->lastw);
548 TAILQ_FOREACH(wl, &old_lastw, sentry) {
549 wl2 = winlink_find_by_index(&s->windows, wl->idx);
550 if (wl2 != NULL)
551 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
554 /* Then free the old winlinks list. */
555 while (!RB_EMPTY(&old_windows)) {
556 wl = RB_ROOT(&old_windows);
557 winlink_remove(&old_windows, wl);