diff options
author | Thomas Adam <thomas@xteddy.org> | 2019-03-18 15:07:51 +0000 |
---|---|---|
committer | Thomas Adam <thomas@xteddy.org> | 2019-03-18 15:07:51 +0000 |
commit | acb2413852b98745b69459d0cdd0d9698cb75e2c (patch) | |
tree | 6af803240715b8a6bccc328d5eda14f237e44944 | |
parent | aa2b3472c515dd1e5f57618e17c0c612cfa3c117 (diff) | |
parent | ce6be7afd4d10b542f9cce8634d6bdd81754f775 (diff) |
Merge branch 'obsd-master'
-rw-r--r-- | cmd-display-message.c | 13 | ||||
-rw-r--r-- | cmd-find.c | 8 | ||||
-rw-r--r-- | cmd-pipe-pane.c | 2 | ||||
-rw-r--r-- | cmd-set-option.c | 12 | ||||
-rw-r--r-- | cmd-show-options.c | 32 | ||||
-rw-r--r-- | cmd.c | 38 | ||||
-rw-r--r-- | environ.c | 20 | ||||
-rw-r--r-- | format.c | 232 | ||||
-rw-r--r-- | options.c | 175 | ||||
-rw-r--r-- | resize.c | 2 | ||||
-rw-r--r-- | screen-redraw.c | 2 | ||||
-rw-r--r-- | server-client.c | 12 | ||||
-rw-r--r-- | session.c | 2 | ||||
-rw-r--r-- | status.c | 237 | ||||
-rw-r--r-- | style.c | 65 | ||||
-rw-r--r-- | tmux.1 | 235 | ||||
-rw-r--r-- | tmux.h | 22 | ||||
-rw-r--r-- | tty-keys.c | 11 | ||||
-rw-r--r-- | tty-term.c | 15 | ||||
-rw-r--r-- | window-client.c | 5 |
20 files changed, 689 insertions, 451 deletions
diff --git a/cmd-display-message.c b/cmd-display-message.c index 6dd210ac..07cd12a8 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = { .name = "display-message", .alias = "display", - .args = { "c:pt:F:", 0, 1 }, - .usage = "[-p] [-c target-client] [-F format] " + .args = { "c:pt:F:v", 0, 1 }, + .usage = "[-pv] [-c target-client] [-F format] " CMD_TARGET_PANE_USAGE " [message]", .target = { 't', CMD_FIND_PANE, 0 }, @@ -60,6 +60,7 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) const char *template; char *msg; struct format_tree *ft; + int flags; if (args_has(args, 'F') && args->argc != 0) { cmdq_error(item, "only one of -F or argument must be given"); @@ -83,10 +84,14 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) target_c = c; else target_c = cmd_find_best_client(s); - ft = format_create(item->client, item, FORMAT_NONE, 0); + if (args_has(self->args, 'v')) + flags = FORMAT_VERBOSE; + else + flags = 0; + ft = format_create(item->client, item, FORMAT_NONE, flags); format_defaults(ft, target_c, s, wl, wp); - msg = format_expand_time(ft, template, 0); + msg = format_expand_time(ft, template); if (args_has(self->args, 'p')) cmdq_print(item, "%s", msg); else if (c != NULL) @@ -1263,17 +1263,17 @@ found: no_session: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find session %s", session); + cmdq_error(item, "can't find session: %s", session); goto error; no_window: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find window %s", window); + cmdq_error(item, "can't find window: %s", window); goto error; no_pane: if (~flags & CMD_FIND_QUIET) - cmdq_error(item, "can't find pane %s", pane); + cmdq_error(item, "can't find pane: %s", pane); goto error; } @@ -1343,7 +1343,7 @@ cmd_find_client(struct cmdq_item *item, const char *target, int quiet) /* If no client found, report an error. */ if (c == NULL && !quiet) - cmdq_error(item, "can't find client %s", copy); + cmdq_error(item, "can't find client: %s", copy); free(copy); log_debug("%s: target %s, return %p", __func__, target, c); diff --git a/cmd-pipe-pane.c b/cmd-pipe-pane.c index 574ad462..ce1b3d37 100644 --- a/cmd-pipe-pane.c +++ b/cmd-pipe-pane.c @@ -109,7 +109,7 @@ cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item) /* Expand the command. */ ft = format_create(item->client, item, FORMAT_NONE, 0); format_defaults(ft, c, s, wl, wp); - cmd = format_expand_time(ft, args->argv[0], 0); + cmd = format_expand_time(ft, args->argv[0]); format_free(ft); /* Fork the child. */ diff --git a/cmd-set-option.c b/cmd-set-option.c index c4b82004..ddbfc334 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -163,11 +163,9 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) parent = options_get(oo, name); /* Check that array options and indexes match up. */ - if (idx != -1) { - if (*name == '@' || options_array_size(parent, NULL) == -1) { - cmdq_error(item, "not an array: %s", argument); - goto fail; - } + if (idx != -1 && (*name == '@' || !options_isarray(parent))) { + cmdq_error(item, "not an array: %s", argument); + goto fail; } /* With -o, check this option is not already set. */ @@ -209,7 +207,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) goto fail; } options_set_string(oo, name, append, "%s", value); - } else if (idx == -1 && options_array_size(parent, NULL) == -1) { + } else if (idx == -1 && !options_isarray(parent)) { error = cmd_set_option_set(self, item, oo, parent, value); if (error != 0) goto fail; @@ -264,7 +262,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item) layout_fix_panes(w); } RB_FOREACH(s, sessions, &sessions) - status_update_saved(s); + status_update_cache(s); /* * Update sizes and redraw. May not always be necessary but do it diff --git a/cmd-show-options.c b/cmd-show-options.c index 2dc3dee3..2948f9ff 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -88,20 +88,20 @@ static void cmd_show_options_print(struct cmd *self, struct cmdq_item *item, struct options_entry *o, int idx) { - const char *name; - const char *value; - char *tmp, *escaped; - u_int size, i; + struct options_array_item *a; + const char *name, *value; + char *tmp, *escaped; if (idx != -1) { xasprintf(&tmp, "%s[%d]", options_name(o), idx); name = tmp; } else { - if (options_array_size(o, &size) != -1) { - for (i = 0; i < size; i++) { - if (options_array_get(o, i) == NULL) - continue; - cmd_show_options_print(self, item, o, i); + if (options_isarray(o)) { + a = options_array_first(o); + while (a != NULL) { + idx = options_array_item_index(a); + cmd_show_options_print(self, item, o, idx); + a = options_array_next(a); } return; } @@ -164,9 +164,10 @@ static enum cmd_retval cmd_show_options_all(struct cmd *self, struct cmdq_item *item, struct options *oo) { - struct options_entry *o; + struct options_entry *o; const struct options_table_entry *oe; - u_int size, idx; + struct options_array_item *a; + u_int idx; o = options_first(oo); while (o != NULL) { @@ -175,13 +176,14 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, o = options_next(o); continue; } - if (options_array_size(o, &size) == -1) + if (!options_isarray(o)) cmd_show_options_print(self, item, o, -1); else { - for (idx = 0; idx < size; idx++) { - if (options_array_get(o, idx) == NULL) - continue; + a = options_array_first(o); + while (a != NULL) { + idx = options_array_item_index(a); cmd_show_options_print(self, item, o, idx); + a = options_array_next(a); } } o = options_next(o); @@ -318,31 +318,31 @@ cmd_stringify_argv(int argc, char **argv) static int cmd_try_alias(int *argc, char ***argv) { - struct options_entry *o; - int old_argc = *argc, new_argc; - char **old_argv = *argv, **new_argv; - u_int size, idx; - int i; - size_t wanted; - const char *s, *cp = NULL; + struct options_entry *o; + struct options_array_item *a; + int old_argc = *argc, new_argc, i; + char **old_argv = *argv, **new_argv; + size_t wanted; + const char *s, *cp = NULL; o = options_get_only(global_options, "command-alias"); - if (o == NULL || options_array_size(o, &size) == -1 || size == 0) + if (o == NULL) return (-1); - wanted = strlen(old_argv[0]); - for (idx = 0; idx < size; idx++) { - s = options_array_get(o, idx); - if (s == NULL) - continue; - cp = strchr(s, '='); - if (cp == NULL || (size_t)(cp - s) != wanted) - continue; - if (strncmp(old_argv[0], s, wanted) == 0) - break; + a = options_array_first(o); + while (a != NULL) { + s = options_array_item_value(a); + if (s != NULL) { + cp = strchr(s, '='); + if (cp != NULL && + (size_t)(cp - s) == wanted && + strncmp(old_argv[0], s, wanted) == 0) + break; + } + a = options_array_next(a); } - if (idx == size) + if (a == NULL) return (-1); if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0) @@ -174,22 +174,26 @@ environ_unset(struct environ *env, const char *name) void environ_update(struct options *oo, struct environ *src, struct environ *dst) { - struct environ_entry *envent; - struct options_entry *o; - u_int size, idx; - const char *value; + struct environ_entry *envent; + struct options_entry *o; + struct options_array_item *a; + const char *value; o = options_get(oo, "update-environment"); - if (o == NULL || options_array_size(o, &size) == -1) + if (o == NULL) return; - for (idx = 0; idx < size; idx++) { - value = options_array_get(o, idx); - if (value == NULL) + a = options_array_first(o); + while (a != NULL) { + value = options_array_item_value(a); + if (value == NULL) { + a = options_array_next(a); continue; + } if ((envent = environ_find(src, value)) == NULL) environ_clear(dst, value); else environ_set(dst, envent->name, "%s", envent->value); + a = options_array_next(a); } } @@ -100,6 +100,9 @@ format_job_cmp(struct format_job *fj1, struct format_job *fj2) #define FORMAT_WINDOWS 0x100 #define FORMAT_PANES 0x200 +/* Limit on recursion. */ +#define FORMAT_LOOP_LIMIT 10 + /* Entry in format tree. */ struct format_entry { char *key; @@ -121,13 +124,15 @@ struct format_tree { struct client *client; u_int tag; int flags; + time_t time; + u_int loop; RB_HEAD(format_entry_tree, format_entry) tree; }; static int format_entry_cmp(struct format_entry *, struct format_entry *); RB_GENERATE_STATIC(format_entry_tree, format_entry, entry, format_entry_cmp); -/* Format modifiers. */ +/* Format modifier. */ struct format_modifier { char modifier[3]; u_int size; @@ -203,6 +208,36 @@ static const char *format_lower[] = { NULL /* z */ }; +/* Is logging enabled? */ +static inline int +format_logging(struct format_tree *ft) +{ + return (log_get_level() != 0 || (ft->flags & FORMAT_VERBOSE)); +} + +/* Log a message if verbose. */ +static void printflike(3, 4) +format_log1(struct format_tree *ft, const char *from, const char *fmt, ...) +{ + va_list ap; + char *s; + static const char spaces[] = " "; + + if (!format_logging(ft)) + return; + + va_start(ap, fmt); + vasprintf(&s, fmt, ap); + va_end(ap); + + log_debug("%s: %s", from, s); + if (ft->item != NULL && (ft->flags & FORMAT_VERBOSE)) + cmdq_print(ft->item, "#%.*s%s", ft->loop, spaces, s); + + free(s); +} +#define format_log(ft, fmt, ...) format_log1(ft, __func__, fmt, ##__VA_ARGS__) + /* Format job update callback. */ static void format_job_update(struct job *job) @@ -311,7 +346,9 @@ format_job_get(struct format_tree *ft, const char *cmd) force = (ft->flags & FORMAT_FORCE); t = time(NULL); - if (fj->job == NULL && (force || fj->last != t)) { + if (force && fj->job != NULL) + job_free(fj->job); + if (force || (fj->job == NULL && fj->last != t)) { fj->job = job_run(expanded, NULL, server_client_get_cwd(ft->client, NULL), format_job_update, format_job_complete, NULL, fj, JOB_NOWAIT); @@ -697,6 +734,7 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags) ft->tag = tag; ft->flags = flags; + ft->time = time(NULL); format_add(ft, "version", "%s", VERSION); format_add_cb(ft, "host", format_cb_host); @@ -1154,7 +1192,9 @@ format_substitute(const char *source, const char *from, const char *to) static char * format_loop_sessions(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *expanded, *value; size_t valuelen; struct session *s; @@ -1163,7 +1203,12 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) valuelen = 1; RB_FOREACH(s, sessions, &sessions) { - expanded = format_single(item, fmt, ft->c, ft->s, NULL, NULL); + format_log(ft, "session loop: $%u", s->id); + nft = format_create(c, item, FORMAT_NONE, ft->flags); + nft->loop = ft->loop; + format_defaults(nft, ft->c, s, NULL, NULL); + expanded = format_expand(nft, fmt); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); @@ -1179,13 +1224,18 @@ format_loop_sessions(struct format_tree *ft, const char *fmt) static char * format_loop_windows(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *all, *active, *use, *expanded, *value; size_t valuelen; struct winlink *wl; + struct window *w; - if (ft->s == NULL) + if (ft->s == NULL) { + format_log(ft, "window loop but no session"); return (NULL); + } if (format_choose(ft, fmt, &all, &active, 0) != 0) { all = xstrdup(fmt); @@ -1196,11 +1246,17 @@ format_loop_windows(struct format_tree *ft, const char *fmt) valuelen = 1; RB_FOREACH(wl, winlinks, &ft->s->windows) { + w = wl->window; + format_log(ft, "window loop: %u @%u", wl->idx, w->id); if (active != NULL && wl == ft->s->curw) use = active; else use = all; - expanded = format_single(item, use, ft->c, ft->s, wl, NULL); + nft = format_create(c, item, FORMAT_WINDOW|w->id, ft->flags); + nft->loop = ft->loop; + format_defaults(nft, ft->c, ft->s, wl, NULL); + expanded = format_expand(nft, use); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); @@ -1219,13 +1275,17 @@ format_loop_windows(struct format_tree *ft, const char *fmt) static char * format_loop_panes(struct format_tree *ft, const char *fmt) { + struct client *c = ft->client; struct cmdq_item *item = ft->item; + struct format_tree *nft; char *all, *active, *use, *expanded, *value; size_t valuelen; struct window_pane *wp; - if (ft->w == NULL) + if (ft->w == NULL) { + format_log(ft, "pane loop but no window"); return (NULL); + } if (format_choose(ft, fmt, &all, &active, 0) != 0) { all = xstrdup(fmt); @@ -1236,11 +1296,16 @@ format_loop_panes(struct format_tree *ft, const char *fmt) valuelen = 1; TAILQ_FOREACH(wp, &ft->w->panes, entry) { + format_log(ft, "pane loop: %%%u", wp->id); if (active != NULL && wp == ft->w->active) use = active; else use = all; - expanded = format_single(item, use, ft->c, ft->s, ft->wl, wp); + nft = format_create(c, item, FORMAT_PANE|wp->id, ft->flags); + nft->loop = ft->loop; + format_defaults(nft, ft->c, ft->s, ft->wl, wp); + expanded = format_expand(nft, use); + format_free(nft); valuelen += strlen(expanded); value = xrealloc(value, valuelen); @@ -1264,25 +1329,27 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, const char *errptr, *copy, *cp; char *copy0, *condition, *found, *new; char *value, *left, *right; - char tmp[64]; size_t valuelen; int modifiers = 0, limit = 0; struct format_modifier *list, *fm, *cmp = NULL, *search = NULL; struct format_modifier *sub = NULL; u_int i, count; + int j; /* Make a copy of the key. */ copy = copy0 = xstrndup(key, keylen); - log_debug("%s: format = '%s'", __func__, copy); /* Process modifier list. */ list = format_build_modifiers(ft, ©, &count); for (i = 0; i < count; i++) { - xsnprintf(tmp, sizeof tmp, "%s: modifier %u", __func__, i); - log_debug("%s = %s", tmp, list[i].modifier); - cmd_log_argv(list[i].argc, list[i].argv, tmp); - fm = &list[i]; + if (format_logging(ft)) { + format_log(ft, "modifier %u is %s", i, fm->modifier); + for (j = 0; j < fm->argc; j++) { + format_log(ft, "modifier %u argument %d: %s", i, + j, fm->argv[j]); + } + } if (fm->size == 1) { switch (fm->modifier[0]) { case 'm': @@ -1365,14 +1432,22 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, goto fail; } else if (search != NULL) { /* Search in pane. */ - if (wp == NULL) + if (wp == NULL) { + format_log(ft, "search '%s' but no pane", copy); value = xstrdup("0"); - else + } else { + format_log(ft, "search '%s' pane %%%u", copy, wp->id); xasprintf(&value, "%u", window_pane_search(wp, copy)); + } } else if (cmp != NULL) { /* Comparison of left and right. */ - if (format_choose(ft, copy, &left, &right, 1) != 0) + if (format_choose(ft, copy, &left, &right, 1) != 0) { + format_log(ft, "compare %s syntax error: %s", + cmp->modifier, copy); goto fail; + } + format_log(ft, "compare %s left is: %s", cmp->modifier, left); + format_log(ft, "compare %s right is: %s", cmp->modifier, right); if (strcmp(cmp->modifier, "||") == 0) { if (format_true(left) || format_true(right)) @@ -1407,9 +1482,12 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, } else if (*copy == '?') { /* Conditional: check first and choose second or third. */ cp = format_skip(copy + 1, ","); - if (cp == NULL) + if (cp == NULL) { + format_log(ft, "condition syntax error: %s", copy + 1); goto fail; + } condition = xstrndup(copy + 1, cp - (copy + 1)); + format_log(ft, "condition is: %s", condition); found = format_find(ft, condition, modifiers); if (found == NULL) { @@ -1422,27 +1500,42 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen, if (strcmp(found, condition) == 0) { free(found); found = xstrdup(""); + format_log(ft, "condition '%s' found: %s", + condition, found); + } else { + format_log(ft, + "condition '%s' not found; assuming false", + condition); } - } - free(condition); + } else + format_log(ft, "condition '%s' found", condition); if (format_choose(ft, cp + 1, &left, &right, 0) != 0) { + format_log(ft, "condition '%s' syntax error: %s", + condition, cp + 1); free(found); goto fail; } - if (format_true(found)) + if (format_true(found)) { + format_log(ft, "condition '%s' is true", condition); value = format_expand(ft, left); - else + } else { + format_log(ft, "condition '%s' is false", condition); value = format_expand(ft, right); + } free(right); free(left); + free(condition); free(found); } else { /* Neither: look up directly. */ value = format_find(ft, copy, modifiers); - if (value == NULL) + if (value == NULL) { + format_log(ft, "format '%s' not found", copy); value = xstrdup(""); + } else + format_log(ft, "format '%s' found: %s", copy, value); } done: @@ -1453,7 +1546,7 @@ done: value = new; } else if (modifiers & FORMAT_EXPANDTIME) { - new = format_expand_time(ft, value, 0); + new = format_expand_time(ft, value); free(value); value = new; } @@ -1461,6 +1554,8 @@ done: /* Perform substitution if any. */ if (sub != NULL) { new = format_substitute(value, sub->argv[0], sub->argv[1]); + format_log(ft, "substituted '%s' to '%s: %s", sub->argv[0], + sub->argv[1], new); free(value); value = new; } @@ -1468,16 +1563,17 @@ done: /* Truncate the value if needed. */ if (limit > 0) { new = utf8_trimcstr(value, limit); + format_log(ft, "applied length limit %d: %s", limit, new); free(value); value = new; } else if (limit < 0) { new = utf8_rtrimcstr(value, -limit); + format_log(ft, "applied length limit %d: %s", limit, new); free(value); value = new; } /* Expand the buffer and copy in the value. */ - log_debug("%s: '%s' -> '%s'", __func__, copy0, value); valuelen = strlen(value); while (*len - *off < valuelen + 1) { *buf = xreallocarray(*buf, 2, *len); @@ -1486,48 +1582,50 @@ done: memcpy(*buf + *off, value, valuelen); *off += valuelen; + format_log(ft, "replaced '%s' with '%s'", copy0, value); free(value); + format_free_modifiers(list, count); free(copy0); return (0); fail: + format_log(ft, "failed %s", copy0); format_free_modifiers(list, count); free(copy0); 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) +/* Expand keys in a template. */ +static char * +format_expand1(struct format_tree *ft, const char *fmt, int time) { + char *buf, *out, *name; + const char *ptr, *s; + size_t off, len, n, outlen; + int ch, brackets; struct tm *tm; - char s[2048]; + char expanded[8192]; if (fmt == NULL || *fmt == '\0') return (xstrdup("")); - if (t == 0) - t = time(NULL); - tm = localtime(&t); - - if (strftime(s, sizeof s, fmt, tm) == 0) + if (ft->loop == FORMAT_LOOP_LIMIT) return (xstrdup("")); + ft->loop++; - return (format_expand(ft, s)); -} - -/* Expand keys in a template. */ -char * -format_expand(struct format_tree *ft, const char *fmt) -{ - char *buf, *out, *name; - const char *ptr, *s, *saved = fmt; - size_t off, len, n, outlen; - int ch, brackets; + format_log(ft, "expanding format: %s", fmt); - if (fmt == NULL) - return (xstrdup("")); + if (time) { + tm = localtime(&ft->time); + if (strftime(expanded, sizeof expanded, fmt, tm) == 0) { + format_log(ft, "format is too long"); + return (xstrdup("")); + } + if (format_logging(ft) && strcmp(expanded, fmt) != 0) + format_log(ft, "after time expanded: %s", expanded); + fmt = expanded; + } len = 64; buf = xmalloc(len); @@ -1544,7 +1642,7 @@ format_expand(struct format_tree *ft, const char *fmt) } fmt++; - ch = (u_char) *fmt++; + ch = (u_char)*fmt++; switch (ch) { case '(': brackets = 1; @@ -1558,15 +1656,19 @@ format_expand(struct format_tree *ft, const char *fmt) break; n = ptr - fmt; - if (ft->flags & FORMAT_NOJOBS) + name = xstrndup(fmt, n); + format_log(ft, "found #(): %s", name); + + if (ft->flags & FORMAT_NOJOBS) { out = xstrdup(""); - else { - name = xstrndup(fmt, n); + format_log(ft, "#() is disabled"); + } else { out = format_job_get(ft, name); - free(name); + format_log(ft, "#() result: %s", out); } - outlen = strlen(out); + free(name); + outlen = strlen(out); while (len - off < outlen + 1) { buf = xreallocarray(buf, 2, len); len *= 2; @@ -1584,6 +1686,7 @@ format_expand(struct format_tree *ft, const char *fmt) break; n = ptr - fmt; + format_log(ft, "found #{}: %.*s", (int)n, fmt); if (format_replace(ft, fmt, n, &buf, &len, &off) != 0) break; fmt += n + 1; @@ -1591,6 +1694,7 @@ format_expand(struct format_tree *ft, const char *fmt) case '}': case '#': case ',': + format_log(ft, "found #%c", ch); while (len - off < 2) { buf = xreallocarray(buf, 2, len); len *= 2; @@ -1613,6 +1717,7 @@ format_expand(struct format_tree *ft, const char *fmt) continue; } n = strlen(s); + format_log(ft, "found #%c: %s", ch, s); if (format_replace(ft, s, n, &buf, &len, &off) != 0) break; continue; @@ -1622,10 +1727,26 @@ format_expand(struct format_tree *ft, const char *fmt) } buf[off] = '\0'; - log_debug("%s: '%s' -> '%s'", __func__, saved, buf); + format_log(ft, "result is: %s", buf); + ft->loop--; + return (buf); } +/* Expand keys in a template, passing through strftime first. */ +char * +format_expand_time(struct format_tree *ft, const char *fmt) +{ + return (format_expand1(ft, fmt, 1)); +} + +/* Expand keys in a template. */ +char * +format_expand(struct format_tree *ft, const char *fmt) +{ + return (format_expand1(ft, fmt, 0)); +} + /* Expand a single string. */ char * format_single(struct cmdq_item *item, const char *fmt, struct client *c, @@ -1813,6 +1934,11 @@ format_defaults_winlink(struct format_tree *ft, struct winlink *wl) format_add(ft, "window_flags", "%s", window_printable_flags(wl)); format_add(ft, "window_active", "%d", wl == s->curw); + format_add(ft, "window_start_flag", "%d", + !!(wl == RB_MIN(winlinks, &s->windows))); + format_add(ft, "window_end_flag", "%d", + !!(wl == RB_MAX(winlinks, &s->windows))); + format_add(ft, "window_bell_flag", "%d", !!(wl->flags & WINLINK_BELL)); format_add(ft, "window_activity_flag", "%d", @@ -30,6 +30,23 @@ * a red-black tree. */ +struct options_array_item { + u_int index; + char *value; + RB_ENTRY(options_array_item) entry; +}; +RB_HEAD(options_array, options_array_item); +static int +options_array_cmp(struct options_array_item *a1, struct options_array_item *a2) +{ + if (a1->index < a2->index) + return (-1); + if (a1->index > a2->index) + return (1); + return (0); +} +RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp); + struct options_entry { struct options *owner; @@ -40,10 +57,7 @@ struct options_entry { char *string; long long number; struct style style; - struct { - const char **array; - u_int arraysize; - }; + struct options_array array; }; RB_ENTRY(options_entry) entry; @@ -56,8 +70,6 @@ struct options { static struct options_entry *options_add(struct options *, const char *); -#define OPTIONS_ARRAY_LIMIT 1000 - #define OPTIONS_IS_STRING(o) \ ((o)->tableentry == NULL || \ (o)->tableentry->type == OPTIONS_TABLE_STRING) @@ -163,18 +175,26 @@ options_empty(struct options *oo, const struct options_table_entry *oe) o = options_add(oo, oe->name); o->tableentry = oe; + if (oe->type == OPTIONS_TABLE_ARRAY) + RB_INIT(&o->array); + return (o); } struct options_entry * options_default(struct options *oo, const struct options_table_entry *oe) { - struct options_entry *o; + struct options_entry *o; + u_int i; o = options_empty(oo, oe); - if (oe->type == OPTIONS_TABLE_ARRAY) - options_array_assign(o, oe->default_str); - else if (oe->type == OPTIONS_TABLE_STRING) + if (oe->type == OPTIONS_TABLE_ARRAY) { + if (oe->default_arr != NULL) { + for (i = 0; oe->default_arr[i] != NULL; i++) + options_array_set(o, i, oe->default_arr[i], 0); + } else + options_array_assign(o, oe->default_str); + } else if (oe->type == OPTIONS_TABLE_STRING) o->string = xstrdup(oe->default_str); else if (oe->type == OPTIONS_TABLE_STYLE) { style_set(&o->style, &grid_default_cell); @@ -205,15 +225,11 @@ void options_remove(struct options_entry *o) { struct options *oo = o->owner; - u_int i; if (OPTIONS_IS_STRING(o)) - free((void *)o->string); - else if (OPTIONS_IS_ARRAY(o)) { - for (i = 0; i < o->arraysize; i++) - free((void *)o->array[i]); - free(o->array); - } + free(o->string); + else if (OPTIONS_IS_ARRAY(o)) + options_array_clear(o); RB_REMOVE(options_tree, &oo->tree, o); free(o); @@ -231,62 +247,79 @@ options_table_entry(struct options_entry *o) return (o->tableentry); } +static struct options_array_item * +options_array_item(struct options_entry *o, u_int idx) +{ + struct options_array_item a; + + a.index = idx; + return (RB_FIND(options_array, &o->array, &a)); +} + +static void +options_array_free(struct options_entry *o, struct options_array_item *a) +{ + free(a->value); + RB_REMOVE(options_array, &o->array, a); + free(a); +} + void options_array_clear(struct options_entry *o) { - if (OPTIONS_IS_ARRAY(o)) - o->arraysize = 0; + struct options_array_item *a, *a1; + + if (!OPTIONS_IS_ARRAY(o)) + return; + + RB_FOREACH_SAFE(a, options_array, &o->array, a1) + options_array_free(o, a); } const char * options_array_get(struct options_entry *o, u_int idx) { + struct options_array_item *a; + if (!OPTIONS_IS_ARRAY(o)) return (NULL); - if (idx >= o->arraysize) + a = options_array_item(o, idx); + if (a == NULL) return (NULL); - return (o->array[idx]); + return (a->value); } int options_array_set(struct options_entry *o, u_int idx, const char *value, int append) { - char *new; - u_int i; + struct options_array_item *a; + char *new; if (!OPTIONS_IS_ARRAY(o)) return (-1); - if (idx >= OPTIONS_ARRAY_LIMIT) - return (-1); - if (idx >= o->arraysize) { - o->array = xreallocarray(o->array, idx + 1, sizeof *o->array); - for (i = o->arraysize; i < idx + 1; i++) - o->array[i] = NULL; - o->arraysize = idx + 1; + a = options_array_item(o, idx); + if (value == NULL) { + if (a != NULL) + options_array_free(o, a); + return (0); } - new = NULL; - if (value != NULL) { - if (o->array[idx] != NULL && append) - xasprintf(&new, "%s%s", o->array[idx], value); + if (a == NULL) { + a = xcalloc(1, sizeof *a); + a->index = idx; + a->value = xstrdup(value); + RB_INSERT(options_array, &o->array, a); + } else |