We ignore SIGWINCH until ready, so send a MSG_RESIZE immediately when
[tmux-openbsd.git] / names.c
blobce5d0c51a666a6a8e005bd50cdcddf9e6d1bcf85
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(int, short, void *);
30 char *parse_window_name(struct window *, 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(w, name + 1);
77 else
78 wname = parse_window_name(w, 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, w->active->cmd));
102 return (parse_window_name(w, w->active->shell));
105 char *
106 parse_window_name(struct window *w, const char *in)
108 char *copy, *name, *ptr, *prefix;
109 size_t prefixlen;
111 prefix = options_get_string(&w->options, "command-prefix");
112 prefixlen = strlen(prefix);
114 name = copy = xstrdup(in);
115 if (strncmp(name, prefix, prefixlen) == 0)
116 name = name + prefixlen;
118 while (*name == ' ')
119 name++;
120 if ((ptr = strchr(name, ' ')) != NULL)
121 *ptr = '\0';
123 if (*name != '\0') {
124 ptr = name + strlen(name) - 1;
125 while (ptr > name && !isalnum((u_char)*ptr))
126 *ptr-- = '\0';
129 if (*name == '/')
130 name = basename(name);
131 name = xstrdup(name);
132 free(copy);
133 return (name);