summaryrefslogtreecommitdiffstats
path: root/arguments.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2020-06-12 10:01:19 +0100
committerThomas Adam <thomas@xteddy.org>2020-06-12 10:01:19 +0100
commitbd3fb2fb108c030d6acef8579027e13a92c7f42f (patch)
tree0676d889c284711404fc001bd8d87bfe8442574f /arguments.c
parentb5c86fdc0c1081af382e92e67b5a339908b49587 (diff)
parentd8d77691043a5ecd504fb2a82e4e312d947ab19f (diff)
Merge branch 'obsd-master'
Diffstat (limited to 'arguments.c')
-rw-r--r--arguments.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/arguments.c b/arguments.c
index 166eb1ba..3ce0e680 100644
--- a/arguments.c
+++ b/arguments.c
@@ -211,32 +211,35 @@ args_print(struct args *args)
char *
args_escape(const char *s)
{
- static const char quoted[] = " #\"';${}";
+ static const char dquoted[] = " #';${}";
+ static const char squoted[] = " \"";
char *escaped, *result;
- int flags;
+ int flags, quotes = 0;
if (*s == '\0') {
xasprintf(&result, "''");
return (result);
}
+ if (s[strcspn(s, dquoted)] != '\0')
+ quotes = '"';
+ else if (s[strcspn(s, squoted)] != '\0')
+ quotes = '\'';
+
if (s[0] != ' ' &&
- (strchr(quoted, s[0]) != NULL || s[0] == '~') &&
- s[1] == '\0') {
+ s[1] == '\0' &&
+ (quotes != 0 || s[0] == '~')) {
xasprintf(&escaped, "\\%c", s[0]);
return (escaped);
}
- if (strchr(s, ' ') != NULL && strchr(s, '\'') == NULL) {
- xasprintf(&escaped, "'%s'", s);
- return (escaped);
- }
-
flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
- if (s[strcspn(s, quoted)] != '\0')
+ if (quotes == '"')
flags |= VIS_DQ;
utf8_stravis(&escaped, s, flags);
- if (flags & VIS_DQ) {
+ if (quotes == '\'')
+ xasprintf(&result, "'%s'", escaped);
+ else if (quotes == '"') {
if (*escaped == '~')
xasprintf(&result, "\"\\%s\"", escaped);
else