[BACK]Return to session.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/session.c, Revision 1.70

1.70    ! nicm        1: /* $OpenBSD: session.c,v 1.69 2016/10/19 08:17:11 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.62      nicm        4:  * Copyright (c) 2007 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>
                     20: #include <sys/time.h>
                     21:
1.6       nicm       22: #include <paths.h>
1.1       nicm       23: #include <string.h>
                     24: #include <stdlib.h>
                     25: #include <unistd.h>
1.10      nicm       26: #include <time.h>
1.1       nicm       27:
                     28: #include "tmux.h"
                     29:
1.63      nicm       30: struct sessions                sessions;
                     31: static u_int           next_session_id;
                     32: struct session_groups  session_groups;
1.1       nicm       33:
1.63      nicm       34: static void    session_free(int, short, void *);
1.50      nicm       35:
1.63      nicm       36: static void    session_lock_timer(int, short, void *);
1.52      nicm       37:
1.63      nicm       38: static struct winlink *session_next_alert(struct winlink *);
                     39: static struct winlink *session_previous_alert(struct winlink *);
1.21      nicm       40:
1.64      nicm       41: static void    session_group_remove(struct session *);
                     42: static u_int   session_group_count(struct session_group *);
                     43: static void    session_group_synchronize1(struct session *, struct session *);
                     44:
1.25      nicm       45: RB_GENERATE(sessions, session, entry, session_cmp);
                     46:
                     47: int
                     48: session_cmp(struct session *s1, struct session *s2)
                     49: {
                     50:        return (strcmp(s1->name, s2->name));
                     51: }
                     52:
1.21      nicm       53: /*
                     54:  * Find if session is still alive. This is true if it is still on the global
                     55:  * sessions list.
                     56:  */
                     57: int
                     58: session_alive(struct session *s)
                     59: {
1.25      nicm       60:        struct session *s_loop;
1.21      nicm       61:
1.25      nicm       62:        RB_FOREACH(s_loop, sessions, &sessions) {
                     63:                if (s_loop == s)
                     64:                        return (1);
                     65:        }
                     66:        return (0);
1.21      nicm       67: }
1.1       nicm       68:
                     69: /* Find session by name. */
                     70: struct session *
                     71: session_find(const char *name)
                     72: {
1.25      nicm       73:        struct session  s;
                     74:
                     75:        s.name = (char *) name;
                     76:        return (RB_FIND(sessions, &sessions, &s));
1.48      nicm       77: }
                     78:
                     79: /* Find session by id parsed from a string. */
                     80: struct session *
                     81: session_find_by_id_str(const char *s)
                     82: {
                     83:        const char      *errstr;
                     84:        u_int            id;
                     85:
                     86:        if (*s != '$')
                     87:                return (NULL);
                     88:
                     89:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                     90:        if (errstr != NULL)
                     91:                return (NULL);
                     92:        return (session_find_by_id(id));
1.25      nicm       93: }
                     94:
1.38      nicm       95: /* Find session by id. */
1.25      nicm       96: struct session *
1.38      nicm       97: session_find_by_id(u_int id)
1.25      nicm       98: {
1.1       nicm       99:        struct session  *s;
                    100:
1.25      nicm      101:        RB_FOREACH(s, sessions, &sessions) {
1.38      nicm      102:                if (s->id == id)
1.1       nicm      103:                        return (s);
                    104:        }
                    105:        return (NULL);
                    106: }
                    107:
                    108: /* Create a new session. */
                    109: struct session *
1.44      nicm      110: session_create(const char *name, int argc, char **argv, const char *path,
1.58      nicm      111:     const char *cwd, struct environ *env, struct termios *tio, int idx,
                    112:     u_int sx, u_int sy, char **cause)
1.1       nicm      113: {
                    114:        struct session  *s;
1.44      nicm      115:        struct winlink  *wl;
1.1       nicm      116:
1.52      nicm      117:        s = xcalloc(1, sizeof *s);
1.50      nicm      118:        s->references = 1;
1.1       nicm      119:        s->flags = 0;
1.7       nicm      120:
1.58      nicm      121:        s->cwd = xstrdup(cwd);
1.19      nicm      122:
1.1       nicm      123:        s->curw = NULL;
1.11      nicm      124:        TAILQ_INIT(&s->lastw);
1.1       nicm      125:        RB_INIT(&s->windows);
1.7       nicm      126:
1.57      nicm      127:        s->environ = environ_create();
1.3       nicm      128:        if (env != NULL)
1.57      nicm      129:                environ_copy(env, s->environ);
1.61      nicm      130:
1.56      nicm      131:        s->options = options_create(global_s_options);
1.61      nicm      132:        s->hooks = hooks_create(global_hooks);
1.8       nicm      133:
                    134:        s->tio = NULL;
                    135:        if (tio != NULL) {
                    136:                s->tio = xmalloc(sizeof *s->tio);
                    137:                memcpy(s->tio, tio, sizeof *s->tio);
                    138:        }
1.1       nicm      139:
                    140:        s->sx = sx;
                    141:        s->sy = sy;
                    142:
1.32      nicm      143:        if (name != NULL) {
1.1       nicm      144:                s->name = xstrdup(name);
1.38      nicm      145:                s->id = next_session_id++;
1.32      nicm      146:        } else {
                    147:                s->name = NULL;
                    148:                do {
1.38      nicm      149:                        s->id = next_session_id++;
1.46      nicm      150:                        free(s->name);
1.38      nicm      151:                        xasprintf(&s->name, "%u", s->id);
1.32      nicm      152:                } while (RB_FIND(sessions, &sessions, s) != NULL);
                    153:        }
1.25      nicm      154:        RB_INSERT(sessions, &sessions, s);
1.15      nicm      155:
1.59      nicm      156:        log_debug("new session %s $%u", s->name, s->id);
                    157:
1.52      nicm      158:        if (gettimeofday(&s->creation_time, NULL) != 0)
                    159:                fatal("gettimeofday failed");
                    160:        session_update_activity(s, &s->creation_time);
                    161:
1.44      nicm      162:        if (argc >= 0) {
                    163:                wl = session_new(s, NULL, argc, argv, path, cwd, idx, cause);
                    164:                if (wl == NULL) {
1.11      nicm      165:                        session_destroy(s);
                    166:                        return (NULL);
                    167:                }
                    168:                session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      169:        }
                    170:
                    171:        log_debug("session %s created", s->name);
                    172:
                    173:        return (s);
                    174: }
                    175:
1.50      nicm      176: /* Remove a reference from a session. */
                    177: void
                    178: session_unref(struct session *s)
                    179: {
                    180:        log_debug("session %s has %d references", s->name, s->references);
                    181:
                    182:        s->references--;
                    183:        if (s->references == 0)
                    184:                event_once(-1, EV_TIMEOUT, session_free, s, NULL);
                    185: }
                    186:
                    187: /* Free session. */
1.63      nicm      188: static void
1.60      nicm      189: session_free(__unused int fd, __unused short events, void *arg)
1.50      nicm      190: {
                    191:        struct session  *s = arg;
                    192:
1.55      nicm      193:        log_debug("session %s freed (%d references)", s->name, s->references);
1.50      nicm      194:
1.53      nicm      195:        if (s->references == 0) {
1.57      nicm      196:                environ_free(s->environ);
1.61      nicm      197:
1.56      nicm      198:                options_free(s->options);
1.61      nicm      199:                hooks_free(s->hooks);
1.56      nicm      200:
1.53      nicm      201:                free(s->name);
1.50      nicm      202:                free(s);
1.53      nicm      203:        }
1.50      nicm      204: }
                    205:
1.1       nicm      206: /* Destroy a session. */
                    207: void
                    208: session_destroy(struct session *s)
                    209: {
1.33      nicm      210:        struct winlink  *wl;
1.39      nicm      211:
1.1       nicm      212:        log_debug("session %s destroyed", s->name);
1.66      nicm      213:        s->curw = NULL;
1.1       nicm      214:
1.25      nicm      215:        RB_REMOVE(sessions, &sessions, s);
1.68      nicm      216:        notify_session("session-closed", s);
1.1       nicm      217:
1.36      nicm      218:        free(s->tio);
1.8       nicm      219:
1.52      nicm      220:        if (event_initialized(&s->lock_timer))
                    221:                event_del(&s->lock_timer);
                    222:
1.11      nicm      223:        session_group_remove(s);
1.1       nicm      224:
1.11      nicm      225:        while (!TAILQ_EMPTY(&s->lastw))
                    226:                winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
1.33      nicm      227:        while (!RB_EMPTY(&s->windows)) {
                    228:                wl = RB_ROOT(&s->windows);
1.68      nicm      229:                notify_session_window("window-unlinked", s, wl->window);
1.33      nicm      230:                winlink_remove(&s->windows, wl);
                    231:        }
1.1       nicm      232:
1.58      nicm      233:        free((void *)s->cwd);
1.15      nicm      234:
1.50      nicm      235:        session_unref(s);
1.31      nicm      236: }
                    237:
1.42      nicm      238: /* Check a session name is valid: not empty and no colons or periods. */
1.31      nicm      239: int
                    240: session_check_name(const char *name)
                    241: {
1.42      nicm      242:        return (*name != '\0' && name[strcspn(name, ":.")] == '\0');
1.27      nicm      243: }
                    244:
1.52      nicm      245: /* Lock session if it has timed out. */
1.63      nicm      246: static void
1.60      nicm      247: session_lock_timer(__unused int fd, __unused short events, void *arg)
1.52      nicm      248: {
                    249:        struct session  *s = arg;
                    250:
                    251:        if (s->flags & SESSION_UNATTACHED)
                    252:                return;
                    253:
                    254:        log_debug("session %s locked, activity time %lld", s->name,
                    255:            (long long)s->activity_time.tv_sec);
                    256:
                    257:        server_lock_session(s);
                    258:        recalculate_sizes();
                    259: }
                    260:
1.51      nicm      261: /* Update activity time. */
1.27      nicm      262: void
1.51      nicm      263: session_update_activity(struct session *s, struct timeval *from)
1.27      nicm      264: {
1.51      nicm      265:        struct timeval  *last = &s->last_activity_time;
1.52      nicm      266:        struct timeval   tv;
1.51      nicm      267:
                    268:        memcpy(last, &s->activity_time, sizeof *last);
                    269:        if (from == NULL)
                    270:                gettimeofday(&s->activity_time, NULL);
                    271:        else
                    272:                memcpy(&s->activity_time, from, sizeof s->activity_time);
1.59      nicm      273:
                    274:        log_debug("session %s activity %lld.%06d (last %lld.%06d)", s->name,
                    275:            (long long)s->activity_time.tv_sec, (int)s->activity_time.tv_usec,
                    276:            (long long)last->tv_sec, (int)last->tv_usec);
1.52      nicm      277:
                    278:        if (evtimer_initialized(&s->lock_timer))
                    279:                evtimer_del(&s->lock_timer);
                    280:        else
                    281:                evtimer_set(&s->lock_timer, session_lock_timer, s);
                    282:
                    283:        if (~s->flags & SESSION_UNATTACHED) {
                    284:                timerclear(&tv);
1.56      nicm      285:                tv.tv_sec = options_get_number(s->options, "lock-after-time");
1.52      nicm      286:                if (tv.tv_sec != 0)
                    287:                        evtimer_add(&s->lock_timer, &tv);
                    288:        }
1.20      nicm      289: }
                    290:
                    291: /* Find the next usable session. */
                    292: struct session *
                    293: session_next_session(struct session *s)
                    294: {
                    295:        struct session *s2;
                    296:
1.25      nicm      297:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      298:                return (NULL);
                    299:
1.29      nicm      300:        s2 = RB_NEXT(sessions, &sessions, s);
1.28      nicm      301:        if (s2 == NULL)
                    302:                s2 = RB_MIN(sessions, &sessions);
1.25      nicm      303:        if (s2 == s)
                    304:                return (NULL);
1.20      nicm      305:        return (s2);
                    306: }
                    307:
                    308: /* Find the previous usable session. */
                    309: struct session *
                    310: session_previous_session(struct session *s)
                    311: {
                    312:        struct session *s2;
                    313:
1.25      nicm      314:        if (RB_EMPTY(&sessions) || !session_alive(s))
1.20      nicm      315:                return (NULL);
                    316:
1.29      nicm      317:        s2 = RB_PREV(sessions, &sessions, s);
1.28      nicm      318:        if (s2 == NULL)
                    319:                s2 = RB_MAX(sessions, &sessions);
1.25      nicm      320:        if (s2 == s)
                    321:                return (NULL);
1.20      nicm      322:        return (s2);
1.1       nicm      323: }
                    324:
                    325: /* Create a new window on a session. */
                    326: struct winlink *
1.44      nicm      327: session_new(struct session *s, const char *name, int argc, char **argv,
1.58      nicm      328:     const char *path, const char *cwd, int idx, char **cause)
1.1       nicm      329: {
                    330:        struct window   *w;
1.30      nicm      331:        struct winlink  *wl;
1.57      nicm      332:        struct environ  *env;
1.6       nicm      333:        const char      *shell;
1.1       nicm      334:        u_int            hlimit;
                    335:
1.30      nicm      336:        if ((wl = winlink_add(&s->windows, idx)) == NULL) {
                    337:                xasprintf(cause, "index in use: %d", idx);
                    338:                return (NULL);
                    339:        }
1.70    ! nicm      340:        wl->session = s;
1.30      nicm      341:
1.57      nicm      342:        env = environ_create();
                    343:        environ_copy(global_environ, env);
                    344:        environ_copy(s->environ, env);
                    345:        server_fill_environ(s, env);
1.1       nicm      346:
1.56      nicm      347:        shell = options_get_string(s->options, "default-shell");
1.6       nicm      348:        if (*shell == '\0' || areshell(shell))
                    349:                shell = _PATH_BSHELL;
                    350:
1.56      nicm      351:        hlimit = options_get_number(s->options, "history-limit");
1.67      nicm      352:        w = window_create_spawn(name, argc, argv, path, shell, cwd, env, s->tio,
1.44      nicm      353:            s->sx, s->sy, hlimit, cause);
1.3       nicm      354:        if (w == NULL) {
1.30      nicm      355:                winlink_remove(&s->windows, wl);
1.57      nicm      356:                environ_free(env);
1.1       nicm      357:                return (NULL);
1.3       nicm      358:        }
1.30      nicm      359:        winlink_set_window(wl, w);
1.68      nicm      360:        notify_session_window("window-linked", s, w);
1.57      nicm      361:        environ_free(env);
1.1       nicm      362:
1.30      nicm      363:        session_group_synchronize_from(s);
                    364:        return (wl);
1.1       nicm      365: }
                    366:
                    367: /* Attach a window to a session. */
                    368: struct winlink *
                    369: session_attach(struct session *s, struct window *w, int idx, char **cause)
                    370: {
                    371:        struct winlink  *wl;
                    372:
1.30      nicm      373:        if ((wl = winlink_add(&s->windows, idx)) == NULL) {
1.1       nicm      374:                xasprintf(cause, "index in use: %d", idx);
1.30      nicm      375:                return (NULL);
                    376:        }
1.70    ! nicm      377:        wl->session = s;
1.30      nicm      378:        winlink_set_window(wl, w);
1.68      nicm      379:        notify_session_window("window-linked", s, w);
1.30      nicm      380:
1.11      nicm      381:        session_group_synchronize_from(s);
1.1       nicm      382:        return (wl);
                    383: }
                    384:
                    385: /* Detach a window from a session. */
                    386: int
                    387: session_detach(struct session *s, struct winlink *wl)
                    388: {
                    389:        if (s->curw == wl &&
1.66      nicm      390:            session_last(s) != 0 &&
                    391:            session_previous(s, 0) != 0)
1.1       nicm      392:                session_next(s, 0);
                    393:
1.18      nicm      394:        wl->flags &= ~WINLINK_ALERTFLAGS;
1.68      nicm      395:        notify_session_window("window-unlinked", s, wl->window);
1.1       nicm      396:        winlink_stack_remove(&s->lastw, wl);
                    397:        winlink_remove(&s->windows, wl);
1.66      nicm      398:
1.11      nicm      399:        session_group_synchronize_from(s);
1.66      nicm      400:
1.1       nicm      401:        if (RB_EMPTY(&s->windows)) {
                    402:                session_destroy(s);
                    403:                return (1);
                    404:        }
                    405:        return (0);
                    406: }
                    407:
                    408: /* Return if session has window. */
1.47      nicm      409: int
1.1       nicm      410: session_has(struct session *s, struct window *w)
                    411: {
                    412:        struct winlink  *wl;
                    413:
1.70    ! nicm      414:        TAILQ_FOREACH(wl, &w->winlinks, wentry) {
        !           415:                if (wl->session == s)
1.47      nicm      416:                        return (1);
1.1       nicm      417:        }
1.47      nicm      418:        return (0);
1.49      nicm      419: }
                    420:
                    421: /*
                    422:  * Return 1 if a window is linked outside this session (not including session
                    423:  * groups). The window must be in this session!
                    424:  */
                    425: int
                    426: session_is_linked(struct session *s, struct window *w)
                    427: {
                    428:        struct session_group    *sg;
                    429:
                    430:        if ((sg = session_group_find(s)) != NULL)
                    431:                return (w->references != session_group_count(sg));
                    432:        return (w->references != 1);
1.1       nicm      433: }
                    434:
1.63      nicm      435: static struct winlink *
1.18      nicm      436: session_next_alert(struct winlink *wl)
1.1       nicm      437: {
                    438:        while (wl != NULL) {
1.18      nicm      439:                if (wl->flags & WINLINK_ALERTFLAGS)
1.1       nicm      440:                        break;
1.14      nicm      441:                wl = winlink_next(wl);
1.1       nicm      442:        }
                    443:        return (wl);
                    444: }
                    445:
                    446: /* Move session to next window. */
                    447: int
1.17      nicm      448: session_next(struct session *s, int alert)
1.1       nicm      449: {
                    450:        struct winlink  *wl;
                    451:
                    452:        if (s->curw == NULL)
                    453:                return (-1);
                    454:
1.14      nicm      455:        wl = winlink_next(s->curw);
1.17      nicm      456:        if (alert)
1.18      nicm      457:                wl = session_next_alert(wl);
1.1       nicm      458:        if (wl == NULL) {
                    459:                wl = RB_MIN(winlinks, &s->windows);
1.18      nicm      460:                if (alert && ((wl = session_next_alert(wl)) == NULL))
1.1       nicm      461:                        return (-1);
                    462:        }
1.37      nicm      463:        return (session_set_current(s, wl));
1.1       nicm      464: }
                    465:
1.63      nicm      466: static struct winlink *
1.18      nicm      467: session_previous_alert(struct winlink *wl)
1.1       nicm      468: {
                    469:        while (wl != NULL) {
1.18      nicm      470:                if (wl->flags & WINLINK_ALERTFLAGS)
1.1       nicm      471:                        break;
1.14      nicm      472:                wl = winlink_previous(wl);
1.1       nicm      473:        }
                    474:        return (wl);
                    475: }
                    476:
                    477: /* Move session to previous window. */
                    478: int
1.17      nicm      479: session_previous(struct session *s, int alert)
1.1       nicm      480: {
                    481:        struct winlink  *wl;
                    482:
                    483:        if (s->curw == NULL)
                    484:                return (-1);
                    485:
1.14      nicm      486:        wl = winlink_previous(s->curw);
1.17      nicm      487:        if (alert)
1.18      nicm      488:                wl = session_previous_alert(wl);
1.1       nicm      489:        if (wl == NULL) {
                    490:                wl = RB_MAX(winlinks, &s->windows);
1.18      nicm      491:                if (alert && (wl = session_previous_alert(wl)) == NULL)
1.1       nicm      492:                        return (-1);
                    493:        }
1.37      nicm      494:        return (session_set_current(s, wl));
1.1       nicm      495: }
                    496:
                    497: /* Move session to specific window. */
                    498: int
                    499: session_select(struct session *s, int idx)
                    500: {
                    501:        struct winlink  *wl;
                    502:
                    503:        wl = winlink_find_by_index(&s->windows, idx);
1.37      nicm      504:        return (session_set_current(s, wl));
1.1       nicm      505: }
                    506:
                    507: /* Move session to last used window. */
                    508: int
                    509: session_last(struct session *s)
                    510: {
                    511:        struct winlink  *wl;
                    512:
1.11      nicm      513:        wl = TAILQ_FIRST(&s->lastw);
1.37      nicm      514:        if (wl == NULL)
                    515:                return (-1);
                    516:        if (wl == s->curw)
                    517:                return (1);
                    518:
                    519:        return (session_set_current(s, wl));
                    520: }
                    521:
                    522: /* Set current winlink to wl .*/
                    523: int
                    524: session_set_current(struct session *s, struct winlink *wl)
                    525: {
1.1       nicm      526:        if (wl == NULL)
                    527:                return (-1);
                    528:        if (wl == s->curw)
                    529:                return (1);
                    530:
                    531:        winlink_stack_remove(&s->lastw, wl);
                    532:        winlink_stack_push(&s->lastw, s->curw);
                    533:        s->curw = wl;
1.35      nicm      534:        winlink_clear_flags(wl);
1.54      nicm      535:        window_update_activity(wl->window);
1.1       nicm      536:        return (0);
1.11      nicm      537: }
                    538:
                    539: /* Find the session group containing a session. */
                    540: struct session_group *
                    541: session_group_find(struct session *target)
                    542: {
                    543:        struct session_group    *sg;
                    544:        struct session          *s;
                    545:
                    546:        TAILQ_FOREACH(sg, &session_groups, entry) {
                    547:                TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    548:                        if (s == target)
                    549:                                return (sg);
                    550:                }
                    551:        }
                    552:        return (NULL);
                    553: }
                    554:
                    555: /* Find session group index. */
                    556: u_int
                    557: session_group_index(struct session_group *sg)
                    558: {
                    559:        struct session_group   *sg2;
                    560:        u_int                   i;
                    561:
                    562:        i = 0;
                    563:        TAILQ_FOREACH(sg2, &session_groups, entry) {
                    564:                if (sg == sg2)
                    565:                        return (i);
                    566:                i++;
                    567:        }
                    568:
                    569:        fatalx("session group not found");
                    570: }
                    571:
                    572: /*
                    573:  * Add a session to the session group containing target, creating it if
1.15      nicm      574:  * necessary.
1.11      nicm      575:  */
                    576: void
                    577: session_group_add(struct session *target, struct session *s)
                    578: {
                    579:        struct session_group    *sg;
                    580:
                    581:        if ((sg = session_group_find(target)) == NULL) {
                    582:                sg = xmalloc(sizeof *sg);
                    583:                TAILQ_INSERT_TAIL(&session_groups, sg, entry);
                    584:                TAILQ_INIT(&sg->sessions);
                    585:                TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
                    586:        }
                    587:        TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
                    588: }
                    589:
                    590: /* Remove a session from its group and destroy the group if empty. */
1.64      nicm      591: static void
1.11      nicm      592: session_group_remove(struct session *s)
                    593: {
                    594:        struct session_group    *sg;
                    595:
                    596:        if ((sg = session_group_find(s)) == NULL)
                    597:                return;
                    598:        TAILQ_REMOVE(&sg->sessions, s, gentry);
                    599:        if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
                    600:                TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
                    601:        if (TAILQ_EMPTY(&sg->sessions)) {
                    602:                TAILQ_REMOVE(&session_groups, sg, entry);
1.36      nicm      603:                free(sg);
1.11      nicm      604:        }
                    605: }
                    606:
1.45      nicm      607: /* Count number of sessions in session group. */
1.64      nicm      608: static u_int
1.45      nicm      609: session_group_count(struct session_group *sg)
                    610: {
                    611:        struct session  *s;
                    612:        u_int            n;
                    613:
                    614:        n = 0;
                    615:        TAILQ_FOREACH(s, &sg->sessions, gentry)
                    616:            n++;
                    617:        return (n);
                    618: }
                    619:
1.11      nicm      620: /* Synchronize a session to its session group. */
                    621: void
                    622: session_group_synchronize_to(struct session *s)
                    623: {
                    624:        struct session_group    *sg;
                    625:        struct session          *target;
                    626:
                    627:        if ((sg = session_group_find(s)) == NULL)
                    628:                return;
                    629:
                    630:        target = NULL;
                    631:        TAILQ_FOREACH(target, &sg->sessions, gentry) {
                    632:                if (target != s)
                    633:                        break;
                    634:        }
                    635:        session_group_synchronize1(target, s);
                    636: }
                    637:
                    638: /* Synchronize a session group to a session. */
                    639: void
                    640: session_group_synchronize_from(struct session *target)
                    641: {
                    642:        struct session_group    *sg;
                    643:        struct session          *s;
                    644:
                    645:        if ((sg = session_group_find(target)) == NULL)
                    646:                return;
                    647:
                    648:        TAILQ_FOREACH(s, &sg->sessions, gentry) {
                    649:                if (s != target)
                    650:                        session_group_synchronize1(target, s);
                    651:        }
                    652: }
                    653:
                    654: /*
                    655:  * Synchronize a session with a target session. This means destroying all
                    656:  * winlinks then recreating them, then updating the current window, last window
                    657:  * stack and alerts.
                    658:  */
1.64      nicm      659: static void
1.11      nicm      660: session_group_synchronize1(struct session *target, struct session *s)
                    661: {
                    662:        struct winlinks          old_windows, *ww;
                    663:        struct winlink_stack     old_lastw;
                    664:        struct winlink          *wl, *wl2;
                    665:
                    666:        /* Don't do anything if the session is empty (it'll be destroyed). */
                    667:        ww = &target->windows;
                    668:        if (RB_EMPTY(ww))
                    669:                return;
                    670:
                    671:        /* If the current window has vanished, move to the next now. */
1.16      nicm      672:        if (s->curw != NULL &&
                    673:            winlink_find_by_index(ww, s->curw->idx) == NULL &&
                    674:            session_last(s) != 0 && session_previous(s, 0) != 0)
                    675:                session_next(s, 0);
1.11      nicm      676:
                    677:        /* Save the old pointer and reset it. */
                    678:        memcpy(&old_windows, &s->windows, sizeof old_windows);
                    679:        RB_INIT(&s->windows);
                    680:
                    681:        /* Link all the windows from the target. */
1.18      nicm      682:        RB_FOREACH(wl, winlinks, ww) {
1.30      nicm      683:                wl2 = winlink_add(&s->windows, wl->idx);
1.70    ! nicm      684:                wl2->session = s;
1.30      nicm      685:                winlink_set_window(wl2, wl->window);
1.68      nicm      686:                notify_session_window("window-linked", s, wl2->window);
1.18      nicm      687:                wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    688:        }
1.11      nicm      689:
                    690:        /* Fix up the current window. */
                    691:        if (s->curw != NULL)
                    692:                s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
                    693:        else
                    694:                s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
                    695:
                    696:        /* Fix up the last window stack. */
                    697:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    698:        TAILQ_INIT(&s->lastw);
                    699:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
                    700:                wl2 = winlink_find_by_index(&s->windows, wl->idx);
                    701:                if (wl2 != NULL)
                    702:                        TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
                    703:        }
                    704:
                    705:        /* Then free the old winlinks list. */
                    706:        while (!RB_EMPTY(&old_windows)) {
                    707:                wl = RB_ROOT(&old_windows);
1.45      nicm      708:                wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
                    709:                if (wl2 == NULL)
1.68      nicm      710:                        notify_session_window("window-unlinked", s, wl->window);
1.13      nicm      711:                winlink_remove(&old_windows, wl);
1.11      nicm      712:        }
1.34      nicm      713: }
                    714:
                    715: /* Renumber the windows across winlinks attached to a specific session. */
                    716: void
                    717: session_renumber_windows(struct session *s)
                    718: {
                    719:        struct winlink          *wl, *wl1, *wl_new;
                    720:        struct winlinks          old_wins;
                    721:        struct winlink_stack     old_lastw;
                    722:        int                      new_idx, new_curw_idx;
                    723:
                    724:        /* Save and replace old window list. */
                    725:        memcpy(&old_wins, &s->windows, sizeof old_wins);
                    726:        RB_INIT(&s->windows);
                    727:
                    728:        /* Start renumbering from the base-index if it's set. */
1.56      nicm      729:        new_idx = options_get_number(s->options, "base-index");
1.34      nicm      730:        new_curw_idx = 0;
                    731:
                    732:        /* Go through the winlinks and assign new indexes. */
                    733:        RB_FOREACH(wl, winlinks, &old_wins) {
                    734:                wl_new = winlink_add(&s->windows, new_idx);
1.70    ! nicm      735:                wl_new->session = s;
1.34      nicm      736:                winlink_set_window(wl_new, wl->window);
                    737:                wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
                    738:
                    739:                if (wl == s->curw)
                    740:                        new_curw_idx = wl_new->idx;
                    741:
                    742:                new_idx++;
                    743:        }
                    744:
                    745:        /* Fix the stack of last windows now. */
                    746:        memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
                    747:        TAILQ_INIT(&s->lastw);
                    748:        TAILQ_FOREACH(wl, &old_lastw, sentry) {
1.40      nicm      749:                wl_new = winlink_find_by_window(&s->windows, wl->window);
1.34      nicm      750:                if (wl_new != NULL)
                    751:                        TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
                    752:        }
                    753:
                    754:        /* Set the current window. */
                    755:        s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
                    756:
                    757:        /* Free the old winlinks (reducing window references too). */
                    758:        RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
                    759:                winlink_remove(&old_wins, wl);
1.1       nicm      760: }