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

1.7     ! nicm        1: /* $OpenBSD: session.c,v 1.6 2009/09/01 13:09:49 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>
                     26:
                     27: #include "tmux.h"
                     28:
                     29: /* Global session list. */
                     30: struct sessions        sessions;
1.7     ! nicm       31: struct sessions dead_sessions;
1.1       nicm       32:
                     33: struct winlink *session_next_activity(struct session *, struct winlink *);
                     34: struct winlink *session_previous_activity(struct session *, struct winlink *);
                     35:
                     36: void
                     37: session_alert_cancel(struct session *s, struct winlink *wl)
                     38: {
                     39:        struct session_alert    *sa, *sb;
                     40:
                     41:        sa = SLIST_FIRST(&s->alerts);
                     42:        while (sa != NULL) {
                     43:                sb = sa;
                     44:                sa = SLIST_NEXT(sa, entry);
                     45:
                     46:                if (wl == NULL || sb->wl == wl) {
                     47:                        SLIST_REMOVE(&s->alerts, sb, session_alert, entry);
                     48:                        xfree(sb);
                     49:                }
                     50:        }
                     51: }
                     52:
                     53: void
                     54: session_alert_add(struct session *s, struct window *w, int type)
                     55: {
                     56:        struct session_alert    *sa;
                     57:        struct winlink          *wl;
                     58:
                     59:        RB_FOREACH(wl, winlinks, &s->windows) {
                     60:                if (wl == s->curw)
                     61:                        continue;
                     62:
                     63:                if (wl->window == w &&
                     64:                    !session_alert_has(s, wl, type)) {
                     65:                        sa = xmalloc(sizeof *sa);
                     66:                        sa->wl = wl;
                     67:                        sa->type = type;
                     68:                        SLIST_INSERT_HEAD(&s->alerts, sa, entry);
                     69:                }
                     70:        }
                     71: }
                     72:
                     73: int
                     74: session_alert_has(struct session *s, struct winlink *wl, int type)
                     75: {
                     76:        struct session_alert    *sa;
                     77:
                     78:        SLIST_FOREACH(sa, &s->alerts, entry) {
                     79:                if (sa->wl == wl && sa->type == type)
                     80:                        return (1);
                     81:        }
                     82:
                     83:        return (0);
                     84: }
                     85:
                     86: int
                     87: session_alert_has_window(struct session *s, struct window *w, int type)
                     88: {
                     89:        struct session_alert    *sa;
                     90:
                     91:        SLIST_FOREACH(sa, &s->alerts, entry) {
                     92:                if (sa->wl->window == w && sa->type == type)
                     93:                        return (1);
                     94:        }
                     95:
                     96:        return (0);
                     97: }
                     98:
                     99: /* Find session by name. */
                    100: struct session *
                    101: session_find(const char *name)
                    102: {
                    103:        struct session  *s;
                    104:        u_int            i;
                    105:
                    106:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    107:                s = ARRAY_ITEM(&sessions, i);
                    108:                if (s != NULL && strcmp(s->name, name) == 0)
                    109:                        return (s);
                    110:        }
                    111:
                    112:        return (NULL);
                    113: }
                    114:
                    115: /* Create a new session. */
                    116: struct session *
1.3       nicm      117: session_create(const char *name, const char *cmd, const char *cwd,
1.5       nicm      118:     struct environ *env, struct termios *tio, int idx, u_int sx, u_int sy,
                    119:     char **cause)
1.1       nicm      120: {
                    121:        struct session  *s;
                    122:        u_int            i;
                    123:
                    124:        s = xmalloc(sizeof *s);
1.7     ! nicm      125:        s->references = 0;
1.1       nicm      126:        s->flags = 0;
1.7     ! nicm      127:
1.1       nicm      128:        if (gettimeofday(&s->tv, NULL) != 0)
                    129:                fatal("gettimeofday");
1.7     ! nicm      130:
1.1       nicm      131:        s->curw = NULL;
                    132:        SLIST_INIT(&s->lastw);
                    133:        RB_INIT(&s->windows);
                    134:        SLIST_INIT(&s->alerts);
1.7     ! nicm      135:
1.1       nicm      136:        paste_init_stack(&s->buffers);
1.7     ! nicm      137:
1.2       nicm      138:        options_init(&s->options, &global_s_options);
1.3       nicm      139:        environ_init(&s->environ);
                    140:        if (env != NULL)
                    141:                environ_copy(env, &s->environ);
1.4       nicm      142:        memcpy(&s->tio, tio, sizeof s->tio);
1.1       nicm      143:
                    144:        s->sx = sx;
                    145:        s->sy = sy;
                    146:
                    147:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    148:                if (ARRAY_ITEM(&sessions, i) == NULL) {
                    149:                        ARRAY_SET(&sessions, i, s);
                    150:                        break;
                    151:                }
                    152:        }
                    153:        if (i == ARRAY_LENGTH(&sessions))
                    154:                ARRAY_ADD(&sessions, s);
                    155:
                    156:        if (name != NULL)
                    157:                s->name = xstrdup(name);
                    158:        else
                    159:                xasprintf(&s->name, "%u", i);
1.5       nicm      160:        if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
1.1       nicm      161:                session_destroy(s);
                    162:                return (NULL);
                    163:        }
1.5       nicm      164:        session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      165:
                    166:        log_debug("session %s created", s->name);
                    167:
                    168:        return (s);
                    169: }
                    170:
                    171: /* Destroy a session. */
                    172: void
                    173: session_destroy(struct session *s)
                    174: {
                    175:        u_int   i;
                    176:
                    177:        log_debug("session %s destroyed", s->name);
                    178:
                    179:        if (session_index(s, &i) != 0)
                    180:                fatalx("session not found");
                    181:        ARRAY_SET(&sessions, i, NULL);
                    182:        while (!ARRAY_EMPTY(&sessions) && ARRAY_LAST(&sessions) == NULL)
                    183:                ARRAY_TRUNC(&sessions, 1);
                    184:
                    185:        session_alert_cancel(s, NULL);
1.3       nicm      186:        environ_free(&s->environ);
1.1       nicm      187:        options_free(&s->options);
                    188:        paste_free_stack(&s->buffers);
                    189:
                    190:        while (!SLIST_EMPTY(&s->lastw))
                    191:                winlink_stack_remove(&s->lastw, SLIST_FIRST(&s->lastw));
                    192:        while (!RB_EMPTY(&s->windows))
                    193:                winlink_remove(&s->windows, RB_ROOT(&s->windows));
                    194:
                    195:        xfree(s->name);
1.7     ! nicm      196:
        !           197:        for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
        !           198:                if (ARRAY_ITEM(&dead_sessions, i) == NULL) {
        !           199:                        ARRAY_SET(&dead_sessions, i, s);
        !           200:                        break;
        !           201:                }
        !           202:        }
        !           203:        if (i == ARRAY_LENGTH(&dead_sessions))
        !           204:                ARRAY_ADD(&dead_sessions, s);
        !           205:        s->flags |= SESSION_DEAD;
1.1       nicm      206: }
                    207:
                    208: /* Find session index. */
                    209: int
                    210: session_index(struct session *s, u_int *i)
                    211: {
                    212:        for (*i = 0; *i < ARRAY_LENGTH(&sessions); (*i)++) {
                    213:                if (s == ARRAY_ITEM(&sessions, *i))
                    214:                        return (0);
                    215:        }
                    216:        return (-1);
                    217: }
                    218:
                    219: /* Create a new window on a session. */
                    220: struct winlink *
1.4       nicm      221: session_new(struct session *s,
1.1       nicm      222:     const char *name, const char *cmd, const char *cwd, int idx, char **cause)
                    223: {
                    224:        struct window   *w;
1.3       nicm      225:        struct environ   env;
1.6       nicm      226:        const char      *shell;
1.1       nicm      227:        u_int            hlimit;
                    228:
1.3       nicm      229:        environ_init(&env);
                    230:        environ_copy(&global_environ, &env);
                    231:        environ_copy(&s->environ, &env);
                    232:        server_fill_environ(s, &env);
1.1       nicm      233:
1.6       nicm      234:        shell = options_get_string(&s->options, "default-shell");
                    235:        if (*shell == '\0' || areshell(shell))
                    236:                shell = _PATH_BSHELL;
                    237:
1.1       nicm      238:        hlimit = options_get_number(&s->options, "history-limit");
1.4       nicm      239:        w = window_create(
1.6       nicm      240:            name, cmd, shell, cwd, &env, &s->tio, s->sx, s->sy, hlimit, cause);
1.3       nicm      241:        if (w == NULL) {
                    242:                environ_free(&env);
1.1       nicm      243:                return (NULL);
1.3       nicm      244:        }
                    245:        environ_free(&env);
1.1       nicm      246:
                    247:        if (options_get_number(&s->options, "set-remain-on-exit"))
                    248:                options_set_number(&w->options, "remain-on-exit", 1);
                    249:
                    250:        return (session_attach(s, w, idx, cause));
                    251: }
                    252:
                    253: /* Attach a window to a session. */
                    254: struct winlink *
                    255: session_attach(struct session *s, struct window *w, int idx, char **cause)
                    256: {
                    257:        struct winlink  *wl;
                    258:
                    259:        if ((wl = winlink_add(&s->windows, w, idx)) == NULL)
                    260:                xasprintf(cause, "index in use: %d", idx);
                    261:        return (wl);
                    262: }
                    263:
                    264: /* Detach a window from a session. */
                    265: int
                    266: session_detach(struct session *s, struct winlink *wl)
                    267: {
                    268:        if (s->curw == wl &&
                    269:            session_last(s) != 0 && session_previous(s, 0) != 0)
                    270:                session_next(s, 0);
                    271:
                    272:        session_alert_cancel(s, wl);
                    273:        winlink_stack_remove(&s->lastw, wl);
                    274:        winlink_remove(&s->windows, wl);
                    275:        if (RB_EMPTY(&s->windows)) {
                    276:                session_destroy(s);
                    277:                return (1);
                    278:        }
                    279:        return (0);
                    280: }
                    281:
                    282: /* Return if session has window. */
                    283: int
                    284: session_has(struct session *s, struct window *w)
                    285: {
                    286:        struct winlink  *wl;
                    287:
                    288:        RB_FOREACH(wl, winlinks, &s->windows) {
                    289:                if (wl->window == w)
                    290:                        return (1);
                    291:        }
                    292:        return (0);
                    293: }
                    294:
                    295: struct winlink *
                    296: session_next_activity(struct session *s, struct winlink *wl)
                    297: {
                    298:        while (wl != NULL) {
                    299:                if (session_alert_has(s, wl, WINDOW_BELL))
                    300:                        break;
                    301:                if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    302:                        break;
                    303:                if (session_alert_has(s, wl, WINDOW_CONTENT))
                    304:                        break;
                    305:                wl = winlink_next(&s->windows, wl);
                    306:        }
                    307:        return (wl);
                    308: }
                    309:
                    310: /* Move session to next window. */
                    311: int
                    312: session_next(struct session *s, int activity)
                    313: {
                    314:        struct winlink  *wl;
                    315:
                    316:        if (s->curw == NULL)
                    317:                return (-1);
                    318:
                    319:        wl = winlink_next(&s->windows, s->curw);
                    320:        if (activity)
                    321:                wl = session_next_activity(s, wl);
                    322:        if (wl == NULL) {
                    323:                wl = RB_MIN(winlinks, &s->windows);
                    324:                if (activity && ((wl = session_next_activity(s, wl)) == NULL))
                    325:                        return (-1);
                    326:        }
                    327:        if (wl == s->curw)
                    328:                return (1);
                    329:        winlink_stack_remove(&s->lastw, wl);
                    330:        winlink_stack_push(&s->lastw, s->curw);
                    331:        s->curw = wl;
                    332:        session_alert_cancel(s, wl);
                    333:        return (0);
                    334: }
                    335:
                    336: struct winlink *
                    337: session_previous_activity(struct session *s, struct winlink *wl)
                    338: {
                    339:        while (wl != NULL) {
                    340:                if (session_alert_has(s, wl, WINDOW_BELL))
                    341:                        break;
                    342:                if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    343:                        break;
                    344:                if (session_alert_has(s, wl, WINDOW_CONTENT))
                    345:                        break;
                    346:                wl = winlink_previous(&s->windows, wl);
                    347:        }
                    348:        return (wl);
                    349: }
                    350:
                    351: /* Move session to previous window. */
                    352: int
                    353: session_previous(struct session *s, int activity)
                    354: {
                    355:        struct winlink  *wl;
                    356:
                    357:        if (s->curw == NULL)
                    358:                return (-1);
                    359:
                    360:        wl = winlink_previous(&s->windows, s->curw);
                    361:        if (activity)
                    362:                wl = session_previous_activity(s, wl);
                    363:        if (wl == NULL) {
                    364:                wl = RB_MAX(winlinks, &s->windows);
                    365:                if (activity && (wl = session_previous_activity(s, wl)) == NULL)
                    366:                        return (-1);
                    367:        }
                    368:        if (wl == s->curw)
                    369:                return (1);
                    370:        winlink_stack_remove(&s->lastw, wl);
                    371:        winlink_stack_push(&s->lastw, s->curw);
                    372:        s->curw = wl;
                    373:        session_alert_cancel(s, wl);
                    374:        return (0);
                    375: }
                    376:
                    377: /* Move session to specific window. */
                    378: int
                    379: session_select(struct session *s, int idx)
                    380: {
                    381:        struct winlink  *wl;
                    382:
                    383:        wl = winlink_find_by_index(&s->windows, idx);
                    384:        if (wl == NULL)
                    385:                return (-1);
                    386:        if (wl == s->curw)
                    387:                return (1);
                    388:        winlink_stack_remove(&s->lastw, wl);
                    389:        winlink_stack_push(&s->lastw, s->curw);
                    390:        s->curw = wl;
                    391:        session_alert_cancel(s, wl);
                    392:        return (0);
                    393: }
                    394:
                    395: /* Move session to last used window. */
                    396: int
                    397: session_last(struct session *s)
                    398: {
                    399:        struct winlink  *wl;
                    400:
                    401:        wl = SLIST_FIRST(&s->lastw);
                    402:        if (wl == NULL)
                    403:                return (-1);
                    404:        if (wl == s->curw)
                    405:                return (1);
                    406:
                    407:        winlink_stack_remove(&s->lastw, wl);
                    408:        winlink_stack_push(&s->lastw, s->curw);
                    409:        s->curw = wl;
                    410:        session_alert_cancel(s, wl);
                    411:        return (0);
                    412: }