aboutsummaryrefslogtreecommitdiff
path: root/format.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2015-11-18 16:42:25 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2015-11-18 16:42:25 +0000
commita58eaec5403d39f2e89e79146a0ffbb90a956679 (patch)
tree07928a20cc78ba386cf1fb20b9aa09e90961fecd /format.c
parent8b4b3ff4fcc73578f66697b158b56fdc28895368 (diff)
parent7fe8edc3962d5c30bf87677ecb2cf8633404f63c (diff)
downloadrtmux-a58eaec5403d39f2e89e79146a0ffbb90a956679.tar.gz
rtmux-a58eaec5403d39f2e89e79146a0ffbb90a956679.tar.bz2
rtmux-a58eaec5403d39f2e89e79146a0ffbb90a956679.zip
Merge branch 'master' of github.com:tmux/tmux
Diffstat (limited to 'format.c')
-rw-r--r--format.c88
1 files changed, 70 insertions, 18 deletions
diff --git a/format.c b/format.c
index 28c01a49..afa6af5d 100644
--- a/format.c
+++ b/format.c
@@ -101,6 +101,7 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2)
#define FORMAT_TIMESTRING 0x1
#define FORMAT_BASENAME 0x2
#define FORMAT_DIRNAME 0x4
+#define FORMAT_SUBSTITUTE 0x8
/* Entry in format tree. */
struct format_entry {
@@ -260,7 +261,7 @@ format_job_get(struct format_tree *ft, const char *cmd)
/* Remove old jobs. */
void
-format_job_timer(unused int fd, unused short events, unused void *arg)
+format_job_timer(__unused int fd, __unused short events, __unused void *arg)
{
struct format_job *fj, *fj1;
time_t now;
@@ -289,7 +290,7 @@ format_job_timer(unused int fd, unused short events, unused void *arg)
/* Callback for host. */
void
-format_cb_host(unused struct format_tree *ft, struct format_entry *fe)
+format_cb_host(__unused struct format_tree *ft, struct format_entry *fe)
{
char host[HOST_NAME_MAX + 1];
@@ -301,7 +302,7 @@ format_cb_host(unused struct format_tree *ft, struct format_entry *fe)
/* Callback for host_short. */
void
-format_cb_host_short(unused struct format_tree *ft, struct format_entry *fe)
+format_cb_host_short(__unused struct format_tree *ft, struct format_entry *fe)
{
char host[HOST_NAME_MAX + 1], *cp;
@@ -316,7 +317,7 @@ format_cb_host_short(unused struct format_tree *ft, struct format_entry *fe)
/* Callback for pid. */
void
-format_cb_pid(unused struct format_tree *ft, struct format_entry *fe)
+format_cb_pid(__unused struct format_tree *ft, struct format_entry *fe)
{
xasprintf(&fe->value, "%ld", (long)getpid());
}
@@ -699,8 +700,9 @@ int
format_replace(struct format_tree *ft, const char *key, size_t keylen,
char **buf, size_t *len, size_t *off)
{
- char *copy, *copy0, *endptr, *ptr, *saved, *trimmed, *value;
- size_t valuelen;
+ char *copy, *copy0, *endptr, *ptr, *found, *new, *value;
+ char *from = NULL, *to = NULL;
+ size_t valuelen, newlen, fromlen, tolen, used;
u_long limit = 0;
int modifiers = 0, brackets;
@@ -738,6 +740,29 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
modifiers |= FORMAT_TIMESTRING;
copy += 2;
break;
+ case 's':
+ if (copy[1] != '/')
+ break;
+ from = copy + 2;
+ for (copy = from; *copy != '\0' && *copy != '/'; copy++)
+ /* nothing */;
+ if (copy[0] != '/' || copy == from) {
+ copy = copy0;
+ break;
+ }
+ copy[0] = '\0';
+ to = copy + 1;
+ for (copy = to; *copy != '\0' && *copy != '/'; copy++)
+ /* nothing */;
+ if (copy[0] != '/' || copy[1] != ':') {
+ copy = copy0;
+ break;
+ }
+ copy[0] = '\0';
+
+ modifiers |= FORMAT_SUBSTITUTE;
+ copy += 2;
+ break;
}
/*
@@ -751,7 +776,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
*ptr = '\0';
value = ptr + 1;
- saved = format_find(ft, copy + 1, modifiers);
+ found = format_find(ft, copy + 1, modifiers);
brackets = 0;
for (ptr = ptr + 1; *ptr != '\0'; ptr++) {
@@ -765,29 +790,56 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
if (*ptr == '\0')
goto fail;
- if (saved != NULL && *saved != '\0' &&
- (saved[0] != '0' || saved[1] != '\0')) {
+ if (found != NULL && *found != '\0' &&
+ (found[0] != '0' || found[1] != '\0')) {
*ptr = '\0';
} else
value = ptr + 1;
value = format_expand(ft, value);
- free(saved);
- saved = value;
+ free(found);
} else {
- saved = value = format_find(ft, copy, modifiers);
+ value = format_find(ft, copy, modifiers);
if (value == NULL)
- saved = value = xstrdup("");
+ value = xstrdup("");
+ }
+
+ /* Perform substitution if any. */
+ if (modifiers & FORMAT_SUBSTITUTE) {
+ fromlen = strlen(from);
+ tolen = strlen(to);
+
+ newlen = strlen(value) + 1;
+ copy = new = xmalloc(newlen);
+ for (ptr = value; *ptr != '\0'; /* nothing */) {
+ if (strncmp(ptr, from, fromlen) != 0) {
+ *new++ = *ptr++;
+ continue;
+ }
+ used = new - copy;
+
+ newlen += tolen;
+ copy = xrealloc(copy, newlen);
+
+ new = copy + used;
+ memcpy(new, to, tolen);
+
+ new += tolen;
+ ptr += fromlen;
+ }
+ *new = '\0';
+ free(value);
+ value = copy;
}
/* Truncate the value if needed. */
if (limit != 0) {
- value = trimmed = utf8_trimcstr(value, limit);
- free(saved);
- saved = trimmed;
+ new = utf8_trimcstr(value, limit);
+ free(value);
+ value = new;
}
- valuelen = strlen(value);
/* Expand the buffer and copy in the value. */
+ valuelen = strlen(value);
while (*len - *off < valuelen + 1) {
*buf = xreallocarray(*buf, 2, *len);
*len *= 2;
@@ -795,7 +847,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
memcpy(*buf + *off, value, valuelen);
*off += valuelen;
- free(saved);
+ free(value);
free(copy0);
return (0);