xref: /openbsd-src/usr.bin/tmux/names.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /* $OpenBSD: names.c,v 1.9 2009/11/04 23:54:57 nicm Exp $ */
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 
28 void	 window_name_callback(unused int, unused short, void *);
29 char	*parse_window_name(const char *);
30 
31 void
32 queue_window_name(struct window *w)
33 {
34 	struct timeval	tv;
35 
36 	tv.tv_sec = 0;
37 	tv.tv_usec = NAME_INTERVAL * 1000L;
38 
39 	evtimer_del(&w->name_timer);
40 	evtimer_set(&w->name_timer, window_name_callback, w);
41 	evtimer_add(&w->name_timer, &tv);
42 }
43 
44 void
45 window_name_callback(unused int fd, unused short events, void *data)
46 {
47 	struct window	*w = data;
48 	char		*name, *wname;
49 
50 	queue_window_name(w);	/* XXX even if the option is off? */
51 	if (!options_get_number(&w->options, "automatic-rename"))
52 		return;
53 
54 	if (w->active->screen != &w->active->base)
55 		name = NULL;
56 	else
57 		name = get_proc_name(w->active->fd, w->active->tty);
58 	if (name == NULL)
59 		wname = default_window_name(w);
60 	else {
61 		/*
62 		 * If tmux is using the default command, it will be a login
63 		 * shell and argv[0] may have a - prefix. Remove this if it is
64 		 * present. Ick.
65 		 */
66 		if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
67 		    name != NULL && name[0] == '-' && name[1] != '\0')
68 			wname = parse_window_name(name + 1);
69 		else
70 				wname = parse_window_name(name);
71 		xfree(name);
72 	}
73 
74 	if (w->active->fd == -1) {
75 		xasprintf(&name, "%s[dead]", wname);
76 		xfree(wname);
77 		wname = name;
78 	}
79 
80 	if (strcmp(wname, w->name) == 0)
81 		xfree(wname);
82 	else {
83 		xfree(w->name);
84 		w->name = wname;
85 		server_status_window(w);
86 	}
87 }
88 
89 char *
90 default_window_name(struct window *w)
91 {
92 	if (w->active->screen != &w->active->base)
93 		return (xstrdup("[tmux]"));
94 	if (w->active->cmd != NULL && *w->active->cmd != '\0')
95 		return (parse_window_name(w->active->cmd));
96 	return (parse_window_name(w->active->shell));
97 }
98 
99 char *
100 parse_window_name(const char *in)
101 {
102 	char	*copy, *name, *ptr;
103 
104 	name = copy = xstrdup(in);
105 	if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
106 		name = name + (sizeof "exec ") - 1;
107 
108 	while (*name == ' ')
109 		name++;
110 	if ((ptr = strchr(name, ' ')) != NULL)
111 		*ptr = '\0';
112 
113 	if (*name != '\0') {
114 		ptr = name + strlen(name) - 1;
115 		while (ptr > name && !isalnum((u_char)*ptr))
116 			*ptr-- = '\0';
117 	}
118 
119 	if (*name == '/')
120 		name = basename(name);
121 	name = xstrdup(name);
122 	xfree(copy);
123 	return (name);
124 }
125