Merge branch 'obsd-master'

This commit is contained in:
Thomas Adam 2020-06-02 11:29:48 +01:00
commit 31a51cae1d
7 changed files with 223 additions and 178 deletions

View File

@ -45,6 +45,7 @@
struct control_block { struct control_block {
size_t size; size_t size;
char *line; char *line;
uint64_t t;
TAILQ_ENTRY(control_block) entry; TAILQ_ENTRY(control_block) entry;
TAILQ_ENTRY(control_block) all_entry; TAILQ_ENTRY(control_block) all_entry;
@ -152,6 +153,21 @@ control_add_pane(struct client *c, struct window_pane *wp)
return (cp); return (cp);
} }
/* Get actual pane for this client. */
static struct window_pane *
control_window_pane(struct client *c, u_int pane)
{
struct window_pane *wp;
if (c->session == NULL)
return (NULL);
if ((wp = window_pane_find_by_id(pane)) == NULL)
return (NULL);
if (winlink_find_by_window(&c->session->windows, wp->window) == NULL)
return (NULL);
return (wp);
}
/* Reset control offsets. */ /* Reset control offsets. */
void void
control_reset_offsets(struct client *c) control_reset_offsets(struct client *c)
@ -253,6 +269,7 @@ control_write(struct client *c, const char *fmt, ...)
cb = xcalloc(1, sizeof *cb); cb = xcalloc(1, sizeof *cb);
xvasprintf(&cb->line, fmt, ap); xvasprintf(&cb->line, fmt, ap);
TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry); TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry);
cb->t = get_timer();
log_debug("%s: %s: storing line: %s", __func__, c->name, cb->line); log_debug("%s: %s: storing line: %s", __func__, c->name, cb->line);
bufferevent_enable(cs->write_event, EV_WRITE); bufferevent_enable(cs->write_event, EV_WRITE);
@ -290,6 +307,7 @@ control_write_output(struct client *c, struct window_pane *wp)
cb = xcalloc(1, sizeof *cb); cb = xcalloc(1, sizeof *cb);
cb->size = new_size; cb->size = new_size;
TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry); TAILQ_INSERT_TAIL(&cs->all_blocks, cb, all_entry);
cb->t = get_timer();
TAILQ_INSERT_TAIL(&cp->blocks, cb, entry); TAILQ_INSERT_TAIL(&cp->blocks, cb, entry);
log_debug("%s: %s: new output block of %zu for %%%u", __func__, c->name, log_debug("%s: %s: new output block of %zu for %%%u", __func__, c->name,
@ -446,17 +464,17 @@ static int
control_write_pending(struct client *c, struct control_pane *cp, size_t limit) control_write_pending(struct client *c, struct control_pane *cp, size_t limit)
{ {
struct control_state *cs = c->control_state; struct control_state *cs = c->control_state;
struct session *s = c->session;
struct window_pane *wp = NULL; struct window_pane *wp = NULL;
struct evbuffer *message = NULL; struct evbuffer *message = NULL;
size_t used = 0, size; size_t used = 0, size;
struct control_block *cb, *cb1; struct control_block *cb, *cb1;
if (s == NULL || wp = control_window_pane(c, cp->pane);
(wp = window_pane_find_by_id(cp->pane)) == NULL || if (wp == NULL) {
winlink_find_by_window(&s->windows, wp->window) == NULL) { TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) {
TAILQ_FOREACH_SAFE(cb, &cp->blocks, entry, cb1) TAILQ_REMOVE(&cp->blocks, cb, entry);
control_free_block(cs, cb); control_free_block(cs, cb);
}
control_flush_all_blocks(c); control_flush_all_blocks(c);
return (0); return (0);
} }

313
format.c
View File

@ -38,13 +38,9 @@
* string. * string.
*/ */
struct format_entry;
typedef void (*format_cb)(struct format_tree *, struct format_entry *);
static char *format_job_get(struct format_tree *, const char *); static char *format_job_get(struct format_tree *, const char *);
static void format_job_timer(int, short, void *); static void format_job_timer(int, short, void *);
static void format_add_cb(struct format_tree *, const char *, format_cb);
static int format_replace(struct format_tree *, const char *, size_t, static int format_replace(struct format_tree *, const char *, size_t,
char **, size_t *, size_t *); char **, size_t *, size_t *);
static void format_defaults_session(struct format_tree *, static void format_defaults_session(struct format_tree *,
@ -421,50 +417,51 @@ format_job_timer(__unused int fd, __unused short events, __unused void *arg)
} }
/* Callback for host. */ /* Callback for host. */
static void static char *
format_cb_host(__unused struct format_tree *ft, struct format_entry *fe) format_cb_host(__unused struct format_tree *ft)
{ {
char host[HOST_NAME_MAX + 1]; char host[HOST_NAME_MAX + 1];
if (gethostname(host, sizeof host) != 0) if (gethostname(host, sizeof host) != 0)
fe->value = xstrdup(""); return (xstrdup(""));
else return (xstrdup(host));
fe->value = xstrdup(host);
} }
/* Callback for host_short. */ /* Callback for host_short. */
static void static char *
format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe) format_cb_host_short(__unused struct format_tree *ft)
{ {
char host[HOST_NAME_MAX + 1], *cp; char host[HOST_NAME_MAX + 1], *cp;
if (gethostname(host, sizeof host) != 0) if (gethostname(host, sizeof host) != 0)
fe->value = xstrdup(""); return (xstrdup(""));
else {
if ((cp = strchr(host, '.')) != NULL) if ((cp = strchr(host, '.')) != NULL)
*cp = '\0'; *cp = '\0';
fe->value = xstrdup(host); return (xstrdup(host));
}
} }
/* Callback for pid. */ /* Callback for pid. */
static void static char *
format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe) format_cb_pid(__unused struct format_tree *ft)
{ {
xasprintf(&fe->value, "%ld", (long)getpid()); char *value;
xasprintf(&value, "%ld", (long)getpid());
return (value);
} }
/* Callback for session_attached_list. */ /* Callback for session_attached_list. */
static void static char *
format_cb_session_attached_list(struct format_tree *ft, struct format_entry *fe) format_cb_session_attached_list(struct format_tree *ft)
{ {
struct session *s = ft->s; struct session *s = ft->s;
struct client *loop; struct client *loop;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
if (s == NULL) if (s == NULL)
return; return (NULL);
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -479,20 +476,21 @@ format_cb_session_attached_list(struct format_tree *ft, struct format_entry *fe)
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for session_alerts. */ /* Callback for session_alerts. */
static void static char *
format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe) format_cb_session_alerts(struct format_tree *ft)
{ {
struct session *s = ft->s; struct session *s = ft->s;
struct winlink *wl; struct winlink *wl;
char alerts[1024], tmp[16]; char alerts[1024], tmp[16];
if (s == NULL) if (s == NULL)
return; return (NULL);
*alerts = '\0'; *alerts = '\0';
RB_FOREACH(wl, winlinks, &s->windows) { RB_FOREACH(wl, winlinks, &s->windows) {
@ -510,19 +508,19 @@ format_cb_session_alerts(struct format_tree *ft, struct format_entry *fe)
if (wl->flags & WINLINK_SILENCE) if (wl->flags & WINLINK_SILENCE)
strlcat(alerts, "~", sizeof alerts); strlcat(alerts, "~", sizeof alerts);
} }
fe->value = xstrdup(alerts); return (xstrdup(alerts));
} }
/* Callback for session_stack. */ /* Callback for session_stack. */
static void static char *
format_cb_session_stack(struct format_tree *ft, struct format_entry *fe) format_cb_session_stack(struct format_tree *ft)
{ {
struct session *s = ft->s; struct session *s = ft->s;
struct winlink *wl; struct winlink *wl;
char result[1024], tmp[16]; char result[1024], tmp[16];
if (s == NULL) if (s == NULL)
return; return (NULL);
xsnprintf(result, sizeof result, "%u", s->curw->idx); xsnprintf(result, sizeof result, "%u", s->curw->idx);
TAILQ_FOREACH(wl, &s->lastw, sentry) { TAILQ_FOREACH(wl, &s->lastw, sentry) {
@ -532,16 +530,17 @@ format_cb_session_stack(struct format_tree *ft, struct format_entry *fe)
strlcat(result, ",", sizeof result); strlcat(result, ",", sizeof result);
strlcat(result, tmp, sizeof result); strlcat(result, tmp, sizeof result);
} }
fe->value = xstrdup(result); return (xstrdup(result));
} }
/* Callback for window_stack_index. */ /* Callback for window_stack_index. */
static void static char *
format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe) format_cb_window_stack_index(struct format_tree *ft)
{ {
struct session *s = ft->wl->session; struct session *s = ft->wl->session;
struct winlink *wl; struct winlink *wl;
u_int idx; u_int idx;
char *value = NULL;
idx = 0; idx = 0;
TAILQ_FOREACH(wl, &s->lastw, sentry) { TAILQ_FOREACH(wl, &s->lastw, sentry) {
@ -549,21 +548,21 @@ format_cb_window_stack_index(struct format_tree *ft, struct format_entry *fe)
if (wl == ft->wl) if (wl == ft->wl)
break; break;
} }
if (wl != NULL) if (wl == NULL)
xasprintf(&fe->value, "%u", idx); return (xstrdup("0"));
else xasprintf(&value, "%u", idx);
fe->value = xstrdup("0"); return (value);
} }
/* Callback for window_linked_sessions_list. */ /* Callback for window_linked_sessions_list. */
static void static char *
format_cb_window_linked_sessions_list(struct format_tree *ft, format_cb_window_linked_sessions_list(struct format_tree *ft)
struct format_entry *fe)
{ {
struct window *w = ft->wl->window; struct window *w = ft->wl->window;
struct winlink *wl; struct winlink *wl;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -576,36 +575,38 @@ format_cb_window_linked_sessions_list(struct format_tree *ft,
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for window_active_sessions. */ /* Callback for window_active_sessions. */
static void static char *
format_cb_window_active_sessions(struct format_tree *ft, format_cb_window_active_sessions(struct format_tree *ft)
struct format_entry *fe)
{ {
struct window *w = ft->wl->window; struct window *w = ft->wl->window;
struct winlink *wl; struct winlink *wl;
u_int n = 0; u_int n = 0;
char *value;
TAILQ_FOREACH(wl, &w->winlinks, wentry) { TAILQ_FOREACH(wl, &w->winlinks, wentry) {
if (wl->session->curw == wl) if (wl->session->curw == wl)
n++; n++;
} }
xasprintf(&fe->value, "%u", n); xasprintf(&value, "%u", n);
return (value);
} }
/* Callback for window_active_sessions_list. */ /* Callback for window_active_sessions_list. */
static void static char *
format_cb_window_active_sessions_list(struct format_tree *ft, format_cb_window_active_sessions_list(struct format_tree *ft)
struct format_entry *fe)
{ {
struct window *w = ft->wl->window; struct window *w = ft->wl->window;
struct winlink *wl; struct winlink *wl;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -620,18 +621,20 @@ format_cb_window_active_sessions_list(struct format_tree *ft,
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for window_active_clients. */ /* Callback for window_active_clients. */
static void static char *
format_cb_window_active_clients(struct format_tree *ft, struct format_entry *fe) format_cb_window_active_clients(struct format_tree *ft)
{ {
struct window *w = ft->wl->window; struct window *w = ft->wl->window;
struct client *loop; struct client *loop;
struct session *client_session; struct session *client_session;
u_int n = 0; u_int n = 0;
char *value;
TAILQ_FOREACH(loop, &clients, entry) { TAILQ_FOREACH(loop, &clients, entry) {
client_session = loop->session; client_session = loop->session;
@ -642,19 +645,20 @@ format_cb_window_active_clients(struct format_tree *ft, struct format_entry *fe)
n++; n++;
} }
xasprintf(&fe->value, "%u", n); xasprintf(&value, "%u", n);
return (value);
} }
/* Callback for window_active_clients_list. */ /* Callback for window_active_clients_list. */
static void static char *
format_cb_window_active_clients_list(struct format_tree *ft, format_cb_window_active_clients_list(struct format_tree *ft)
struct format_entry *fe)
{ {
struct window *w = ft->wl->window; struct window *w = ft->wl->window;
struct client *loop; struct client *loop;
struct session *client_session; struct session *client_session;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -673,58 +677,58 @@ format_cb_window_active_clients_list(struct format_tree *ft,
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for window_layout. */ /* Callback for window_layout. */
static void static char *
format_cb_window_layout(struct format_tree *ft, struct format_entry *fe) format_cb_window_layout(struct format_tree *ft)
{ {
struct window *w = ft->w; struct window *w = ft->w;
if (w == NULL) if (w == NULL)
return; return (NULL);
if (w->saved_layout_root != NULL) if (w->saved_layout_root != NULL)
fe->value = layout_dump(w->saved_layout_root); return (layout_dump(w->saved_layout_root));
else return (layout_dump(w->layout_root));
fe->value = layout_dump(w->layout_root);
} }
/* Callback for window_visible_layout. */ /* Callback for window_visible_layout. */
static void static char *
format_cb_window_visible_layout(struct format_tree *ft, struct format_entry *fe) format_cb_window_visible_layout(struct format_tree *ft)
{ {
struct window *w = ft->w; struct window *w = ft->w;
if (w == NULL) if (w == NULL)
return; return (NULL);
fe->value = layout_dump(w->layout_root); return (layout_dump(w->layout_root));
} }
/* Callback for pane_start_command. */ /* Callback for pane_start_command. */
static void static char *
format_cb_start_command(struct format_tree *ft, struct format_entry *fe) format_cb_start_command(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
fe->value = cmd_stringify_argv(wp->argc, wp->argv); return (cmd_stringify_argv(wp->argc, wp->argv));
} }
/* Callback for pane_current_command. */ /* Callback for pane_current_command. */
static void static char *
format_cb_current_command(struct format_tree *ft, struct format_entry *fe) format_cb_current_command(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
char *cmd; char *cmd, *value;
if (wp == NULL || wp->shell == NULL) if (wp == NULL || wp->shell == NULL)
return; return (NULL);
cmd = osdep_get_name(wp->fd, wp->tty); cmd = osdep_get_name(wp->fd, wp->tty);
if (cmd == NULL || *cmd == '\0') { if (cmd == NULL || *cmd == '\0') {
@ -735,37 +739,40 @@ format_cb_current_command(struct format_tree *ft, struct format_entry *fe)
cmd = xstrdup(wp->shell); cmd = xstrdup(wp->shell);
} }
} }
fe->value = parse_window_name(cmd); value = parse_window_name(cmd);
free(cmd); free(cmd);
return (value);
} }
/* Callback for pane_current_path. */ /* Callback for pane_current_path. */
static void static char *
format_cb_current_path(struct format_tree *ft, struct format_entry *fe) format_cb_current_path(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
char *cwd; char *cwd;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
cwd = osdep_get_cwd(wp->fd); cwd = osdep_get_cwd(wp->fd);
if (cwd != NULL) if (cwd == NULL)
fe->value = xstrdup(cwd); return (NULL);
return (xstrdup(cwd));
} }
/* Callback for history_bytes. */ /* Callback for history_bytes. */
static void static char *
format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe) format_cb_history_bytes(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct grid *gd; struct grid *gd;
struct grid_line *gl; struct grid_line *gl;
size_t size = 0; size_t size = 0;
u_int i; u_int i;
char *value;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
gd = wp->base.grid; gd = wp->base.grid;
for (i = 0; i < gd->hsize + gd->sy; i++) { for (i = 0; i < gd->hsize + gd->sy; i++) {
@ -775,20 +782,22 @@ format_cb_history_bytes(struct format_tree *ft, struct format_entry *fe)
} }
size += (gd->hsize + gd->sy) * sizeof *gl; size += (gd->hsize + gd->sy) * sizeof *gl;
xasprintf(&fe->value, "%zu", size); xasprintf(&value, "%zu", size);
return (value);
} }
/* Callback for history_all_bytes. */ /* Callback for history_all_bytes. */
static void static char *
format_cb_history_all_bytes(struct format_tree *ft, struct format_entry *fe) format_cb_history_all_bytes(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct grid *gd; struct grid *gd;
struct grid_line *gl; struct grid_line *gl;
u_int i, lines, cells = 0, extended_cells = 0; u_int i, lines, cells = 0, extended_cells = 0;
char *value;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
gd = wp->base.grid; gd = wp->base.grid;
lines = gd->hsize + gd->sy; lines = gd->hsize + gd->sy;
@ -798,22 +807,24 @@ format_cb_history_all_bytes(struct format_tree *ft, struct format_entry *fe)
extended_cells += gl->extdsize; extended_cells += gl->extdsize;
} }
xasprintf(&fe->value, "%u,%zu,%u,%zu,%u,%zu", lines, xasprintf(&value, "%u,%zu,%u,%zu,%u,%zu", lines,
lines * sizeof *gl, cells, cells * sizeof *gl->celldata, lines * sizeof *gl, cells, cells * sizeof *gl->celldata,
extended_cells, extended_cells * sizeof *gl->extddata); extended_cells, extended_cells * sizeof *gl->extddata);
return (value);
} }
/* Callback for pane_tabs. */ /* Callback for pane_tabs. */
static void static char *
format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe) format_cb_pane_tabs(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct evbuffer *buffer; struct evbuffer *buffer;
u_int i; u_int i;
int size; int size;
char *value = NULL;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -827,25 +838,27 @@ format_cb_pane_tabs(struct format_tree *ft, struct format_entry *fe)
evbuffer_add_printf(buffer, "%u", i); evbuffer_add_printf(buffer, "%u", i);
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for session_group_list. */ /* Callback for session_group_list. */
static void static char *
format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe) format_cb_session_group_list(struct format_tree *ft)
{ {
struct session *s = ft->s; struct session *s = ft->s;
struct session_group *sg; struct session_group *sg;
struct session *loop; struct session *loop;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
if (s == NULL) if (s == NULL)
return; return (NULL);
sg = session_group_contains(s); sg = session_group_contains(s);
if (sg == NULL) if (sg == NULL)
return; return (NULL);
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -858,26 +871,27 @@ format_cb_session_group_list(struct format_tree *ft, struct format_entry *fe)
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for session_group_attached_list. */ /* Callback for session_group_attached_list. */
static void static char *
format_cb_session_group_attached_list(struct format_tree *ft, format_cb_session_group_attached_list(struct format_tree *ft)
struct format_entry *fe)
{ {
struct session *s = ft->s, *client_session, *session_loop; struct session *s = ft->s, *client_session, *session_loop;
struct session_group *sg; struct session_group *sg;
struct client *loop; struct client *loop;
struct evbuffer *buffer; struct evbuffer *buffer;
int size; int size;
char *value = NULL;
if (s == NULL) if (s == NULL)
return; return (NULL);
sg = session_group_contains(s); sg = session_group_contains(s);
if (sg == NULL) if (sg == NULL)
return; return (NULL);
buffer = evbuffer_new(); buffer = evbuffer_new();
if (buffer == NULL) if (buffer == NULL)
@ -897,36 +911,40 @@ format_cb_session_group_attached_list(struct format_tree *ft,
} }
if ((size = EVBUFFER_LENGTH(buffer)) != 0) if ((size = EVBUFFER_LENGTH(buffer)) != 0)
xasprintf(&fe->value, "%.*s", size, EVBUFFER_DATA(buffer)); xasprintf(&value, "%.*s", size, EVBUFFER_DATA(buffer));
evbuffer_free(buffer); evbuffer_free(buffer);
return (value);
} }
/* Callback for pane_in_mode. */ /* Callback for pane_in_mode. */
static void static char *
format_cb_pane_in_mode(struct format_tree *ft, struct format_entry *fe) format_cb_pane_in_mode(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
u_int n = 0; u_int n = 0;
struct window_mode_entry *wme; struct window_mode_entry *wme;
char *value;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
TAILQ_FOREACH(wme, &wp->modes, entry) TAILQ_FOREACH(wme, &wp->modes, entry)
n++; n++;
xasprintf(&fe->value, "%u", n); xasprintf(&value, "%u", n);
return (value);
} }
/* Callback for pane_at_top. */ /* Callback for pane_at_top. */
static void static char *
format_cb_pane_at_top(struct format_tree *ft, struct format_entry *fe) format_cb_pane_at_top(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct window *w; struct window *w;
int status, flag; int status, flag;
char *value;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
w = wp->window; w = wp->window;
status = options_get_number(w->options, "pane-border-status"); status = options_get_number(w->options, "pane-border-status");
@ -934,19 +952,21 @@ format_cb_pane_at_top(struct format_tree *ft, struct format_entry *fe)
flag = (wp->yoff == 1); flag = (wp->yoff == 1);
else else
flag = (wp->yoff == 0); flag = (wp->yoff == 0);
xasprintf(&fe->value, "%d", flag); xasprintf(&value, "%d", flag);
return (value);
} }
/* Callback for pane_at_bottom. */ /* Callback for pane_at_bottom. */
static void static char *
format_cb_pane_at_bottom(struct format_tree *ft, struct format_entry *fe) format_cb_pane_at_bottom(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct window *w; struct window *w;
int status, flag; int status, flag;
char *value;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
w = wp->window; w = wp->window;
status = options_get_number(w->options, "pane-border-status"); status = options_get_number(w->options, "pane-border-status");
@ -954,22 +974,25 @@ format_cb_pane_at_bottom(struct format_tree *ft, struct format_entry *fe)
flag = (wp->yoff + wp->sy == w->sy - 1); flag = (wp->yoff + wp->sy == w->sy - 1);
else else
flag = (wp->yoff + wp->sy == w->sy); flag = (wp->yoff + wp->sy == w->sy);
xasprintf(&fe->value, "%d", flag); xasprintf(&value, "%d", flag);
return (value);
} }
/* Callback for cursor_character. */ /* Callback for cursor_character. */
static void static char *
format_cb_cursor_character(struct format_tree *ft, struct format_entry *fe) format_cb_cursor_character(struct format_tree *ft)
{ {
struct window_pane *wp = ft->wp; struct window_pane *wp = ft->wp;
struct grid_cell gc; struct grid_cell gc;
char *value = NULL;
if (wp == NULL) if (wp == NULL)
return; return (NULL);
grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc); grid_view_get_cell(wp->base.grid, wp->base.cx, wp->base.cy, &gc);
if (~gc.flags & GRID_FLAG_PADDING) if (~gc.flags & GRID_FLAG_PADDING)
xasprintf(&fe->value, "%.*s", (int)gc.data.size, gc.data.data); xasprintf(&value, "%.*s", (int)gc.data.size, gc.data.data);
return (value);
} }
/* Return word at given coordinates. Caller frees. */ /* Return word at given coordinates. Caller frees. */
@ -1043,8 +1066,8 @@ format_grid_word(struct grid *gd, u_int x, u_int y)
} }
/* Callback for mouse_word. */ /* Callback for mouse_word. */
static void static char *
format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe) format_cb_mouse_word(struct format_tree *ft)
{ {
struct window_pane *wp; struct window_pane *wp;
struct grid *gd; struct grid *gd;
@ -1052,25 +1075,21 @@ format_cb_mouse_word(struct format_tree *ft, struct format_entry *fe)
char *s; char *s;
if (!ft->m.valid) if (!ft->m.valid)
return; return (NULL);
wp = cmd_mouse_pane(&ft->m, NULL, NULL); wp = cmd_mouse_pane(&ft->m, NULL, NULL);
if (wp == NULL) if (wp == NULL)
return; return (NULL);
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
return; return (NULL);
if (!TAILQ_EMPTY(&wp->modes)) { if (!TAILQ_EMPTY(&wp->modes)) {
if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode || if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
TAILQ_FIRST(&wp->modes)->mode == &window_view_mode) TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
s = window_copy_get_word(wp, x, y); return (s = window_copy_get_word(wp, x, y));
else return (NULL);
s = NULL;
} else {
gd = wp->base.grid;
s = format_grid_word(gd, x, gd->hsize + y);
} }
if (s != NULL) gd = wp->base.grid;
fe->value = s; return (format_grid_word(gd, x, gd->hsize + y));
} }
/* Return line at given coordinates. Caller frees. */ /* Return line at given coordinates. Caller frees. */
@ -1100,34 +1119,29 @@ format_grid_line(struct grid *gd, u_int y)
} }
/* Callback for mouse_line. */ /* Callback for mouse_line. */
static void static char *
format_cb_mouse_line(struct format_tree *ft, struct format_entry *fe) format_cb_mouse_line(struct format_tree *ft)
{ {
struct window_pane *wp; struct window_pane *wp;
struct grid *gd; struct grid *gd;
u_int x, y; u_int x, y;
char *s;
if (!ft->m.valid) if (!ft->m.valid)
return; return (NULL);
wp = cmd_mouse_pane(&ft->m, NULL, NULL); wp = cmd_mouse_pane(&ft->m, NULL, NULL);
if (wp == NULL) if (wp == NULL)
return; return (NULL);
if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0) if (cmd_mouse_at(wp, &ft->m, &x, &y, 0) != 0)
return; return (NULL);
if (!TAILQ_EMPTY(&wp->modes)) { if (!TAILQ_EMPTY(&wp->modes)) {
if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode || if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode ||
TAILQ_FIRST(&wp->modes)->mode == &window_view_mode) TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
s = window_copy_get_line(wp, y); return (window_copy_get_line(wp, y));
else return (NULL);
s = NULL;
} else {
gd = wp->base.grid;
s = format_grid_line(gd, gd->hsize + y);
} }
if (s != NULL) gd = wp->base.grid;
fe->value = s; return (format_grid_line(gd, gd->hsize + y));
} }
/* Merge one format tree into another. */ /* Merge one format tree into another. */
@ -1244,7 +1258,7 @@ format_each(struct format_tree *ft, void (*cb)(const char *, const char *,
cb(fe->key, s, arg); cb(fe->key, s, arg);
} else { } else {
if (fe->value == NULL && fe->cb != NULL) { if (fe->value == NULL && fe->cb != NULL) {
fe->cb(ft, fe); fe->value = fe->cb(ft);
if (fe->value == NULL) if (fe->value == NULL)
fe->value = xstrdup(""); fe->value = xstrdup("");
} }
@ -1304,7 +1318,7 @@ format_add_tv(struct format_tree *ft, const char *key, struct timeval *tv)
} }
/* Add a key and function. */ /* Add a key and function. */
static void void
format_add_cb(struct format_tree *ft, const char *key, format_cb cb) format_add_cb(struct format_tree *ft, const char *key, format_cb cb)
{ {
struct format_entry *fe; struct format_entry *fe;
@ -1427,10 +1441,11 @@ format_find(struct format_tree *ft, const char *key, int modifiers,
t = fe->t; t = fe->t;
goto found; goto found;
} }
if (fe->value == NULL && fe->cb != NULL) if (fe->value == NULL && fe->cb != NULL) {
fe->cb(ft, fe); fe->value = fe->cb(ft);
if (fe->value == NULL) if (fe->value == NULL)
fe->value = xstrdup(""); fe->value = xstrdup("");
}
found = xstrdup(fe->value); found = xstrdup(fe->value);
goto found; goto found;
} }

View File

@ -1508,6 +1508,7 @@ server_client_check_pane_buffer(struct window_pane *wp)
struct window_pane_offset *wpo; struct window_pane_offset *wpo;
int off = 1, flag; int off = 1, flag;
u_int attached_clients = 0; u_int attached_clients = 0;
size_t new_size;
/* /*
* Work out the minimum used size. This is the most that can be removed * Work out the minimum used size. This is the most that can be removed
@ -1533,12 +1534,15 @@ server_client_check_pane_buffer(struct window_pane *wp)
if (!flag) if (!flag)
off = 0; off = 0;
log_debug("%s: %s has %zu bytes used for %%%u", __func__, window_pane_get_new_data(wp, wpo, &new_size);
c->name, wpo->used - wp->base_offset, wp->id); log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
if (wpo->used - wp->base_offset > SERVER_CLIENT_PANE_LIMIT) { __func__, c->name, wpo->used - wp->base_offset, new_size,
wp->id);
if (new_size > SERVER_CLIENT_PANE_LIMIT) {
control_flush(c); control_flush(c);
c->flags |= CLIENT_EXIT; c->flags |= CLIENT_EXIT;
} else if (wpo->used < minimum) }
if (wpo->used < minimum)
minimum = wpo->used; minimum = wpo->used;
} }
if (attached_clients == 0) if (attached_clients == 0)
@ -1773,7 +1777,9 @@ server_client_check_exit(struct client *c)
struct client_file *cf; struct client_file *cf;
const char *name = c->exit_session; const char *name = c->exit_session;
if ((c->flags & CLIENT_EXITED) || (~c->flags & CLIENT_EXIT)) if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
return;
if (~c->flags & CLIENT_EXIT)
return; return;
if (c->flags & CLIENT_CONTROL) { if (c->flags & CLIENT_CONTROL) {

14
tmux.c
View File

@ -237,6 +237,20 @@ setblocking(int fd, int state)
} }
} }
uint64_t
get_timer(void)
{
struct timespec ts;
/*
* We want a timestamp in milliseconds suitable for time measurement,
* so prefer the monotonic clock.
*/
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
clock_gettime(CLOCK_REALTIME, &ts);
return ((ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL));
}
const char * const char *
sig2name(int signo) sig2name(int signo)
{ {

3
tmux.h
View File

@ -1854,6 +1854,7 @@ extern int ptm_fd;
extern const char *shell_command; extern const char *shell_command;
int checkshell(const char *); int checkshell(const char *);
void setblocking(int, int); void setblocking(int, int);
uint64_t get_timer(void);
const char *sig2name(int); const char *sig2name(int);
const char *find_cwd(void); const char *find_cwd(void);
const char *find_home(void); const char *find_home(void);
@ -1914,6 +1915,7 @@ char *paste_make_sample(struct paste_buffer *);
#define FORMAT_WINDOW 0x40000000U #define FORMAT_WINDOW 0x40000000U
struct format_tree; struct format_tree;
struct format_modifier; struct format_modifier;
typedef char *(*format_cb)(struct format_tree *);
const char *format_skip(const char *, const char *); const char *format_skip(const char *, const char *);
int format_true(const char *); int format_true(const char *);
struct format_tree *format_create(struct client *, struct cmdq_item *, int, struct format_tree *format_create(struct client *, struct cmdq_item *, int,
@ -1924,6 +1926,7 @@ void printflike(3, 4) format_add(struct format_tree *, const char *,
const char *, ...); const char *, ...);
void format_add_tv(struct format_tree *, const char *, void format_add_tv(struct format_tree *, const char *,
struct timeval *); struct timeval *);
void format_add_cb(struct format_tree *, const char *, format_cb);
void format_each(struct format_tree *, void (*)(const char *, void format_each(struct format_tree *, void (*)(const char *,
const char *, void *), void *); const char *, void *), void *);
char *format_expand_time(struct format_tree *, const char *); char *format_expand_time(struct format_tree *, const char *);

View File

@ -637,8 +637,6 @@ tty_keys_next(struct tty *tty)
struct mouse_event m = { 0 }; struct mouse_event m = { 0 };
struct key_event *event; struct key_event *event;
gettimeofday(&tv, NULL);
/* Get key buffer. */ /* Get key buffer. */
buf = EVBUFFER_DATA(tty->in); buf = EVBUFFER_DATA(tty->in);
len = EVBUFFER_LENGTH(tty->in); len = EVBUFFER_LENGTH(tty->in);

View File

@ -2934,15 +2934,6 @@ window_copy_search(struct window_mode_entry *wme, int direction, int regex)
return (found); return (found);
} }
static uint64_t
window_copy_get_time(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return ((tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000ULL));
}
static int static int
window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
int regex) int regex)
@ -2985,11 +2976,11 @@ window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
return (0); return (0);
} }
} }
tstart = window_copy_get_time(); tstart = get_timer();
start = 0; start = 0;
end = gd->hsize + gd->sy; end = gd->hsize + gd->sy;
stop = window_copy_get_time() + WINDOW_COPY_SEARCH_ALL_TIMEOUT; stop = get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
again: again:
free(data->searchmark); free(data->searchmark);
@ -3027,7 +3018,7 @@ again:
px++; px++;
} }
t = window_copy_get_time(); t = get_timer();
if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) { if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
data->timeout = 1; data->timeout = 1;
break; break;