summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2013-10-10 11:50:22 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2013-10-10 11:50:22 +0000
commitcbfbf1fe086a18687e7993ebfd1b20837514e556 (patch)
treef1d73ea2994378fcb59da5f736fc7dcb087be32c
parent0b9a96f3371a05f08e6e625faedb0b91c218554f (diff)
Add length limit operator for formats.
-rw-r--r--usr.bin/tmux/format.c42
-rw-r--r--usr.bin/tmux/options-table.c4
-rw-r--r--usr.bin/tmux/tmux.18
3 files changed, 44 insertions, 10 deletions
diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c
index d5b31a78778..ccfd2861eb8 100644
--- a/usr.bin/tmux/format.c
+++ b/usr.bin/tmux/format.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: format.c,v 1.29 2013/10/10 11:47:52 nicm Exp $ */
+/* $OpenBSD: format.c,v 1.30 2013/10/10 11:50:20 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -18,6 +18,8 @@
#include <sys/types.h>
+#include <ctype.h>
+#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -188,18 +190,40 @@ format_find(struct format_tree *ft, const char *key)
* #{?blah,a,b} is replace with a if blah exists and is nonzero else b.
*/
int
-format_replace(struct format_tree *ft,
- const char *key, size_t keylen, char **buf, size_t *len, size_t *off)
+format_replace(struct format_tree *ft, const char *key, size_t keylen,
+ char **buf, size_t *len, size_t *off)
{
- char *copy, *ptr;
+ char *copy, *copy0, *endptr, *ptr;
const char *value;
size_t valuelen;
+ u_long limit = ULONG_MAX;
/* Make a copy of the key. */
- copy = xmalloc(keylen + 1);
+ copy0 = copy = xmalloc(keylen + 1);
memcpy(copy, key, keylen);
copy[keylen] = '\0';
+ /* Is there a length limit or whatnot? */
+ if (!islower((u_char) *copy) && *copy != '?') {
+ while (*copy != ':' && *copy != '\0') {
+ switch (*copy) {
+ case '=':
+ errno = 0;
+ limit = strtoul(copy + 1, &endptr, 10);
+ if (errno == ERANGE && limit == ULONG_MAX)
+ goto fail;
+ copy = endptr;
+ break;
+ default:
+ copy++;
+ break;
+ }
+ }
+ if (*copy != ':')
+ goto fail;
+ copy++;
+ }
+
/*
* Is this a conditional? If so, check it exists and extract either the
* first or second element. If not, look up the key directly.
@@ -230,6 +254,10 @@ format_replace(struct format_tree *ft,
}
valuelen = strlen(value);
+ /* Truncate the value if needed. */
+ if (valuelen > limit)
+ valuelen = limit;
+
/* Expand the buffer and copy in the value. */
while (*len - *off < valuelen + 1) {
*buf = xrealloc(*buf, 2, *len);
@@ -238,11 +266,11 @@ format_replace(struct format_tree *ft,
memcpy(*buf + *off, value, valuelen);
*off += valuelen;
- free(copy);
+ free(copy0);
return (0);
fail:
- free(copy);
+ free(copy0);
return (-1);
}
diff --git a/usr.bin/tmux/options-table.c b/usr.bin/tmux/options-table.c
index 90bd2915ad4..7bbb34195e6 100644
--- a/usr.bin/tmux/options-table.c
+++ b/usr.bin/tmux/options-table.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: options-table.c,v 1.39 2013/10/05 13:56:48 nicm Exp $ */
+/* $OpenBSD: options-table.c,v 1.40 2013/10/10 11:50:20 nicm Exp $ */
/*
* Copyright (c) 2011 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -387,7 +387,7 @@ const struct options_table_entry session_options_table[] = {
{ .name = "status-right",
.type = OPTIONS_TABLE_STRING,
- .default_str = "\"#22T\" %H:%M %d-%b-%y"
+ .default_str = "\"#{=22:pane_title}\" %H:%M %d-%b-%y"
},
{ .name = "status-right-attr",
diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1
index 461319b65de..0f05c1f4077 100644
--- a/usr.bin/tmux/tmux.1
+++ b/usr.bin/tmux/tmux.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: tmux.1,v 1.366 2013/10/10 11:47:52 nicm Exp $
+.\" $OpenBSD: tmux.1,v 1.367 2013/10/10 11:50:21 nicm Exp $
.\"
.\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
.\"
@@ -3024,6 +3024,12 @@ will include the string
if the session is attached and the string
.Ql not attached
if it is unattached.
+A limit may be placed on the length of the resultant string by prefixing it
+by an
+.Ql = ,
+a number and a colon, so
+.Ql #{=10:pane_title}
+will include at most the first 10 characters of the pane title.
.Pp
The following variables are available, where appropriate:
.Bl -column "XXXXXXXXXXXXXXXXXXX" "XXXXX"