Fix compiler warnings, missing #include. From Thomas Adam.
[tmux-openbsd.git] / session.c
blobc7b54a2b3d3a66098d023f093db5f89a66649b1d
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_id;
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 id. */
74 struct session *
75 session_find_by_id(u_int id)
77 struct session *s;
79 RB_FOREACH(s, sessions, &sessions) {
80 if (s->id == id)
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->id = next_session_id++;
125 } else {
126 s->name = NULL;
127 do {
128 s->id = next_session_id++;
129 free (s->name);
130 xasprintf(&s->name, "%u", s->id);
131 } while (RB_FIND(sessions, &sessions, s) != NULL);
133 RB_INSERT(sessions, &sessions, s);
135 if (cmd != NULL) {
136 if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
137 session_destroy(s);
138 return (NULL);
140 session_select(s, RB_ROOT(&s->windows)->idx);
143 log_debug("session %s created", s->name);
144 notify_session_created(s);
146 return (s);
149 /* Destroy a session. */
150 void
151 session_destroy(struct session *s)
153 struct winlink *wl;
154 log_debug("session %s destroyed", s->name);
156 RB_REMOVE(sessions, &sessions, s);
157 notify_session_closed(s);
159 free(s->tio);
161 session_group_remove(s);
162 environ_free(&s->environ);
163 options_free(&s->options);
165 while (!TAILQ_EMPTY(&s->lastw))
166 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
167 while (!RB_EMPTY(&s->windows)) {
168 wl = RB_ROOT(&s->windows);
169 notify_window_unlinked(s, wl->window);
170 winlink_remove(&s->windows, wl);
173 free(s->cwd);
175 RB_INSERT(sessions, &dead_sessions, s);
178 /* Check a session name is valid: not empty and no colons. */
180 session_check_name(const char *name)
182 return (*name != '\0' && strchr(name, ':') == NULL);
185 /* Update session active time. */
186 void
187 session_update_activity(struct session *s)
189 if (gettimeofday(&s->activity_time, NULL) != 0)
190 fatal("gettimeofday");
193 /* Find the next usable session. */
194 struct session *
195 session_next_session(struct session *s)
197 struct session *s2;
199 if (RB_EMPTY(&sessions) || !session_alive(s))
200 return (NULL);
202 s2 = RB_NEXT(sessions, &sessions, s);
203 if (s2 == NULL)
204 s2 = RB_MIN(sessions, &sessions);
205 if (s2 == s)
206 return (NULL);
207 return (s2);
210 /* Find the previous usable session. */
211 struct session *
212 session_previous_session(struct session *s)
214 struct session *s2;
216 if (RB_EMPTY(&sessions) || !session_alive(s))
217 return (NULL);
219 s2 = RB_PREV(sessions, &sessions, s);
220 if (s2 == NULL)
221 s2 = RB_MAX(sessions, &sessions);
222 if (s2 == s)
223 return (NULL);
224 return (s2);
227 /* Create a new window on a session. */
228 struct winlink *
229 session_new(struct session *s,
230 const char *name, const char *cmd, const char *cwd, int idx, char **cause)
232 struct window *w;
233 struct winlink *wl;
234 struct environ env;
235 const char *shell;
236 u_int hlimit;
238 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
239 xasprintf(cause, "index in use: %d", idx);
240 return (NULL);
243 environ_init(&env);
244 environ_copy(&global_environ, &env);
245 environ_copy(&s->environ, &env);
246 server_fill_environ(s, &env);
248 shell = options_get_string(&s->options, "default-shell");
249 if (*shell == '\0' || areshell(shell))
250 shell = _PATH_BSHELL;
252 hlimit = options_get_number(&s->options, "history-limit");
253 w = window_create(
254 name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
255 if (w == NULL) {
256 winlink_remove(&s->windows, wl);
257 environ_free(&env);
258 return (NULL);
260 winlink_set_window(wl, w);
261 notify_window_linked(s, w);
262 environ_free(&env);
264 if (options_get_number(&s->options, "set-remain-on-exit"))
265 options_set_number(&w->options, "remain-on-exit", 1);
267 session_group_synchronize_from(s);
268 return (wl);
271 /* Attach a window to a session. */
272 struct winlink *
273 session_attach(struct session *s, struct window *w, int idx, char **cause)
275 struct winlink *wl;
277 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
278 xasprintf(cause, "index in use: %d", idx);
279 return (NULL);
281 winlink_set_window(wl, w);
282 notify_window_linked(s, w);
284 session_group_synchronize_from(s);
285 return (wl);
288 /* Detach a window from a session. */
290 session_detach(struct session *s, struct winlink *wl)
292 if (s->curw == wl &&
293 session_last(s) != 0 && session_previous(s, 0) != 0)
294 session_next(s, 0);
296 wl->flags &= ~WINLINK_ALERTFLAGS;
297 notify_window_unlinked(s, wl->window);
298 winlink_stack_remove(&s->lastw, wl);
299 winlink_remove(&s->windows, wl);
300 session_group_synchronize_from(s);
301 if (RB_EMPTY(&s->windows)) {
302 session_destroy(s);
303 return (1);
305 return (0);
308 /* Return if session has window. */
309 struct winlink *
310 session_has(struct session *s, struct window *w)
312 struct winlink *wl;
314 RB_FOREACH(wl, winlinks, &s->windows) {
315 if (wl->window == w)
316 return (wl);
318 return (NULL);
321 struct winlink *
322 session_next_alert(struct winlink *wl)
324 while (wl != NULL) {
325 if (wl->flags & WINLINK_ALERTFLAGS)
326 break;
327 wl = winlink_next(wl);
329 return (wl);
332 /* Move session to next window. */
334 session_next(struct session *s, int alert)
336 struct winlink *wl;
338 if (s->curw == NULL)
339 return (-1);
341 wl = winlink_next(s->curw);
342 if (alert)
343 wl = session_next_alert(wl);
344 if (wl == NULL) {
345 wl = RB_MIN(winlinks, &s->windows);
346 if (alert && ((wl = session_next_alert(wl)) == NULL))
347 return (-1);
349 return (session_set_current(s, wl));
352 struct winlink *
353 session_previous_alert(struct winlink *wl)
355 while (wl != NULL) {
356 if (wl->flags & WINLINK_ALERTFLAGS)
357 break;
358 wl = winlink_previous(wl);
360 return (wl);
363 /* Move session to previous window. */
365 session_previous(struct session *s, int alert)
367 struct winlink *wl;
369 if (s->curw == NULL)
370 return (-1);
372 wl = winlink_previous(s->curw);
373 if (alert)
374 wl = session_previous_alert(wl);
375 if (wl == NULL) {
376 wl = RB_MAX(winlinks, &s->windows);
377 if (alert && (wl = session_previous_alert(wl)) == NULL)
378 return (-1);
380 return (session_set_current(s, wl));
383 /* Move session to specific window. */
385 session_select(struct session *s, int idx)
387 struct winlink *wl;
389 wl = winlink_find_by_index(&s->windows, idx);
390 return (session_set_current(s, wl));
393 /* Move session to last used window. */
395 session_last(struct session *s)
397 struct winlink *wl;
399 wl = TAILQ_FIRST(&s->lastw);
400 if (wl == NULL)
401 return (-1);
402 if (wl == s->curw)
403 return (1);
405 return (session_set_current(s, wl));
408 /* Set current winlink to wl .*/
410 session_set_current(struct session *s, struct winlink *wl)
412 if (wl == NULL)
413 return (-1);
414 if (wl == s->curw)
415 return (1);
417 winlink_stack_remove(&s->lastw, wl);
418 winlink_stack_push(&s->lastw, s->curw);
419 s->curw = wl;
420 winlink_clear_flags(wl);
421 return (0);
424 /* Find the session group containing a session. */
425 struct session_group *
426 session_group_find(struct session *target)
428 struct session_group *sg;
429 struct session *s;
431 TAILQ_FOREACH(sg, &session_groups, entry) {
432 TAILQ_FOREACH(s, &sg->sessions, gentry) {
433 if (s == target)
434 return (sg);
437 return (NULL);
440 /* Find session group index. */
441 u_int
442 session_group_index(struct session_group *sg)
444 struct session_group *sg2;
445 u_int i;
447 i = 0;
448 TAILQ_FOREACH(sg2, &session_groups, entry) {
449 if (sg == sg2)
450 return (i);
451 i++;
454 fatalx("session group not found");
458 * Add a session to the session group containing target, creating it if
459 * necessary.
461 void
462 session_group_add(struct session *target, struct session *s)
464 struct session_group *sg;
466 if ((sg = session_group_find(target)) == NULL) {
467 sg = xmalloc(sizeof *sg);
468 TAILQ_INSERT_TAIL(&session_groups, sg, entry);
469 TAILQ_INIT(&sg->sessions);
470 TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
472 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
475 /* Remove a session from its group and destroy the group if empty. */
476 void
477 session_group_remove(struct session *s)
479 struct session_group *sg;
481 if ((sg = session_group_find(s)) == NULL)
482 return;
483 TAILQ_REMOVE(&sg->sessions, s, gentry);
484 if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
485 TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
486 if (TAILQ_EMPTY(&sg->sessions)) {
487 TAILQ_REMOVE(&session_groups, sg, entry);
488 free(sg);
492 /* Synchronize a session to its session group. */
493 void
494 session_group_synchronize_to(struct session *s)
496 struct session_group *sg;
497 struct session *target;
499 if ((sg = session_group_find(s)) == NULL)
500 return;
502 target = NULL;
503 TAILQ_FOREACH(target, &sg->sessions, gentry) {
504 if (target != s)
505 break;
507 session_group_synchronize1(target, s);
510 /* Synchronize a session group to a session. */
511 void
512 session_group_synchronize_from(struct session *target)
514 struct session_group *sg;
515 struct session *s;
517 if ((sg = session_group_find(target)) == NULL)
518 return;
520 TAILQ_FOREACH(s, &sg->sessions, gentry) {
521 if (s != target)
522 session_group_synchronize1(target, s);
527 * Synchronize a session with a target session. This means destroying all
528 * winlinks then recreating them, then updating the current window, last window
529 * stack and alerts.
531 void
532 session_group_synchronize1(struct session *target, struct session *s)
534 struct winlinks old_windows, *ww;
535 struct winlink_stack old_lastw;
536 struct winlink *wl, *wl2;
538 /* Don't do anything if the session is empty (it'll be destroyed). */
539 ww = &target->windows;
540 if (RB_EMPTY(ww))
541 return;
543 /* If the current window has vanished, move to the next now. */
544 if (s->curw != NULL &&
545 winlink_find_by_index(ww, s->curw->idx) == NULL &&
546 session_last(s) != 0 && session_previous(s, 0) != 0)
547 session_next(s, 0);
549 /* Save the old pointer and reset it. */
550 memcpy(&old_windows, &s->windows, sizeof old_windows);
551 RB_INIT(&s->windows);
553 /* Link all the windows from the target. */
554 RB_FOREACH(wl, winlinks, ww) {
555 wl2 = winlink_add(&s->windows, wl->idx);
556 winlink_set_window(wl2, wl->window);
557 notify_window_linked(s, wl2->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 if (winlink_find_by_window_id(&s->windows, wl->window->id) == NULL)
580 notify_window_unlinked(s, wl->window);
581 winlink_remove(&old_windows, wl);
585 /* Renumber the windows across winlinks attached to a specific session. */
586 void
587 session_renumber_windows(struct session *s)
589 struct winlink *wl, *wl1, *wl_new;
590 struct winlinks old_wins;
591 struct winlink_stack old_lastw;
592 int new_idx, new_curw_idx;
594 /* Save and replace old window list. */
595 memcpy(&old_wins, &s->windows, sizeof old_wins);
596 RB_INIT(&s->windows);
598 /* Start renumbering from the base-index if it's set. */
599 new_idx = options_get_number(&s->options, "base-index");
600 new_curw_idx = 0;
602 /* Go through the winlinks and assign new indexes. */
603 RB_FOREACH(wl, winlinks, &old_wins) {
604 wl_new = winlink_add(&s->windows, new_idx);
605 winlink_set_window(wl_new, wl->window);
606 wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
608 if (wl == s->curw)
609 new_curw_idx = wl_new->idx;
611 new_idx++;
614 /* Fix the stack of last windows now. */
615 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
616 TAILQ_INIT(&s->lastw);
617 TAILQ_FOREACH(wl, &old_lastw, sentry) {
618 wl_new = winlink_find_by_index(&s->windows, wl->idx);
619 if (wl_new != NULL)
620 TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
623 /* Set the current window. */
624 s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
626 /* Free the old winlinks (reducing window references too). */
627 RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
628 winlink_remove(&old_wins, wl);