Annotation of src/usr.bin/tmux/names.c, Revision 1.36
1.36 ! nicm 1: /* $OpenBSD: names.c,v 1.35 2016/07/15 09:27:35 nicm Exp $ */
1.1 nicm 2:
3: /*
1.34 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.16 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
1.36 ! nicm 28: static void name_time_callback(int, short, void *);
! 29: static int name_time_expired(struct window *, struct timeval *);
1.28 nicm 30:
1.36 ! nicm 31: static void
1.31 nicm 32: name_time_callback(__unused int fd, __unused short events, void *arg)
1.1 nicm 33: {
1.29 nicm 34: struct window *w = arg;
1.28 nicm 35:
1.29 nicm 36: /* The event loop will call check_window_name for us on the way out. */
37: log_debug("@%u name timer expired", w->id);
38: }
39:
1.36 ! nicm 40: static int
1.29 nicm 41: name_time_expired(struct window *w, struct timeval *tv)
42: {
43: struct timeval offset;
1.28 nicm 44:
1.29 nicm 45: timersub(tv, &w->name_time, &offset);
46: if (offset.tv_sec != 0 || offset.tv_usec > NAME_INTERVAL)
47: return (0);
48: return (NAME_INTERVAL - offset.tv_usec);
1.28 nicm 49: }
50:
51: void
1.29 nicm 52: check_window_name(struct window *w)
1.28 nicm 53: {
1.29 nicm 54: struct timeval tv, next;
1.28 nicm 55: char *name;
1.29 nicm 56: int left;
1.17 nicm 57:
58: if (w->active == NULL)
59: return;
1.25 nicm 60:
1.30 nicm 61: if (!options_get_number(w->options, "automatic-rename"))
1.29 nicm 62: return;
63:
64: if (~w->active->flags & PANE_CHANGED) {
65: log_debug("@%u active pane not changed", w->id);
1.15 nicm 66: return;
1.28 nicm 67: }
1.29 nicm 68: log_debug("@%u active pane changed", w->id);
1.26 nicm 69:
1.29 nicm 70: gettimeofday(&tv, NULL);
71: left = name_time_expired(w, &tv);
72: if (left != 0) {
73: if (!event_initialized(&w->name_event))
74: evtimer_set(&w->name_event, name_time_callback, w);
75: if (!evtimer_pending(&w->name_event, NULL)) {
1.35 nicm 76: log_debug("@%u name timer queued (%d left)", w->id,
77: left);
1.29 nicm 78: timerclear(&next);
79: next.tv_usec = left;
80: event_add(&w->name_event, &next);
1.35 nicm 81: } else {
82: log_debug("@%u name timer already queued (%d left)",
83: w->id, left);
84: }
1.26 nicm 85: return;
1.29 nicm 86: }
87: memcpy(&w->name_time, &tv, sizeof w->name_time);
88: if (event_initialized(&w->name_event))
89: evtimer_del(&w->name_event);
90:
1.26 nicm 91: w->active->flags &= ~PANE_CHANGED;
1.9 nicm 92:
1.22 nicm 93: name = format_window_name(w);
94: if (strcmp(name, w->name) != 0) {
1.29 nicm 95: log_debug("@%u new name %s (was %s)", w->id, name, w->name);
1.22 nicm 96: window_set_name(w, name);
1.9 nicm 97: server_status_window(w);
1.29 nicm 98: } else
99: log_debug("@%u name not changed (still %s)", w->id, w->name);
100:
1.22 nicm 101: free(name);
1.1 nicm 102: }
103:
104: char *
105: default_window_name(struct window *w)
106: {
1.23 nicm 107: char *cmd, *s;
108:
109: cmd = cmd_stringify_argv(w->active->argc, w->active->argv);
110: if (cmd != NULL && *cmd != '\0')
111: s = parse_window_name(cmd);
112: else
113: s = parse_window_name(w->active->shell);
114: free(cmd);
115: return (s);
1.1 nicm 116: }
117:
118: char *
1.22 nicm 119: format_window_name(struct window *w)
120: {
121: struct format_tree *ft;
122: char *fmt, *name;
123:
1.33 nicm 124: ft = format_create(NULL, 0);
1.24 nicm 125: format_defaults_window(ft, w);
126: format_defaults_pane(ft, w->active);
1.22 nicm 127:
1.30 nicm 128: fmt = options_get_string(w->options, "automatic-rename-format");
1.22 nicm 129: name = format_expand(ft, fmt);
130:
131: format_free(ft);
132: return (name);
133: }
134:
135: char *
1.21 nicm 136: parse_window_name(const char *in)
1.1 nicm 137: {
1.21 nicm 138: char *copy, *name, *ptr;
1.1 nicm 139:
140: name = copy = xstrdup(in);
1.21 nicm 141: if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
142: name = name + (sizeof "exec ") - 1;
1.1 nicm 143:
1.22 nicm 144: while (*name == ' ' || *name == '-')
1.1 nicm 145: name++;
146: if ((ptr = strchr(name, ' ')) != NULL)
147: *ptr = '\0';
148:
149: if (*name != '\0') {
150: ptr = name + strlen(name) - 1;
1.2 ray 151: while (ptr > name && !isalnum((u_char)*ptr))
1.1 nicm 152: *ptr-- = '\0';
153: }
154:
155: if (*name == '/')
156: name = basename(name);
157: name = xstrdup(name);
1.16 nicm 158: free(copy);
1.1 nicm 159: return (name);
160: }