GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/names.c Lines: 0 65 0.0 %
Date: 2016-12-06 Branches: 0 40 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: names.c,v 1.35 2016/07/15 09:27:35 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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
26
#include "tmux.h"
27
28
void	name_time_callback(int, short, void *);
29
int	name_time_expired(struct window *, struct timeval *);
30
31
void
32
name_time_callback(__unused int fd, __unused short events, void *arg)
33
{
34
	struct window	*w = arg;
35
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
40
int
41
name_time_expired(struct window *w, struct timeval *tv)
42
{
43
	struct timeval	offset;
44
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);
49
}
50
51
void
52
check_window_name(struct window *w)
53
{
54
	struct timeval	 tv, next;
55
	char		*name;
56
	int		 left;
57
58
	if (w->active == NULL)
59
		return;
60
61
	if (!options_get_number(w->options, "automatic-rename"))
62
		return;
63
64
	if (~w->active->flags & PANE_CHANGED) {
65
		log_debug("@%u active pane not changed", w->id);
66
		return;
67
	}
68
	log_debug("@%u active pane changed", w->id);
69
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)) {
76
			log_debug("@%u name timer queued (%d left)", w->id,
77
			    left);
78
			timerclear(&next);
79
			next.tv_usec = left;
80
			event_add(&w->name_event, &next);
81
		} else {
82
			log_debug("@%u name timer already queued (%d left)",
83
			    w->id, left);
84
		}
85
		return;
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
91
	w->active->flags &= ~PANE_CHANGED;
92
93
	name = format_window_name(w);
94
	if (strcmp(name, w->name) != 0) {
95
		log_debug("@%u new name %s (was %s)", w->id, name, w->name);
96
		window_set_name(w, name);
97
		server_status_window(w);
98
	} else
99
		log_debug("@%u name not changed (still %s)", w->id, w->name);
100
101
	free(name);
102
}
103
104
char *
105
default_window_name(struct window *w)
106
{
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);
116
}
117
118
char *
119
format_window_name(struct window *w)
120
{
121
	struct format_tree	*ft;
122
	char			*fmt, *name;
123
124
	ft = format_create(NULL, 0);
125
	format_defaults_window(ft, w);
126
	format_defaults_pane(ft, w->active);
127
128
	fmt = options_get_string(w->options, "automatic-rename-format");
129
	name = format_expand(ft, fmt);
130
131
	format_free(ft);
132
	return (name);
133
}
134
135
char *
136
parse_window_name(const char *in)
137
{
138
	char	*copy, *name, *ptr;
139
140
	name = copy = xstrdup(in);
141
	if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
142
		name = name + (sizeof "exec ") - 1;
143
144
	while (*name == ' ' || *name == '-')
145
		name++;
146
	if ((ptr = strchr(name, ' ')) != NULL)
147
		*ptr = '\0';
148
149
	if (*name != '\0') {
150
		ptr = name + strlen(name) - 1;
151
		while (ptr > name && !isalnum((u_char)*ptr))
152
			*ptr-- = '\0';
153
	}
154
155
	if (*name == '/')
156
		name = basename(name);
157
	name = xstrdup(name);
158
	free(copy);
159
	return (name);
160
}