2009-06-02 00:58:49 +02:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <libgen.h>
|
2012-07-10 13:53:01 +02:00
|
|
|
#include <stdlib.h>
|
2009-06-02 00:58:49 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2013-03-24 10:21:27 +01:00
|
|
|
void window_name_callback(int, short, void *);
|
|
|
|
char *parse_window_name(struct window *, const char *);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
void
|
2009-11-05 00:54:57 +01:00
|
|
|
queue_window_name(struct window *w)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2009-11-05 00:54:57 +01:00
|
|
|
struct timeval tv;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2009-11-05 00:54:57 +01:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = NAME_INTERVAL * 1000L;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2012-03-17 19:24:07 +01:00
|
|
|
if (event_initialized(&w->name_timer))
|
|
|
|
evtimer_del(&w->name_timer);
|
2009-11-05 00:54:57 +01:00
|
|
|
evtimer_set(&w->name_timer, window_name_callback, w);
|
|
|
|
evtimer_add(&w->name_timer, &tv);
|
|
|
|
}
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2009-11-05 00:54:57 +01:00
|
|
|
void
|
|
|
|
window_name_callback(unused int fd, unused short events, void *data)
|
|
|
|
{
|
|
|
|
struct window *w = data;
|
|
|
|
char *name, *wname;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
2012-08-21 12:00:33 +02:00
|
|
|
if (w->active == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-04-11 09:45:30 +02:00
|
|
|
if (!options_get_number(&w->options, "automatic-rename")) {
|
|
|
|
if (event_initialized(&w->name_timer))
|
|
|
|
event_del(&w->name_timer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
queue_window_name(w);
|
2009-08-18 23:18:20 +02:00
|
|
|
|
2009-11-05 00:54:57 +01:00
|
|
|
if (w->active->screen != &w->active->base)
|
|
|
|
name = NULL;
|
|
|
|
else
|
|
|
|
name = get_proc_name(w->active->fd, w->active->tty);
|
|
|
|
if (name == NULL)
|
|
|
|
wname = default_window_name(w);
|
|
|
|
else {
|
2009-12-03 23:50:09 +01:00
|
|
|
/*
|
2009-11-05 00:54:57 +01:00
|
|
|
* If tmux is using the default command, it will be a login
|
|
|
|
* shell and argv[0] may have a - prefix. Remove this if it is
|
|
|
|
* present. Ick.
|
|
|
|
*/
|
|
|
|
if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
|
|
|
|
name != NULL && name[0] == '-' && name[1] != '\0')
|
2013-03-24 10:21:27 +01:00
|
|
|
wname = parse_window_name(w, name + 1);
|
2009-06-02 00:58:49 +02:00
|
|
|
else
|
2013-03-24 10:21:27 +01:00
|
|
|
wname = parse_window_name(w, name);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(name);
|
2009-11-05 00:54:57 +01:00
|
|
|
}
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2009-11-05 00:54:57 +01:00
|
|
|
if (w->active->fd == -1) {
|
|
|
|
xasprintf(&name, "%s[dead]", wname);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(wname);
|
2009-11-05 00:54:57 +01:00
|
|
|
wname = name;
|
|
|
|
}
|
2009-12-03 23:50:09 +01:00
|
|
|
|
2012-03-09 22:11:28 +01:00
|
|
|
if (strcmp(wname, w->name)) {
|
|
|
|
window_set_name(w, wname);
|
2009-11-05 00:54:57 +01:00
|
|
|
server_status_window(w);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
2012-07-10 13:53:01 +02:00
|
|
|
free(wname);
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
default_window_name(struct window *w)
|
|
|
|
{
|
|
|
|
if (w->active->screen != &w->active->base)
|
|
|
|
return (xstrdup("[tmux]"));
|
2009-07-08 07:26:45 +02:00
|
|
|
if (w->active->cmd != NULL && *w->active->cmd != '\0')
|
2013-03-24 10:21:27 +01:00
|
|
|
return (parse_window_name(w, w->active->cmd));
|
|
|
|
return (parse_window_name(w, w->active->shell));
|
2009-06-02 00:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-03-24 10:21:27 +01:00
|
|
|
parse_window_name(struct window *w, const char *in)
|
2009-06-02 00:58:49 +02:00
|
|
|
{
|
2013-03-24 10:21:27 +01:00
|
|
|
char *copy, *name, *ptr, *prefix;
|
|
|
|
size_t prefixlen;
|
|
|
|
|
|
|
|
prefix = options_get_string(&w->options, "command-prefix");
|
|
|
|
prefixlen = strlen(prefix);
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
name = copy = xstrdup(in);
|
2013-03-24 10:21:27 +01:00
|
|
|
if (strncmp(name, prefix, prefixlen) == 0)
|
|
|
|
name = name + prefixlen;
|
2009-06-02 00:58:49 +02:00
|
|
|
|
|
|
|
while (*name == ' ')
|
|
|
|
name++;
|
|
|
|
if ((ptr = strchr(name, ' ')) != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
if (*name != '\0') {
|
|
|
|
ptr = name + strlen(name) - 1;
|
2009-06-03 06:03:16 +02:00
|
|
|
while (ptr > name && !isalnum((u_char)*ptr))
|
2009-06-02 00:58:49 +02:00
|
|
|
*ptr-- = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*name == '/')
|
|
|
|
name = basename(name);
|
|
|
|
name = xstrdup(name);
|
2012-07-10 13:53:01 +02:00
|
|
|
free(copy);
|
2009-06-02 00:58:49 +02:00
|
|
|
return (name);
|
|
|
|
}
|