1 /* $OpenBSD: names.c,v 1.16 2012/07/10 11:53:01 nicm Exp $ */ 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 <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include "tmux.h" 28 29 void window_name_callback(unused int, unused short, void *); 30 char *parse_window_name(const char *); 31 32 void 33 queue_window_name(struct window *w) 34 { 35 struct timeval tv; 36 37 tv.tv_sec = 0; 38 tv.tv_usec = NAME_INTERVAL * 1000L; 39 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); 44 } 45 46 /* ARGSUSED */ 47 void 48 window_name_callback(unused int fd, unused short events, void *data) 49 { 50 struct window *w = data; 51 char *name, *wname; 52 53 if (!options_get_number(&w->options, "automatic-rename")) { 54 if (event_initialized(&w->name_timer)) 55 event_del(&w->name_timer); 56 return; 57 } 58 queue_window_name(w); 59 60 if (w->active->screen != &w->active->base) 61 name = NULL; 62 else 63 name = get_proc_name(w->active->fd, w->active->tty); 64 if (name == NULL) 65 wname = default_window_name(w); 66 else { 67 /* 68 * If tmux is using the default command, it will be a login 69 * shell and argv[0] may have a - prefix. Remove this if it is 70 * present. Ick. 71 */ 72 if (w->active->cmd != NULL && *w->active->cmd == '\0' && 73 name != NULL && name[0] == '-' && name[1] != '\0') 74 wname = parse_window_name(name + 1); 75 else 76 wname = parse_window_name(name); 77 free(name); 78 } 79 80 if (w->active->fd == -1) { 81 xasprintf(&name, "%s[dead]", wname); 82 free(wname); 83 wname = name; 84 } 85 86 if (strcmp(wname, w->name)) { 87 window_set_name(w, wname); 88 server_status_window(w); 89 } 90 free(wname); 91 } 92 93 char * 94 default_window_name(struct window *w) 95 { 96 if (w->active->screen != &w->active->base) 97 return (xstrdup("[tmux]")); 98 if (w->active->cmd != NULL && *w->active->cmd != '\0') 99 return (parse_window_name(w->active->cmd)); 100 return (parse_window_name(w->active->shell)); 101 } 102 103 char * 104 parse_window_name(const char *in) 105 { 106 char *copy, *name, *ptr; 107 108 name = copy = xstrdup(in); 109 if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0) 110 name = name + (sizeof "exec ") - 1; 111 112 while (*name == ' ') 113 name++; 114 if ((ptr = strchr(name, ' ')) != NULL) 115 *ptr = '\0'; 116 117 if (*name != '\0') { 118 ptr = name + strlen(name) - 1; 119 while (ptr > name && !isalnum((u_char)*ptr)) 120 *ptr-- = '\0'; 121 } 122 123 if (*name == '/') 124 name = basename(name); 125 name = xstrdup(name); 126 free(copy); 127 return (name); 128 } 129