2009-06-01 22:58:49 +00: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 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-11-04 23:54:57 +00:00
|
|
|
void window_name_callback(unused int, unused short, void *);
|
2009-06-01 22:58:49 +00:00
|
|
|
char *parse_window_name(const char *);
|
|
|
|
|
|
|
|
void
|
2009-11-04 23:54:57 +00:00
|
|
|
queue_window_name(struct window *w)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2009-11-04 23:54:57 +00:00
|
|
|
struct timeval tv;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 23:54:57 +00:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = NAME_INTERVAL * 1000L;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-03-17 18:24:07 +00:00
|
|
|
if (event_initialized(&w->name_timer))
|
|
|
|
evtimer_del(&w->name_timer);
|
2009-11-04 23:54:57 +00:00
|
|
|
evtimer_set(&w->name_timer, window_name_callback, w);
|
|
|
|
evtimer_add(&w->name_timer, &tv);
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-11-04 23:54:57 +00:00
|
|
|
void
|
|
|
|
window_name_callback(unused int fd, unused short events, void *data)
|
|
|
|
{
|
|
|
|
struct window *w = data;
|
|
|
|
char *name, *wname;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-08-21 10:00:33 +00:00
|
|
|
if (w->active == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-04-11 07:45:30 +00: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 21:18:20 +00:00
|
|
|
|
2009-11-04 23:54:57 +00: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 22:50:09 +00:00
|
|
|
/*
|
2009-11-04 23:54:57 +00: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')
|
|
|
|
wname = parse_window_name(name + 1);
|
2009-06-01 22:58:49 +00:00
|
|
|
else
|
2012-11-27 09:20:03 +00:00
|
|
|
wname = parse_window_name(name);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(name);
|
2009-11-04 23:54:57 +00:00
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-11-04 23:54:57 +00:00
|
|
|
if (w->active->fd == -1) {
|
|
|
|
xasprintf(&name, "%s[dead]", wname);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(wname);
|
2009-11-04 23:54:57 +00:00
|
|
|
wname = name;
|
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2012-03-09 21:11:28 +00:00
|
|
|
if (strcmp(wname, w->name)) {
|
|
|
|
window_set_name(w, wname);
|
2009-11-04 23:54:57 +00:00
|
|
|
server_status_window(w);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2012-07-10 11:53:01 +00:00
|
|
|
free(wname);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
default_window_name(struct window *w)
|
|
|
|
{
|
|
|
|
if (w->active->screen != &w->active->base)
|
|
|
|
return (xstrdup("[tmux]"));
|
2009-07-08 05:26:45 +00:00
|
|
|
if (w->active->cmd != NULL && *w->active->cmd != '\0')
|
|
|
|
return (parse_window_name(w->active->cmd));
|
2009-09-01 13:09:49 +00:00
|
|
|
return (parse_window_name(w->active->shell));
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
parse_window_name(const char *in)
|
|
|
|
{
|
|
|
|
char *copy, *name, *ptr;
|
|
|
|
|
|
|
|
name = copy = xstrdup(in);
|
|
|
|
if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
|
|
|
|
name = name + (sizeof "exec ") - 1;
|
|
|
|
|
|
|
|
while (*name == ' ')
|
|
|
|
name++;
|
|
|
|
if ((ptr = strchr(name, ' ')) != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
if (*name != '\0') {
|
|
|
|
ptr = name + strlen(name) - 1;
|
2009-06-03 04:03:16 +00:00
|
|
|
while (ptr > name && !isalnum((u_char)*ptr))
|
2009-06-01 22:58:49 +00:00
|
|
|
*ptr-- = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*name == '/')
|
|
|
|
name = basename(name);
|
|
|
|
name = xstrdup(name);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(copy);
|
2009-06-01 22:58:49 +00:00
|
|
|
return (name);
|
|
|
|
}
|