Annotation of src/usr.bin/tmux/window.c, Revision 1.8
1.8 ! nicm 1: /* $OpenBSD: window.c,v 1.7 2009/07/07 19:49:19 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: /*
38: * Each window is attached to one or two panes, each of which is a pty. This
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:
59: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
60:
1.8 ! nicm 61: const char *
! 62: window_default_command(void)
! 63: {
! 64: const char *shell;
! 65: struct passwd *pw;
! 66:
! 67: shell = getenv("SHELL");
! 68: if (shell != NULL && *shell != '\0')
! 69: return (shell);
! 70:
! 71: pw = getpwuid(getuid());
! 72: if (pw != NULL && pw->pw_shell != NULL && *pw->pw_shell != '\0')
! 73: return (pw->pw_shell);
! 74:
! 75: return (_PATH_BSHELL);
! 76: }
! 77:
1.1 nicm 78: int
79: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
80: {
81: return (wl1->idx - wl2->idx);
82: }
83:
84: struct winlink *
85: winlink_find_by_index(struct winlinks *wwl, int idx)
86: {
87: struct winlink wl;
88:
89: if (idx < 0)
90: fatalx("bad index");
91:
92: wl.idx = idx;
93: return (RB_FIND(winlinks, wwl, &wl));
94: }
95:
96: int
97: winlink_next_index(struct winlinks *wwl)
98: {
99: u_int i;
100:
101: for (i = 0; i < INT_MAX; i++) {
102: if (winlink_find_by_index(wwl, i) == NULL)
103: return (i);
104: }
105:
106: fatalx("no free indexes");
107: }
108:
109: u_int
110: winlink_count(struct winlinks *wwl)
111: {
112: struct winlink *wl;
113: u_int n;
114:
115: n = 0;
116: RB_FOREACH(wl, winlinks, wwl)
117: n++;
118:
119: return (n);
120: }
121:
122: struct winlink *
123: winlink_add(struct winlinks *wwl, struct window *w, int idx)
124: {
125: struct winlink *wl;
126:
127: if (idx == -1)
128: idx = winlink_next_index(wwl);
129: else if (winlink_find_by_index(wwl, idx) != NULL)
130: return (NULL);
131:
132: if (idx < 0)
133: fatalx("bad index");
134:
135: wl = xcalloc(1, sizeof *wl);
136: wl->idx = idx;
137: wl->window = w;
138: RB_INSERT(winlinks, wwl, wl);
139:
140: w->references++;
141:
142: return (wl);
143: }
144:
145: void
146: winlink_remove(struct winlinks *wwl, struct winlink *wl)
147: {
148: struct window *w = wl->window;
149:
150: RB_REMOVE(winlinks, wwl, wl);
151: xfree(wl);
152:
153: if (w->references == 0)
154: fatal("bad reference count");
155: w->references--;
156: if (w->references == 0)
157: window_destroy(w);
158: }
159:
160: struct winlink *
161: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
162: {
163: return (RB_NEXT(winlinks, wwl, wl));
164: }
165:
166: struct winlink *
167: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
168: {
169: return (RB_PREV(winlinks, wwl, wl));
170: }
171:
172: void
173: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
174: {
175: if (wl == NULL)
176: return;
177:
178: winlink_stack_remove(stack, wl);
179: SLIST_INSERT_HEAD(stack, wl, sentry);
180: }
181:
182: void
183: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
184: {
185: struct winlink *wl2;
186:
187: if (wl == NULL)
188: return;
189:
190: SLIST_FOREACH(wl2, stack, sentry) {
191: if (wl2 == wl) {
192: SLIST_REMOVE(stack, wl, winlink, sentry);
193: return;
194: }
195: }
196: }
197:
198: int
199: window_index(struct window *s, u_int *i)
200: {
201: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
202: if (s == ARRAY_ITEM(&windows, *i))
203: return (0);
204: }
205: return (-1);
206: }
207:
208: struct window *
209: window_create1(u_int sx, u_int sy)
210: {
211: struct window *w;
212: u_int i;
213:
214: w = xmalloc(sizeof *w);
215: w->name = NULL;
216: w->flags = 0;
217:
218: TAILQ_INIT(&w->panes);
219: w->active = NULL;
220: w->layout = 0;
221:
222: w->sx = sx;
223: w->sy = sy;
224:
1.7 nicm 225: options_init(&w->options, &global_w_options);
1.1 nicm 226:
227: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
228: if (ARRAY_ITEM(&windows, i) == NULL) {
229: ARRAY_SET(&windows, i, w);
230: break;
231: }
232: }
233: if (i == ARRAY_LENGTH(&windows))
234: ARRAY_ADD(&windows, w);
235: w->references = 0;
236:
237: return (w);
238: }
239:
240: struct window *
241: window_create(const char *name, const char *cmd, const char *cwd,
242: const char **envp, u_int sx, u_int sy, u_int hlimit, char **cause)
243: {
244: struct window *w;
245:
246: w = window_create1(sx, sy);
247: if (window_add_pane(w, -1, cmd, cwd, envp, hlimit, cause) == NULL) {
248: window_destroy(w);
249: return (NULL);
250: }
251: w->active = TAILQ_FIRST(&w->panes);
252:
253: if (name != NULL) {
254: w->name = xstrdup(name);
255: options_set_number(&w->options, "automatic-rename", 0);
256: } else
257: w->name = default_window_name(w);
258: return (w);
259: }
260:
261: void
262: window_destroy(struct window *w)
263: {
264: u_int i;
265:
266: if (window_index(w, &i) != 0)
267: fatalx("index not found");
268: ARRAY_SET(&windows, i, NULL);
269: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
270: ARRAY_TRUNC(&windows, 1);
271:
272: options_free(&w->options);
273:
274: window_destroy_panes(w);
275:
276: if (w->name != NULL)
277: xfree(w->name);
278: xfree(w);
279: }
280:
281: int
282: window_resize(struct window *w, u_int sx, u_int sy)
283: {
284: w->sx = sx;
285: w->sy = sy;
286:
287: return (0);
288: }
289:
290: void
291: window_set_active_pane(struct window *w, struct window_pane *wp)
292: {
293: w->active = wp;
294: while (w->active->flags & PANE_HIDDEN)
295: w->active = TAILQ_PREV(w->active, window_panes, entry);
296: }
297:
298: struct window_pane *
299: window_add_pane(struct window *w, int wanty, const char *cmd,
300: const char *cwd, const char **envp, u_int hlimit, char **cause)
301: {
302: struct window_pane *wp;
303: u_int sizey;
304:
305: if (TAILQ_EMPTY(&w->panes))
306: wanty = w->sy;
307: else {
308: sizey = w->active->sy - 1; /* for separator */
309: if (sizey < PANE_MINIMUM * 2) {
310: *cause = xstrdup("pane too small");
311: return (NULL);
312: }
313:
314: if (wanty == -1)
315: wanty = sizey / 2;
316:
317: if (wanty < PANE_MINIMUM)
318: wanty = PANE_MINIMUM;
319: if ((u_int) wanty > sizey - PANE_MINIMUM)
320: wanty = sizey - PANE_MINIMUM;
321:
322: window_pane_resize(w->active, w->sx, sizey - wanty);
323: }
324:
325: wp = window_pane_create(w, w->sx, wanty, hlimit);
326: if (TAILQ_EMPTY(&w->panes))
327: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
328: else
329: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
330: if (window_pane_spawn(wp, cmd, cwd, envp, cause) != 0) {
331: window_remove_pane(w, wp);
332: return (NULL);
333: }
334: return (wp);
335: }
336:
337: void
338: window_remove_pane(struct window *w, struct window_pane *wp)
339: {
340: w->active = TAILQ_PREV(wp, window_panes, entry);
341: if (w->active == NULL)
342: w->active = TAILQ_NEXT(wp, entry);
343:
344: TAILQ_REMOVE(&w->panes, wp, entry);
345: window_pane_destroy(wp);
346: }
347:
348: struct window_pane *
349: window_pane_at_index(struct window *w, u_int idx)
350: {
351: struct window_pane *wp;
352: u_int n;
353:
354: n = 0;
355: TAILQ_FOREACH(wp, &w->panes, entry) {
356: if (n == idx)
357: return (wp);
358: n++;
359: }
360: return (NULL);
361: }
362:
363: u_int
364: window_count_panes(struct window *w)
365: {
366: struct window_pane *wp;
367: u_int n;
368:
369: n = 0;
370: TAILQ_FOREACH(wp, &w->panes, entry)
371: n++;
372: return (n);
373: }
374:
375: void
376: window_destroy_panes(struct window *w)
377: {
378: struct window_pane *wp;
379:
380: while (!TAILQ_EMPTY(&w->panes)) {
381: wp = TAILQ_FIRST(&w->panes);
382: TAILQ_REMOVE(&w->panes, wp, entry);
383: window_pane_destroy(wp);
384: }
385: }
386:
387: struct window_pane *
388: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
389: {
390: struct window_pane *wp;
391:
392: wp = xcalloc(1, sizeof *wp);
393: wp->window = w;
394:
395: wp->cmd = NULL;
396: wp->cwd = NULL;
397:
398: wp->fd = -1;
399: wp->in = buffer_create(BUFSIZ);
400: wp->out = buffer_create(BUFSIZ);
401:
402: wp->mode = NULL;
403:
404: wp->xoff = 0;
405: wp->yoff = 0;
406:
407: wp->sx = sx;
408: wp->sy = sy;
409:
410: screen_init(&wp->base, sx, sy, hlimit);
411: wp->screen = &wp->base;
412:
413: input_init(wp);
414:
415: return (wp);
416: }
417:
418: void
419: window_pane_destroy(struct window_pane *wp)
420: {
421: if (wp->fd != -1)
422: close(wp->fd);
423:
424: input_free(wp);
425:
426: window_pane_reset_mode(wp);
427: screen_free(&wp->base);
428:
429: buffer_destroy(wp->in);
430: buffer_destroy(wp->out);
431:
432: if (wp->cwd != NULL)
433: xfree(wp->cwd);
434: if (wp->cmd != NULL)
435: xfree(wp->cmd);
436: xfree(wp);
437: }
438:
439: int
440: window_pane_spawn(struct window_pane *wp,
441: const char *cmd, const char *cwd, const char **envp, char **cause)
442: {
443: struct winsize ws;
444: int mode;
1.8 ! nicm 445: const char **envq, *ptr;
! 446: char *argv0;
1.1 nicm 447: struct timeval tv;
448:
449: if (wp->fd != -1)
450: close(wp->fd);
451: if (cmd != NULL) {
452: if (wp->cmd != NULL)
453: xfree(wp->cmd);
454: wp->cmd = xstrdup(cmd);
455: }
456: if (cwd != NULL) {
457: if (wp->cwd != NULL)
458: xfree(wp->cwd);
459: wp->cwd = xstrdup(cwd);
460: }
461:
462: memset(&ws, 0, sizeof ws);
463: ws.ws_col = screen_size_x(&wp->base);
464: ws.ws_row = screen_size_y(&wp->base);
465:
466: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
467: fatal("gettimeofday");
468: tv.tv_sec = 0;
469: tv.tv_usec = NAME_INTERVAL * 1000L;
470: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
471:
472: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
473: case -1:
474: wp->fd = -1;
475: xasprintf(cause, "%s: %s", cmd, strerror(errno));
476: return (-1);
477: case 0:
478: if (chdir(wp->cwd) != 0)
479: chdir("/");
480: for (envq = envp; *envq != NULL; envq++) {
1.2 nicm 481: if (putenv(xstrdup(*envq)) != 0)
1.1 nicm 482: fatal("putenv failed");
483: }
484: sigreset();
485: log_close();
486:
1.8 ! nicm 487: if (*wp->cmd != '\0') {
! 488: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
! 489: fatal("execl failed");
! 490: }
! 491:
! 492: /* No command; fork a login shell. */
! 493: cmd = window_default_command();
! 494: if ((ptr = strrchr(cmd, '/')) != NULL && *(ptr + 1) != '\0')
! 495: xasprintf(&argv0, "-%s", ptr + 1);
! 496: else
! 497: xasprintf(&argv0, "-%s", cmd);
! 498: execl(cmd, argv0, (char *) NULL);
1.1 nicm 499: fatal("execl failed");
500: }
501:
502: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
503: fatal("fcntl failed");
504: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
505: fatal("fcntl failed");
506: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
507: fatal("fcntl failed");
508:
509: return (0);
510: }
511:
512: int
513: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
514: {
515: struct winsize ws;
516:
517: if (sx == wp->sx && sy == wp->sy)
518: return (-1);
519: wp->sx = sx;
520: wp->sy = sy;
521:
522: memset(&ws, 0, sizeof ws);
523: ws.ws_col = sx;
524: ws.ws_row = sy;
525:
526: screen_resize(&wp->base, sx, sy);
527: if (wp->mode != NULL)
528: wp->mode->resize(wp, sx, sy);
529:
530: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
531: fatal("ioctl failed");
532: return (0);
533: }
534:
535: int
536: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
537: {
538: struct screen *s;
539:
540: if (wp->mode != NULL || wp->mode == mode)
541: return (1);
542:
543: wp->mode = mode;
544:
545: if ((s = wp->mode->init(wp)) != NULL)
546: wp->screen = s;
547: server_redraw_window(wp->window);
548: return (0);
549: }
550:
551: void
552: window_pane_reset_mode(struct window_pane *wp)
553: {
554: if (wp->mode == NULL)
555: return;
556:
557: wp->mode->free(wp);
558: wp->mode = NULL;
559:
560: wp->screen = &wp->base;
561: server_redraw_window(wp->window);
562: }
563:
564: void
565: window_pane_parse(struct window_pane *wp)
566: {
567: input_parse(wp);
568: }
569:
570: void
571: window_pane_key(struct window_pane *wp, struct client *c, int key)
572: {
1.3 nicm 573: if (wp->fd == -1)
574: return;
575:
1.1 nicm 576: if (wp->mode != NULL) {
577: if (wp->mode->key != NULL)
578: wp->mode->key(wp, c, key);
579: } else
580: input_key(wp, key);
581: }
582:
583: void
584: window_pane_mouse(
585: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
586: {
1.3 nicm 587: if (wp->fd == -1)
588: return;
589:
1.1 nicm 590: /* XXX convert from 1-based? */
591:
592: if (x < wp->xoff || x >= wp->xoff + wp->sx)
593: return;
594: if (y < wp->yoff || y >= wp->yoff + wp->sy)
595: return;
596: x -= wp->xoff;
597: y -= wp->yoff;
598:
599: if (wp->mode != NULL) {
600: if (wp->mode->mouse != NULL)
601: wp->mode->mouse(wp, c, b, x, y);
602: } else
603: input_mouse(wp, b, x, y);
604: }
605:
606: char *
1.5 nicm 607: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 608: {
1.4 nicm 609: struct screen *s = &wp->base;
1.5 nicm 610: char *newsearchstr, *line, *msg;
1.4 nicm 611: u_int i;
612:
1.5 nicm 613: msg = NULL;
614: xasprintf(&newsearchstr, "*%s*", searchstr);
615:
1.4 nicm 616: for (i = 0; i < screen_size_y(s); i++) {
617: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 618: if (fnmatch(newsearchstr, line, 0) == 0) {
619: msg = line;
620: if (lineno != NULL)
621: *lineno = i;
622: break;
623: }
1.4 nicm 624: xfree(line);
625: }
1.5 nicm 626:
627: xfree(newsearchstr);
628: return (msg);
1.1 nicm 629: }