summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/arguments.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2019-05-23 14:03:45 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2019-05-23 14:03:45 +0000
commit4cefe275754a7dd624b1e7c3aab51a4b4b92c6b7 (patch)
tree064a04a3b5e60e5be349de562cfb824bff054c40 /usr.bin/tmux/arguments.c
parenta06ea0786589f7b036e0ce81c2590041542bda85 (diff)
Break the argument escaping code into a separate function and use it to
escape key bindings in list-keys. Also escape ~ and ; and $ properly.
Diffstat (limited to 'usr.bin/tmux/arguments.c')
-rw-r--r--usr.bin/tmux/arguments.c63
1 files changed, 40 insertions, 23 deletions
diff --git a/usr.bin/tmux/arguments.c b/usr.bin/tmux/arguments.c
index 051adf26dd9..143089e28b8 100644
--- a/usr.bin/tmux/arguments.c
+++ b/usr.bin/tmux/arguments.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: arguments.c,v 1.21 2019/04/28 20:05:50 nicm Exp $ */
+/* $OpenBSD: arguments.c,v 1.22 2019/05/23 14:03:44 nicm Exp $ */
/*
* Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -141,23 +141,15 @@ static void
args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
struct args_value *value)
{
- static const char quoted[] = " #\"';$";
- char *escaped;
- int flags;
+ char *escaped;
if (**buf != '\0')
args_print_add(buf, len, " -%c ", entry->flag);
else
args_print_add(buf, len, "-%c ", entry->flag);
- flags = VIS_OCTAL|VIS_TAB|VIS_NL;
- if (value->value[strcspn(value->value, quoted)] != '\0')
- flags |= VIS_DQ;
- utf8_stravis(&escaped, value->value, flags);
- if (flags & VIS_DQ)
- args_print_add(buf, len, "\"%s\"", escaped);
- else
- args_print_add(buf, len, "%s", escaped);
+ escaped = args_escape(value->value);
+ args_print_add(buf, len, "%s", escaped);
free(escaped);
}
@@ -165,21 +157,13 @@ args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
static void
args_print_add_argument(char **buf, size_t *len, const char *argument)
{
- static const char quoted[] = " #\"';$";
- char *escaped;
- int flags;
+ char *escaped;
if (**buf != '\0')
args_print_add(buf, len, " ");
- flags = VIS_OCTAL|VIS_TAB|VIS_NL;
- if (argument[strcspn(argument, quoted)] != '\0')
- flags |= VIS_DQ;
- utf8_stravis(&escaped, argument, flags);
- if (flags & VIS_DQ)
- args_print_add(buf, len, "\"%s\"", escaped);
- else
- args_print_add(buf, len, "%s", escaped);
+ escaped = args_escape(argument);
+ args_print_add(buf, len, "%s", escaped);
free(escaped);
}
@@ -219,6 +203,39 @@ args_print(struct args *args)
return (buf);
}
+/* Escape an argument. */
+char *
+args_escape(const char *s)
+{
+ static const char quoted[] = " #\"';$";
+ char *escaped, *result;
+ int flags;
+
+ if ((strchr(quoted, s[0]) != NULL || s[0] == '~') && s[1] == '\0') {
+ xasprintf(&escaped, "\\%c", s[0]);
+ return (escaped);
+ }
+
+ flags = VIS_OCTAL|VIS_TAB|VIS_NL;
+ if (s[strcspn(s, quoted)] != '\0')
+ flags |= VIS_DQ;
+ utf8_stravis(&escaped, s, flags);
+
+ if (flags & VIS_DQ) {
+ if (*escaped == '~')
+ xasprintf(&result, "\"\\%s\"", escaped);
+ else
+ xasprintf(&result, "\"%s\"", escaped);
+ } else {
+ if (*escaped == '~')
+ xasprintf(&result, "\\%s", escaped);
+ else
+ result = xstrdup(escaped);
+ }
+ free(escaped);
+ return (result);
+}
+
/* Return if an argument is present. */
int
args_has(struct args *args, u_char ch)