2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2009-01-20 20:35:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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>
|
2009-06-25 18:49:02 +02:00
|
|
|
#include <libgen.h>
|
2012-07-11 21:34:16 +02:00
|
|
|
#include <stdlib.h>
|
2009-01-20 20:35:03 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2013-02-22 15:31:38 +01:00
|
|
|
void window_name_callback(int, short, void *);
|
|
|
|
char *parse_window_name(struct window *, const char *);
|
2009-01-20 20:35:03 +01:00
|
|
|
|
|
|
|
void
|
2009-11-09 00:22:24 +01:00
|
|
|
queue_window_name(struct window *w)
|
2009-01-20 20:35:03 +01:00
|
|
|
{
|
2009-11-09 00:22:24 +01:00
|
|
|
struct timeval tv;
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-11-09 00:22:24 +01:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = NAME_INTERVAL * 1000L;
|
2009-01-20 20:35:03 +01:00
|
|
|
|
2012-03-18 02:59:34 +01:00
|
|
|
if (event_initialized(&w->name_timer))
|
|
|
|
evtimer_del(&w->name_timer);
|
2009-11-09 00:22:24 +01:00
|
|
|
evtimer_set(&w->name_timer, window_name_callback, w);
|
|
|
|
evtimer_add(&w->name_timer, &tv);
|
|
|
|
}
|
2009-01-20 20:35:03 +01:00
|
|
|
|
2009-11-09 00:22:24 +01:00
|
|
|
void
|
|
|
|
window_name_callback(unused int fd, unused short events, void *data)
|
|
|
|
{
|
|
|
|
struct window *w = data;
|
|
|
|
char *name, *wname;
|
2009-01-20 20:35:03 +01:00
|
|
|
|
2012-08-31 11:22:08 +02:00
|
|
|
if (w->active == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-04-12 14:44:26 +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-20 13:44:18 +02:00
|
|
|
|
2009-11-09 00:22:24 +01:00
|
|
|
if (w->active->screen != &w->active->base)
|
|
|
|
name = NULL;
|
|
|
|
else
|
2009-11-09 00:23:36 +01:00
|
|
|
name = osdep_get_name(w->active->fd, w->active->tty);
|
2009-11-09 00:22:24 +01:00
|
|
|
if (name == NULL)
|
|
|
|
wname = default_window_name(w);
|
|
|
|
else {
|
2009-12-04 23:14:47 +01:00
|
|
|
/*
|
2009-11-09 00:22:24 +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-02-22 15:31:38 +01:00
|
|
|
wname = parse_window_name(w, name + 1);
|
2009-02-13 01:43:04 +01:00
|
|
|
else
|
2013-02-22 15:31:38 +01:00
|
|
|
wname = parse_window_name(w, name);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(name);
|
2009-11-09 00:22:24 +01:00
|
|
|
}
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-11-09 00:22:24 +01:00
|
|
|
if (w->active->fd == -1) {
|
|
|
|
xasprintf(&name, "%s[dead]", wname);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(wname);
|
2009-11-09 00:22:24 +01:00
|
|
|
wname = name;
|
|
|
|
}
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2012-03-18 02:28:56 +01:00
|
|
|
if (strcmp(wname, w->name)) {
|
|
|
|
window_set_name(w, wname);
|
2009-11-09 00:22:24 +01:00
|
|
|
server_status_window(w);
|
2009-01-20 20:35:03 +01:00
|
|
|
}
|
2012-07-11 21:34:16 +02:00
|
|
|
free(wname);
|
2009-01-20 20:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
default_window_name(struct window *w)
|
|
|
|
{
|
|
|
|
if (w->active->screen != &w->active->base)
|
|
|
|
return (xstrdup("[tmux]"));
|
2009-07-02 20:17:46 +02:00
|
|
|
if (w->active->cmd != NULL && *w->active->cmd != '\0')
|
2013-02-22 15:31:38 +01:00
|
|
|
return (parse_window_name(w, w->active->cmd));
|
|
|
|
return (parse_window_name(w, w->active->shell));
|
2009-01-20 20:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-02-22 15:31:38 +01:00
|
|
|
parse_window_name(struct window *w, const char *in)
|
2009-01-20 20:35:03 +01:00
|
|
|
{
|
2013-02-22 15:31:38 +01:00
|
|
|
char *copy, *name, *ptr, *prefix;
|
|
|
|
size_t prefixlen;
|
|
|
|
|
|
|
|
prefix = options_get_string(&w->options, "command-prefix");
|
|
|
|
prefixlen = strlen(prefix);
|
2009-01-20 20:35:03 +01:00
|
|
|
|
|
|
|
name = copy = xstrdup(in);
|
2013-02-22 15:31:38 +01:00
|
|
|
if (strncmp(name, prefix, prefixlen) == 0)
|
|
|
|
name = name + prefixlen;
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-01-20 20:35:03 +01:00
|
|
|
while (*name == ' ')
|
|
|
|
name++;
|
|
|
|
if ((ptr = strchr(name, ' ')) != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
if (*name != '\0') {
|
|
|
|
ptr = name + strlen(name) - 1;
|
2009-06-25 17:45:24 +02:00
|
|
|
while (ptr > name && !isalnum((u_char)*ptr))
|
2009-01-20 20:35:03 +01:00
|
|
|
*ptr-- = '\0';
|
|
|
|
}
|
2009-05-04 19:58:27 +02:00
|
|
|
|
2009-01-20 20:35:03 +01:00
|
|
|
if (*name == '/')
|
2009-06-25 18:47:00 +02:00
|
|
|
name = basename(name);
|
2009-01-20 20:35:03 +01:00
|
|
|
name = xstrdup(name);
|
2012-07-11 21:34:16 +02:00
|
|
|
free(copy);
|
2009-01-20 20:35:03 +01:00
|
|
|
return (name);
|
|
|
|
}
|