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

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