tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

names.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
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 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 *);
32 
33 static void
34 name_time_callback(__unused int fd, __unused short events, void *arg)
35 {
36  struct window *w = arg;
37 
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 
42 static int
43 name_time_expired(struct window *w, struct timeval *tv)
44 {
45  struct timeval offset;
46 
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);
51 }
52 
53 void
55 {
56  struct timeval tv, next;
57  char *name;
58  int left;
59 
60  if (w->active == NULL)
61  return;
62 
63  if (!options_get_number(w->options, "automatic-rename"))
64  return;
65 
66  if (~w->active->flags & PANE_CHANGED) {
67  log_debug("@%u active pane not changed", w->id);
68  return;
69  }
70  log_debug("@%u active pane changed", w->id);
71 
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)) {
78  log_debug("@%u name timer queued (%d left)", w->id,
79  left);
80  timerclear(&next);
81  next.tv_usec = left;
82  event_add(&w->name_event, &next);
83  } else {
84  log_debug("@%u name timer already queued (%d left)",
85  w->id, left);
86  }
87  return;
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 
93  w->active->flags &= ~~PANE_CHANGED;
94 
96  if (strcmp(name, w->name) != 0) {
97  log_debug("@%u new name %s (was %s)", w->id, name, w->name);
101  } else
102  log_debug("@%u name not changed (still %s)", w->id, w->name);
103 
104  free(name);
105 }
106 
107 char *
109 {
110  char *cmd, *s;
111 
112  if (w->active == NULL)
113  return (xstrdup(""));
115  if (cmd != NULL && *cmd != '\0')
116  s = parse_window_name(cmd);
117  else
118  s = parse_window_name(w->active->shell);
119  free(cmd);
120  return (s);
121 }
122 
123 static char *
125 {
126  struct format_tree *ft;
127  const char *fmt;
128  char *name;
129 
130  ft = format_create(NULL, NULL, FORMAT_WINDOW|w->id, 0);
133 
134  fmt = options_get_string(w->options, "automatic-rename-format");
135  name = format_expand(ft, fmt);
136 
137  format_free(ft);
138  return (name);
139 }
140 
141 char *
142 parse_window_name(const char *in)
143 {
144  char *copy, *name, *ptr;
145 
146  name = copy = xstrdup(in);
147  if (*name == '"')
148  name++;
149  name[strcspn (name, "\"")] = '\0';
150 
151  if (strncmp(name, "exec ", (sizeof "exec ") - 1) == 0)
152  name = name + (sizeof "exec ") - 1;
153 
154  while (*name == ' ' || *name == '-')
155  name++;
156  if ((ptr = strchr(name, ' ')) != NULL)
157  *ptr = '\0';
158 
159  if (*name != '\0') {
160  ptr = name + strlen(name) - 1;
161  while (ptr > name &&
162  !isalnum((u_char)*ptr) &&
163  !ispunct((u_char)*ptr))
164  *ptr-- = '\0';
165  }
166 
167  if (*name == '/')
168  name = basename(name);
169  name = xstrdup(name);
170  free(copy);
171  return (name);
172 }
char * cmd_stringify_argv(int argc, char **argv)
Definition: cmd.c:357
#define __unused
Definition: compat.h:60
#define timersub(tvp, uvp, vvp)
Definition: compat.h:249
struct format_tree * format_create(struct client *c, struct cmdq_item *item, int tag, int flags)
Definition: format.c:3042
char * format_expand(struct format_tree *ft, const char *fmt)
Definition: format.c:4609
void format_free(struct format_tree *ft)
Definition: format.c:3066
void format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
Definition: format.c:4773
void format_defaults_window(struct format_tree *ft, struct window *w)
Definition: format.c:4757
const char * name
Definition: layout-set.c:38
void log_debug(const char *msg,...)
Definition: log.c:130
char * parse_window_name(const char *in)
Definition: names.c:142
static int name_time_expired(struct window *, struct timeval *)
Definition: names.c:43
static char * format_window_name(struct window *)
Definition: names.c:124
void check_window_name(struct window *w)
Definition: names.c:54
char * default_window_name(struct window *w)
Definition: names.c:108
static void name_time_callback(int, short, void *)
Definition: names.c:34
const char * options_get_string(struct options *oo, const char *name)
Definition: options.c:686
long long options_get_number(struct options *oo, const char *name)
Definition: options.c:699
void server_redraw_window_borders(struct window *w)
Definition: server-fn.c:105
void server_status_window(struct window *w)
Definition: server-fn.c:116
Definition: cmd.c:212
struct window * w
Definition: format.c:135
char * shell
Definition: tmux.h:994
int argc
Definition: tmux.h:992
char ** argv
Definition: tmux.h:993
int flags
Definition: tmux.h:977
Definition: tmux.h:1041
u_int id
Definition: tmux.h:1042
struct timeval name_time
Definition: tmux.h:1047
char * name
Definition: tmux.h:1045
struct options * options
Definition: tmux.h:1085
struct event name_event
Definition: tmux.h:1046
struct window_pane * active
Definition: tmux.h:1054
#define PANE_CHANGED
Definition: tmux.h:985
#define FORMAT_WINDOW
Definition: tmux.h:1975
#define NAME_INTERVAL
Definition: tmux.h:87
void window_set_name(struct window *, const char *)
Definition: window.c:401
char * xstrdup(const char *str)
Definition: xmalloc.c:89