summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.bin/tmux/format.c36
-rw-r--r--usr.bin/tmux/xmalloc.c12
-rw-r--r--usr.bin/tmux/xmalloc.h3
3 files changed, 34 insertions, 17 deletions
diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c
index 2fc6c461c39..0b3ccd06259 100644
--- a/usr.bin/tmux/format.c
+++ b/usr.bin/tmux/format.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: format.c,v 1.112 2016/10/16 19:36:37 nicm Exp $ */
+/* $OpenBSD: format.c,v 1.113 2016/11/17 10:06:08 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -78,6 +78,7 @@ static void format_defaults_winlink(struct format_tree *, struct session *,
/* Entry in format job tree. */
struct format_job {
const char *cmd;
+ const char *expanded;
time_t last;
char *out;
@@ -232,22 +233,33 @@ format_job_callback(struct job *job)
static char *
format_job_get(struct format_tree *ft, const char *cmd)
{
- struct format_job fj0, *fj;
- time_t t;
+ struct format_job fj0, *fj;
+ time_t t;
+ char *expanded;
+ int force;
fj0.cmd = cmd;
if ((fj = RB_FIND(format_job_tree, &format_jobs, &fj0)) == NULL) {
fj = xcalloc(1, sizeof *fj);
fj->cmd = xstrdup(cmd);
+ fj->expanded = NULL;
xasprintf(&fj->out, "<'%s' not ready>", fj->cmd);
RB_INSERT(format_job_tree, &format_jobs, fj);
}
+ expanded = format_expand(ft, cmd);
+ if (fj->expanded == NULL || strcmp(expanded, fj->expanded) != 0) {
+ free((void *)fj->expanded);
+ fj->expanded = xstrdup(expanded);
+ force = 1;
+ } else
+ force = (ft->flags & FORMAT_FORCE);
+
t = time(NULL);
- if (fj->job == NULL && ((ft->flags & FORMAT_FORCE) || fj->last != t)) {
- fj->job = job_run(fj->cmd, NULL, NULL, format_job_callback,
+ if (fj->job == NULL && (force || fj->last != t)) {
+ fj->job = job_run(expanded, NULL, NULL, format_job_callback,
NULL, fj);
if (fj->job == NULL) {
free(fj->out);
@@ -259,6 +271,7 @@ format_job_get(struct format_tree *ft, const char *cmd)
if (ft->flags & FORMAT_STATUS)
fj->status = 1;
+ free(expanded);
return (format_expand(ft, fj->out));
}
@@ -281,6 +294,7 @@ format_job_timer(__unused int fd, __unused short events, __unused void *arg)
if (fj->job != NULL)
job_free(fj->job);
+ free((void *)fj->expanded);
free((void *)fj->cmd);
free(fj->out);
@@ -883,7 +897,7 @@ format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
char *
format_expand(struct format_tree *ft, const char *fmt)
{
- char *buf, *tmp, *cmd, *out;
+ char *buf, *out;
const char *ptr, *s, *saved = fmt;
size_t off, len, n, outlen;
int ch, brackets;
@@ -920,17 +934,9 @@ format_expand(struct format_tree *ft, const char *fmt)
break;
n = ptr - fmt;
- tmp = xmalloc(n + 1);
- memcpy(tmp, fmt, n);
- tmp[n] = '\0';
- cmd = format_expand(ft, tmp);
-
- out = format_job_get(ft, cmd);
+ out = format_job_get(ft, xstrndup(fmt, n));
outlen = strlen(out);
- free(cmd);
- free(tmp);
-
while (len - off < outlen + 1) {
buf = xreallocarray(buf, 2, len);
len *= 2;
diff --git a/usr.bin/tmux/xmalloc.c b/usr.bin/tmux/xmalloc.c
index d1a3dc587c6..367db15ea52 100644
--- a/usr.bin/tmux/xmalloc.c
+++ b/usr.bin/tmux/xmalloc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: xmalloc.c,v 1.10 2016/04/04 16:19:43 nicm Exp $ */
+/* $OpenBSD: xmalloc.c,v 1.11 2016/11/17 10:06:08 nicm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -81,6 +81,16 @@ xstrdup(const char *str)
return cp;
}
+char *
+xstrndup(const char *str, size_t maxlen)
+{
+ char *cp;
+
+ if ((cp = strndup(str, maxlen)) == NULL)
+ fatalx("xstrndup: %s", strerror(errno));
+ return cp;
+}
+
int
xasprintf(char **ret, const char *fmt, ...)
{
diff --git a/usr.bin/tmux/xmalloc.h b/usr.bin/tmux/xmalloc.h
index 6d30e3cfdab..76d93a26fc3 100644
--- a/usr.bin/tmux/xmalloc.h
+++ b/usr.bin/tmux/xmalloc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: xmalloc.h,v 1.1 2015/11/18 13:06:54 nicm Exp $ */
+/* $OpenBSD: xmalloc.h,v 1.2 2016/11/17 10:06:08 nicm Exp $ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -24,6 +24,7 @@ void *xcalloc(size_t, size_t);
void *xrealloc(void *, size_t);
void *xreallocarray(void *, size_t, size_t);
char *xstrdup(const char *);
+char *xstrndup(const char *, size_t);
int xasprintf(char **, const char *, ...)
__attribute__((__format__ (printf, 2, 3)))
__attribute__((__nonnull__ (2)));