Annotation of src/usr.bin/tmux/names.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: names.c,v 1.14 2012/04/08 06:47:26 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.15 ! nicm 52: if (!options_get_number(&w->options, "automatic-rename")) {
! 53: if (event_initialized(&w->name_timer))
! 54: event_del(&w->name_timer);
! 55: return;
! 56: }
! 57: queue_window_name(w);
1.9 nicm 58:
59: if (w->active->screen != &w->active->base)
60: name = NULL;
61: else
62: name = get_proc_name(w->active->fd, w->active->tty);
63: if (name == NULL)
64: wname = default_window_name(w);
65: else {
1.11 nicm 66: /*
1.9 nicm 67: * If tmux is using the default command, it will be a login
68: * shell and argv[0] may have a - prefix. Remove this if it is
69: * present. Ick.
70: */
71: if (w->active->cmd != NULL && *w->active->cmd == '\0' &&
72: name != NULL && name[0] == '-' && name[1] != '\0')
73: wname = parse_window_name(name + 1);
1.1 nicm 74: else
1.4 nicm 75: wname = parse_window_name(name);
1.9 nicm 76: xfree(name);
77: }
1.11 nicm 78:
1.9 nicm 79: if (w->active->fd == -1) {
80: xasprintf(&name, "%s[dead]", wname);
81: xfree(wname);
82: wname = name;
83: }
1.11 nicm 84:
1.12 nicm 85: if (strcmp(wname, w->name)) {
86: window_set_name(w, wname);
1.9 nicm 87: server_status_window(w);
1.1 nicm 88: }
1.12 nicm 89: xfree(wname);
1.1 nicm 90: }
91:
92: char *
93: default_window_name(struct window *w)
94: {
95: if (w->active->screen != &w->active->base)
96: return (xstrdup("[tmux]"));
1.4 nicm 97: if (w->active->cmd != NULL && *w->active->cmd != '\0')
98: return (parse_window_name(w->active->cmd));
1.6 nicm 99: return (parse_window_name(w->active->shell));
1.1 nicm 100: }
101:
102: char *
103: parse_window_name(const char *in)
104: {
105: char *copy, *name, *ptr;
106:
107: name = copy = xstrdup(in);
108: if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
109: name = name + (sizeof "exec ") - 1;
110:
111: while (*name == ' ')
112: name++;
113: if ((ptr = strchr(name, ' ')) != NULL)
114: *ptr = '\0';
115:
116: if (*name != '\0') {
117: ptr = name + strlen(name) - 1;
1.2 ray 118: while (ptr > name && !isalnum((u_char)*ptr))
1.1 nicm 119: *ptr-- = '\0';
120: }
121:
122: if (*name == '/')
123: name = basename(name);
124: name = xstrdup(name);
125: xfree(copy);
126: return (name);
127: }