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

Annotation of src/usr.bin/tmux/window.c, Revision 1.62

1.62    ! nicm        1: /* $OpenBSD: window.c,v 1.61 2010/12/30 21:35:17 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/ioctl.h>
                     21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.5       nicm       24: #include <fnmatch.h>
1.1       nicm       25: #include <paths.h>
1.8       nicm       26: #include <pwd.h>
1.1       nicm       27: #include <signal.h>
                     28: #include <stdint.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <termios.h>
                     32: #include <unistd.h>
                     33: #include <util.h>
                     34:
                     35: #include "tmux.h"
                     36:
                     37: /*
1.14      nicm       38:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       39:  * file contains code to handle them.
                     40:  *
                     41:  * A pane has two buffers attached, these are filled and emptied by the main
                     42:  * server poll loop. Output data is received from pty's in screen format,
                     43:  * translated and returned as a series of escape sequences and strings via
                     44:  * input_parse (in input.c). Input data is received as key codes and written
                     45:  * directly via input_key.
                     46:  *
                     47:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     48:  * state and is redisplayed when the window is reattached to a client.
                     49:  *
                     50:  * Windows are stored directly on a global array and wrapped in any number of
                     51:  * winlink structs to be linked onto local session RB trees. A reference count
                     52:  * is maintained and a window removed from the global list and destroyed when
                     53:  * it reaches zero.
                     54:  */
                     55:
                     56: /* Global window list. */
                     57: struct windows windows;
                     58:
1.37      nicm       59: void   window_pane_read_callback(struct bufferevent *, void *);
                     60: void   window_pane_error_callback(struct bufferevent *, short, void *);
                     61:
1.1       nicm       62: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     63:
                     64: int
                     65: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     66: {
                     67:        return (wl1->idx - wl2->idx);
1.12      nicm       68: }
                     69:
                     70: struct winlink *
                     71: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                     72: {
                     73:        struct winlink  *wl;
                     74:
                     75:        RB_FOREACH(wl, winlinks, wwl) {
                     76:                if (wl->window == w)
                     77:                        return (wl);
                     78:        }
                     79:
                     80:        return (NULL);
1.1       nicm       81: }
                     82:
                     83: struct winlink *
                     84: winlink_find_by_index(struct winlinks *wwl, int idx)
                     85: {
                     86:        struct winlink  wl;
                     87:
                     88:        if (idx < 0)
                     89:                fatalx("bad index");
                     90:
                     91:        wl.idx = idx;
                     92:        return (RB_FIND(winlinks, wwl, &wl));
                     93: }
                     94:
                     95: int
1.22      nicm       96: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm       97: {
1.22      nicm       98:        int     i;
1.1       nicm       99:
1.22      nicm      100:        i = idx;
                    101:        do {
1.1       nicm      102:                if (winlink_find_by_index(wwl, i) == NULL)
                    103:                        return (i);
1.22      nicm      104:                if (i == INT_MAX)
                    105:                        i = 0;
                    106:                else
                    107:                        i++;
                    108:        } while (i != idx);
                    109:        return (-1);
1.1       nicm      110: }
                    111:
                    112: u_int
                    113: winlink_count(struct winlinks *wwl)
                    114: {
                    115:        struct winlink  *wl;
                    116:        u_int            n;
                    117:
                    118:        n = 0;
                    119:        RB_FOREACH(wl, winlinks, wwl)
                    120:                n++;
                    121:
                    122:        return (n);
                    123: }
                    124:
                    125: struct winlink *
                    126: winlink_add(struct winlinks *wwl, struct window *w, int idx)
                    127: {
                    128:        struct winlink  *wl;
                    129:
1.22      nicm      130:        if (idx < 0) {
                    131:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    132:                        return (NULL);
                    133:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      134:                return (NULL);
                    135:
                    136:        wl = xcalloc(1, sizeof *wl);
                    137:        wl->idx = idx;
                    138:        wl->window = w;
                    139:        RB_INSERT(winlinks, wwl, wl);
                    140:
                    141:        w->references++;
                    142:
                    143:        return (wl);
                    144: }
                    145:
                    146: void
                    147: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    148: {
                    149:        struct window   *w = wl->window;
                    150:
                    151:        RB_REMOVE(winlinks, wwl, wl);
1.40      nicm      152:        if (wl->status_text != NULL)
                    153:                xfree(wl->status_text);
1.1       nicm      154:        xfree(wl);
                    155:
                    156:        if (w->references == 0)
                    157:                fatal("bad reference count");
                    158:        w->references--;
                    159:        if (w->references == 0)
                    160:                window_destroy(w);
                    161: }
                    162:
                    163: struct winlink *
1.41      nicm      164: winlink_next(struct winlink *wl)
1.1       nicm      165: {
                    166:        return (RB_NEXT(winlinks, wwl, wl));
                    167: }
                    168:
                    169: struct winlink *
1.41      nicm      170: winlink_previous(struct winlink *wl)
1.1       nicm      171: {
                    172:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      173: }
                    174:
                    175: struct winlink *
1.53      nicm      176: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      177: {
                    178:        for (; n > 0; n--) {
                    179:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      180:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      181:        }
                    182:
                    183:        return (wl);
                    184: }
                    185:
                    186: struct winlink *
1.53      nicm      187: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      188: {
                    189:        for (; n > 0; n--) {
                    190:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      191:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      192:        }
                    193:
                    194:        return (wl);
1.1       nicm      195: }
                    196:
                    197: void
                    198: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    199: {
                    200:        if (wl == NULL)
                    201:                return;
                    202:
                    203:        winlink_stack_remove(stack, wl);
1.28      nicm      204:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      205: }
                    206:
                    207: void
                    208: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    209: {
                    210:        struct winlink  *wl2;
                    211:
                    212:        if (wl == NULL)
                    213:                return;
1.42      nicm      214:
1.28      nicm      215:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      216:                if (wl2 == wl) {
1.28      nicm      217:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      218:                        return;
                    219:                }
                    220:        }
                    221: }
                    222:
                    223: int
                    224: window_index(struct window *s, u_int *i)
                    225: {
                    226:        for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
                    227:                if (s == ARRAY_ITEM(&windows, *i))
                    228:                        return (0);
                    229:        }
                    230:        return (-1);
                    231: }
                    232:
                    233: struct window *
                    234: window_create1(u_int sx, u_int sy)
                    235: {
                    236:        struct window   *w;
                    237:        u_int            i;
                    238:
1.38      nicm      239:        w = xcalloc(1, sizeof *w);
1.1       nicm      240:        w->name = NULL;
                    241:        w->flags = 0;
                    242:
                    243:        TAILQ_INIT(&w->panes);
                    244:        w->active = NULL;
1.14      nicm      245:
1.17      nicm      246:        w->lastlayout = -1;
1.14      nicm      247:        w->layout_root = NULL;
1.42      nicm      248:
1.1       nicm      249:        w->sx = sx;
                    250:        w->sy = sy;
                    251:
1.38      nicm      252:        queue_window_name(w);
                    253:
1.7       nicm      254:        options_init(&w->options, &global_w_options);
1.1       nicm      255:
                    256:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    257:                if (ARRAY_ITEM(&windows, i) == NULL) {
                    258:                        ARRAY_SET(&windows, i, w);
                    259:                        break;
                    260:                }
                    261:        }
                    262:        if (i == ARRAY_LENGTH(&windows))
                    263:                ARRAY_ADD(&windows, w);
                    264:        w->references = 0;
                    265:
                    266:        return (w);
                    267: }
                    268:
                    269: struct window *
1.23      nicm      270: window_create(const char *name, const char *cmd, const char *shell,
                    271:     const char *cwd, struct environ *env, struct termios *tio,
                    272:     u_int sx, u_int sy, u_int hlimit,char **cause)
1.1       nicm      273: {
1.14      nicm      274:        struct window           *w;
                    275:        struct window_pane      *wp;
1.1       nicm      276:
                    277:        w = window_create1(sx, sy);
1.16      nicm      278:        wp = window_add_pane(w, hlimit);
1.14      nicm      279:        layout_init(w);
1.23      nicm      280:        if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1       nicm      281:                window_destroy(w);
                    282:                return (NULL);
                    283:        }
                    284:        w->active = TAILQ_FIRST(&w->panes);
                    285:        if (name != NULL) {
                    286:                w->name = xstrdup(name);
                    287:                options_set_number(&w->options, "automatic-rename", 0);
                    288:        } else
                    289:                w->name = default_window_name(w);
                    290:        return (w);
                    291: }
                    292:
                    293: void
                    294: window_destroy(struct window *w)
                    295: {
                    296:        u_int   i;
                    297:
                    298:        if (window_index(w, &i) != 0)
                    299:                fatalx("index not found");
                    300:        ARRAY_SET(&windows, i, NULL);
                    301:        while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
                    302:                ARRAY_TRUNC(&windows, 1);
                    303:
1.14      nicm      304:        if (w->layout_root != NULL)
                    305:                layout_free(w);
                    306:
1.38      nicm      307:        evtimer_del(&w->name_timer);
                    308:
1.1       nicm      309:        options_free(&w->options);
                    310:
                    311:        window_destroy_panes(w);
                    312:
                    313:        if (w->name != NULL)
                    314:                xfree(w->name);
                    315:        xfree(w);
                    316: }
                    317:
1.15      nicm      318: void
1.1       nicm      319: window_resize(struct window *w, u_int sx, u_int sy)
                    320: {
                    321:        w->sx = sx;
                    322:        w->sy = sy;
                    323: }
                    324:
                    325: void
                    326: window_set_active_pane(struct window *w, struct window_pane *wp)
                    327: {
1.59      nicm      328:        if (wp == w->active)
                    329:                return;
1.58      nicm      330:        w->last = w->active;
1.1       nicm      331:        w->active = wp;
1.10      nicm      332:        while (!window_pane_visible(w->active)) {
1.1       nicm      333:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      334:                if (w->active == NULL)
                    335:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    336:                if (w->active == wp)
                    337:                        return;
1.29      nicm      338:        }
                    339: }
                    340:
                    341: void
                    342: window_set_active_at(struct window *w, u_int x, u_int y)
                    343: {
                    344:        struct window_pane      *wp;
                    345:
                    346:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.59      nicm      347:                if (wp == w->active || !window_pane_visible(wp))
1.29      nicm      348:                        continue;
                    349:                if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    350:                        continue;
                    351:                if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    352:                        continue;
                    353:                window_set_active_pane(w, wp);
                    354:                break;
1.10      nicm      355:        }
1.1       nicm      356: }
                    357:
                    358: struct window_pane *
1.16      nicm      359: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      360: {
                    361:        struct window_pane      *wp;
                    362:
1.14      nicm      363:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      364:        if (TAILQ_EMPTY(&w->panes))
                    365:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    366:        else
                    367:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    368:        return (wp);
                    369: }
                    370:
                    371: void
                    372: window_remove_pane(struct window *w, struct window_pane *wp)
                    373: {
1.57      nicm      374:        if (wp == w->active) {
1.58      nicm      375:                w->active = w->last;
                    376:                w->last = NULL;
                    377:                if (w->active == NULL) {
                    378:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    379:                        if (w->active == NULL)
                    380:                                w->active = TAILQ_NEXT(wp, entry);
                    381:                }
                    382:        } else if (wp == w->last)
                    383:                w->last = NULL;
1.1       nicm      384:
                    385:        TAILQ_REMOVE(&w->panes, wp, entry);
                    386:        window_pane_destroy(wp);
                    387: }
                    388:
                    389: struct window_pane *
                    390: window_pane_at_index(struct window *w, u_int idx)
                    391: {
                    392:        struct window_pane      *wp;
                    393:        u_int                    n;
                    394:
                    395:        n = 0;
                    396:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    397:                if (n == idx)
                    398:                        return (wp);
                    399:                n++;
                    400:        }
                    401:        return (NULL);
1.53      nicm      402: }
                    403:
                    404: struct window_pane *
                    405: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    406: {
                    407:        for (; n > 0; n--) {
                    408:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    409:                        wp = TAILQ_FIRST(&w->panes);
                    410:        }
                    411:
                    412:        return (wp);
                    413: }
                    414:
                    415: struct window_pane *
                    416: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    417:     u_int n)
                    418: {
                    419:        for (; n > 0; n--) {
                    420:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    421:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    422:        }
                    423:
                    424:        return (wp);
1.13      nicm      425: }
                    426:
                    427: u_int
                    428: window_pane_index(struct window *w, struct window_pane *wp)
                    429: {
                    430:        struct window_pane      *wq;
                    431:        u_int                    n;
                    432:
                    433:        n = 0;
                    434:        TAILQ_FOREACH(wq, &w->panes, entry) {
                    435:                if (wp == wq)
                    436:                        break;
                    437:                n++;
                    438:        }
                    439:        return (n);
1.1       nicm      440: }
                    441:
                    442: u_int
                    443: window_count_panes(struct window *w)
                    444: {
                    445:        struct window_pane      *wp;
                    446:        u_int                    n;
                    447:
                    448:        n = 0;
                    449:        TAILQ_FOREACH(wp, &w->panes, entry)
                    450:                n++;
                    451:        return (n);
                    452: }
                    453:
                    454: void
                    455: window_destroy_panes(struct window *w)
                    456: {
                    457:        struct window_pane      *wp;
                    458:
                    459:        while (!TAILQ_EMPTY(&w->panes)) {
                    460:                wp = TAILQ_FIRST(&w->panes);
                    461:                TAILQ_REMOVE(&w->panes, wp, entry);
                    462:                window_pane_destroy(wp);
                    463:        }
1.61      nicm      464: }
                    465:
                    466: /* Return list of printable window flag symbols. No flags is just a space. */
                    467: char *
                    468: window_printable_flags(struct session *s, struct winlink *wl)
                    469: {
                    470:        char    flags[BUFSIZ];
                    471:        int     pos;
                    472:
                    473:        pos = 0;
                    474:        if (wl->flags & WINLINK_ACTIVITY)
                    475:                flags[pos++] = '#';
                    476:        if (wl->flags & WINLINK_BELL)
                    477:                flags[pos++] = '!';
                    478:        if (wl->flags & WINLINK_CONTENT)
                    479:                flags[pos++] = '+';
                    480:        if (wl->flags & WINLINK_SILENCE)
                    481:                flags[pos++] = '~';
                    482:        if (wl == s->curw)
                    483:                flags[pos++] = '*';
                    484:        if (wl == TAILQ_FIRST(&s->lastw))
                    485:                flags[pos++] = '-';
                    486:        if (pos == 0)
                    487:                flags[pos++] = ' ';
                    488:        flags[pos] = '\0';
                    489:        return (xstrdup(flags));
1.1       nicm      490: }
                    491:
                    492: struct window_pane *
                    493: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    494: {
                    495:        struct window_pane      *wp;
                    496:
                    497:        wp = xcalloc(1, sizeof *wp);
                    498:        wp->window = w;
                    499:
                    500:        wp->cmd = NULL;
1.23      nicm      501:        wp->shell = NULL;
1.1       nicm      502:        wp->cwd = NULL;
                    503:
                    504:        wp->fd = -1;
1.37      nicm      505:        wp->event = NULL;
1.1       nicm      506:
                    507:        wp->mode = NULL;
1.14      nicm      508:
                    509:        wp->layout_cell = NULL;
1.1       nicm      510:
                    511:        wp->xoff = 0;
1.42      nicm      512:        wp->yoff = 0;
1.1       nicm      513:
                    514:        wp->sx = sx;
                    515:        wp->sy = sy;
                    516:
1.32      nicm      517:        wp->pipe_fd = -1;
                    518:        wp->pipe_off = 0;
1.36      nicm      519:        wp->pipe_event = NULL;
1.32      nicm      520:
1.9       nicm      521:        wp->saved_grid = NULL;
                    522:
1.1       nicm      523:        screen_init(&wp->base, sx, sy, hlimit);
                    524:        wp->screen = &wp->base;
                    525:
                    526:        input_init(wp);
                    527:
                    528:        return (wp);
                    529: }
                    530:
                    531: void
                    532: window_pane_destroy(struct window_pane *wp)
                    533: {
1.55      nicm      534:        window_pane_reset_mode(wp);
                    535:
1.37      nicm      536:        if (wp->fd != -1) {
1.1       nicm      537:                close(wp->fd);
1.37      nicm      538:                bufferevent_free(wp->event);
                    539:        }
1.1       nicm      540:
                    541:        input_free(wp);
                    542:
                    543:        screen_free(&wp->base);
1.9       nicm      544:        if (wp->saved_grid != NULL)
                    545:                grid_destroy(wp->saved_grid);
1.1       nicm      546:
1.32      nicm      547:        if (wp->pipe_fd != -1) {
                    548:                close(wp->pipe_fd);
1.36      nicm      549:                bufferevent_free(wp->pipe_event);
1.32      nicm      550:        }
                    551:
1.1       nicm      552:        if (wp->cwd != NULL)
                    553:                xfree(wp->cwd);
1.23      nicm      554:        if (wp->shell != NULL)
                    555:                xfree(wp->shell);
1.1       nicm      556:        if (wp->cmd != NULL)
                    557:                xfree(wp->cmd);
                    558:        xfree(wp);
                    559: }
                    560:
                    561: int
1.23      nicm      562: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21      nicm      563:     const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1       nicm      564: {
1.47      nicm      565:        struct winsize   ws;
                    566:        char            *argv0;
                    567:        const char      *ptr;
                    568:        struct termios   tio2;
1.1       nicm      569:
1.37      nicm      570:        if (wp->fd != -1) {
1.1       nicm      571:                close(wp->fd);
1.37      nicm      572:                bufferevent_free(wp->event);
                    573:        }
1.1       nicm      574:        if (cmd != NULL) {
                    575:                if (wp->cmd != NULL)
                    576:                        xfree(wp->cmd);
                    577:                wp->cmd = xstrdup(cmd);
                    578:        }
1.23      nicm      579:        if (shell != NULL) {
                    580:                if (wp->shell != NULL)
                    581:                        xfree(wp->shell);
                    582:                wp->shell = xstrdup(shell);
                    583:        }
1.1       nicm      584:        if (cwd != NULL) {
                    585:                if (wp->cwd != NULL)
                    586:                        xfree(wp->cwd);
                    587:                wp->cwd = xstrdup(cwd);
                    588:        }
                    589:
                    590:        memset(&ws, 0, sizeof ws);
                    591:        ws.ws_col = screen_size_x(&wp->base);
                    592:        ws.ws_row = screen_size_y(&wp->base);
                    593:
1.42      nicm      594:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      595:        case -1:
                    596:                wp->fd = -1;
                    597:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
                    598:                return (-1);
                    599:        case 0:
                    600:                if (chdir(wp->cwd) != 0)
                    601:                        chdir("/");
1.25      nicm      602:
                    603:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    604:                        fatal("tcgetattr failed");
                    605:                if (tio != NULL)
                    606:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    607:                tio2.c_cc[VERASE] = '\177';
                    608:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    609:                        fatal("tcgetattr failed");
1.18      nicm      610:
1.56      nicm      611:                closefrom(STDERR_FILENO + 1);
                    612:
1.47      nicm      613:                environ_push(env);
1.18      nicm      614:
1.54      nicm      615:                clear_signals(1);
1.1       nicm      616:                log_close();
                    617:
1.8       nicm      618:                if (*wp->cmd != '\0') {
1.24      nicm      619:                        /* Set SHELL but only if it is currently not useful. */
                    620:                        shell = getenv("SHELL");
                    621:                        if (shell == NULL || *shell == '\0' || areshell(shell))
                    622:                                setenv("SHELL", wp->shell, 1);
                    623:
1.8       nicm      624:                        execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
                    625:                        fatal("execl failed");
                    626:                }
                    627:
                    628:                /* No command; fork a login shell. */
1.23      nicm      629:                ptr = strrchr(wp->shell, '/');
                    630:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      631:                        xasprintf(&argv0, "-%s", ptr + 1);
                    632:                else
1.23      nicm      633:                        xasprintf(&argv0, "-%s", wp->shell);
1.24      nicm      634:                setenv("SHELL", wp->shell, 1);
1.23      nicm      635:                execl(wp->shell, argv0, (char *) NULL);
1.1       nicm      636:                fatal("execl failed");
                    637:        }
                    638:
1.62    ! nicm      639:        setblocking(wp->fd, 0);
        !           640:
1.37      nicm      641:        wp->event = bufferevent_new(wp->fd,
                    642:            window_pane_read_callback, NULL, window_pane_error_callback, wp);
                    643:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      644:
                    645:        return (0);
                    646: }
                    647:
1.41      nicm      648: /* ARGSUSED */
1.15      nicm      649: void
1.37      nicm      650: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    651: {
1.46      nicm      652:        struct window_pane     *wp = data;
                    653:        char                   *new_data;
                    654:        size_t                  new_size;
                    655:
                    656:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
                    657:        if (wp->pipe_fd != -1 && new_size > 0) {
                    658:                new_data = EVBUFFER_DATA(wp->event->input);
                    659:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    660:        }
                    661:
                    662:        input_parse(wp);
1.37      nicm      663:
1.46      nicm      664:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60      nicm      665:
                    666:        /*
                    667:         * If we get here, we're not outputting anymore, so set the silence
                    668:         * flag on the window.
                    669:         */
                    670:        wp->window->flags |= WINDOW_SILENCE;
                    671:        if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
                    672:                fatal("gettimeofday failed.");
1.37      nicm      673: }
                    674:
1.41      nicm      675: /* ARGSUSED */
1.37      nicm      676: void
                    677: window_pane_error_callback(
                    678:     unused struct bufferevent *bufev, unused short what, void *data)
                    679: {
                    680:        struct window_pane *wp = data;
                    681:
1.39      nicm      682:        server_destroy_pane(wp);
1.37      nicm      683: }
                    684:
                    685: void
1.1       nicm      686: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    687: {
                    688:        struct winsize  ws;
                    689:
                    690:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      691:                return;
1.1       nicm      692:        wp->sx = sx;
                    693:        wp->sy = sy;
                    694:
                    695:        memset(&ws, 0, sizeof ws);
                    696:        ws.ws_col = sx;
                    697:        ws.ws_row = sy;
                    698:
                    699:        screen_resize(&wp->base, sx, sy);
                    700:        if (wp->mode != NULL)
                    701:                wp->mode->resize(wp, sx, sy);
                    702:
                    703:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    704:                fatal("ioctl failed");
1.44      nicm      705: }
                    706:
                    707: /*
                    708:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    709:  * history is not updated
                    710:  */
                    711: void
                    712: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
                    713: {
                    714:        struct screen   *s = &wp->base;
                    715:        u_int            sx, sy;
                    716:
                    717:        if (wp->saved_grid != NULL)
                    718:                return;
                    719:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    720:                return;
                    721:        sx = screen_size_x(s);
                    722:        sy = screen_size_y(s);
                    723:
                    724:        wp->saved_grid = grid_create(sx, sy, 0);
                    725:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    726:        wp->saved_cx = s->cx;
                    727:        wp->saved_cy = s->cy;
                    728:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    729:
                    730:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    731:
                    732:        wp->base.grid->flags &= ~GRID_HISTORY;
                    733:
                    734:        wp->flags |= PANE_REDRAW;
                    735: }
                    736:
                    737: /* Exit alternate screen mode and restore the copied grid. */
                    738: void
                    739: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
                    740: {
                    741:        struct screen   *s = &wp->base;
                    742:        u_int            sx, sy;
                    743:
                    744:        if (wp->saved_grid == NULL)
                    745:                return;
                    746:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    747:                return;
                    748:        sx = screen_size_x(s);
                    749:        sy = screen_size_y(s);
                    750:
                    751:        /*
                    752:         * If the current size is bigger, temporarily resize to the old size
                    753:         * before copying back.
                    754:         */
                    755:        if (sy > wp->saved_grid->sy)
                    756:                screen_resize(s, sx, wp->saved_grid->sy);
                    757:
                    758:        /* Restore the grid, cursor position and cell. */
                    759:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                    760:        s->cx = wp->saved_cx;
                    761:        if (s->cx > screen_size_x(s) - 1)
                    762:                s->cx = screen_size_x(s) - 1;
                    763:        s->cy = wp->saved_cy;
                    764:        if (s->cy > screen_size_y(s) - 1)
                    765:                s->cy = screen_size_y(s) - 1;
                    766:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                    767:
                    768:        /*
                    769:         * Turn history back on (so resize can use it) and then resize back to
                    770:         * the current size.
                    771:         */
                    772:        wp->base.grid->flags |= GRID_HISTORY;
                    773:        if (sy > wp->saved_grid->sy)
                    774:                screen_resize(s, sx, sy);
                    775:
                    776:        grid_destroy(wp->saved_grid);
                    777:        wp->saved_grid = NULL;
                    778:
                    779:        wp->flags |= PANE_REDRAW;
1.1       nicm      780: }
                    781:
                    782: int
                    783: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                    784: {
                    785:        struct screen   *s;
                    786:
1.15      nicm      787:        if (wp->mode != NULL)
1.1       nicm      788:                return (1);
                    789:        wp->mode = mode;
                    790:
                    791:        if ((s = wp->mode->init(wp)) != NULL)
                    792:                wp->screen = s;
1.34      nicm      793:        wp->flags |= PANE_REDRAW;
1.1       nicm      794:        return (0);
                    795: }
                    796:
                    797: void
                    798: window_pane_reset_mode(struct window_pane *wp)
                    799: {
                    800:        if (wp->mode == NULL)
                    801:                return;
                    802:
                    803:        wp->mode->free(wp);
                    804:        wp->mode = NULL;
                    805:
                    806:        wp->screen = &wp->base;
1.34      nicm      807:        wp->flags |= PANE_REDRAW;
1.1       nicm      808: }
                    809:
                    810: void
1.51      nicm      811: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1       nicm      812: {
1.27      nicm      813:        struct window_pane      *wp2;
                    814:
1.30      nicm      815:        if (!window_pane_visible(wp))
1.3       nicm      816:                return;
                    817:
1.1       nicm      818:        if (wp->mode != NULL) {
                    819:                if (wp->mode->key != NULL)
1.51      nicm      820:                        wp->mode->key(wp, sess, key);
1.27      nicm      821:                return;
1.30      nicm      822:        }
1.27      nicm      823:
1.30      nicm      824:        if (wp->fd == -1)
                    825:                return;
1.27      nicm      826:        input_key(wp, key);
                    827:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                    828:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    829:                        if (wp2 == wp || wp2->mode != NULL)
                    830:                                continue;
                    831:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                    832:                                input_key(wp2, key);
                    833:                }
                    834:        }
1.1       nicm      835: }
                    836:
                    837: void
                    838: window_pane_mouse(
1.51      nicm      839:     struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1       nicm      840: {
1.30      nicm      841:        if (!window_pane_visible(wp))
1.3       nicm      842:                return;
                    843:
1.31      nicm      844:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm      845:                return;
1.31      nicm      846:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm      847:                return;
1.31      nicm      848:        m->x -= wp->xoff;
                    849:        m->y -= wp->yoff;
1.1       nicm      850:
                    851:        if (wp->mode != NULL) {
                    852:                if (wp->mode->mouse != NULL)
1.51      nicm      853:                        wp->mode->mouse(wp, sess, m);
1.30      nicm      854:        } else if (wp->fd != -1)
1.31      nicm      855:                input_mouse(wp, m);
1.10      nicm      856: }
                    857:
                    858: int
                    859: window_pane_visible(struct window_pane *wp)
                    860: {
                    861:        struct window   *w = wp->window;
                    862:
                    863:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                    864:                return (0);
                    865:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                    866:                return (0);
                    867:        return (1);
1.1       nicm      868: }
                    869:
                    870: char *
1.5       nicm      871: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1       nicm      872: {
1.4       nicm      873:        struct screen   *s = &wp->base;
1.5       nicm      874:        char            *newsearchstr, *line, *msg;
1.4       nicm      875:        u_int            i;
                    876:
1.5       nicm      877:        msg = NULL;
                    878:        xasprintf(&newsearchstr, "*%s*", searchstr);
                    879:
1.4       nicm      880:        for (i = 0; i < screen_size_y(s); i++) {
                    881:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm      882:                if (fnmatch(newsearchstr, line, 0) == 0) {
                    883:                        msg = line;
                    884:                        if (lineno != NULL)
                    885:                                *lineno = i;
                    886:                        break;
                    887:                }
1.4       nicm      888:                xfree(line);
                    889:        }
1.5       nicm      890:
                    891:        xfree(newsearchstr);
                    892:        return (msg);
1.45      nicm      893: }
                    894:
                    895: /* Find the pane directly above another. */
                    896: struct window_pane *
                    897: window_pane_find_up(struct window_pane *wp)
                    898: {
                    899:        struct window_pane     *wp2;
                    900:        u_int                   left, top;
                    901:
                    902:        if (wp == NULL || !window_pane_visible(wp))
                    903:                return (NULL);
                    904:
                    905:        top = wp->yoff;
                    906:        if (top == 0)
                    907:                top = wp->window->sy + 1;
                    908:        left = wp->xoff;
                    909:
                    910:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    911:                if (!window_pane_visible(wp2))
                    912:                        continue;
                    913:                if (wp2->yoff + wp2->sy + 1 != top)
                    914:                        continue;
                    915:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    916:                        return (wp2);
                    917:        }
                    918:        return (NULL);
                    919: }
                    920:
                    921: /* Find the pane directly below another. */
                    922: struct window_pane *
                    923: window_pane_find_down(struct window_pane *wp)
                    924: {
                    925:        struct window_pane     *wp2;
                    926:        u_int                   left, bottom;
                    927:
                    928:        if (wp == NULL || !window_pane_visible(wp))
                    929:                return (NULL);
                    930:
                    931:        bottom = wp->yoff + wp->sy + 1;
                    932:        if (bottom >= wp->window->sy)
                    933:                bottom = 0;
                    934:        left = wp->xoff;
                    935:
                    936:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    937:                if (!window_pane_visible(wp2))
                    938:                        continue;
                    939:                if (wp2->yoff != bottom)
                    940:                        continue;
                    941:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    942:                        return (wp2);
                    943:        }
                    944:        return (NULL);
                    945: }
                    946:
                    947: /*
                    948:  * Find the pane directly to the left of another, adjacent to the left side and
                    949:  * containing the top edge.
                    950:  */
                    951: struct window_pane *
                    952: window_pane_find_left(struct window_pane *wp)
                    953: {
                    954:        struct window_pane     *wp2;
                    955:        u_int                   left, top;
                    956:
                    957:        if (wp == NULL || !window_pane_visible(wp))
                    958:                return (NULL);
                    959:
                    960:        left = wp->xoff;
                    961:        if (left == 0)
                    962:                left = wp->window->sx + 1;
                    963:        top = wp->yoff;
                    964:
                    965:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    966:                if (!window_pane_visible(wp2))
                    967:                        continue;
                    968:                if (wp2->xoff + wp2->sx + 1 != left)
                    969:                        continue;
                    970:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                    971:                        return (wp2);
                    972:        }
                    973:        return (NULL);
                    974: }
                    975:
                    976: /*
                    977:  * Find the pane directly to the right of another, that is adjacent to the
                    978:  * right edge and including the top edge.
                    979:  */
                    980: struct window_pane *
                    981: window_pane_find_right(struct window_pane *wp)
                    982: {
                    983:        struct window_pane     *wp2;
                    984:        u_int                   right, top;
                    985:
                    986:        if (wp == NULL || !window_pane_visible(wp))
                    987:                return (NULL);
                    988:
                    989:        right = wp->xoff + wp->sx + 1;
                    990:        if (right >= wp->window->sx)
                    991:                right = 0;
                    992:        top = wp->yoff;
                    993:
                    994:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    995:                if (!window_pane_visible(wp2))
                    996:                        continue;
                    997:                if (wp2->xoff != right)
                    998:                        continue;
                    999:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                   1000:                        return (wp2);
                   1001:        }
                   1002:        return (NULL);
1.1       nicm     1003: }