[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.48

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