From 4bb5a1d6a7d1cbf2aa3647afeab270f1afa5d4a7 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 18 Jan 2017 10:00:50 +0000 Subject: [PATCH 1/2] Run arguments through vis() as well when printing them. --- arguments.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/arguments.c b/arguments.c index 2aedd7ce..c4c8e891 100644 --- a/arguments.c +++ b/arguments.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "tmux.h" @@ -130,9 +131,10 @@ char * args_print(struct args *args) { size_t len; - char *buf; - int i; + char *buf, *escaped; + int i, flags; struct args_entry *entry; + static const char quoted[] = " #\"';$"; len = 1; buf = xcalloc(1, len); @@ -156,20 +158,32 @@ args_print(struct args *args) args_print_add(&buf, &len, " -%c ", entry->flag); else args_print_add(&buf, &len, "-%c ", entry->flag); - if (strchr(entry->value, ' ') != NULL) - args_print_add(&buf, &len, "\"%s\"", entry->value); + + flags = VIS_OCTAL|VIS_TAB|VIS_NL; + if (entry->value[strcspn(entry->value, quoted)] != '\0') + flags |= VIS_DQ; + stravis(&escaped, entry->value, flags); + if (flags & VIS_DQ) + args_print_add(&buf, &len, "\"%s\"", escaped); else - args_print_add(&buf, &len, "%s", entry->value); + args_print_add(&buf, &len, "%s", escaped); + free(escaped); } /* And finally the argument vector. */ for (i = 0; i < args->argc; i++) { if (*buf != '\0') args_print_add(&buf, &len, " "); - if (strchr(args->argv[i], ' ') != NULL) - args_print_add(&buf, &len, "\"%s\"", args->argv[i]); + + flags = VIS_OCTAL|VIS_TAB|VIS_NL; + if (args->argv[i][strcspn(args->argv[i], quoted)] != '\0') + flags |= VIS_DQ; + stravis(&escaped, args->argv[i], flags); + if (flags & VIS_DQ) + args_print_add(&buf, &len, "\"%s\"", escaped); else - args_print_add(&buf, &len, "%s", args->argv[i]); + args_print_add(&buf, &len, "%s", escaped); + free(escaped); } return (buf); From faa05703091de29e5af06777513acd7a562433ba Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 18 Jan 2017 10:08:05 +0000 Subject: [PATCH 2/2] Plain stravis() because it will mangle UTF-8 characters, so add utf8_stravis() which calls our existing utf8_strvis() and use it instead --- arguments.c | 4 ++-- cmd-show-options.c | 2 +- tmux.h | 1 + utf8.c | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/arguments.c b/arguments.c index c4c8e891..5ac51ddc 100644 --- a/arguments.c +++ b/arguments.c @@ -162,7 +162,7 @@ args_print(struct args *args) flags = VIS_OCTAL|VIS_TAB|VIS_NL; if (entry->value[strcspn(entry->value, quoted)] != '\0') flags |= VIS_DQ; - stravis(&escaped, entry->value, flags); + utf8_stravis(&escaped, entry->value, flags); if (flags & VIS_DQ) args_print_add(&buf, &len, "\"%s\"", escaped); else @@ -178,7 +178,7 @@ args_print(struct args *args) flags = VIS_OCTAL|VIS_TAB|VIS_NL; if (args->argv[i][strcspn(args->argv[i], quoted)] != '\0') flags |= VIS_DQ; - stravis(&escaped, args->argv[i], flags); + utf8_stravis(&escaped, args->argv[i], flags); if (flags & VIS_DQ) args_print_add(&buf, &len, "\"%s\"", escaped); else diff --git a/cmd-show-options.c b/cmd-show-options.c index 5929e915..f7df2b11 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -105,7 +105,7 @@ cmd_show_options_print(struct cmd *self, struct cmdq_item *item, if (args_has(self->args, 'v')) cmdq_print(item, "%s", value); else if (options_isstring(o)) { - stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ); + utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ); cmdq_print(item, "%s \"%s\"", name, escaped); free(escaped); } else diff --git a/tmux.h b/tmux.h index 322547b0..eb1660dd 100644 --- a/tmux.h +++ b/tmux.h @@ -2295,6 +2295,7 @@ enum utf8_state utf8_append(struct utf8_data *, u_char); enum utf8_state utf8_combine(const struct utf8_data *, wchar_t *); enum utf8_state utf8_split(wchar_t, struct utf8_data *); int utf8_strvis(char *, const char *, size_t, int); +int utf8_stravis(char **, const char *, int); char *utf8_sanitize(const char *); size_t utf8_strlen(const struct utf8_data *); u_int utf8_strwidth(const struct utf8_data *, ssize_t); diff --git a/utf8.c b/utf8.c index 5ab2127a..ea8c99a2 100644 --- a/utf8.c +++ b/utf8.c @@ -193,6 +193,20 @@ utf8_strvis(char *dst, const char *src, size_t len, int flag) return (dst - start); } +/* Same as utf8_strvis but allocate the buffer. */ +int +utf8_stravis(char **dst, const char *src, int flag) +{ + char *buf; + int len; + + buf = xreallocarray(NULL, 4, strlen(src) + 1); + len = utf8_strvis(buf, src, strlen(src), flag); + + *dst = xrealloc(buf, len + 1); + return (len); +} + /* * Sanitize a string, changing any UTF-8 characters to '_'. Caller should free * the returned string. Anything not valid printable ASCII or UTF-8 is