summaryrefslogtreecommitdiff
path: root/usr.bin/tmux
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2015-02-06 17:11:40 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2015-02-06 17:11:40 +0000
commitdfc5d64b8e5c5cd2dff1791503dd818ecf924be1 (patch)
tree0664d73861c5604a3dcea7bc791f50bf4ab9ef8e /usr.bin/tmux
parentcdc6bb8060137b0027903c59e1e00006b3368e81 (diff)
Add format_expand_time and use it instead of status_replace where
command execution is not needed.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r--usr.bin/tmux/cmd-pipe-pane.c23
-rw-r--r--usr.bin/tmux/format.c32
-rw-r--r--usr.bin/tmux/status.c29
-rw-r--r--usr.bin/tmux/tmux.h3
4 files changed, 74 insertions, 13 deletions
diff --git a/usr.bin/tmux/cmd-pipe-pane.c b/usr.bin/tmux/cmd-pipe-pane.c
index 5c31492291c..4c9c0002a80 100644
--- a/usr.bin/tmux/cmd-pipe-pane.c
+++ b/usr.bin/tmux/cmd-pipe-pane.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-pipe-pane.c,v 1.28 2015/02/01 23:43:23 nicm Exp $ */
+/* $OpenBSD: cmd-pipe-pane.c,v 1.29 2015/02/06 17:11:39 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -22,6 +22,7 @@
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
+#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
@@ -49,11 +50,14 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct client *c;
+ struct session *s;
+ struct winlink *wl;
struct window_pane *wp;
- char *command;
+ char *cmd;
int old_fd, pipe_fd[2], null_fd;
+ struct format_tree *ft;
- if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
+ if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL)
return (CMD_RETURN_ERROR);
c = cmd_find_client(cmdq, NULL, 1);
@@ -84,10 +88,18 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR);
}
+ /* Expand the command. */
+ ft = format_create();
+ format_defaults(ft, c, s, wl, wp);
+ cmd = format_expand_time(ft, args->argv[0], time(NULL));
+ format_free(ft);
+
/* Fork the child. */
switch (fork()) {
case -1:
cmdq_error(cmdq, "fork error: %s", strerror(errno));
+
+ free(cmd);
return (CMD_RETURN_ERROR);
case 0:
/* Child process. */
@@ -109,8 +121,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
closefrom(STDERR_FILENO + 1);
- command = status_replace(c, NULL, args->argv[0], time(NULL), 0);
- execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
+ execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
_exit(1);
default:
/* Parent process. */
@@ -124,6 +135,8 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
bufferevent_enable(wp->pipe_event, EV_WRITE);
setblocking(wp->pipe_fd, 0);
+
+ free(cmd);
return (CMD_RETURN_NORMAL);
}
}
diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c
index 77ae4096144..3cb90a60c96 100644
--- a/usr.bin/tmux/format.c
+++ b/usr.bin/tmux/format.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: format.c,v 1.57 2015/02/05 10:29:43 nicm Exp $ */
+/* $OpenBSD: format.c,v 1.58 2015/02/06 17:11:39 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -326,6 +326,33 @@ fail:
return (-1);
}
+/* Expand keys in a template, passing through strftime first. */
+char *
+format_expand_time(struct format_tree *ft, const char *fmt, time_t t)
+{
+ char *tmp, *expanded;
+ size_t tmplen;
+ struct tm *tm;
+
+ if (fmt == NULL)
+ return (xstrdup(""));
+
+ tm = localtime(&t);
+
+ tmp = NULL;
+ tmplen = strlen(fmt);
+
+ do {
+ tmp = xreallocarray(tmp, 2, tmplen);
+ tmplen *= 2;
+ } while (strftime(tmp, tmplen, fmt, tm) == 0);
+
+ expanded = format_expand(ft, tmp);
+ free(tmp);
+
+ return (expanded);
+}
+
/* Expand keys in a template. */
char *
format_expand(struct format_tree *ft, const char *fmt)
@@ -335,6 +362,9 @@ format_expand(struct format_tree *ft, const char *fmt)
size_t off, len, n;
int ch, brackets;
+ if (fmt == NULL)
+ return (xstrdup(""));
+
len = 64;
buf = xmalloc(len);
off = 0;
diff --git a/usr.bin/tmux/status.c b/usr.bin/tmux/status.c
index f031a8e10d8..a05639854bb 100644
--- a/usr.bin/tmux/status.c
+++ b/usr.bin/tmux/status.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: status.c,v 1.121 2015/02/05 10:29:43 nicm Exp $ */
+/* $OpenBSD: status.c,v 1.122 2015/02/06 17:11:39 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -760,14 +760,20 @@ status_prompt_set(struct client *c, const char *msg, const char *input,
int (*callbackfn)(void *, const char *), void (*freefn)(void *),
void *data, int flags)
{
- int keys;
+ struct format_tree *ft;
+ int keys;
+ time_t t;
+
+ ft = format_create();
+ format_defaults(ft, c, NULL, NULL, NULL);
+ t = time(NULL);
status_message_clear(c);
status_prompt_clear(c);
- c->prompt_string = status_replace(c, NULL, msg, time(NULL), 0);
+ c->prompt_string = format_expand_time(ft, msg, time(NULL));
- c->prompt_buffer = status_replace(c, NULL, input, time(NULL), 0);
+ c->prompt_buffer = format_expand_time(ft, input, time(NULL));
c->prompt_index = strlen(c->prompt_buffer);
c->prompt_callbackfn = callbackfn;
@@ -786,6 +792,8 @@ status_prompt_set(struct client *c, const char *msg, const char *input,
c->tty.flags |= (TTY_NOCURSOR|TTY_FREEZE);
c->flags |= CLIENT_STATUS;
+
+ format_free(ft);
}
/* Remove status line prompt. */
@@ -814,16 +822,25 @@ status_prompt_clear(struct client *c)
void
status_prompt_update(struct client *c, const char *msg, const char *input)
{
+ struct format_tree *ft;
+ time_t t;
+
+ ft = format_create();
+ format_defaults(ft, c, NULL, NULL, NULL);
+ t = time(NULL);
+
free(c->prompt_string);
- c->prompt_string = status_replace(c, NULL, msg, time(NULL), 0);
+ c->prompt_string = format_expand_time(ft, msg, time(NULL));
free(c->prompt_buffer);
- c->prompt_buffer = status_replace(c, NULL, input, time(NULL), 0);
+ c->prompt_buffer = format_expand_time(ft, input, time(NULL));
c->prompt_index = strlen(c->prompt_buffer);
c->prompt_hindex = 0;
c->flags |= CLIENT_STATUS;
+
+ format_free(ft);
}
/* Draw client prompt on status line of present else on last line. */
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index 233857a3f87..4aebcb46bf1 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.488 2015/02/05 10:29:43 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.489 2015/02/06 17:11:39 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -1512,6 +1512,7 @@ void format_free(struct format_tree *);
void printflike(3, 4) format_add(struct format_tree *, const char *,
const char *, ...);
const char *format_find(struct format_tree *, const char *);
+char *format_expand_time(struct format_tree *, const char *, time_t);
char *format_expand(struct format_tree *, const char *);
void format_defaults(struct format_tree *, struct client *,
struct session *, struct winlink *, struct window_pane *);