Annotation of src/usr.bin/tmux/names.c, Revision 1.14
1.14 ! nicm 1: /* $OpenBSD: names.c,v 1.13 2012/03/17 18:24:07 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <ctype.h>
22: #include <libgen.h>
23: #include <string.h>
24: #include <unistd.h>
25:
26: #include "tmux.h"
27:
1.9 nicm 28: void window_name_callback(unused int, unused short, void *);
1.1 nicm 29: char *parse_window_name(const char *);
30:
31: void
1.9 nicm 32: queue_window_name(struct window *w)
1.1 nicm 33: {
1.9 nicm 34: struct timeval tv;
1.1 nicm 35:
1.9 nicm 36: tv.tv_sec = 0;
37: tv.tv_usec = NAME_INTERVAL * 1000L;
1.1 nicm 38:
1.13 nicm 39: if (event_initialized(&w->name_timer))
40: evtimer_del(&w->name_timer);
1.9 nicm 41: evtimer_set(&w->name_timer, window_name_callback, w);
42: evtimer_add(&w->name_timer, &tv);
43: }
1.5 nicm 44:
1.10 nicm 45: /* ARGSUSED */
1.9 nicm 46: void
47: window_name_callback(unused int fd, unused short events, void *data)
48: {
49: struct window *w = data;
50: char *name, *wname;
1.1 nicm 51:
1.14 ! nicm 52: queue_window_name(w); /* stopped when option turned off */
1.9 nicm 53:
54: if (w->active->screen != &w->active->base)
55: name = NULL;
56: else
57: name = get_proc_name(w->active->fd, w->active->tty);
58: if (name == NULL)
59: wname = default_window_name(w);
60: else {
1.11 nicm 61: /*
1.9 nicm 62: * If tmux is using the default command, it will be a login
63: * shell and argv[0] may have a - prefix. Remove this if it is
64: * present. Ick.
65: */
66: if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
67: name != NULL && name[0] == '-' && name[1] != '\0')
68: wname = parse_window_name(name + 1);
1.1 nicm 69: else
1.4 nicm 70: wname = parse_window_name(name);
1.9 nicm 71: xfree(name);
72: }
1.11 nicm 73:
1.9 nicm 74: if (w->active->fd == -1) {
75: xasprintf(&name, "%s[dead]", wname);
76: xfree(wname);
77: wname = name;
78: }
1.11 nicm 79:
1.12 nicm 80: if (strcmp(wname, w->name)) {
81: window_set_name(w, wname);
1.9 nicm 82: server_status_window(w);
1.1 nicm 83: }
1.12 nicm 84: xfree(wname);
1.1 nicm 85: }
86:
87: char *
88: default_window_name(struct window *w)
89: {
90: if (w->active->screen != &w->active->base)
91: return (xstrdup("[tmux]"));
1.4 nicm 92: if (w->active->cmd != NULL && *w->active->cmd != '\0')
93: return (parse_window_name(w->active->cmd));
1.6 nicm 94: return (parse_window_name(w->active->shell));
1.1 nicm 95: }
96:
97: char *
98: parse_window_name(const char *in)
99: {
100: char *copy, *name, *ptr;
101:
102: name = copy = xstrdup(in);
103: if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
104: name = name + (sizeof "exec ") - 1;
105:
106: while (*name == ' ')
107: name++;
108: if ((ptr = strchr(name, ' ')) != NULL)
109: *ptr = '\0';
110:
111: if (*name != '\0') {
112: ptr = name + strlen(name) - 1;
1.2 ray 113: while (ptr > name && !isalnum((u_char)*ptr))
1.1 nicm 114: *ptr-- = '\0';
115: }
116:
117: if (*name == '/')
118: name = basename(name);
119: name = xstrdup(name);
120: xfree(copy);
121: return (name);
122: }