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