Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (106 sloc) 3.169 kb
7216c39 @kfebert Imported Upstream version 1.5
kfebert authored
1 /* $Id: names.c 2553 2011-07-09 09:42:33Z tcunha $ */
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
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>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "tmux.h"
27
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
28 void window_name_callback(unused int, unused short, void *);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
29 char *parse_window_name(const char *);
30
31 void
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
32 queue_window_name(struct window *w)
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
33 {
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
34 struct timeval tv;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
35
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
36 tv.tv_sec = 0;
37 tv.tv_usec = NAME_INTERVAL * 1000L;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
38
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
39 evtimer_del(&w->name_timer);
40 evtimer_set(&w->name_timer, window_name_callback, w);
41 evtimer_add(&w->name_timer, &tv);
42 }
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
43
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
44 /* ARGSUSED */
45 void
46 window_name_callback(unused int fd, unused short events, void *data)
47 {
48 struct window *w = data;
49 char *name, *wname;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
50
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
51 queue_window_name(w); /* XXX even if the option is off? */
52 if (!options_get_number(&w->options, "automatic-rename"))
53 return;
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
54
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
55 if (w->active->screen != &w->active->base)
56 name = NULL;
57 else
58 name = osdep_get_name(w->active->fd, w->active->tty);
59 if (name == NULL)
60 wname = default_window_name(w);
61 else {
62 /*
63 * If tmux is using the default command, it will be a login
64 * shell and argv[0] may have a - prefix. Remove this if it is
65 * present. Ick.
66 */
67 if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
68 name != NULL && name[0] == '-' && name[1] != '\0')
69 wname = parse_window_name(name + 1);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
70 else
71 wname = parse_window_name(name);
dda8e8e @kfebert Imported Upstream version 1.2
kfebert authored
72 xfree(name);
73 }
74
75 if (w->active->fd == -1) {
76 xasprintf(&name, "%s[dead]", wname);
77 xfree(wname);
78 wname = name;
79 }
80
81 if (strcmp(wname, w->name) == 0)
82 xfree(wname);
83 else {
84 xfree(w->name);
85 w->name = wname;
86 server_status_window(w);
8fade26 Imported Upstream version 1.1
Karl Ferdinand Ebert authored
87 }
88 }
89
90 char *
91 default_window_name(struct window *w)
92 {
93 if (w->active->screen != &w->active->base)
94 return (xstrdup("[tmux]"));
95 if (w->active->cmd != NULL && *w->active->cmd != '\0')
96 return (parse_window_name(w->active->cmd));
97 return (parse_window_name(w->active->shell));
98 }
99
100 char *
101 parse_window_name(const char *in)
102 {
103 char *copy, *name, *ptr;
104
105 name = copy = xstrdup(in);
106 if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
107 name = name + (sizeof "exec ") - 1;
108
109 while (*name == ' ')
110 name++;
111 if ((ptr = strchr(name, ' ')) != NULL)
112 *ptr = '\0';
113
114 if (*name != '\0') {
115 ptr = name + strlen(name) - 1;
116 while (ptr > name && !isalnum((u_char)*ptr))
117 *ptr-- = '\0';
118 }
119
120 if (*name == '/')
121 name = basename(name);
122 name = xstrdup(name);
123 xfree(copy);
124 return (name);
125 }
Something went wrong with that request. Please try again.