Annotation of src/usr.bin/tmux/names.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: names.c,v 1.24 2015/02/05 10:29:43 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 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:
21: #include <ctype.h>
22: #include <libgen.h>
1.16 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
1.21 nicm 28: void window_name_callback(unused int, unused short, void *);
1.1 nicm 29:
30: void
1.9 nicm 31: queue_window_name(struct window *w)
1.1 nicm 32: {
1.9 nicm 33: struct timeval tv;
1.1 nicm 34:
1.9 nicm 35: tv.tv_sec = 0;
36: tv.tv_usec = NAME_INTERVAL * 1000L;
1.1 nicm 37:
1.13 nicm 38: if (event_initialized(&w->name_timer))
39: evtimer_del(&w->name_timer);
1.9 nicm 40: evtimer_set(&w->name_timer, window_name_callback, w);
41: evtimer_add(&w->name_timer, &tv);
42: }
1.5 nicm 43:
1.9 nicm 44: void
45: window_name_callback(unused int fd, unused short events, void *data)
46: {
47: struct window *w = data;
1.22 nicm 48: char *name;
1.17 nicm 49:
50: if (w->active == NULL)
51: return;
1.25 ! nicm 52:
! 53: if (~w->active->flags & PANE_CHANGED)
! 54: return;
! 55: w->active->flags &= ~PANE_CHANGED;
1.1 nicm 56:
1.15 nicm 57: if (!options_get_number(&w->options, "automatic-rename")) {
58: if (event_initialized(&w->name_timer))
59: event_del(&w->name_timer);
60: return;
61: }
62: queue_window_name(w);
1.9 nicm 63:
1.22 nicm 64: name = format_window_name(w);
65: if (strcmp(name, w->name) != 0) {
66: window_set_name(w, name);
1.9 nicm 67: server_status_window(w);
1.1 nicm 68: }
1.22 nicm 69: free(name);
1.1 nicm 70: }
71:
72: char *
73: default_window_name(struct window *w)
74: {
1.23 nicm 75: char *cmd, *s;
76:
77: cmd = cmd_stringify_argv(w->active->argc, w->active->argv);
78: if (cmd != NULL && *cmd != '\0')
79: s = parse_window_name(cmd);
80: else
81: s = parse_window_name(w->active->shell);
82: free(cmd);
83: return (s);
1.1 nicm 84: }
85:
86: char *
1.22 nicm 87: format_window_name(struct window *w)
88: {
89: struct format_tree *ft;
90: char *fmt, *name;
91:
92: ft = format_create();
1.24 nicm 93: format_defaults_window(ft, w);
94: format_defaults_pane(ft, w->active);
1.22 nicm 95:
96: fmt = options_get_string(&w->options, "automatic-rename-format");
97: name = format_expand(ft, fmt);
98:
99: format_free(ft);
100: return (name);
101: }
102:
103: char *
1.21 nicm 104: parse_window_name(const char *in)
1.1 nicm 105: {
1.21 nicm 106: char *copy, *name, *ptr;
1.1 nicm 107:
108: name = copy = xstrdup(in);
1.21 nicm 109: if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
110: name = name + (sizeof "exec ") - 1;
1.1 nicm 111:
1.22 nicm 112: while (*name == ' ' || *name == '-')
1.1 nicm 113: name++;
114: if ((ptr = strchr(name, ' ')) != NULL)
115: *ptr = '\0';
116:
117: if (*name != '\0') {
118: ptr = name + strlen(name) - 1;
1.2 ray 119: while (ptr > name && !isalnum((u_char)*ptr))
1.1 nicm 120: *ptr-- = '\0';
121: }
122:
123: if (*name == '/')
124: name = basename(name);
125: name = xstrdup(name);
1.16 nicm 126: free(copy);
1.1 nicm 127: return (name);
128: }