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

1.9     ! nicm        1: /* $OpenBSD: session.c,v 1.8 2009/09/16 12:35:04 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)
1.9     ! nicm      129:                fatal("gettimeofday failed");
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.8       nicm      142:
                    143:        s->tio = NULL;
                    144:        if (tio != NULL) {
                    145:                s->tio = xmalloc(sizeof *s->tio);
                    146:                memcpy(s->tio, tio, sizeof *s->tio);
                    147:        }
1.1       nicm      148:
                    149:        s->sx = sx;
                    150:        s->sy = sy;
                    151:
                    152:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    153:                if (ARRAY_ITEM(&sessions, i) == NULL) {
                    154:                        ARRAY_SET(&sessions, i, s);
                    155:                        break;
                    156:                }
                    157:        }
                    158:        if (i == ARRAY_LENGTH(&sessions))
                    159:                ARRAY_ADD(&sessions, s);
                    160:
                    161:        if (name != NULL)
                    162:                s->name = xstrdup(name);
                    163:        else
                    164:                xasprintf(&s->name, "%u", i);
1.5       nicm      165:        if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
1.1       nicm      166:                session_destroy(s);
                    167:                return (NULL);
                    168:        }
1.5       nicm      169:        session_select(s, RB_ROOT(&s->windows)->idx);
1.1       nicm      170:
                    171:        log_debug("session %s created", s->name);
                    172:
                    173:        return (s);
                    174: }
                    175:
                    176: /* Destroy a session. */
                    177: void
                    178: session_destroy(struct session *s)
                    179: {
                    180:        u_int   i;
                    181:
                    182:        log_debug("session %s destroyed", s->name);
                    183:
                    184:        if (session_index(s, &i) != 0)
                    185:                fatalx("session not found");
                    186:        ARRAY_SET(&sessions, i, NULL);
                    187:        while (!ARRAY_EMPTY(&sessions) && ARRAY_LAST(&sessions) == NULL)
                    188:                ARRAY_TRUNC(&sessions, 1);
                    189:
1.8       nicm      190:        if (s->tio != NULL)
                    191:                xfree(s->tio);
                    192:
1.1       nicm      193:        session_alert_cancel(s, NULL);
1.3       nicm      194:        environ_free(&s->environ);
1.1       nicm      195:        options_free(&s->options);
                    196:        paste_free_stack(&s->buffers);
                    197:
                    198:        while (!SLIST_EMPTY(&s->lastw))
                    199:                winlink_stack_remove(&s->lastw, SLIST_FIRST(&s->lastw));
                    200:        while (!RB_EMPTY(&s->windows))
                    201:                winlink_remove(&s->windows, RB_ROOT(&s->windows));
                    202:
                    203:        xfree(s->name);
1.7       nicm      204:
                    205:        for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
                    206:                if (ARRAY_ITEM(&dead_sessions, i) == NULL) {
                    207:                        ARRAY_SET(&dead_sessions, i, s);
                    208:                        break;
                    209:                }
                    210:        }
                    211:        if (i == ARRAY_LENGTH(&dead_sessions))
                    212:                ARRAY_ADD(&dead_sessions, s);
                    213:        s->flags |= SESSION_DEAD;
1.1       nicm      214: }
                    215:
                    216: /* Find session index. */
                    217: int
                    218: session_index(struct session *s, u_int *i)
                    219: {
                    220:        for (*i = 0; *i < ARRAY_LENGTH(&sessions); (*i)++) {
                    221:                if (s == ARRAY_ITEM(&sessions, *i))
                    222:                        return (0);
                    223:        }
                    224:        return (-1);
                    225: }
                    226:
                    227: /* Create a new window on a session. */
                    228: struct winlink *
1.4       nicm      229: session_new(struct session *s,
1.1       nicm      230:     const char *name, const char *cmd, const char *cwd, int idx, char **cause)
                    231: {
                    232:        struct window   *w;
1.3       nicm      233:        struct environ   env;
1.6       nicm      234:        const char      *shell;
1.1       nicm      235:        u_int            hlimit;
                    236:
1.3       nicm      237:        environ_init(&env);
                    238:        environ_copy(&global_environ, &env);
                    239:        environ_copy(&s->environ, &env);
                    240:        server_fill_environ(s, &env);
1.1       nicm      241:
1.6       nicm      242:        shell = options_get_string(&s->options, "default-shell");
                    243:        if (*shell == '\0' || areshell(shell))
                    244:                shell = _PATH_BSHELL;
                    245:
1.1       nicm      246:        hlimit = options_get_number(&s->options, "history-limit");
1.4       nicm      247:        w = window_create(
1.8       nicm      248:            name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
1.3       nicm      249:        if (w == NULL) {
                    250:                environ_free(&env);
1.1       nicm      251:                return (NULL);
1.3       nicm      252:        }
                    253:        environ_free(&env);
1.1       nicm      254:
                    255:        if (options_get_number(&s->options, "set-remain-on-exit"))
                    256:                options_set_number(&w->options, "remain-on-exit", 1);
                    257:
                    258:        return (session_attach(s, w, idx, cause));
                    259: }
                    260:
                    261: /* Attach a window to a session. */
                    262: struct winlink *
                    263: session_attach(struct session *s, struct window *w, int idx, char **cause)
                    264: {
                    265:        struct winlink  *wl;
                    266:
                    267:        if ((wl = winlink_add(&s->windows, w, idx)) == NULL)
                    268:                xasprintf(cause, "index in use: %d", idx);
                    269:        return (wl);
                    270: }
                    271:
                    272: /* Detach a window from a session. */
                    273: int
                    274: session_detach(struct session *s, struct winlink *wl)
                    275: {
                    276:        if (s->curw == wl &&
                    277:            session_last(s) != 0 && session_previous(s, 0) != 0)
                    278:                session_next(s, 0);
                    279:
                    280:        session_alert_cancel(s, wl);
                    281:        winlink_stack_remove(&s->lastw, wl);
                    282:        winlink_remove(&s->windows, wl);
                    283:        if (RB_EMPTY(&s->windows)) {
                    284:                session_destroy(s);
                    285:                return (1);
                    286:        }
                    287:        return (0);
                    288: }
                    289:
                    290: /* Return if session has window. */
                    291: int
                    292: session_has(struct session *s, struct window *w)
                    293: {
                    294:        struct winlink  *wl;
                    295:
                    296:        RB_FOREACH(wl, winlinks, &s->windows) {
                    297:                if (wl->window == w)
                    298:                        return (1);
                    299:        }
                    300:        return (0);
                    301: }
                    302:
                    303: struct winlink *
                    304: session_next_activity(struct session *s, struct winlink *wl)
                    305: {
                    306:        while (wl != NULL) {
                    307:                if (session_alert_has(s, wl, WINDOW_BELL))
                    308:                        break;
                    309:                if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    310:                        break;
                    311:                if (session_alert_has(s, wl, WINDOW_CONTENT))
                    312:                        break;
                    313:                wl = winlink_next(&s->windows, wl);
                    314:        }
                    315:        return (wl);
                    316: }
                    317:
                    318: /* Move session to next window. */
                    319: int
                    320: session_next(struct session *s, int activity)
                    321: {
                    322:        struct winlink  *wl;
                    323:
                    324:        if (s->curw == NULL)
                    325:                return (-1);
                    326:
                    327:        wl = winlink_next(&s->windows, s->curw);
                    328:        if (activity)
                    329:                wl = session_next_activity(s, wl);
                    330:        if (wl == NULL) {
                    331:                wl = RB_MIN(winlinks, &s->windows);
                    332:                if (activity && ((wl = session_next_activity(s, wl)) == NULL))
                    333:                        return (-1);
                    334:        }
                    335:        if (wl == s->curw)
                    336:                return (1);
                    337:        winlink_stack_remove(&s->lastw, wl);
                    338:        winlink_stack_push(&s->lastw, s->curw);
                    339:        s->curw = wl;
                    340:        session_alert_cancel(s, wl);
                    341:        return (0);
                    342: }
                    343:
                    344: struct winlink *
                    345: session_previous_activity(struct session *s, struct winlink *wl)
                    346: {
                    347:        while (wl != NULL) {
                    348:                if (session_alert_has(s, wl, WINDOW_BELL))
                    349:                        break;
                    350:                if (session_alert_has(s, wl, WINDOW_ACTIVITY))
                    351:                        break;
                    352:                if (session_alert_has(s, wl, WINDOW_CONTENT))
                    353:                        break;
                    354:                wl = winlink_previous(&s->windows, wl);
                    355:        }
                    356:        return (wl);
                    357: }
                    358:
                    359: /* Move session to previous window. */
                    360: int
                    361: session_previous(struct session *s, int activity)
                    362: {
                    363:        struct winlink  *wl;
                    364:
                    365:        if (s->curw == NULL)
                    366:                return (-1);
                    367:
                    368:        wl = winlink_previous(&s->windows, s->curw);
                    369:        if (activity)
                    370:                wl = session_previous_activity(s, wl);
                    371:        if (wl == NULL) {
                    372:                wl = RB_MAX(winlinks, &s->windows);
                    373:                if (activity && (wl = session_previous_activity(s, wl)) == NULL)
                    374:                        return (-1);
                    375:        }
                    376:        if (wl == s->curw)
                    377:                return (1);
                    378:        winlink_stack_remove(&s->lastw, wl);
                    379:        winlink_stack_push(&s->lastw, s->curw);
                    380:        s->curw = wl;
                    381:        session_alert_cancel(s, wl);
                    382:        return (0);
                    383: }
                    384:
                    385: /* Move session to specific window. */
                    386: int
                    387: session_select(struct session *s, int idx)
                    388: {
                    389:        struct winlink  *wl;
                    390:
                    391:        wl = winlink_find_by_index(&s->windows, idx);
                    392:        if (wl == NULL)
                    393:                return (-1);
                    394:        if (wl == s->curw)
                    395:                return (1);
                    396:        winlink_stack_remove(&s->lastw, wl);
                    397:        winlink_stack_push(&s->lastw, s->curw);
                    398:        s->curw = wl;
                    399:        session_alert_cancel(s, wl);
                    400:        return (0);
                    401: }
                    402:
                    403: /* Move session to last used window. */
                    404: int
                    405: session_last(struct session *s)
                    406: {
                    407:        struct winlink  *wl;
                    408:
                    409:        wl = SLIST_FIRST(&s->lastw);
                    410:        if (wl == NULL)
                    411:                return (-1);
                    412:        if (wl == s->curw)
                    413:                return (1);
                    414:
                    415:        winlink_stack_remove(&s->lastw, wl);
                    416:        winlink_stack_push(&s->lastw, s->curw);
                    417:        s->curw = wl;
                    418:        session_alert_cancel(s, wl);
                    419:        return (0);
                    420: }