Revert the command-prefix change which breaks sequences of commands.
[tmux-openbsd.git] / names.c
blob76dec82c5d313ff9410783f44992d4e9a3565748
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <libgen.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "tmux.h"
29 void window_name_callback(unused int, unused short, void *);
30 char *parse_window_name(const char *);
32 void
33 queue_window_name(struct window *w)
35 struct timeval tv;
37 tv.tv_sec = 0;
38 tv.tv_usec = NAME_INTERVAL * 1000L;
40 if (event_initialized(&w->name_timer))
41 evtimer_del(&w->name_timer);
42 evtimer_set(&w->name_timer, window_name_callback, w);
43 evtimer_add(&w->name_timer, &tv);
46 void
47 window_name_callback(unused int fd, unused short events, void *data)
49 struct window *w = data;
50 char *name, *wname;
52 if (w->active == NULL)
53 return;
55 if (!options_get_number(&w->options, "automatic-rename")) {
56 if (event_initialized(&w->name_timer))
57 event_del(&w->name_timer);
58 return;
60 queue_window_name(w);
62 if (w->active->screen != &w->active->base)
63 name = NULL;
64 else
65 name = get_proc_name(w->active->fd, w->active->tty);
66 if (name == NULL)
67 wname = default_window_name(w);
68 else {
70 * If tmux is using the default command, it will be a login
71 * shell and argv[0] may have a - prefix. Remove this if it is
72 * present. Ick.
74 if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
75 name != NULL && name[0] == '-' && name[1] != '\0')
76 wname = parse_window_name(name + 1);
77 else
78 wname = parse_window_name(name);
79 free(name);
82 if (w->active->fd == -1) {
83 xasprintf(&name, "%s[dead]", wname);
84 free(wname);
85 wname = name;
88 if (strcmp(wname, w->name)) {
89 window_set_name(w, wname);
90 server_status_window(w);
92 free(wname);
95 char *
96 default_window_name(struct window *w)
98 if (w->active->screen != &w->active->base)
99 return (xstrdup("[tmux]"));
100 if (w->active->cmd != NULL && *w->active->cmd != '\0')
101 return (parse_window_name(w->active->cmd));
102 return (parse_window_name(w->active->shell));
105 char *
106 parse_window_name(const char *in)
108 char *copy, *name, *ptr;
110 name = copy = xstrdup(in);
111 if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
112 name = name + (sizeof "exec ") - 1;
114 while (*name == ' ')
115 name++;
116 if ((ptr = strchr(name, ' ')) != NULL)
117 *ptr = '\0';
119 if (*name != '\0') {
120 ptr = name + strlen(name) - 1;
121 while (ptr > name && !isalnum((u_char)*ptr))
122 *ptr-- = '\0';
125 if (*name == '/')
126 name = basename(name);
127 name = xstrdup(name);
128 free(copy);
129 return (name);