diff --git a/arguments.c b/arguments.c index 5a2ea41b..37028648 100644 --- a/arguments.c +++ b/arguments.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "tmux.h" @@ -129,9 +130,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); @@ -155,20 +157,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; + utf8_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; + utf8_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); diff --git a/cmd-show-options.c b/cmd-show-options.c index c7621cb8..79a10ead 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -104,7 +104,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 d89fab62..c0daf5f6 100644 --- a/tmux.h +++ b/tmux.h @@ -2297,6 +2297,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 9d3f8735..d94a6437 100644 --- a/utf8.c +++ b/utf8.c @@ -218,6 +218,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